<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Validator;
use App\Responses;
use App\Helper;
use App\Models\Users;
use App\Models\UsersMenuPermissions;

class UsersMenuPermissionsController extends Controller
{
    public function view_menu_permissions(Request $req)
    {
		$data = [
			'user' => $req->auth,
		];
        return view('menu_v1.configs.usersMenuPermissions', $data);
    }

    /**
	 * API
	 */

	public function api_list_menu_permissions(Request $req)
	{
		try {
			$now = time();
			$input = [];
			$rulesInput = [];

			// validasi input
			// $isValidInput = Validator::make($input, $rulesInput);
			// if (!$isValidInput->passes()) {
			// 	$apiResp = Responses::bad_input($isValidInput->messages()->first());
			// 	return new Response($apiResp, $apiResp['meta']['code']);
			// }
			$filter = [];

			$list = UsersMenuPermissions::listPermissionsMenus($filter);
			foreach ($list as $key => $row) {
				$list[$key]->DT_RowIndex = $key + 1;
				$list[$key]->action = '-';
			}

			$apiResp = Responses::success('success list menu permissions');
			$apiResp['count'] = count($list);
			$apiResp['data'] = $list;
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_show_menu_permissions(Request $req, $id)
	{
		try {
			$now = time();

			$input = [
				'id' => $id,
			];
			$rulesInput = [
				'id' => 'required|integer|not_in:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$permis = UsersMenuPermissions::showPermissionsMenusById($id);
			if (count($permis) < 1) {
				$apiResp = Responses::not_found('permissions not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}
			
			$apiResp = Responses::success('success get detail permissions');
			$apiResp['data'] = $permis[0];
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_add_menu_permissions(Request $req)
	{
		try {
			$now = time();

			$input = [
				'name' => $req->name,
				// menu admin
				'menu_trx' => $req->menu_trx,
				'menu_company' => $req->menu_company,
				'menu_zone' => $req->menu_zone,
				'menu_users' => $req->menu_users,
				'menu_conf_rates' => $req->menu_conf_rates,
				'menu_conf_insurance' => $req->menu_conf_insurance,
				'menu_conf_truck_type' => $req->menu_conf_truck_type,
				'menu_devices' => $req->menu_devices,
				'menu_logs_devices' => $req->menu_logs_devices,
				'menu_conf_adt_items' => $req->menu_conf_adt_items,
				'menu_drivers' => $req->menu_drivers,
				'menu_vehicles' => $req->menu_vehicles,
				'menu_menu_permission' => $req->menu_menu_permission,
				// menu finance
				'menu_fnc_ledger_balance' => $req->menu_fnc_ledger_balance,
				'menu_fnc_payment' => $req->menu_fnc_payment,
				'menu_fnc_billing' => $req->menu_fnc_billing,
				'menu_fnc_conf_adt_items' => $req->menu_fnc_conf_adt_items,
				'status' => $req->status,
			];
			$rulesInput = [
				'name' => 'required|string',
				// menu admin
				'menu_trx' => 'required|numeric|max:2',
				'menu_company' => 'required|numeric|max:2',
				'menu_zone' => 'required|numeric|max:2',
				'menu_users' => 'required|numeric|max:2',
				'menu_conf_rates' => 'required|numeric|max:2',
				'menu_conf_insurance' => 'required|numeric|max:2',
				'menu_conf_truck_type' => 'required|numeric|max:2',
				'menu_devices' => 'required|numeric|max:2',
				'menu_logs_devices' => 'required|numeric|max:2',
				'menu_conf_adt_items' => 'required|numeric|max:2',
				'menu_drivers' => 'required|numeric|max:2',
				'menu_vehicles' => 'required|numeric|max:2',
				'menu_menu_permission' => 'required|numeric|max:2',
				// menu finance
				'menu_fnc_ledger_balance' => 'required|numeric|max:2',
				'menu_fnc_payment' => 'required|numeric|max:2',
				'menu_fnc_billing' => 'required|numeric|max:2',
				'menu_fnc_conf_adt_items' => 'required|numeric|max:2',
				'status' => 'required|integer|min:0|max:2',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$isAdmin = 0;
			$isFinance = 0;
			if ($req->menu_trx || $req->menu_company || $req->menu_zone || $req->menu_users || $req->menu_conf_rates || $req->menu_conf_insurance || $req->menu_conf_truck_type || $req->menu_devices || $req->menu_logs_devices || $req->menu_conf_adt_items || $req->menu_drivers || $req->menu_vehicles) {
				$isAdmin = 1;
			}
			if ($req->menu_fnc_ledger_balance || $req->menu_fnc_payment || $req->menu_fnc_billing || $req->menu_fnc_conf_adt_items) {
				$isFinance = 1;
			}

			if ($isAdmin && $isFinance) {
				$apiResp = Responses::bad_request('Jika salah satu menu admin diizinkan, maka tidak bisa mengizinkan menu finance');
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			DB::beginTransaction();

			$insPer = [
				'name' => $req->name,
				// menu admin
				'is_trx' => $req->menu_trx,
				'is_company' => $req->menu_company,
				'is_zone' => $req->menu_zone,
				'is_users' => $req->menu_users,
				'is_conf_rates' => $req->menu_conf_rates,
				'is_conf_insurance' => $req->menu_conf_insurance,
				'is_conf_truck_type' => $req->menu_conf_truck_type,
				'is_devices' => $req->menu_devices,
				'is_logs_devices' => $req->menu_logs_devices,
				'is_conf_adt_items' => $req->menu_conf_adt_items,
				'is_drivers' => $req->menu_drivers,
				'is_vehicles' => $req->menu_vehicles,
				'is_menu_permission' => $req->menu_menu_permission,
				// menu finance
				'is_fnc_ledger_balance' => $req->menu_fnc_ledger_balance,
				'is_fnc_payment' => $req->menu_fnc_payment,
				'is_fnc_billing' => $req->menu_fnc_billing,
				'is_fnc_conf_adt_items' => $req->menu_fnc_conf_adt_items,
				'is_active' => $req->status,
			];
			$id = UsersMenuPermissions::add($insPer);

			$apiResp = Responses::created('success add new permission');

			DB::commit();
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_updt_menu_permissions(Request $req, $id)
	{
		try {
			$now = time();

			$input = [
				'name' => $req->name,
				// menu admin
				'menu_trx' => $req->menu_trx,
				'menu_company' => $req->menu_company,
				'menu_zone' => $req->menu_zone,
				'menu_users' => $req->menu_users,
				'menu_conf_rates' => $req->menu_conf_rates,
				'menu_conf_insurance' => $req->menu_conf_insurance,
				'menu_conf_truck_type' => $req->menu_conf_truck_type,
				'menu_devices' => $req->menu_devices,
				'menu_logs_devices' => $req->menu_logs_devices,
				'menu_conf_adt_items' => $req->menu_conf_adt_items,
				'menu_drivers' => $req->menu_drivers,
				'menu_vehicles' => $req->menu_vehicles,
				'menu_menu_permission' => $req->menu_menu_permission,
				// menu finance
				'menu_fnc_ledger_balance' => $req->menu_fnc_ledger_balance,
				'menu_fnc_payment' => $req->menu_fnc_payment,
				'menu_fnc_billing' => $req->menu_fnc_billing,
				'menu_fnc_conf_adt_items' => $req->menu_fnc_conf_adt_items,
				'status' => $req->status,
			];
			$rulesInput = [
				'name' => 'required|string',
				// menu admin
				'menu_trx' => 'required|numeric|max:2',
				'menu_company' => 'required|numeric|max:2',
				'menu_zone' => 'required|numeric|max:2',
				'menu_users' => 'required|numeric|max:2',
				'menu_conf_rates' => 'required|numeric|max:2',
				'menu_conf_insurance' => 'required|numeric|max:2',
				'menu_conf_truck_type' => 'required|numeric|max:2',
				'menu_devices' => 'required|numeric|max:2',
				'menu_logs_devices' => 'required|numeric|max:2',
				'menu_conf_adt_items' => 'required|numeric|max:2',
				'menu_drivers' => 'required|numeric|max:2',
				'menu_vehicles' => 'required|numeric|max:2',
				'menu_menu_permission' => 'required|numeric|max:2',
				// menu finance
				'menu_fnc_ledger_balance' => 'required|numeric|max:2',
				'menu_fnc_payment' => 'required|numeric|max:2',
				'menu_fnc_billing' => 'required|numeric|max:2',
				'menu_fnc_conf_adt_items' => 'required|numeric|max:2',
				'status' => 'required|integer|min:0|max:2',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$isAdmin = 0;
			$isFinance = 0;
			if ($req->menu_trx || $req->menu_company || $req->menu_zone || $req->menu_users || $req->menu_conf_rates || $req->menu_conf_insurance || $req->menu_conf_truck_type || $req->menu_devices || $req->menu_logs_devices || $req->menu_conf_adt_items || $req->menu_drivers || $req->menu_vehicles) {
				$isAdmin = 1;
			}
			if ($req->menu_fnc_ledger_balance || $req->menu_fnc_payment || $req->menu_fnc_billing || $req->menu_fnc_conf_adt_items) {
				$isFinance = 1;
			}

			if ($isAdmin && $isFinance) {
				$apiResp = Responses::bad_request('Jika salah satu menu admin diizinkan, maka tidak bisa mengizinkan menu finance');
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			DB::beginTransaction();

			$updtPer = [
				'name' => $req->name,
				// menu admin
				'is_trx' => $req->menu_trx,
				'is_company' => $req->menu_company,
				'is_zone' => $req->menu_zone,
				'is_users' => $req->menu_users,
				'is_conf_rates' => $req->menu_conf_rates,
				'is_conf_insurance' => $req->menu_conf_insurance,
				'is_conf_truck_type' => $req->menu_conf_truck_type,
				'is_devices' => $req->menu_devices,
				'is_logs_devices' => $req->menu_logs_devices,
				'is_conf_adt_items' => $req->menu_conf_adt_items,
				'is_drivers' => $req->menu_drivers,
				'is_vehicles' => $req->menu_vehicles,
				'is_menu_permission' => $req->menu_menu_permission,
				// menu finance
				'is_fnc_ledger_balance' => $req->menu_fnc_ledger_balance,
				'is_fnc_payment' => $req->menu_fnc_payment,
				'is_fnc_billing' => $req->menu_fnc_billing,
				'is_fnc_conf_adt_items' => $req->menu_fnc_conf_adt_items,
				'is_active' => $req->status,
			];
			UsersMenuPermissions::updt($id, $updtPer);

			$apiResp = Responses::created('success update permissions');

			DB::commit();
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}

	public function api_del_menu_permissions(Request $req, $id)
	{
		try {
			$now = time();

			$input = [
				'id' => $id,
			];
			$rulesInput = [
				'id' => 'required|integer|not_in:0',
			];

			// validasi input
			$isValidInput = Validator::make($input, $rulesInput);
			if (!$isValidInput->passes()) {
				$apiResp = Responses::bad_input($isValidInput->messages()->first());
				return new Response($apiResp, $apiResp['meta']['code']);
			}

			$a_item = UsersMenuPermissions::showPermissionsMenusById($id);
			if (count($a_item) < 1) {
				$apiResp = Responses::not_found('permissions not found');
				return (new Response($apiResp, $apiResp['meta']['code']));
			}

			DB::beginTransaction();

			UsersMenuPermissions::updt($id, [
				'dlt' => $now,
				'dlt_by' => $req->auth->uid,
			]);

			DB::commit();
			
			$apiResp = Responses::success('success delete permissions');
			return (new Response($apiResp, $apiResp['meta']['code']));
		} catch (\Exception $e) {
			DB::rollBack();
			$apiResp = Responses::error($e->getMessage());
			return (new Response($apiResp, $apiResp['meta']['code']));
		}
	}
}
