@php
$user = Auth::user();
@endphp
@extends('app.app')
@section('title')
    User Checker
@endsection
@section('customcss')
@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">
                            <p class="card-title text-bold mb-0">Checker</p>
                            <p class="card-subtitle text-muted">Checker list</p>
                        </div>
                    </div>
                </div>
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-hover" id="tChecker">
                            <thead>
                                <tr class="">
                                    <th class="text-nowrap">#</th>
                                    <th class="text-nowrap text-center">Tindakan</th>
                                    <th class="text-nowrap">Kode Order</th>
                                    <th class="text-nowrap">Lokasi Penjemputan</th>
                                    <th class="text-nowrap">Lokasi Pengantaran</th>
                                    <th class="text-nowrap">Kendaraan</th>
                                    <th class="text-nowrap">Pengemudi</th>
                                    <th class="text-nowrap text-center">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                {{-- <tr class="">
                                    <td class="text-nowrap">1</td>
                                    <td class="text-nowrap text-center">
                                        <a href="{{ route('view_user_checker_view') }}" class="text-danger">
                                            <span class="icon ion-eye fz-16"></span>
                                        </a>
                                    </td>
                                    <td class="text-nowrap">#BKS00928887210001</td>
                                    <td class="text-nowrap">Gudang Pluit SiCepat (Jakarta)</td>
                                    <td class="text-nowrap">Kantor Pusat SiCepat (yogyakarta)</td>
                                    <td class="text-nowrap">AB 2201 XY</td>
                                    <td class="text-nowrap">Mansur Iman Darma</td>
                                    <td class="text-nowrap text-center"><span class="badge bg-success">Submited</span></td>
                                </tr> --}}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('customjs')
    <script>
        'use strict';
        const State = {
            file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
            storage_lara: "{{ asset('storage') }}/",
            stts_order: {
                wait: "{{ App\Models\Orders::STTS_WAIT }}",
                confirm: "{{ App\Models\Orders::STTS_CONFIRM }}",
                have_get_vhc: "{{ App\Models\Orders::STTS_HAVE_GET_VHC }}",
                pck: "{{ App\Models\Orders::STTS_PCK }}",
                go: "{{ App\Models\Orders::STTS_GO }}",
                arv: "{{ App\Models\Orders::STTS_ARV }}",
                drop: "{{ App\Models\Orders::STTS_DROP }}",
                client_pay: "{{ App\Models\Orders::STTS_CLIENT_PAY }}",
                vendor_payed: "{{ App\Models\Orders::STTS_VENDOR_PAYED }}",
                close: "{{ App\Models\Orders::STTS_CLOSE }}",
                cancel: "{{ App\Models\Orders::STTS_CANCEL }}",
            },
            type_check: {
                current: "{{ $user->chk_type }}",
                pickup: "{{ App\Models\Users::CHK_TYPE_PICKUP }}",
                drop: "{{ App\Models\Users::CHK_TYPE_DROP }}",
            },
            chk_pck_stts: {
                no: "{{ App\Models\OrdersPickups::CHK_STTS_NO }}",
                submit: "{{ App\Models\OrdersPickups::CHK_STTS_SUBMIT }}",
            },
            chk_drop_stts: {
                no: "{{ App\Models\OrdersDrops::CHK_STTS_NO }}",
                submit: "{{ App\Models\OrdersDrops::CHK_STTS_SUBMIT }}",
            },
            chk_stts: {
                waiting: "{{ App\Models\Orders::CHK_STTS_WAITING }}",
                has_pickup: "{{ App\Models\Orders::CHK_STTS_HAS_PICKUP }}",
                has_drop: "{{ App\Models\Orders::CHK_STTS_HAS_DROP }}",
            },
            ord_pck_drop: {
                is_aprv_no: "{{ App\Models\OrdersPckDrop::IS_APRV_NO }}",
                is_aprv_yes: "{{ App\Models\OrdersPckDrop::IS_APRV_YES }}",
            },
            url_view_order: "{{ route('view_user_checker_view') }}",
        };

        const Wrapper = {
            activate: function() {
                Wrapper.event();
                OrdTable.activate();
            },
            event: function() {},
        };

        const OrdTable = {
            activate: function() {
                OrdTable.reload();
            },
            reload: function() {
                // $('#tChecker').DataTable();
                // if (User.Table.firstInitDataTable == 1) { loadTableSkeletonLoading() } else { User.Table.firstInitDataTable = 1; }
                $('#tChecker').DataTable({
                    processing: true,
                    serverSide: false,
                    bLengthChange: true,
                    deferRender: true,
                    destroy: true,
                    ajax: {
                        url: "{{ route('api_user_checker_list_orders') }}?couple_pck_drop=1&order_by=desc",
                        type: 'GET',
                        complete: function(jqXHR, textStatus, c) {
                            let count = jqXHR.responseJSON.count;
                            if (typeof count != 'undefined') {
                                $('#count_orders').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 = ``;
                                action += `<a href="${State.url_view_order}?code=${row.ord_code}" class="text-danger">
                                    <span class="icon ion-eye fz-16"></span>
                                </a>`;
                                return action;
                            }
                        },
                        {
                            data: 'ord_code',
                            className: 'text-start',
                            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);
                                $(td).attr('data-phone', rowData.phone);
                                $(td).attr('data-phone_code', rowData.phone_code);
                                $(td).attr('data-client_group_name', rowData.client_group_name);
                            },
                            render: function(data, type, row, meta) {
                                return '#' + data;
                            },
                        },
                        {
                            data: 'pck_name',
                            className: 'text-start text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        {
                            data: 'drop_name',
                            className: 'text-start text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                        },
                        {
                            data: 'vhc_nopol1',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (row.vhc_nopol1) {
                                    return row.vhc_nopol1 + ' ' + row.vhc_nopol2 + ' ' + row
                                        .vhc_nopol3;
                                }
                                return `<span class="text-danger">not yet assigned</span>`;
                            },
                        },
                        {
                            data: 'drv_name',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (data) {
                                    return data;
                                }
                                return `<span class="text-danger">not yet assigned</span>`;
                            },
                        },
                        // {
                        //     data: 'pck_chk_stts',
                        //     className: 'text-center',
                        //     visible: true,
                        //     orderable: true,
                        //     searchable: true,
                        //     render: function(data, type, row, meta) {
                        //         // if (State.type_check.pickup == State.type_check.current) {
                        //         //     if (State.chk_pck_stts.no == row.pck_chk_stts) {
                        //         //         return '<span class="badge bg-warning text-dark">Waiting</span>';
                        //         //     } else if (State.chk_pck_stts.submit == row.pck_chk_stts) {
                        //         //         return '<span class="badge bg-success text-white">Submited</span>';
                        //         //     } else {
                        //         //         return '<span class="badge bg-secondary text-white">Unknown</span>';
                        //         //     }
                        //         // } else if (State.type_check.drop == State.type_check.current) {
                        //         //     if (State.chk_drop_stts.no == row.drop_chk_stts) {
                        //         //         return '<span class="badge bg-warning text-dark">Waiting</span>';
                        //         //     } else if (State.chk_drop_stts.submit == row.drop_chk_stts) {
                        //         //         return '<span class="badge bg-success text-white">Submited</span>';
                        //         //     } else {
                        //         //         return '<span class="badge bg-secondary text-white">Unknown</span>';
                        //         //     }
                        //         // } else {
                        //         //     return '<span class="badge bg-secondary text-white">Unknown Checker</span>';
                        //         // }
                        //         if (State.chk_stts.waiting == row.chk_stts) {
                        //             return '<span class="badge bg-danger text-white">Lapor Penjemputan</span>';
                        //         } else if (State.chk_stts.has_pickup == row.chk_stts) {
                        //             return '<span class="badge bg-danger text-white">Lapor Pengantaran</span>';
                        //         } else if (State.chk_stts.has_drop == row.chk_stts) {
                        //             return '<span class="badge bg-success text-white">Laporan Selesai</span>';
                        //         } else {
                        //             return '<span class="badge bg-secondary text-white">Unknown</span>';
                        //         }
                        //     },
                        {
                            data: 'is_aprv_pck',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                if (State.ord_pck_drop.is_aprv_yes == data) {
                                    return '<span class="badge bg-success text-white">Disetujui</span>';
                                } else {
                                    return '<span class="badge bg-danger text-white">Belum Disetujui</span>';
                                }
                            },
                        },
                    ],
                });
            },
        };

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