@extends('app.app')

@php
    $no_permission = App\Models\UsersMenuPermissions::IS_NOPERMISSION;
    $no_permission_text = 'Ditolak';
    $is_permission = App\Models\UsersMenuPermissions::IS_PERMISSION;
    $is_permission_text = 'Diizinkan';
@endphp

@section('title')
    Menu Permissions
@endsection

@section('customcss')
    <style>
        th {
            border: 1px solid #bbb;
        }
    </style>
@endsection

@section('content')
    <div class="container-fluid">
        <div class="content">
            <div class="card">
                <div class="card-header">
                    <div class="row d-flex align-items-center">
                        <div class="col-3">
                            <p class="card-title text-bold mb-0">Menu Permissions (<span id="count_users_menu_permissions">0</span>)</p>
                        </div>
                        <div class="col text-end">
                            <button id="btnMdlNewPermission" class="btn btn-sm btn-danger">Tambah Permission Baru</button>
                        </div>
                        <div class="col-auto text-end ps-0">
                            {{-- <button class="btn btn-sm btn-danger">Upload</button> --}}
                            {{-- <button class="btn btn-sm btn-danger">Download</button> --}}
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table id="tPermissions" class="table table-hover table-border dataTable">
                            <thead>
                                <tr class="">
                                    <th class="" rowspan="2">#</th>
                                    <th class="text-center" rowspan="2"></th>
                                    <th class="" rowspan="2">Nama Permission</th>
                                    <th class="text-center" colspan="12">Menu Admin</th>
                                    <th class="text-center" colspan="4">Menu Finance</th>
                                    <th class="text-center" rowspan="2">Status</th>
                                </tr>
                                <tr>
                                    {{-- menu admin --}}
                                    <th class="">Menu Transaksi</th>
                                    <th class="">Menu Perusahaan</th>
                                    <th class="">Menu Zona</th>
                                    <th class="">Menu Pengguna</th>
                                    <th class="">Menu Konfigurasi Harga</th>
                                    <th class="">Menu Konfigurasi Asuransi</th>
                                    <th class="">Menu Konfigurasi Tipe Kendaraan</th>
                                    <th class="">Menu Konfigurasi Master Devices</th>
                                    <th class="">Menu Konfigurasi Daftar GPS</th>
                                    <th class="">Menu Konfigurasi Additional Items</th>
                                    <th class="">Menu Pengemudi</th>
                                    <th class="">Menu Kendaraan</th>
                                    {{-- menu finance --}}
                                    <th class="">Menu Ledger Balance</th>
                                    <th class="">Menu Pembayaran</th>
                                    <th class="">Menu Penagihan</th>
                                    <th class="">Menu Konfigurasi Additional Items</th>
                                </tr>
                            </thead>
                            <tbody>

                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="mdlNewPermission" aria-labelledby="mdlNewPermissionLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="mdlNewPermissionLabel">Tambah Permission Baru</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="formNewPermission" method="POST">
                        {{-- Primary --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Primary</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-name" class="col-form-label">Nama Permission<span class="text-danger">*</span></label>
                                    <input type="text" name="add-name" id="add-name" class="form-control">
                                </div>
                            </div>
                        </div>
                        {{-- Menu Admin --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Menu Admin</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_trx" class="col-form-label">Menu Transaksi<span class="text-danger">*</span></label>
                                    <select name="add-menu_trx" id="add-menu_trx" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_company" class="col-form-label">Menu Perusahaan<span class="text-danger">*</span></label>
                                    <select name="add-menu_company" id="add-menu_company" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_zone" class="col-form-label">Menu Zona<span class="text-danger">*</span></label>
                                    <select name="add-menu_zone" id="add-menu_zone" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_users" class="col-form-label">Menu Pengguna<span class="text-danger">*</span></label>
                                    <select name="add-menu_users" id="add-menu_users" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_conf_rates" class="col-form-label">Menu Konfigurasi Harga<span class="text-danger">*</span></label>
                                    <select name="add-menu_conf_rates" id="add-menu_conf_rates" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_conf_insurance" class="col-form-label">Menu Konfigurasi Asuransi<span class="text-danger">*</span></label>
                                    <select name="add-menu_conf_insurance" id="add-menu_conf_insurance" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_conf_truck_type" class="col-form-label">Menu Konfigurasi Tipe Kendaraan<span class="text-danger">*</span></label>
                                    <select name="add-menu_conf_truck_type" id="add-menu_conf_truck_type" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_devices" class="col-form-label">Menu Konfigurasi Master Devices<span class="text-danger">*</span></label>
                                    <select name="add-menu_devices" id="add-menu_devices" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_logs_devices" class="col-form-label">Menu Konfigurasi Daftar GPS<span class="text-danger">*</span></label>
                                    <select name="add-menu_logs_devices" id="add-menu_logs_devices" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_conf_adt_items" class="col-form-label">Menu Konfigurasi Additional Items<span class="text-danger">*</span></label>
                                    <select name="add-menu_conf_adt_items" id="add-menu_conf_adt_items" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_drivers" class="col-form-label">Menu Pengemudi<span class="text-danger">*</span></label>
                                    <select name="add-menu_drivers" id="add-menu_drivers" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_vehicles" class="col-form-label">Menu Kendaraan<span class="text-danger">*</span></label>
                                    <select name="add-menu_vehicles" id="add-menu_vehicles" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        {{-- Menu Finance --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Menu Finance</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_fnc_ledger_balance" class="col-form-label">Menu Ledger Balance<span class="text-danger">*</span></label>
                                    <select name="add-menu_fnc_ledger_balance" id="add-menu_fnc_ledger_balance" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_fnc_payment" class="col-form-label">Menu Pembayaran<span class="text-danger">*</span></label>
                                    <select name="add-menu_fnc_payment" id="add-menu_fnc_payment" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_fnc_billing" class="col-form-label">Menu Penagihan<span class="text-danger">*</span></label>
                                    <select name="add-menu_fnc_billing" id="add-menu_fnc_billing" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-menu_fnc_conf_adt_items" class="col-form-label">Menu Konfigurasi Additional Item<span class="text-danger">*</span></label>
                                    <select name="add-menu_fnc_conf_adt_items" id="add-menu_fnc_conf_adt_items" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        {{-- Utilites --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Utilities</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="add-status" class="form-label">Status<span class="text-danger">*</span></label>
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="add-status" name="add-status">
                                        <label class="form-check-label" for="add-status"><span class="text-dark" id="add-txtStatus">Inactive</span></label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button id="btnSubmitNewPer" type="button" class="btn btn-sm btn-danger">Submit data</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="mdlUpdtPermission" aria-labelledby="mdlUpdtPermissionLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-dialog-scrollable">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="mdlUpdtPermissionLabel">Edit Item</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <form id="formUpdtPermission" method="PUT">
                        {{-- Primary --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Primary</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-name" class="col-form-label">Nama Permission<span class="text-danger">*</span></label>
                                    <input type="text" name="updt-name" id="updt-name" class="form-control">
                                </div>
                            </div>
                        </div>
                        {{-- Menu Admin --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Menu Admin</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_trx" class="col-form-label">Menu Transaksi<span class="text-danger">*</span></label>
                                    <select name="updt-menu_trx" id="updt-menu_trx" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_company" class="col-form-label">Menu Perusahaan<span class="text-danger">*</span></label>
                                    <select name="updt-menu_company" id="updt-menu_company" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_zone" class="col-form-label">Menu Zona<span class="text-danger">*</span></label>
                                    <select name="updt-menu_zone" id="updt-menu_zone" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_users" class="col-form-label">Menu Pengguna<span class="text-danger">*</span></label>
                                    <select name="updt-menu_users" id="updt-menu_users" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_conf_rates" class="col-form-label">Menu Konfigurasi Harga<span class="text-danger">*</span></label>
                                    <select name="updt-menu_conf_rates" id="updt-menu_conf_rates" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_conf_insurance" class="col-form-label">Menu Konfigurasi Asuransi<span class="text-danger">*</span></label>
                                    <select name="updt-menu_conf_insurance" id="updt-menu_conf_insurance" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_conf_truck_type" class="col-form-label">Menu Konfigurasi Tipe Kendaraan<span class="text-danger">*</span></label>
                                    <select name="updt-menu_conf_truck_type" id="updt-menu_conf_truck_type" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_devices" class="col-form-label">Menu Konfigurasi Master Devices<span class="text-danger">*</span></label>
                                    <select name="updt-menu_devices" id="updt-menu_devices" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_logs_devices" class="col-form-label">Menu Konfigurasi Daftar GPS<span class="text-danger">*</span></label>
                                    <select name="updt-menu_logs_devices" id="updt-menu_logs_devices" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_conf_adt_items" class="col-form-label">Menu Konfigurasi Additional Items<span class="text-danger">*</span></label>
                                    <select name="updt-menu_conf_adt_items" id="updt-menu_conf_adt_items" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_drivers" class="col-form-label">Menu Pengemudi<span class="text-danger">*</span></label>
                                    <select name="updt-menu_drivers" id="updt-menu_drivers" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_vehicles" class="col-form-label">Menu Kendaraan<span class="text-danger">*</span></label>
                                    <select name="updt-menu_vehicles" id="updt-menu_vehicles" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        {{-- Menu Finance --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Menu Finance</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_fnc_ledger_balance" class="col-form-label">Menu Ledger Balance<span class="text-danger">*</span></label>
                                    <select name="updt-menu_fnc_ledger_balance" id="updt-menu_fnc_ledger_balance" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_fnc_payment" class="col-form-label">Menu Pembayaran<span class="text-danger">*</span></label>
                                    <select name="updt-menu_fnc_payment" id="updt-menu_fnc_payment" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_fnc_billing" class="col-form-label">Menu Penagihan<span class="text-danger">*</span></label>
                                    <select name="updt-menu_fnc_billing" id="updt-menu_fnc_billing" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                                <div class="col-sm-12 col-md-6">
                                    <label for="updt-menu_fnc_conf_adt_items" class="col-form-label">Menu Konfigurasi Additional Item<span class="text-danger">*</span></label>
                                    <select name="updt-menu_fnc_conf_adt_items" id="updt-menu_fnc_conf_adt_items" class="form-control" style="width:100%;">
                                        <option value="{{ $no_permission }}">{{ $no_permission_text }}</option>
                                        <option value="{{ $is_permission }}">{{ $is_permission_text }}</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        {{-- Utilites --}}
                        <div class="mb-3">
                            <div class="border-bottom">
                                <h6>Utilities</h6>
                            </div>
                            <div class="row">
                                <div class="col-sm-12">
                                    <label for="updt-status" class="form-label">Status<span class="text-danger">*</span></label>
                                    <div class="form-check form-switch">
                                        <input class="form-check-input" type="checkbox" id="updt-status" name="updt-status">
                                        <label class="form-check-label" for="updt-status"><span class="text-dark" id="updt-txtStatus">Inactive</span></label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" id="btnDelPer_updt" class="btn btn-sm btn-warning">Hapus ?</button>
                    <button type="button" class="btn btn-sm btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button id="btnSubmitUpdtPer" type="button" class="btn btn-sm btn-danger">Update Data</button>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" data-bs-backdrop="static" data-bs-keyboard="false" id="mdlDelPer" aria-labelledby="mdlDelPerLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="mdlDelPerLabel">Delete Item</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="d-flex justify-content-center">
                        <p class="mb-0">
                            Kamu yakin ingin menghapus item
                            <a href="#" class="text-danger">
                                <span id="del-name"></span>
                            </a>
                            ?
                        </p>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-sm btn-danger" data-bs-dismiss="modal">Close</button>
                    <button id="btnSubmitDelPer" type="button" class="btn btn-sm btn-secondary">Iya, hapus</button>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('customjs')
    <script src="{{ asset('assets/js/load-image.all.min.js') }}"></script>
    <script>
        'use strict';
        const State = {
            file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
            storage_lara: "{{ asset('storage') }}/",
            stts_isact: {
                active: "{{ App\Models\UsersMenuPermissions::IS_ACTIVE }}",
                inactive: "{{ App\Models\UsersMenuPermissions::IS_INACTIVE }}",
            },
            stts_permis: {
                permis: "{{ App\Models\UsersMenuPermissions::IS_PERMISSION }}",
                permis_text: "{{ $is_permission_text }}",
                no_permis: "{{ App\Models\UsersMenuPermissions::IS_NOPERMISSION }}",
                no_permis_text: "{{ $no_permission_text }}",
            },
            delay_typing_front: 1000,
        };

        const Wrapper = {
            activate: function() {
                Wrapper.event();
                DTable.activate();
                PerNew.activate();
                PerUpdt.activate();
                PerDel.activate();
            },
            event: function() {
                $('#add-status').bind('change', function() {
                    if ($(this).is(':checked'))
                        $('#add-txtStatus').html('Active')
                    else
                        $('#add-txtStatus').html('Inactive')
                });
                $('#updt-status').bind('change', function() {
                    if ($(this).is(':checked'))
                        $('#updt-txtStatus').html('Active')
                    else
                        $('#updt-txtStatus').html('Inactive')
                });
                // fix modal select2
                $('#add-type').select2({
                    dropdownParent: $('#mdlNewPermission'),
                });
                $('#updt-type').select2({
                    dropdownParent: $('#mdlUpdtPermission'),
                });
            },
        };

        const DTable = {
            activate: function() {
                DTable.reload();
            },
            reload: function() {
                // $('#tPermissions').DataTable();
                // if (Driver.Table.firstInitDataTable == 1) { loadTableSkeletonLoading() } else { Driver.Table.firstInitDataTable = 1; }
                $('#tPermissions').DataTable({
                    processing: true,
                    serverSide: false,
                    bLengthChange: true,
                    deferRender: true,
                    destroy: true,
                    ajax: {
                        url: "{{ route('api_list_menu_permissions') }}",
                        type: 'GET',
                        complete: function(jqXHR, textStatus, c) {
                            let count = jqXHR.responseJSON.count;
                            if (typeof count != 'undefined') {
                                $('#count_users_menu_permissions').text(count);
                            }
                            // removeTableSkeletonLoading()
                        },
                    },
                    deferRender: true,
                    columns: [{
                            data: 'DT_RowIndex',
                            className: 'text-end',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        {
                            data: 'action',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                let action = `
									<a href="#" class="text-decoration-none me-1 btnUpdtPer">
										<span class="icon ion-edit fz-16"></span>
									</a>
                                    `;
                                // <a href="#" class="text-decoration-none text-danger btnDelPer">
                                //     <span class="icon ion-trash-b fz-16"></span>
                                // </a>
                                return action;
                            }
                        },
                        {
                            data: 'name',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            createdCell: function(td, cellData, rowData, row, col) {
                                $(td).attr('data-id', rowData.id);
                                $(td).attr('data-name', rowData.name);
                            },
                        },
                        {
                            data: 'is_trx',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_trx == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_company',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_company == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_zone',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_zone == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_users',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_users == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_conf_rates',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_conf_rates == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_conf_insurance',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_conf_insurance == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_conf_truck_type',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_conf_truck_type == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_devices',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_devices == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_logs_devices',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_logs_devices == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_conf_adt_items',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_conf_adt_items == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_drivers',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_drivers == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_vehicles',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_vehicles == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_fnc_ledger_balance',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_fnc_ledger_balance == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_fnc_payment',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_fnc_payment == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_fnc_billing',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_fnc_billing == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_fnc_conf_adt_items',
                            className: 'text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.is_fnc_conf_adt_items == State.stts_permis.permis) {
                                    return State.stts_permis.permis_text;
                                }
                                return State.stts_permis.no_permis_text;
                            },
                        },
                        {
                            data: 'is_active',
                            className: 'text-center text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (data == State.stts_isact.active) return '<span class="badge rounded-pill bg-success">Active</span>';
                                return '<span class="badge rounded-pill bg-danger">Inactive</span>';
                            }
                        },
                    ],
                });
            },
        };

        const PerNew = {
            activate: function() {
                PerNew.event();
            },
            event: function() {
                // modal
                $('#btnMdlNewPermission').on('click', function() {
                    $('#mdlNewPermission').modal('show');
                });
                $('#mdlNewPermission').on('shown.bs.modal', function() {

                });
                // button
                $('#btnSubmitNewPer').on('click', function() {
                    let data = PerNew.getData();
                    PerNew.submitData(data);
                });
            },
            getData: function() {
                let data = {};

                $.map($('#formNewPermission').serializeArray(), function(n, i) {
                    let key = n['name'].replace('add-', '');
                    data[key] = n['value'];
                });

                if ($('#add-status').prop('checked')) {
                    data.status = State.stts_isact.active;
                } else {
                    data.status = State.stts_isact.inactive;
                }

                return data;
            },
            submitData: async function(data) {
                return new Promise((resolve, reject) => {
                    if (typeof $('#btnSubmitNewPer').attr('disabed') != 'undefined') {
                        resolve({
                            type: 'fail'
                        });
                        return false;
                    }
                    $('#btnSubmitNewPer').attr('disabed', true);
                    $('#btnSubmitNewPer').addClass('d-none');
                    $.ajax({
                        url: "{{ route('api_add_menu_permissions') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data: data,
                        success: (data, textStatus, jqXHR) => {
                            $('#btnSubmitNewPer').removeAttr('disabed');
                            $('#btnSubmitNewPer').removeClass('d-none');
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            Helper.toast('Success', 'just now', 'success menambahkan permission baru');
                            $('#mdlNewPermission').modal('hide');
                            DTable.reload();
                            resolve({
                                type: 'success'
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            $('#btnSubmitNewPer').removeAttr('disabed');
                            $('#btnSubmitNewPer').removeClass('d-none');
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'Please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                })
            },
        }

        const PerUpdt = {
            activate: function() {
                PerUpdt.event();
            },
            event: function() {
                // modal
                $('#tPermissions').on('click', '.btnUpdtPer', async function(e) {
                    let id = $(e.target).closest('tr').find('td[data-id]').data('id');
                    let resp = await PerUpdt.reqData({
                        id
                    });
                    if (resp.type != 'success') {
                        Helper.toast('Permission Not Found', 'just now', 'please try again');
                        return false;
                    }
                    PerUpdt.passDataToView(resp.data);
                });
                $('#mdlUpdtPermission').on('shown.bs.modal', function() {

                });
                // button
                $('#btnSubmitUpdtPer').on('click', function() {
                    let data = PerUpdt.getData();
                    PerUpdt.submitData(data);
                });
            },
            reqData: function(params) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_show_menu_permissions', '') }}/" + params.id,
                        method: 'GET',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                        },
                        data: params,
                        success: (data, textStatus, jqXHR) => {
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                data: data.data
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                });
            },
            passDataToView: async function(data) {
                $('#updt-name').val(data.name);
                $('#updt-menu_company').val(data.is_company).trigger('change');
                $('#updt-menu_conf_adt_items').val(data.is_conf_adt_items).trigger('change');
                $('#updt-menu_conf_insurance').val(data.is_conf_insurance).trigger('change');
                $('#updt-menu_conf_rates').val(data.is_conf_rates).trigger('change');
                $('#updt-menu_conf_truck_type').val(data.is_conf_truck_type).trigger('change');
                $('#updt-menu_devices').val(data.is_devices).trigger('change');
                $('#updt-menu_drivers').val(data.is_drivers).trigger('change');
                $('#updt-menu_logs_devices').val(data.is_logs_devices).trigger('change');
                $('#updt-menu_trx').val(data.is_trx).trigger('change');
                $('#updt-menu_users').val(data.is_users).trigger('change');
                $('#updt-menu_vehicles').val(data.is_vehicles).trigger('change');
                $('#updt-menu_zone').val(data.is_zone).trigger('change');

                if (data.is_active == State.stts_isact.active) {
                    $('#updt-status').prop('checked', true).trigger('change');
                } else {
                    $('#updt-status').prop('checked', false).trigger('change');
                }

                $('#mdlUpdtPermission').data('id', data.id);
                $('#mdlUpdtPermission').modal('show');
            },
            getData: function() {
                let data = {};
                data.id = $('#mdlUpdtPermission').data('id');

                $.map($('#formUpdtPermission').serializeArray(), function(n, i) {
                    let key = n['name'].replace('updt-', '');
                    data[key] = n['value'];
                });

                if ($('#updt-status').prop('checked')) {
                    data.status = State.stts_isact.active;
                } else {
                    data.status = State.stts_isact.inactive;
                }

                return data;
            },
            submitData: async function(data) {
                return new Promise((resolve, reject) => {
                    if (typeof $('#btnSubmitUpdtPer').attr('disabed') != 'undefined') {
                        resolve({
                            type: 'fail'
                        });
                        return false;
                    }
                    $('#btnSubmitUpdtPer').attr('disabed', true);
                    $('#btnSubmitUpdtPer').addClass('d-none');
                    $.ajax({
                        url: "{{ route('api_updt_menu_permissions', '') }}/" + data.id,
                        method: 'PUT',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data: data,
                        success: (data, textStatus, jqXHR) => {
                            $('#btnSubmitUpdtPer').removeAttr('disabed');
                            $('#btnSubmitUpdtPer').removeClass('d-none');
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            Helper.toast('Success', 'just now', 'success update permissions');
                            $('#mdlUpdtPermission').modal('hide');
                            DTable.reload();
                            resolve({
                                type: 'success'
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            $('#btnSubmitUpdtPer').removeAttr('disabed');
                            $('#btnSubmitUpdtPer').removeClass('d-none');
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'Please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                })
            },
        }

        const PerDel = {
            activate: function() {
                PerDel.event();
            },
            event: function() {
                // on table
                $('#tPermissions').on('click', '.btnDelPer', function(e) {
                    let row = $(e.target).closest('tr');
                    let id = row.find('td[data-id]').data('id');
                    let name = row.find('td[data-name]').data('name');
                    PerDel.passDataToView({
                        id,
                        name,
                    });
                    $('#mdlDelPer').data('id', id);
                    $('#mdlDelPer').modal('show');
                });
                $('#btnSubmitDelPer').on('click', function() {
                    let data = {
                        id: $('#mdlDelPer').data('id'),
                    };
                    PerDel.submitData(data);
                });
                // on modal update
                $('#btnDelPer_updt').on('click', function(e) {
                    const {
                        id,
                        name
                    } = PerUpdt.getData();
                    PerDel.passDataToView({
                        id,
                        name,
                    });
                    $('#mdlDelPer').data('id', id);
                    $('#mdlDelPer').modal('show');
                });
            },
            passDataToView: function(data) {
                $('#del-name').text(data.name);
                $('#del-price').text(data.price);
            },
            submitData: async function(data) {
                return new Promise((resolve, reject) => {
                    if (typeof $('#btnSubmitDelPer').attr('disabed') != 'undefined') {
                        resolve({
                            type: 'fail'
                        });
                        return false;
                    }
                    $('#btnSubmitDelPer').attr('disabed', true);
                    $.ajax({
                        url: "{{ route('api_del_menu_permissions', '') }}/" + data.id,
                        method: 'DELETE',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data: data,
                        success: (data, textStatus, jqXHR) => {
                            $('#btnSubmitDelPer').removeAttr('disabed');
                            if (data.meta.type != 'success') {
                                resolve({
                                    type: 'fail'
                                });
                                Helper.toast('Warning', 'just now', data.meta.message);
                                return false;
                            }
                            Helper.toast('Success', 'just now', 'success delete permission');
                            $('#mdlDelPer').modal('hide');
                            $('#mdlUpdtPermission').modal('hide');
                            DTable.reload();
                            resolve({
                                type: 'success'
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            $('#btnSubmitDelPer').removeAttr('disabed');
                            if (jqXHR.status >= 500) {
                                Helper.toast('Error', 'just now', 'Please try again');
                            } else {
                                Helper.toast('Error', 'just now', jqXHR.responseJSON.meta
                                    .message);
                            }
                            resolve({
                                type: 'error'
                            });
                        }
                    })
                })
            },
        }

        Wrapper.activate();
    </script>
@endsection
