<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Validator;
use App\Responses;
use App\Helper;
use App\Models\Users;
use App\Models\Clients;
use App\Models\Vehicles;
use App\Models\Banks;
use App\Models\UsersMenuPermissions;
use Illuminate\Support\Facades\Auth;
use App\Models\UserLogs;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function view_users(Request $req)
    {
        if (
            !auth()
                ->user()
                ->can("user.view")
        ) {
            abort(403, "Unauthorized");
        }
        $data = [
            // "roles" => Users::listRoles($req->auth->role),
            "roles" => DB::table("roles")->get(),
            "pernus" => UsersMenuPermissions::listPermissionsMenus([
                "is_active" => UsersMenuPermissions::IS_ACTIVE,
            ]),
            "vehicles" => Vehicles::getVehicles(),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
        ];

        if ($req->auth->role == Users::ROLE_SUPERADMIN) {
            $data["clients"] = Clients::select2Client();
        } elseif ($req->auth->role == Users::ROLE_ADMIN) {
            $data["clients"] = Clients::select2Client();
        } else {
            $data["clients"] = Clients::select2Client($req->auth->client_group_id);
        }

		$log = [
			"module" => "User",
			"action" => "View",
			"desc" => "Open User menu",
		];
		UserLogs::insert(Auth::user()->id, $log);

        return view("menu_v1.users", $data);
    }

    public function view_profile(Request $req)
    {
        $data = [
            "roles" => DB::table("roles")->get(),
            "pernus" => UsersMenuPermissions::listPermissionsMenus([
                "is_active" => UsersMenuPermissions::IS_ACTIVE,
            ]),
            "vehicles" => Vehicles::getVehicles(),
            "banks" => Banks::listBanks(["is_active" => Banks::IS_ACTIVE]),
        ];

        if ($req->auth->role == Users::ROLE_SUPERADMIN) {
            $data["clients"] = Clients::select2Client();
        } elseif ($req->auth->role == Users::ROLE_ADMIN) {
            $data["clients"] = Clients::select2Client();
        } else {
            $data["clients"] = Clients::select2Client($req->auth->client_group_id);
        }

        return view("menu_v1._profile", $data);
    }

    /**
     * API
     */

    public function api_list_users(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 = [];
            if ($req->cptid) {
                $filter["company"] = $req->cptid;
            }
            $list = Users::listUsers($filter);
            foreach ($list as $key => $row) {
                $list[$key]->DT_RowIndex = $key + 1;
                $list[$key]->count_trx = 0;
                $list[$key]->action = "-";
            }

            $apiResp = Responses::success("success list users");
            $apiResp["data"] = $list;
            $apiResp["count"] = count($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_user(Request $req, $uid)
    {
        try {
            $now = time();

            $input = [
                "uid" => $uid,
            ];
            $rulesInput = [
                "uid" => "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"]);
            }

            $user = Users::showUserById($uid);
            if (count($user) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $apiResp = Responses::success("success get detail user");
            $apiResp["data"] = $user[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_user(Request $req)
    {
        try {
            $now = time();
            $roles = Users::arrRoles();
            $statuses = Users::arrStatus();

            $input = [
                "first_name" => $req->first_name,
                "last_name" => $req->last_name,
                "phone" => $req->phone,
                "email" => $req->email,
                "fulladdress" => $req->fulladdress,
                "password" => $req->password,
                "clients_id" => $req->clients,
                "roles" => $req->roles,
                "chk_type" => $req->chk_type,
                "bank_id" => $req->bank_id,
                "bank_code" => $req->bank_code,
                "bank_short" => $req->bank_short,
                "bank_name" => $req->bank_name,
                "bank_kcp" => $req->bank_branch_name,
                "bank_acc_number" => $req->bank_acc_number,
                "bank_acc_name" => $req->bank_acc_name,
                "status" => $req->status,
                "status_sms" => $req->status_sms,
                "is_tracking" => $req->is_tracking,
                "vehicles" => $req->vehicles,
                "is_vdr_bcng" => $req->is_vdr_bcng,
            ];
            $rulesInput = [
                "first_name" => "required|string|max:125",
                "last_name" => "nullable|max:125",
                "phone" => "required|numeric",
                "email" => "required|email",
                "fulladdress" => "required|string|min:45",
                "password" => "required|string|max:25",
                "clients_id" => "required|integer|not_in:0",
                "roles" => "required|integer|not_in:0",
                "chk_type" => "nullable|integer|not_in:0",
                "bank_id" => "nullable|integer|not_in:0",
                "bank_code" => "nullable|numeric",
                "bank_short" => "nullable|string",
                "bank_name" => "nullable|string",
                "bank_kcp" => "nullable|string",
                "bank_acc_number" => "nullable|numeric",
                "bank_acc_name" => "nullable|string|max:255",
                "status" => "required|integer|not_in:0",
                "status_sms" => "required|integer",
                "is_tracking" => "nullable|numeric",
                "vehicles" => "nullable|array",
                "is_vdr_bcng" => "nullable|numeric",
            ];

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

            if (in_array($req->roles, $roles)) {
            } else {
                $apiResp = Responses::bad_request("role not valid");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            if (in_array($req->status, $statuses)) {
            } else {
                $apiResp = Responses::bad_request("status not valid");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            // if ($req->roles == Users::ROLE_VENDOR) {
            //     if (!$req->bank_id) {
            //         $apiResp = Responses::bad_request("bank wajib diisi");
            //         return new Response($apiResp, $apiResp["meta"]["code"]);
            //     }
            //     if (!$req->bank_acc_number) {
            //         $apiResp = Responses::bad_request("nomor rekening wajib diisi");
            //         return new Response($apiResp, $apiResp["meta"]["code"]);
            //     }
            //     if (!$req->bank_acc_name) {
            //         $apiResp = Responses::bad_request("nama pemilik rekening wajib diisi");
            //         return new Response($apiResp, $apiResp["meta"]["code"]);
            //     }
            // }

            if ($req->clients) {
                $clients = Clients::getClientById($req->clients);
                if (count($clients) < 1) {
                    $apiResp = Responses::not_found("clients not found");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }

            $uniqEmail = Users::getUserByEmail($req->email);
            if (count($uniqEmail) > 0) {
                $apiResp = Responses::bad_request("email has been used");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $uniqPhone = Users::getUserByPhone((int) $req->phone);
            if (count($uniqPhone) > 0) {
                $apiResp = Responses::bad_request("phone has been used");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $uniqCPhone = Clients::getClientByPhone((int) $req->phone);
            if (count($uniqCPhone) > 0) {
                $apiResp = Responses::bad_request("phone has been used");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $vhcs = null;
            if ($req->is_tracking == Users::IS_TRACK_VHC_YES) {
                if (!$req->vehicles) {
                    $apiResp = Responses::bad_request("vehicles must be filled");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
                foreach ($req->vehicles as $k => $v) {
                    $vhcs .= $v . ",";
                }
                if ($vhcs) {
                    if (substr($vhcs, -1) === ",") {
                        $vhcs = substr($vhcs, 0, -1);
                    }
                }
            }

            $data = [
                "first_name" => $req->first_name,
                "last_name" => $req->last_name ?? null,
                "email" => $req->email,
                "phone" => (int) $req->phone,
                "phone_code" => Users::DEFAULT_PHONE_CODE,
                "fulladdress" => $req->fulladdress,
                "password" => Hash::make($req->password),
                "role" => $req->roles,
                "client_id" => $req->clients,
                "client_group_id" => $req->clients,
                "status" => $req->status,
                "is_tracking" => $req->is_tracking,
                "vhcs" => $vhcs,
                "crt" => $now,
                "crt_by" => $req->auth->uid,
                "updt" => $now,
                "updt_by" => $req->auth->uid,
				"status_sms" => $req->status_sms,
            ];
            if ($req->roles == Users::ROLE_CHECKER) {
                // $data['chk_type'] = $req->chk_type;
                $data["chk_type"] = Users::CHK_TYPE_ALL;
            }
            if ($req->roles == Users::ROLE_VENDOR) {
                $data["bank_id"] = $req->bank_id;
                $data["bank_code"] = $req->bank_code;
                $data["bank_name"] = $req->bank_name;
                $data["bank_short_name"] = $req->bank_short;
                $data["bank_branch_name"] = $req->bank_branch_name;
                $data["bank_acc_number"] = $req->bank_acc_number;
                $data["bank_acc_name"] = $req->bank_acc_name;
                $data["is_vdr_bcng"] = $req->is_vdr_bcng;
            }

            DB::beginTransaction();

            $user_id = Users::addUser($data);

            if ($req->roles) {
                DB::table("model_has_roles")->insert([
                    "role_id" => $req->roles,
                    "model_type" => "App\\User", // atau 'App\User' tergantung struktur kamu
                    "model_id" => $user_id, // ID user-nya
                ]);
            }

            $apiResp = Responses::created("success add new user");

            DB::commit();

			$log = [
				"module" => "User",
				"action" => "Create",
				"desc" => "Add new user : ".$req->email,
			];
			UserLogs::insert(Auth::user()->id, $log);

            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_edit_user(Request $req)
    {
        try {
            $now = time();
            $roles = Users::arrRoles();
            $statuses = Users::arrStatus();

            $input = [
                "uid" => $req->uid,
                "is_tracking" => $req->is_tracking,
                "vehicles" => $req->vehicles,
                "bank_id" => $req->bank_id,
                "bank_code" => $req->bank_code,
                "bank_short" => $req->bank_short,
                "bank_name" => $req->bank_name,
                "bank_kcp" => $req->bank_branch_name,
                "bank_acc_number" => $req->bank_acc_number,
                "bank_acc_name" => $req->bank_acc_name,
                "is_vdr_bcng" => $req->is_vdr_bcng,
				"status_sms" => $req->status_sms,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
                "is_tracking" => "nullable|numeric",
                "vehicles" => "nullable|array",
                "bank_id" => "nullable|integer|not_in:0",
                "bank_code" => "nullable|numeric",
                "bank_short" => "nullable|string",
                "bank_name" => "nullable|string",
                "bank_kcp" => "nullable|string",
                "bank_acc_number" => "nullable|numeric",
                "bank_acc_name" => "nullable|string|max:255",
                "is_vdr_bcng" => "nullable|numeric",
				"status_sms" => "required|integer",
            ];
            $data = [
                "id" => $req->uid,
            ];

            if ($req->first_name) {
                $input["first_name"] = $req->first_name;
                $rulesInput["first_name"] = "required|string|max:125";
                $data["first_name"] = $req->first_name;
            }
            if ($req->last_name) {
                $input["last_name"] = $req->last_name;
                $rulesInput["last_name"] = "required|string|max:125";
                $data["last_name"] = $req->last_name ?? null;
            }
            if ($req->email) {
                $input["email"] = $req->email;
                $rulesInput["email"] = "required|email";
                $data["email"] = $req->email;
            }
            if ($req->phone) {
                $input["phone"] = $req->phone;
                $rulesInput["phone"] = "required|integer|not_in:0";
                $data["phone"] = $req->phone;
                $data["phone_code"] = Users::DEFAULT_PHONE_CODE;
            }
            if ($req->fulladdress) {
                $input["fulladdress"] = $req->fulladdress;
                $rulesInput["fulladdress"] = "required|string|min:45";
                $data["fulladdress"] = $req->fulladdress;
            }
            if ($req->password) {
                $input["password"] = $req->password;
                $rulesInput["password"] = "required|string";
                $data["password"] = Hash::make($req->password);
            }
            if ($req->clients) {
                $input["clients"] = $req->clients;
                $rulesInput["clients"] = "required|integer|not_in:0";
                $data["client_group_id"] = $req->clients;

                $clients = Clients::getClientById($req->clients);
                if (count($clients) < 1) {
                    $apiResp = Responses::not_found("clients not found");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
            if ($req->roles) {
                $input["roles"] = $req->roles;
                $rulesInput["roles"] = "required|integer|not_in:0";
                $data["role"] = $req->roles;

                if (in_array($req->roles, $roles)) {
                } else {
                    $apiResp = Responses::bad_request("role not valid");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
            if ($req->roles) {
                if ($req->roles == Users::ROLE_CHECKER) {
                    // $data['chk_type'] = $req->chk_type;
                    $data["chk_type"] = Users::CHK_TYPE_ALL;
                }
            }
            if ($req->status) {
                $input["status"] = $req->status;
                $rulesInput["status"] = "required|integer|not_in:0";
                $data["status"] = $req->status;

                if (in_array($req->status, $statuses)) {
                } else {
                    $apiResp = Responses::bad_request("status not valid");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
			$data["status_sms"] = $req->status_sms;

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

            $getUser = Users::getUserById($req->uid);
            if (count($getUser) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $uniqEmail = Users::getUserByEmail($req->email);
            if (count($uniqEmail) > 0) {
                $notSameUser = 1;
                foreach ($uniqEmail as $key => $row) {
                    if ($row->id == $req->uid) {
                        $notSameUser = 0;
                    }
                }
                if ($notSameUser) {
                    $apiResp = Responses::bad_request("email has been used");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
            $uniqPhone = Users::getUserByPhone($req->phone);
            if (count($uniqPhone) > 0) {
                $notSameUser = 1;
                foreach ($uniqPhone as $key => $row) {
                    if ($row->id == $req->uid) {
                        $notSameUser = 0;
                    }
                }
                if ($notSameUser) {
                    $apiResp = Responses::bad_request("phone has been used");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }

            $vhcs = null;
            if ($req->is_tracking == Users::IS_TRACK_VHC_YES) {
                if (!$req->vehicles) {
                    $apiResp = Responses::bad_request("vehicles must be filled");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
                foreach ($req->vehicles as $k => $v) {
                    $vhcs .= $v . ",";
                }
                if ($vhcs) {
                    if (substr($vhcs, -1) === ",") {
                        $vhcs = substr($vhcs, 0, -1);
                    }
                }
            }
            $data["is_tracking"] = $req->is_tracking;
            $data["vhcs"] = $vhcs;

            $data["updt"] = $now;
            $data["updt_by"] = $req->auth->uid;

            $data["role"] = 2;

            DB::beginTransaction();

            Users::updateUser($req->uid, $data);

            if ($req->roles) {
                DB::table("model_has_roles")
                    ->where("model_id", $req->uid)
                    ->delete();

                DB::table("model_has_roles")->insert([
                    "role_id" => $req->roles,
                    "model_type" => "App\\User",
                    "model_id" => $req->uid,
                ]);
            }

            $apiResp = Responses::created("success update user");

            DB::commit();

			$log = [
				"module" => "User",
				"action" => "Update",
				"desc" => "Update user : ".$req->email,
			];
			UserLogs::insert(Auth::user()->id, $log);

			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_edit_user_profile(Request $req)
    {
        try {
            $now = time();
            $roles = Users::arrRoles();
            $statuses = Users::arrStatus();

            $input = [
                "uid" => $req->uid,
                "is_tracking" => $req->is_tracking,
                "vehicles" => $req->vehicles,
                "bank_id" => $req->bank_id,
                "bank_code" => $req->bank_code,
                "bank_short" => $req->bank_short,
                "bank_name" => $req->bank_name,
                "bank_kcp" => $req->bank_branch_name,
                "bank_acc_number" => $req->bank_acc_number,
                "bank_acc_name" => $req->bank_acc_name,
                "is_vdr_bcng" => $req->is_vdr_bcng,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
                "is_tracking" => "nullable|numeric",
                "vehicles" => "nullable|array",
                "bank_id" => "nullable|integer|not_in:0",
                "bank_code" => "nullable|numeric",
                "bank_short" => "nullable|string",
                "bank_name" => "nullable|string",
                "bank_kcp" => "nullable|string",
                "bank_acc_number" => "nullable|numeric",
                "bank_acc_name" => "nullable|string|max:255",
                "is_vdr_bcng" => "nullable|numeric",
            ];
            $data = [
                "id" => $req->uid,
            ];

            if ($req->first_name) {
                $input["first_name"] = $req->first_name;
                $rulesInput["first_name"] = "required|string|max:125";
                $data["first_name"] = $req->first_name;
            }
            if ($req->last_name) {
                $input["last_name"] = $req->last_name;
                $rulesInput["last_name"] = "required|string|max:125";
                $data["last_name"] = $req->last_name ?? null;
            }
            if ($req->email) {
                $input["email"] = $req->email;
                $rulesInput["email"] = "required|email";
                $data["email"] = $req->email;
            }
            if ($req->phone) {
                $input["phone"] = $req->phone;
                $rulesInput["phone"] = "required|integer|not_in:0";
                $data["phone"] = $req->phone;
                $data["phone_code"] = Users::DEFAULT_PHONE_CODE;
            }
            if ($req->fulladdress) {
                $input["fulladdress"] = $req->fulladdress;
                $rulesInput["fulladdress"] = "required|string|min:45";
                $data["fulladdress"] = $req->fulladdress;
            }
            if ($req->password) {
                $input["password"] = $req->password;
                $rulesInput["password"] = "required|string";
                $data["password"] = Hash::make($req->password);
            }

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

            $getUser = Users::getUserById($req->uid);
            if (count($getUser) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $uniqEmail = Users::getUserByEmail($req->email);
            if (count($uniqEmail) > 0) {
                $notSameUser = 1;
                foreach ($uniqEmail as $key => $row) {
                    if ($row->id == $req->uid) {
                        $notSameUser = 0;
                    }
                }
                if ($notSameUser) {
                    $apiResp = Responses::bad_request("email has been used");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }
            $uniqPhone = Users::getUserByPhone($req->phone);
            if (count($uniqPhone) > 0) {
                $notSameUser = 1;
                foreach ($uniqPhone as $key => $row) {
                    if ($row->id == $req->uid) {
                        $notSameUser = 0;
                    }
                }
                if ($notSameUser) {
                    $apiResp = Responses::bad_request("phone has been used");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
            }

            DB::beginTransaction();

            Users::updateUser($req->uid, $data);

            if ($req->roles) {
                DB::table("model_has_roles")
                    ->where("model_id", $req->uid)
                    ->delete();

                DB::table("model_has_roles")->insert([
                    "role_id" => $req->roles,
                    "model_type" => "App\\User",
                    "model_id" => $req->uid,
                ]);
            }

            $apiResp = Responses::created("success update user");

            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_user(Request $req)
    {
        try {
            $now = time();

            $input = [
                "uid" => $req->uid,
            ];
            $rulesInput = [
                "uid" => "required|integer|not_in:0",
            ];
            $data = [
                "id" => $req->uid,
            ];

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

            $getUser = Users::getUserById($req->uid);
            if (count($getUser) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            $data["dlt"] = $now;
            $data["dlt_by"] = $req->auth->uid;

            DB::beginTransaction();

            Users::updateUser($req->uid, $data);

            $apiResp = Responses::created("success delete user");

            DB::commit();

			$log = [
				"module" => "User",
				"action" => "Delete",
				"desc" => "Delete user : ".$getUser[0]->email,
			];
			UserLogs::insert(Auth::user()->id, $log);

            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_search_user_name(Request $req)
    {
        try {
            $now = time();
            $roles = Users::arrRoles();
            $statuses = Users::arrStatus();

            $input = [
                "name" => $req->name,
                "roles" => $req->roles,
            ];
            $rulesInput = [
                "name" => "required|string|max:125",
                "roles" => "nullable|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"]);
            }

            if ($req->roles) {
                if (in_array($req->roles, $roles)) {
                } else {
                    $apiResp = Responses::bad_request("role not valid");
                    return new Response($apiResp, $apiResp["meta"]["code"]);
                }
                $users = Users::likeName($req->name, $req->roles);
            } else {
                $users = Users::likeName($req->name);
            }

            if (count($users) < 1) {
                $apiResp = Responses::not_found("user not found");
                return new Response($apiResp, $apiResp["meta"]["code"]);
            }

            foreach ($users as $k => $v) {
                // remove from array but no reindex array
                // unset($users[$k]);
                // remove from array and reindex array
                // array_splice($users, $k, 1);
                unset($users[$k]->password);
            }

            $apiResp = Responses::success("success search user by name");
            $apiResp["data"] = $users;

            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 view_user_logs1(Request $req)
	{
		$id = $req->id;

		$user = Users::getUserById($id)[0];
		$data = [
			'user' => $user
		];
		// dd($dtl);
		return view('menu_v1._userLogs', $data);
	}
    public function api_user_logs1(Request $req)
    {
        try {
			$id = $req->id;
			$tgl0 = $req->tgl0;
			$tgl1 = $req->tgl1;

			$d = [$id, $tgl0, $tgl1];
			$list = DB::select("SELECT * FROM t_user_log 
				WHERE userId = ? AND crt BETWEEN ? AND ?
				order by crt desc
			", $d);

			$apiResp = Responses::success("success user logs");
			$apiResp["data"] = $list;
			return new Response($apiResp, $apiResp["meta"]["code"]);
		} catch (\Exception $e) {
            DB::rollBack();
            $apiResp = Responses::error($e->getMessage());
            return new Response($apiResp, $apiResp["meta"]["code"]);
        }
    }
}
