@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="tOrdChk">
                            <thead>
                                <tr class="">
                                    <th class="text-nowrap">#</th>
                                    <th class="text-nowrap text-center">Action</th>
                                    <th class="text-nowrap">Order Number</th>
                                    <th class="text-nowrap">Pickup Location</th>
                                    <th class="text-nowrap">Drop Location</th>
                                    <th class="text-nowrap">Vehicle Info</th>
                                    <th class="text-nowrap">Driver Name</th>
                                    <th class="text-nowrap text-center">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr class="">
                                    <td class="text-nowrap">1</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>
                                    <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>
                                </tr>
                                <tr class="">
                                    <td class="text-nowrap">2</td>
                                    <td class="text-nowrap">#BKS00928887210003</td>
                                    <td class="text-nowrap">Kantor Pusat SiCepat (yogyakarta)</td>
                                    <td class="text-nowrap">Gudang Pluit SiCepat (Jakarta)</td>
                                    <td class="text-nowrap">B 1022 AB</td>
                                    <td class="text-nowrap">Putra Ismail Daud</td>
                                    <td class="text-nowrap text-center"><span
                                            class="badge bg-warning text-dark">Waiting</span></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>
                                </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 }}",
            },
            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() {
                // $('#tOrdChk').DataTable();
                // if (User.Table.firstInitDataTable == 1) { loadTableSkeletonLoading() } else { User.Table.firstInitDataTable = 1; }
                $('#tOrdChk').DataTable({
                    processing: true,
                    serverSide: false,
                    bLengthChange: true,
                    deferRender: true,
                    destroy: true,
                    ajax: {
                        url: "{{ route('api_user_checker_list_orders') }}",
                        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-decoration-none me-1" data-bs-toggle="tooltip"
                                data-bs-placement="bottom" title="Edit">
                                <span class="icon ion-eye text-danger 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.stts_order.client_pay == row.status || State.stts_order.vendor_payed == row.status || State.stts_order.close == row.status) {
                                //     return '<span class="badge bg-danger text-white">Order Close</span>';
                                // } else if (State.stts_order.cancel == row.status) {
                                //     return '<span class="badge bg-danger text-white">Order Cancelled</span>';
                                // }
                                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>';
                                }
                            },
                        },
                    ],
                });
            },
        };

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