@extends('app.app')
@section('title')
    User Vendor
@endsection
@section('customcss')
@endsection

@section('content')
    <div class="container-fluid">
        <div class="content">
            <div class="row">
                <div class="col-sm-12">
                    <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">Transaksi</p>
                                    <p class="card-subtitle text-muted">Daftar Transaksi</p>
                                </div>
                                {{-- <div class="col text-end">
                                    <a href="{{ route('view_user_client_transaction_add') }}" class="btn btn-sm btn-danger">Tambah Transaksi</a>
                                </div> --}}
                                <div class="col-auto text-end ps-0">
                                    <a href="{{ route('view_transactions_spc_add') }}" class="btn btn-sm btn-danger">Transaksi baru</a>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="table-responsive">
                                <table id="tOrders" class="table table-hover dataTable">
                                    <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 text-center">Waktu Pemesanan</th>
                                            <th class="text-nowrap">Lokasi Penjemputan</th>
                                            <th class="text-nowrap">Lokasi Pengantaran</th>
                                            <th class="text-nowrap">Status</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <tr class="">
                                            <td class="text-nowrap">#</td>
                                            <td class="text-nowrap">#BKS00928887210001</td>
                                            <td class="text-nowrap text-center">Feb 20, 2022 17:29:50</td>
                                            <td class="text-nowrap">Gudang Pluit SiCepat (Jakarta)</td>
                                            <td class="text-nowrap">Kantor Pusat SiCepat (yogyakarta)</td>
                                            <td class="text-nowrap"><span class="badge bg-warning text-dark">Waiting For
                                                    Confirmation</span></td>
                                            <td class="text-nowrap text-center">
                                                <a href="{{ route('view_user_vendor_transaction_view') }}" class="text-danger"><span class="icon ion-eye fz-16"></span></a>
                                            </td>
                                        </tr>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </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 }}",
            },
            url_view_order: "{{ route('view_user_vendor_transaction_view') }}",
        };

        const Wrapper = {
            activate: function() {
                Wrapper.event();
                OrdTable.activate();
            },
            event: function() {},
            getHtmlSttsOrd: function(stts) {
                if (State.stts_order.wait == stts) {
                    return '<span class="badge bg-warning text-dark">Menunggu Konfirmasi</span>';
                } else if (State.stts_order.confirm == stts) {
                    return '<span class="badge bg-warning text-dark">Menunggu Konfirmasi</span>';
                } else if (State.stts_order.have_get_vhc == stts) {
                    return '<span class="badge bg-warning text-dark">Kendaraan Menuju Lokasi Penjemputan</span>';
                } else if (State.stts_order.pck == stts) {
                    return '<span class="badge bg-warning text-dark">Pick Up</span>';
                } else if (State.stts_order.go == stts) {
                    return '<span class="badge bg-info text-dark">Menuju Lokasi Pengantaran</span>';
                } else if (State.stts_order.arv == stts) {
                    return '<span class="badge bg-info text-dark">Arrived</span>';
                } else if (State.stts_order.drop == stts) {
                    return '<span class="badge bg-warning text-dark">Drop</span>';
                } else if (State.stts_order.client_pay == stts) {
                    return '<span class="badge bg-danger text-light">Finish (Waiting Payment)</span>';
                } else if (State.stts_order.vendor_payed == stts) {
                    return '<span class="badge bg-danger text-light">Finish (Waiting Payment)</span>';
                } else if (State.stts_order.close == stts) {
                    return '<span class="badge bg-danger text-light">Close</span>';
                } else if (State.stts_order.cancel == stts) {
                    return '<span class="badge bg-danger text-light">Cancel</span>';
                } else {
                    return '<span class="badge bg-secondary text-white">Unknown</span>';
                }
            },
        };

        const OrdTable = {
            activate: function() {
                OrdTable.reload();
            },
            reload: function() {
                // $('#tOrders').DataTable();
                // if (User.Table.firstInitDataTable == 1) { loadTableSkeletonLoading() } else { User.Table.firstInitDataTable = 1; }
                $('#tOrders').DataTable({
                    processing: true,
                    serverSide: false,
                    bLengthChange: true,
                    deferRender: true,
                    destroy: true,
                    ajax: {
                        url: "{{ route('api_user_vendor_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 ord_codes = row.ord_code;
                                if (typeof row.childs != 'undefined' && row.childs.length > 0) {
                                    for (const child of row.childs) {
                                        ord_codes += ',' + child.ord_code;
                                    }
                                }
                                let action = `
                                    <a href="${State.url_view_order}?code=${ord_codes}"
                                        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) {
                                let txt = '#' + data;
                                for (const child of row.childs) {
                                    txt += '<br>#' + child.ord_code;
                                }
                                return txt;
                            },
                        },
                        {
                            data: 'crt',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                let txt = moment.unix(data).format('DD MMM YYYY HH:mm:ss');
                                for (const child of row.childs) {
                                    txt += '<br>' + moment.unix(child.crt).format('DD MMM YYYY HH:mm:ss');
                                }
                                return txt;
                            },
                        },
                        {
                            data: 'pck_name',
                            className: 'text-start text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                let txt = data;
                                for (const child of row.childs) {
                                    txt += '<br>' + child.pck_name;
                                }
                                return txt;
                            },
                        },
                        {
                            data: 'drop_name',
                            className: 'text-start text-nowrap',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                let txt = data;
                                for (const child of row.childs) {
                                    txt += '<br>' + child.drop_name;
                                }
                                return txt;
                            },
                        },
                        {
                            data: 'status',
                            className: 'text-center',
                            visible: true,
                            orderable: true,
                            searchable: true,
                            render: function(data, type, row, meta) {
                                // console.log("row: ", row.status)
                                // let txt = Wrapper.getHtmlSttsOrd(data);
                                // for (const child of row.childs) {
                                //     txt += '<br>' + Wrapper.getHtmlSttsOrd(child.status);
                                // }
                                let a = ''
                                if (row.status == 10) {
                                    a = `<span class="btn btn-sm btn-success">Complete</span>`
                                } else if (row.status == 11) {
                                    a = `<span class="btn btn-sm btn-danger">Cancel</span>`
                                } else {
                                    a = `<span class="btn btn-sm btn-warning">Active</span>`
                                }
                                return a;
                            },
                        },
                    ],
                });
            },
        };

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