@extends('app.app')
@section('title')
    Transactions Special
@endsection
@section('content')
    <div class="container-fluid">
        <div class="content">
            <div class="row">
                <div class="col-sm-6">
                    <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">Tambah Pengiriman Baru (Khusus)</p>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row mb-3 d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Pengiriman Untuk Client</p>
                                    {{-- <small>Masukkan tanggal, jam, lokasi penjemputan, dan lokasi pengantaran yang ada pada daftar zona yang telah didaftarkan</small> --}}
                                </div>
                                <div class="col-sm-5 col-5 mb-3">
                                    <label for="add-client" class="form-label text-nowrap">Pilih Client</label>
                                    <select name="add-client" id="add-client" class="select2 form-control">
                                        <option value="" selected disabled>Belum ada Client yang dipilih</option>
                                        @foreach ($uclients as $uclient)
                                            <option value="{{ $uclient->id }}" data-uid="{{ $uclient->id }}"
                                                data-cptid="{{ $uclient->client_group_id }}">
                                                {{ $uclient->first_name." ($uclient->client_group_name)" }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="row mb-3 d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Penjemputan dan Pengantaran</p>
                                    {{-- <small>select time, pickup and drop zone from your zone list. Can't find your zone? <a href="{{ route('view_zone') }}">Add new zone</a></small> --}}
                                    <small>Masukkan tanggal, jam, lokasi penjemputan, dan lokasi pengantaran yang ada pada daftar zona yang telah didaftarkan</small>
                                </div>
                                <div class="col-sm-5 col-5 mb-3">
                                    <label for="add-pickup_date" class="form-label text-nowrap">Tanggal Penjemputan</label>
                                    <input type="date" name="add-pickup_date" id="add-pickup_date" class="form-control">
                                </div>
                                <div class="col-2"></div>
                                <div class="col-sm-5 col-5 mb-3">
                                    <label for="add-pickup_time" class="form-label text-nowrap">Waktu Penjemputan</label>
                                    {{-- <input type="time" name="add-pickup_time" id="add-pickup_time" class="form-control"
                                        min="23:30"> --}}
                                    <div class="row">
                                        <div class="col-md-4 col-lg-3" style="padding:0 0 0 0.75rem;">
                                            <select name="add-pickup_time_hour" id="add-pickup_time_hour" class="form-control">
                                                <option value="" selected disabled hidden>--</option>
                                                @for ($i=0; $i<24; $i++)
                                                <option value="{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}">{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}</option>
                                                @endfor
                                            </select>
                                        </div>
                                        <div class="col-md-4 col-lg-3" style="padding:0 0 0 0.75rem;">
                                            <select name="add-pickup_time_minute" id="add-pickup_time_minute" class="form-control">
                                                <option value="" selected disabled hidden>--</option>
                                                @for ($i=0; $i<60; $i++)
                                                <option value="{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}">{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}</option>
                                                @endfor
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row mb-3 d-flex align-items-center">
                                <div class="col-5 mb-3">
                                    <div class="form-group mb-2">
                                        <label for="add-pickup_zone" class="form-label">Lokasi Penjemputan</label>
                                        <select name="add-pickup_zone" id="add-pickup_zone" class="select2 form-control">
                                            <option value="" selected disabled>Belum ada lokasi yang dipilih</option>
                                            {{-- @foreach ($pickups as $pickup)
                                                <option value="{{ $pickup->id }}" data-id="{{ $pickup->id }}"
                                                    data-title="{{ $pickup->name }}" data-addr="{{ $pickup->fulladdress }}">
                                                    {{ $pickup->name }}</option>
                                            @endforeach --}}
                                        </select>
                                    </div>
                                    <div class="form-group mb-2">
                                        <label for="add-pickup_pic_name" class="form-label text-nowrap">Nama Pengirim</label>
                                        <input type="text" name="add-pickup_pic_name" id="add-pickup_pic_name" class="form-control">
                                    </div>
                                    <div class="form-group mb-2">
                                        <label for="add-pickup_pic_phone" class="form-label text-nowrap">No Telepon Pengirim</label>
                                        <input type="text" name="add-pickup_pic_phone" id="add-pickup_pic_phone" class="form-control">
                                    </div>
                                </div>
                                <div class="col-2 text-center">
                                    <label for="" class="form-label mb-0 mt-2">&nbsp;</label>
                                    {{-- <p class="mb-0">to</p> --}}
                                    <span class="ion-arrow-right-c text-danger" style="font-size: 15px"></span>
                                </div>
                                <div class="col-5 mb-3">
                                    <div class="form-group mb-2">
                                        <label for="add-drop_zone" class="form-label">Lokasi Pengantaran</label>
                                        <select name="add-drop_zone" id="add-drop_zone" class="select2 form-control">
                                            <option value="" selected disabled>Belum ada lokasi yang dipilih</option>
                                            {{-- @foreach ($drops as $drop)
                                                <option value="{{ $drop->id }}" data-id="{{ $drop->id }}"
                                                    data-title="{{ $drop->name }}" data-addr="{{ $drop->fulladdress }}">
                                                    {{ $drop->name }}</option>
                                            @endforeach --}}
                                        </select>
                                    </div>
                                    <div class="form-group mb-2">
                                        <label for="add-drop_pic_name" class="form-label text-nowrap">Nama Penerima</label>
                                        <input type="text" name="add-drop_pic_name" id="add-drop_pic_name" class="form-control">
                                    </div>
                                    <div class="form-group mb-2">
                                        <label for="add-drop_pic_phone" class="form-label text-nowrap">No Telepon Penerima</label>
                                        <input type="text" name="add-drop_pic_phone" id="add-drop_pic_phone" class="form-control">
                                    </div>
                                </div>
                            </div>
                            <div class="row d-flex align-items-center mb-3">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Informasi Kendaraan</p>
                                    {{-- <small>You must fill in at least 1 of the package information.</small> --}}
                                </div>
                                {{-- <div class="col-7 mb-3 text-bold">Description</div>
                                <div class="col-5 mb-3 text-bold">Unit</div>

                                <div class="col-7 mb-3">Weight</div>
                                <div class="col-5 mb-3 d-flex align-items-center">
                                    <input type="number" name="add-weight" id="add-weight" class="form-control w-75 me-2">Kg
                                </div>

                                <div class="col-7 mb-3">Volume</div>
                                <div class="col-5 mb-3 d-flex align-items-center">
                                    <input type="number" name="add-cbm" id="add-cbm" class="form-control w-75 me-2">m³
                                </div>

                                <div class="col-7 mb-3">Koli</div>
                                <div class="col-5 mb-3 d-flex align-items-center">
                                    <input type="number" name="add-koli" id="add-koli" class="form-control w-75 me-2">pcs
                                </div> --}}
                                <div class="col-5 mb-3">
                                    <label for="add-truck_type" class="form-label">Jenis Kendaraan</label>
                                    <select name="add-truck_type" id="add-truck_type" class="select2 form-control">
                                        {{-- <option value="" data-name="Let system choose automatically" selected>Let system
                                            choose automatically
                                        </option> --}}
                                        <option value="" selected disabled>Pilih kendaraan</option>
                                        @foreach ($truck_types as $tt)
                                            <option value="{{ $tt->type_id }}" data-id="{{ $tt->id }}" data-type_id="{{ $tt->type_id }}"
                                                data-name="{{ $tt->type_name }}">
                                                {{ $tt->type_name }}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            {{-- <div class="row d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Package list</p>
                                    <small>Upload your packing list according to the specified format. don't have it? <a
                                            href="#" id="download_template_packing_list">Download packing list
                                            template</a></small>
                                </div>
                                <div id="add-group_upload_packinglist" class="col-sm-12">
                                    <label for="add-packinglist" class="form-label">Upload Packing List</label>
                                    <input class="form-control" type="file" name="add-packinglist" id="add-packinglist"
                                        required>
                                </div>
                                <div id="add-group_upload_packinglist_spinner" class="col-12 d-none">
                                    <div class="spinner-border" role="status">
                                        <span class="visually-hidden">Loading...</span>
                                    </div>
                                </div>
                            </div> --}}
                            <div class="row d-flex align-items-center mb-3">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Asuransi</p>
                                    <small>Apakah Anda ingin menambahkan Asuransi untuk pengiriman ini?</small>
                                </div>
                                <div class="col-5">
                                    <div class="form-check">
                                        <input class="form-check-input" type="checkbox" value="" id="insuranceCheck">
                                        <label class="form-check-label" for="insuranceCheck">
                                            Ya, Asuransikan pengiriman
                                        </label>
                                    </div>
                                </div>
                            </div>
                            <div class="row d-flex align-items-center mb-3 pb-3 d-none" id="insurancePrice">
                                <div class="col-5">
                                    <label for="insuranceInput" class="form-label">Total Harga Pertanggungan (Rp)</label>
                                    <input type="text" name="insuranceInput" class="form-control" id="insuranceInput">
                                </div>
                            </div>
                            <div class="row d-flex align-items-center mb-3">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Harga</p>
                                    <small>Masukkan harga jual kepada client sudah termasuk premi asuransi.</small>
                                </div>
                                <div class="col-5">
                                    <label for="add-sellPrice" class="form-label">Harga (Rp)</label>
                                    <input type="text" name="add-sellPrice" id="add-sellPrice" class="form-control">
                                </div>
                            </div>
                            {{-- Termin --}}
                            <div class="row d-flex align-items-start mt-3">
                                <div class="col-5">
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Termin 1</p>
                                        <small>Masukkan termin 1</small>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-sell_termin1_amt_type" class="form-label text-nowrap">Jenis Jumlah</label>
                                        <select name="add-sell_termin1_amt_type" id="add-sell_termin1_amt_type" class="select2 form-control">
                                            <option value="" selected>Tidak dipilih</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_FLAT }}">Flat</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_PERCENT }}">Percent</option>
                                        </select>
                                    </div>
                                    <div class="mb-3 sell_subTermin1 d-none">
                                        <label for="add-sell_termin1_amt" class="form-label text-nowrap">Jumlah</label>
                                        <input type="text" name="add-sell_termin1_amt" id="add-sell_termin1_amt" class="form-control" placeholder="">
                                        <small class="text-dark sell_subTermin1-calc_percent">Jumlah Flat: Rp<span id="add-sell_termin1_calc_percent" data-val="0"></span></small>
                                    </div>
                                    <div class="mb-3 sell_subTermin1 d-none">
                                        <label for="add-sell_termin1_date" class="form-label text-nowrap">Tanggal</label>
                                        <input type="date" name="add-sell_termin1_date" id="add-sell_termin1_date" class="form-control">
                                    </div>
                                    <div class="mb-3 sell_subTermin1 d-none">
                                        <label for="add-sell_termin1_time" class="form-label text-nowrap">Waktu</label>
                                        {{-- <input type="time" name="add-sell_termin1_time" id="add-sell_termin1_time" class="form-control"
                                            min="23:30"> --}}
                                        <div class="row">
                                            <div class="col-md-4 col-lg-3" style="padding:0 0 0 0.75rem;">
                                                <select name="add-sell_termin1_time_hour" id="add-sell_termin1_time_hour" class="form-control">
                                                    <option value="" selected disabled hidden>--</option>
                                                    @for ($i=0; $i<24; $i++)
                                                    <option value="{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}">{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}</option>
                                                    @endfor
                                                </select>
                                            </div>
                                            <div class="col-md-4 col-lg-3" style="padding:0 0 0 0.75rem;">
                                                <select name="add-sell_termin1_time_minute" id="add-sell_termin1_time_minute" class="form-control">
                                                    <option value="" selected disabled hidden>--</option>
                                                    @for ($i=0; $i<60; $i++)
                                                    <option value="{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}">{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}</option>
                                                    @endfor
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-2"></div>
                                <div class="col-5">
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Termin 2</p>
                                        <small>Masukkan sell_termin 2</small>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-sell_termin2_amt_type" class="form-label text-nowrap">Jenis Jumlah</label>
                                        <select name="add-sell_termin2_amt_type" id="add-sell_termin2_amt_type" class="select2 form-control" disabled>
                                            <option value="" selected>Tidak dipilih</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_FLAT }}">Flat</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_PERCENT }}">Percent</option>
                                        </select>
                                    </div>
                                    <div class="mb-3 sell_subTermin2 d-none">
                                        <label for="add-sell_termin2_amt" class="form-label text-nowrap">Jumlah</label>
                                        <input type="text" name="add-sell_termin2_amt" id="add-sell_termin2_amt" class="form-control" placeholder="" readonly>
                                        <small class="text-dark sell_subTermin2-calc_percent">Jumlah Flat: Rp<span id="add-sell_termin2_calc_percent" data-val="0"></span></small>
                                    </div>
                                </div>
                            </div>
                            <div class="row d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Durasi Pengantaran</p>
                                    <small>Masukkan berapa lama durasi pengantaran.</small>
                                </div>
                                <div class="col-5">
                                    <label for="add-leadTime" class="form-label">Lama Hari</label>
                                    <input type="number" name="add-leadTime" id="add-leadTime" class="form-control">
                                </div>
                            </div>
                        </div>
                        {{-- <div class="card-footer">
                            <div class="row d-flex align-items-center">
                                <div class="col-8">
                                    <p class="mb-0">
                                        Sudahkah Anda mengisi semua kolom yang ada?
                                    </p>
                                </div>
                                <div class="col-4 text-end">
                                    <button class="btn btn-warning btn-block btn-sm" id="btnCalculate">Kalkulasi Sekarang</button>
                                </div>
                            </div>
                        </div> --}}
                    </div>
                </div>
                <div class="col-sm-6">
                    {{-- Vendor & Driver & Termin --}}
                    <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">Vendor & Pengemudi</p>
                                </div>
                            </div>
                        </div>
                        <div class="card-body">
                            <div class="row d-flex align-items-start">
                                <div class="col-sm-5 col-5">
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Informasi Vendor</p>
                                    </div>
                                    <div class="mb-3">
                                        {{-- suggestion --}}
                                        <label for="add-vdr_name" class="form-label text-nowrap">Nama Vendor</label>
                                        <input type="text" name="add-vdr_name" id="add-vdr_name" class="form-control" placeholder="">
                                    </div>
                                    <div class="mb-3">
                                        {{-- lock from here --}}
                                        <label for="add-vdr_phone" class="form-label text-nowrap">Nomor Telepon</label>
                                        <input type="number" name="add-vdr_phone" id="add-vdr_phone" class="form-control" placeholder="">
                                    </div>
                                    <div class="mb-3">
                                        {{-- lock from here --}}
                                        <label for="add-vdr_email" class="form-label text-nowrap">Email</label>
                                        <input type="email" name="add-vdr_email" id="add-vdr_email" class="form-control" placeholder="">
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-vdr_addr" class="form-label text-nowrap">Alamat</label>
                                        <textarea name="add-vdr_addr" id="add-vdr_addr" cols="30" rows="5" class="form-control"></textarea>
                                    </div>
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Pembayaran</p>
                                        <small>Masukkan informasi pembayaran vendor.</small>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-vdr_bank" class="form-label">Bank</label>
                                        <select id="add-vdr_bank" name="add-vdr_bank" class="form-control select2" style="width:100%;">
                                            <option value="" selected disabled>Choose</option>
                                            @foreach ($banks as $bank)
                                                <option value="{{ $bank->bank_code }}" data-id="{{ $bank->id }}" data-code="{{ $bank->bank_code }}" data-short="{{ $bank->bank_short_name }}" data-name="{{ $bank->bank_name }}">{{ $bank->bank_short_name . ' (' . $bank->bank_code . ')' }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-vdr_acc_number" class="form-label">Nomor Rekening</label>
                                        <input type="number" class="form-control form-control-sm" name="add-vdr_acc_number"
                                            id="add-vdr_acc_number">
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-vdr_acc_name" class="form-label">Pemilik Rekening</label>
                                        <input type="text" class="form-control form-control-sm" name="add-vdr_acc_name"
                                            id="add-vdr_acc_name">
                                    </div>
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Biaya</p>
                                        <small>Masukkan harga beli kepada vendor.</small>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-buyPrice" class="form-label">Biaya (Rp)</label>
                                        <input type="text" name="add-buyPrice" id="add-buyPrice" class="form-control">
                                    </div>
                                </div>
                                <div class="col-sm-2"></div>
                                <div class="col-sm-5 col-5">
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Kendaraan & Pengemudi</p>
                                    </div>
                                    <div class="mb-3">
                                        {{-- lock from here --}}
                                        <label for="add-device_id" class="form-label text-nowrap">Device ID</label>
                                        {{-- <input type="number" name="add-device_id" id="add-device_id" class="form-control" placeholder=""> --}}
                                        <select id="add-device_id" class="form-control" style="width:100%;">
                                            <option value="">Tidak ada device yang digunakan</option>
                                            {{-- <option value="{{ $device->id }}" data-device_id="{{ $device->device_id }}" data-simcard="{{ $device->simcard }}">{{ $device->device_id . ' (' . implode(' ', str_split('0' . $device->simcard, 4)) . ')' }}</option> --}}
                                            @foreach ($devices as $device)
                                                <option value="{{ $device->id }}" data-device_id="{{ $device->device_id }}" data-simcard="{{ $device->simcard }}">{{ $device->name . ' (' . implode(' ', str_split($device->device_id, 4)) . ')' }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                    <div class="mb-3">
                                        {{-- lock from here --}}
                                        <label for="add-vhc_nopol" class="form-label text-nowrap">Plat Nomor Kendaraan</label>
                                        <input type="text" name="add-vhc_nopol" id="add-vhc_nopol" class="form-control" placeholder="BG 1989 AD">
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-vhc_stnk_taxexp" class="col-form-label">Tax Expired:</label>
                                        <input type="date" id="add-vhc_stnk_taxexp" class="form-control">
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-vhc_kir_exp" class="col-form-label">Kir Expired:</label>
                                        <input type="date" id="add-vhc_kir_exp" class="form-control">
                                    </div>
                                    <div class="mb-3">
                                        {{-- suggestion --}}
                                        <label for="add-drv_name" class="form-label text-nowrap">Nama Pengemudi 1</label>
                                        <input type="text" name="add-drv_name" id="add-drv_name" class="form-control" placeholder="">
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-drv_name2" class="form-label text-nowrap">Nama Pengemudi 2</label>
                                        <input type="text" name="add-drv_name2" id="add-drv_name2" class="form-control" placeholder="">
                                    </div>
                                    <div class="mb-3">
                                        {{-- lock from here --}}
                                        <label for="add-drv_phone" class="form-label text-nowrap">Nomor Telepon 1</label>
                                        <input type="number" name="add-drv_phone" id="add-drv_phone" class="form-control" placeholder="">
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-drv_phone2" class="form-label text-nowrap">Nomor Telepon 2</label>
                                        <input type="number" name="add-drv_phone2" id="add-drv_phone2" class="form-control" placeholder="">
                                    </div>
                                </div>
                            </div>
                            {{-- Termin --}}
                            <div class="row d-flex align-items-start mt-3">
                                <div class="col-5">
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Termin 1</p>
                                        <small>Masukkan termin 1</small>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-termin1_amt_type" class="form-label text-nowrap">Jenis Jumlah</label>
                                        <select name="add-termin1_amt_type" id="add-termin1_amt_type" class="select2 form-control">
                                            <option value="" selected>Tidak dipilih</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_FLAT }}">Flat</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_PERCENT }}">Percent</option>
                                        </select>
                                    </div>
                                    <div class="mb-3 subTermin1 d-none">
                                        <label for="add-termin1_amt" class="form-label text-nowrap">Jumlah</label>
                                        <input type="text" name="add-termin1_amt" id="add-termin1_amt" class="form-control" placeholder="">
                                        <small class="text-dark subTermin1-calc_percent">Jumlah Flat: Rp<span id="add-termin1_calc_percent" data-val="0"></span></small>
                                    </div>
                                    <div class="mb-3 subTermin1 d-none">
                                        <label for="add-termin1_date" class="form-label text-nowrap">Tanggal</label>
                                        <input type="date" name="add-termin1_date" id="add-termin1_date" class="form-control">
                                    </div>
                                    <div class="mb-3 subTermin1 d-none">
                                        <label for="add-termin1_time" class="form-label text-nowrap">Waktu</label>
                                        {{-- <input type="time" name="add-termin1_time" id="add-termin1_time" class="form-control"
                                            min="23:30"> --}}
                                        <div class="row">
                                            <div class="col-md-4 col-lg-3" style="padding:0 0 0 0.75rem;">
                                                <select name="add-termin1_time_hour" id="add-termin1_time_hour" class="form-control">
                                                    <option value="" selected disabled hidden>--</option>
                                                    @for ($i=0; $i<24; $i++)
                                                    <option value="{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}">{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}</option>
                                                    @endfor
                                                </select>
                                            </div>
                                            <div class="col-md-4 col-lg-3" style="padding:0 0 0 0.75rem;">
                                                <select name="add-termin1_time_minute" id="add-termin1_time_minute" class="form-control">
                                                    <option value="" selected disabled hidden>--</option>
                                                    @for ($i=0; $i<60; $i++)
                                                    <option value="{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}">{{ str_pad($i, 2, '0', STR_PAD_LEFT) }}</option>
                                                    @endfor
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-2"></div>
                                <div class="col-5">
                                    <div class="mb-3">
                                        <p class="text-danger text-bold mb-0">Termin 2</p>
                                        <small>Masukkan termin 2</small>
                                    </div>
                                    <div class="mb-3">
                                        <label for="add-termin2_amt_type" class="form-label text-nowrap">Jenis Jumlah</label>
                                        <select name="add-termin2_amt_type" id="add-termin2_amt_type" class="select2 form-control" disabled>
                                            <option value="" selected>Tidak dipilih</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_FLAT }}">Flat</option>
                                            <option value="{{ \App\Models\OrdersTermins::AMT_TYPE_PERCENT }}">Percent</option>
                                        </select>
                                    </div>
                                    <div class="mb-3 subTermin2 d-none">
                                        <label for="add-termin2_amt" class="form-label text-nowrap">Jumlah</label>
                                        <input type="text" name="add-termin2_amt" id="add-termin2_amt" class="form-control" placeholder="" readonly>
                                        <small class="text-dark subTermin2-calc_percent">Jumlah Flat: Rp<span id="add-termin2_calc_percent" data-val="0"></span></small>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="card-footer">
                            <div class="row d-flex align-items-center">
                                <div class="col-8">
                                    <p class="mb-0">
                                        Sudahkah Anda mengisi semua kolom yang ada?
                                    </p>
                                </div>
                                <div class="col-4 text-end">
                                    <button class="btn btn-warning btn-block btn-sm" id="btnCalculate">Kalkulasi Sekarang</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    {{-- Checkout --}}
                    <div class="card d-none" id="checkOutView">
                        <div class="card-header">
                            <div class="row d-flex align-items-center">
                                <div class="col">
                                    <p class="card-title text-bold mb-0">Checkout</p>
                                </div>
                            </div>
                        </div>
                        <div id="checkout-spinner" class="card-body pb-0 d-none">
                            <div class="row d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <div class="spinner-border" role="status">
                                        <span class="visually-hidden">Loading...</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="card-body pb-0 checkoutBody">
                            <div class="row d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Penjemputan dan Pengantaran</p>
                                </div>
                                <div class="col-sm-5 mb-3">
                                    <label for="" class="form-label">Tanggal Penjemputan</label>
                                    <div class="card bg-light border mb-0">
                                        <div class="card-body">
                                            <p id="checkout-pickup_at" class="mb-0">Feb 22, 2022 23:59:00</p>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-2"></div>
                                <div class="col-sm-5 mb-3">
                                    <label for="" class="form-label">Jenis Kendaraan</label>
                                    <div class="card bg-light border mb-0">
                                        <div class="card-body">
                                            <p id="checkout-truck_type" class="mb-0">CDD Long</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row d-flex align-items-center">
                                <div class="col-5 mb-3">
                                    <label for="" class="form-label">Lokasi Penjemputan</label>
                                    <div class="card bg-light border mb-0">
                                        <div class="card-body">
                                            <p id="checkout-pickup_zone_title" class="text-bold mb-2">Gudang Pluit SiCepat
                                                (Jakarta)</p>
                                            <p id="checkout-pickup_zone_addr" class="mb-0">Jl. Pluit Karang Karya
                                                2 No.33, RT.7/RW.16, Pejagalan,
                                                Kec. Penjaringan, Kota Jkt Utara, Daerah Khusus Ibukota Jakarta 14440</p>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-2 text-center">
                                    <label for="" class="form-label mb-0 mt-0">&nbsp;</label>
                                    {{-- <p class="mb-0">to</p> --}}
                                    <span class="ion-arrow-right-c text-danger" style="font-size: 15px"></span>
                                </div>
                                <div class="col-5 mb-3">
                                    <label for="" class="form-label">Lokasi Pengantaran</label>
                                    <div class="card bg-light border mb-0">
                                        <div class="card-body">
                                            <p id="checkout-drop_zone_title" class="text-bold mb-2">Kantor Pusat SiCepat
                                                (yogyakarta)</p>
                                            <p id="checkout-drop_zone_addr" class="mb-0">Jl. Prof. Herman Yohanes
                                                No.989, Terban, Gondokusuman,
                                                Kota Yogyakarta, Daerah Istimewa Yogyakarta 55223, Indonesia</p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            {{-- <div class="row d-flex align-items-center mb-3">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Informasi Pengiriman</p>
                                </div>
                                <div id="checkout-unit" class="row mb-3">
                                    <div class="col-sm-5">
                                        <label for="" class="form-label">Koli</label>
                                        <div class="card bg-light border mb-0 w-100">
                                            <div class="card-body">
                                                <p class="mb-0">150 Pcs</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-sm-5">
                                    <label for="" class="form-label">Jenis Kendaraan</label>
                                    <div class="card bg-light border mb-0 w-100">
                                        <div class="card-body">
                                            <p id="checkout-truck_type" class="mb-0">Box Truck</p>
                                        </div>
                                    </div>
                                </div>
                            </div> --}}
                            {{-- <div class="row d-flex align-items-center">
                                <div class="col-12 mb-3">
                                    <p class="text-danger text-bold mb-0">Package list</p>
                                </div>
                                <div class="col-sm-12">
                                    <label for="formFile" class="form-label">File Packing List</label>
                                    <div class="d-flex align-items-center mb-3">
                                        <p class="mb-0 text-success me-4">package-list.xlsx</p>
                                        <button id="checkout-download_packing_list"
                                            class="btn btn-sm btn-info">Download</button>
                                    </div>
                                </div>
                            </div> --}}
                            <div class="row">
                                <div class="col-12">
                                    <hr>
                                </div>
                            </div>
                            <div class="row d-flex align-items-center">
                                <div class="col-12 mb-3 mt-3">
                                    <p class="text-danger text-bold mb-0">Pilih Layanan</p>
                                    <small>Berikut layanan yang tersedia untuk pesanan Anda</small>
                                </div>
                                <div class="col-sm-12">
                                    <div class="row mb-3">
                                        <div class="col-12">Durasi Pengantaran</div>
                                    </div>
                                    <div id="checkout-group_pricing">
                                        {{-- <div class="row mb-3 d-flex align-items-center">
                                            <div class="col-10">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="radio" name="chooseFleet"
                                                        id="chooseFleet1" data-lead_time="1" data-price="2040000">
                                                    <label class="form-check-label text-success" for="chooseFleet1">
                                                        1 Day <span class="badge bg-success ms-2">Best services</span>
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="col-2 text-end text-bold">
                                                <p class="text-bold m-0 text-success" for="chooseFleet1">Rp2.040.000</p>
                                            </div>
                                        </div>
                                        <div class="row mb-3 d-flex align-items-center">
                                            <div class="col-10">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="radio" name="chooseFleet"
                                                        id="chooseFleet2" data-lead_time="2" data-price="1740000">
                                                    <label class="form-check-label" for="chooseFleet2">
                                                        2 Days
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="col-2 text-end text-bold">
                                                <p class="text-bold m-0" for="chooseFleet2">Rp1.740.000</p>
                                            </div>
                                        </div>
                                        <div class="row mb-3 d-flex align-items-center">
                                            <div class="col-10">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="radio" name="chooseFleet"
                                                        id="chooseFleet3" data-lead_time="4" data-price="740000">
                                                    <label class="form-check-label" for="chooseFleet3">
                                                        4 Days
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="col-2 text-end text-bold">
                                                <p class="text-bold m-0" for="flexRadioDefault3">Rp740.000</p>
                                            </div>
                                        </div> --}}
                                        <div class="row mb-3 d-flex align-items-center">
                                            <div class="col-6">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="radio" name="chooseFleet"
                                                        data-lead_time_id="${opt.lead_time_id}"
                                                        data-lead_time="${opt.lead_time}" data-price="${opt.price}"
                                                        data-real_price="${opt.real_price}"
                                                        data-disc_price="${opt.disc_price}">
                                                    <label class="form-check-label text-success">
                                                        1 Hari <span class="badge bg-success ms-2">Best services</span>
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="col-6 text-end text-bold">
                                                <p class="text-bold m-0 text-danger"><strike>Rp1.600.000</strike></p>
                                                <p class="text-bold m-0 text-dark">Diskon 10%</p>
                                                <p class="text-bold m-0 text-success">Rp1.440.000</p>
                                            </div>
                                        </div>
                                        <div class="row mb-3 d-flex align-items-center">
                                            <div class="col-6">
                                                <div class="form-check">
                                                    <input class="form-check-input" type="radio" name="chooseFleet"
                                                        data-lead_time_id="${opt.lead_time_id}"
                                                        data-lead_time="${opt.lead_time}" data-price="${opt.price}"
                                                        data-real_price="${opt.real_price}"
                                                        data-disc_price="${opt.disc_price}">
                                                    <label class="form-check-label text-dark">
                                                        2 Hari
                                                    </label>
                                                </div>
                                            </div>
                                            <div class="col-6 text-end text-bold">
                                                <p class="text-bold m-0 text-dark">Rp1.250.000</p>
                                            </div>
                                        </div>
                                        <hr>
                                        <div class="row mb-3 d-flex align-items-center">
                                            <div class="col-6">
                                                <p class="mb-0">Biaya Asuransi</p>
                                            </div>
                                            <div class="col-6 text-end text-bold">
                                                <p class="text-bold m-0 text-dark">Rp302.892</p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="card-footer checkoutBody">
                            <div class="row d-flex align-items-center">
                                <div class="col-8">
                                    <p class="text-danger mb-0">Harga layanan yang tercantum adalah harga perkiraan, bukan
                                        harga Final.</p>
                                    <p class="mb-0">
                                        Sudahkah Anda memeriksa ulang informasi pesanan?
                                    </p>
                                </div>
                                <div class="col-4 text-end">
                                    <button class="btn btn-danger btn-block btn-sm" id="btnOrder" {{-- onclick="location.href='{{ route('view_user_client_transaction') }}'" --}}
                                        disabled>Pesan Sekarang</button>
                                    <div id="btnOrderSpinner" class="d-none">
                                        <div class="spinner-border" role="status">
                                            <span class="visually-hidden">Loading...</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

@section('customcss')
@endsection
@section('customjs')
    <script>
        $("#insuranceCheck").change(function() {
            $("#insurancePrice").toggleClass("d-none")
            if ( $("#insurancePrice").hasClass("d-none") ){
                $("#insuranceInput").removeAttr('required')
            } else {
                $("#insuranceInput").prop('required',true);
            }
        })
    </script>
    <script>
        'use strict';
        const State = {
            file_jimp_worker: "{{ asset('assets/js/worker/jimp.js') }}",
            storage_lara: "{{ asset('storage') }}/",
            checked_packing_list: 0, // 0:not, 1:process, 2:valid, 3:fail
            input_packing_list: [],
            weight: 0,
            cbm: 0,
            koli: 0,
            qty: 0,
            delay_typing_fast: 1000,
            discount_type: {
                no: "{{ App\Models\Clients::DISC_TYPE_NO }}",
                fix: "{{ App\Models\Clients::DISC_TYPE_FIX }}",
                percent: "{{ App\Models\Clients::DISC_TYPE_PERCENT }}",
            },
            workflow_type: {
                pickup: "{{ App\Models\Zone::ZONE_WORKFLOW_PICKUP }}",
                drop: "{{ App\Models\Zone::ZONE_WORKFLOW_DEST }}",
            },
            role: {
                vendor: "{{ App\Models\Users::ROLE_VENDOR }}",
            },
            termin_amt_type: {
                flat: "{{ \App\Models\OrdersTermins::AMT_TYPE_FLAT }}",
                percent: "{{ \App\Models\OrdersTermins::AMT_TYPE_PERCENT }}",
            },
            route_list_trx: "{{ route('view_transactions') }}",
        };

        const Wrapper = {
            activate: function() {
                Wrapper.event();
                TrxNew.activate();
            },
            event: function() {
                $('#checkout-group_pricing').on('click', 'input[name=chooseFleet]', function() {
                    $('#btnOrder').prop("disabled", false);
                });
                // min date is today
                let TimeISOString = moment().toISOString().split('T');
                let time = TimeISOString[1].split('.')[0].split(':');
                $('#add-pickup_date').attr('min', TimeISOString[0]);
                // $('#add-pickup_time').attr('min', time[0] + ':' + time[1]);
            },
            calcMargin: function(sell, buy) {
                return (Number(sell) - Number(buy)).toFixed(2);
            },
            calcPercentMargin: function(margin, buy) {
                return ((Number(margin) / Number(buy)) * 100).toFixed(2);
            },
            calcMarginNPercentFtl(sell_price, buy_price) {
                let margin = Wrapper.calcMargin(sell_price, buy_price);
                let percent = Wrapper.calcPercentMargin(margin, buy_price);
                percent = (isNaN(percent)) ? 0 : percent;
                $('#add-sellPrice').data('margin', (new Intl.NumberFormat('id-ID')).format(margin));
                $('#add-sellPrice').data('percent', percent);
            },
            calcTerminBuyPrice(isAlert = false) {
                let buy_price = Number($('#add-buyPrice').val().split('.').join(''));
                // buy price - termin
                let termin1_amt_type = $('#add-termin1_amt_type').val();
                let termin1_amt = Number($('#add-termin1_amt').val().split('.').join(''));

                if (isNaN(buy_price) || buy_price < 1) {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Biaya belum diisi');
                    return false;
                }
                if (isNaN(termin1_amt) || termin1_amt < 1) {
                    // if (isAlert) Helper.toast('Warning', 'just now', 'Jumlah termin 1 belum diisi');
                    return false;
                }

                if (termin1_amt_type == State.termin_amt_type.flat) {
                    if (buy_price < termin1_amt) {
                        Helper.toast('Warning', 'just now', 'Biaya tidak boleh kurang dari termin 1');
                        $('#add-termin2_amt').val(0);
                        return false;
                    }
                    let price2 = buy_price - termin1_amt;
                    $('#add-termin2_amt').val(price2).trigger('change');
                    $('#add-termin1_calc_percent').data('val', 0);
                    $('#add-termin2_calc_percent').data('val', 0);
                    $('#add-termin1_calc_percent').text((new Intl.NumberFormat('id-ID')).format(0));
                    $('#add-termin2_calc_percent').text((new Intl.NumberFormat('id-ID')).format(0));
                } else if (termin1_amt_type == State.termin_amt_type.percent) {
                    if (termin1_amt > 100) {
                        Helper.toast('Warning', 'just now', 'termin 1 tidak boleh lebih dari 100%');
                        $('#add-termin2_amt').val(0);
                        return false;
                    }
                    let disc_price1 = buy_price * termin1_amt / 100;
                    let price1 = buy_price - disc_price1;
                    let percent2 = (100 - termin1_amt);
                    let disc_price2 = buy_price * percent2 / 100;
                    let price2 = buy_price - disc_price2;
                    $('#add-termin2_amt').val(percent2).trigger('change');
                    $('#add-termin1_calc_percent').data('val', disc_price1);
                    $('#add-termin2_calc_percent').data('val', disc_price2);
                    $('#add-termin1_calc_percent').text((new Intl.NumberFormat('id-ID')).format(disc_price1));
                    $('#add-termin2_calc_percent').text((new Intl.NumberFormat('id-ID')).format(disc_price2));
                } else {
                    $('#add-termin1_calc_percent').data('val', 0);
                    $('#add-termin2_calc_percent').data('val', 0);
                    $('#add-termin1_calc_percent').text('');
                    $('#add-termin2_calc_percent').text('');
                }      
            },
            calcTerminSellPrice(isAlert = false) {
                let sell_price = Number($('#add-sellPrice').val().split('.').join(''));
                // sell price - termin
                let sell_termin1_amt_type = $('#add-sell_termin1_amt_type').val();
                let sell_termin1_amt = Number($('#add-sell_termin1_amt').val().split('.').join(''));

                if (isNaN(sell_price) || sell_price < 1) {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Harga belum diisi');
                    return false;
                }
                if (isNaN(sell_termin1_amt) || sell_termin1_amt < 1) {
                    // if (isAlert) Helper.toast('Warning', 'just now', 'Jumlah termin 1 belum diisi');
                    return false;
                }

                if (sell_termin1_amt_type == State.termin_amt_type.flat) {
                    if (sell_price < sell_termin1_amt) {
                        Helper.toast('Warning', 'just now', 'Harga tidak boleh kurang dari termin 1');
                        $('#add-sell_termin2_amt').val(0);
                        return false;
                    }
                    let price2 = sell_price - sell_termin1_amt;
                    $('#add-sell_termin2_amt').val(price2).trigger('change');
                    $('#add-sell_termin1_calc_percent').data('val', 0);
                    $('#add-sell_termin2_calc_percent').data('val', 0);
                    $('#add-sell_termin1_calc_percent').text((new Intl.NumberFormat('id-ID')).format(0));
                    $('#add-sell_termin2_calc_percent').text((new Intl.NumberFormat('id-ID')).format(0));
                } else if (sell_termin1_amt_type == State.termin_amt_type.percent) {
                    if (sell_termin1_amt > 100) {
                        Helper.toast('Warning', 'just now', 'termin 1 tidak boleh lebih dari 100%');
                        $('#add-sell_termin2_amt').val(0);
                        return false;
                    }
                    let disc_price1 = sell_price * sell_termin1_amt / 100;
                    let price1 = sell_price - disc_price1;
                    let percent2 = (100 - sell_termin1_amt);
                    let disc_price2 = sell_price * percent2 / 100;
                    let price2 = sell_price - disc_price2;
                    $('#add-sell_termin2_amt').val(percent2).trigger('change');
                    $('#add-sell_termin1_calc_percent').data('val', disc_price1);
                    $('#add-sell_termin2_calc_percent').data('val', disc_price2);
                    $('#add-sell_termin1_calc_percent').text((new Intl.NumberFormat('id-ID')).format(disc_price1));
                    $('#add-sell_termin2_calc_percent').text((new Intl.NumberFormat('id-ID')).format(disc_price2));
                } else {
                    $('#add-sell_termin1_calc_percent').data('val', 0);
                    $('#add-sell_termin2_calc_percent').data('val', 0);
                    $('#add-sell_termin1_calc_percent').text('');
                    $('#add-sell_termin2_calc_percent').text('');
                }      
            },
        };

        const Req = {
            check_packing_list: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_user_client_check_packing_list') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message, 'bg-warning', 5000);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again', 'bg-warning', 5000);
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message, 'bg-warning', 5000);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
            list_client_zone: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_list_client_zones') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again');
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
            checkout: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_trx_spc_checkout_v1') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again');
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
            search_vendor_name: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_search_user_name') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again');
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
            search_device_id: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_search_device_id') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again');
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
            search_nopol: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_search_nopol') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again');
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
            search_driver_name: function(data, isAlert = false, cb = null) {
                return new Promise((resolve, reject) => {
                    $.ajax({
                        url: "{{ route('api_search_driver_name') }}",
                        method: 'POST',
                        crossDomain: true,
                        processData: true,
                        headers: {
                            'x-api-key': Helper.getCookie('_trtk'),
                            'x-csrf-token': $('meta[name="csrf-token"]').attr('content'),
                        },
                        data,
                        success: (resp, textStatus, jqXHR) => {
                            if (cb) cb(resp);
                            if (resp.meta.type != 'success') {
                                resolve({
                                    type: 'fail',
                                    msg: resp.meta.message,
                                });
                                if (isAlert) Helper.toast('Warning', 'just now', resp.meta
                                    .message);
                                return false;
                            }
                            resolve({
                                type: 'success',
                                resp: resp,
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            if (cb) cb(jqXHR.responseJSON);
                            if (jqXHR.status >= 500) {
                                if (isAlert) Helper.toast('Error', 'just now',
                                    'please try again');
                            } else {
                                if (isAlert) Helper.toast('Error', 'just now', jqXHR
                                    .responseJSON.meta.message);
                            }
                            resolve({
                                type: 'error',
                                msg: jqXHR.responseJSON.meta.message,
                            });
                        }
                    })
                })
            },
        }

        const TrxNew = {
            activate: function() {
                TrxNew.event();
            },
            event: function() {
                // button
                $('#btnCalculate').on('click', async function() {
                    // general data
                    let data = TrxNew.getData();
                    TrxNew.showCheckoutSpinner();
                    let isValid = TrxNew.checkData(data, true);
                    if (!isValid) {
                        TrxNew.hideCheckoutSpinner();
                        TrxNew.hideCheckout();
                        return false;
                    }

                    // // start packing list data
                    // let check_packing_list = await Req.check_packing_list({
                    //     packing_list: State.input_packing_list,
                    //     // weight: data.weight,
                    //     // cbm: data.cbm,
                    //     // koli: data.koli,
                    // }, true);
                    // if (check_packing_list.type == 'success') {
                    //     State.checked_packing_list = 2;
                    // } else {
                    //     State.checked_packing_list = 3;
                    //     TrxNew.hideCheckoutSpinner();
                    //     TrxNew.hideCheckout();
                    //     return false;
                    // }
                    // data.weight = check_packing_list.resp.data.calc_weight;
                    // State.weight = data.weight;
                    // data.cbm = check_packing_list.resp.data.calc_cbm;
                    // State.cbm = data.cbm;
                    // data.koli = check_packing_list.resp.data.total_koli;
                    // State.koli = data.koli;
                    // // data.qty = check_packing_list.resp.data.total_qty;
                    // // State.qty = data.qty;
                    // // recheck general data again
                    // isValid = TrxNew.checkData(data, true);
                    // if (!isValid) {
                    //     TrxNew.hideCheckoutSpinner();
                    //     TrxNew.hideCheckout();
                    //     return false;
                    // }
                    // // end packing list data

                    // checkout data
                    let checkoutData = await Req.checkout(data, true);
                    if (checkoutData.type != 'success') {
                        TrxNew.hideCheckoutSpinner();
                        TrxNew.hideCheckout();
                        return false;
                    }
                    data.pricing_options = checkoutData.resp.data;
                    data.insurance = checkoutData.resp.insurance;
                    $('#add-vdr_name').data('id', checkoutData.resp.exist_data.vdr_id);
                    $('#add-drv_name').data('id', checkoutData.resp.exist_data.drv_id);
                    $('#add-vhc_nopol').data('id', checkoutData.resp.exist_data.vhc_id);
                    TrxNew.showCheckout(data);
                    TrxNew.hideCheckoutSpinner();
                });
                $('#btnOrder').on('click', function() {
                    let data = TrxNew.getData();

                    // // start packing list data
                    // data.packing_list = State.input_packing_list;
                    // data.weight = State.weight;
                    // data.cbm = State.cbm;
                    // data.koli = State.koli;
                    // // data.qty = State.qty;
                    // // end packing list data

                    TrxNew.showBtnOrdSpinner();
                    let isValid = TrxNew.checkData(data, true);
                    if (!isValid) {
                        TrxNew.hideBtnOrdSpinner();
                        return false;
                    }
                    TrxNew.submitData(data);

                    // static fe
                    // Helper.toast('Success', 'just now', 'success create order');
                    // window.location.href =
                    //     "{{ route('view_user_client_transaction') }}";
                });
                $('#checkout-download_packing_list').on('click', function() {
                    const fileName = 'package-list.xlsx';
                    const sheetName = 'template_packing_list';
                    downloadPackingList(fileName, sheetName, State.input_packing_list);
                });
                // link
                $('#download_template_packing_list').on('click', function() {
                    TrxNew.downloadTemplatePackingList();
                });
                // input
                TrxNew.evInputChange();
                TrxNew.evInputListen();
                TrxNew.evInputBuyPrice();
                TrxNew.evInputSellPrice();
                // select
                $('#add-client').on('change', async function(e) {
                    TrxNew.hideCheckout();
                    let selected = $('#add-client :selected');

                    $('#add-pickup_zone').html('<option value="" selected disabled>Belum ada lokasi yang dipilih</option>');
                    $('#add-drop_zone').html('<option value="" selected disabled>Belum ada lokasi yang dipilih</option>');

                    const listPickupZones = await Req.list_client_zone({
                        cptid: selected.data('cptid'),
                        workflow_type: State.workflow_type.pickup,
                    });
                    if (listPickupZones.type != 'success') {
                        Helper.toast('Warning', 'just now', listPickupZones.msg);
                        $('#add-pickup_zone').val(null).trigger('change');
                        $('#add-pickup_zone').select2();
                        $('#add-drop_zone').val(null).trigger('change');
                        $('#add-drop_zone').select2();
                        return false;
                    }
                    for (let pck of listPickupZones.resp.data) {
                        $('#add-pickup_zone').append(`<option value="${pck.id}" data-id="${pck.id}" data-title="${pck.name}" data-addr="${pck.fulladdress}">${pck.name}</option>`);
                    }
                    $('#add-pickup_zone').select2();

                    const listDropZones = await Req.list_client_zone({
                        cptid: selected.data('cptid'),
                        workflow_type: State.workflow_type.drop,
                    });
                    if (listDropZones.type != 'success') {
                        Helper.toast('Warning', 'just now', listDropZones.msg);
                        $('#add-drop_zone').val(null).trigger('change');
                        $('#add-drop_zone').select2();
                        return false;
                    }
                    for (let drop of listDropZones.resp.data) {
                        $('#add-drop_zone').append(`<option value="${drop.id}" data-id="${drop.id}" data-title="${drop.name}" data-addr="${drop.fulladdress}">${drop.name}</option>`);
                    }
                    $('#add-drop_zone').select2();
                });
                // date
                TrxNew.evDate();
            },
            evInputChange: function() {
                TrxNew.handleUploadPacking();
                $('#add-pickup_date').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-pickup_time').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-pickup_zone').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-drop_zone').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-weight').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-cbm').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-koli').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-truck_type').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-packinglist').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#insuranceCheck').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#insuranceInput').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let price = (new Intl.NumberFormat('id-ID')).format($('#insuranceInput').val().split('.').join(''));
                    $('#insuranceInput').val(price);
                });
                $('#add-vdr_phone').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-vdr_email').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-vdr_addr').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-vhc_nopol').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-vhc_stnk_taxexp').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-vhc_kir_exp').on('change', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-drv_phone').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-drv_phone2').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-leadTime').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
            },
            evInputListen: function() {
                let timeoutSearchVdrName = null;
                $('#add-vdr_name').on('keyup', function(e) {
                    TrxNew.hideCheckout();
                    clearTimeout(timeoutSearchVdrName);
                    timeoutSearchVdrName = setTimeout(async () => {
                        const respSearch = await Req.search_vendor_name({
                            name: $('#add-vdr_name').val(),
                            role: State.role.vendor,
                        }, false);
                        if (respSearch.type !== 'success') return true;
                        const vendor = respSearch.resp.data[0];
                        $('#add-vdr_name').val(vendor.first_name);
                        $('#add-vdr_phone').val(vendor.phone);
                        $('#add-vdr_email').val(vendor.email);
                        $('#add-vdr_addr').val(vendor.fulladdress);
                        $('#add-vdr_bank').val(vendor.bank_code).trigger('change');
                        $('#add-vdr_acc_name').val(vendor.bank_acc_name);
                        $('#add-vdr_acc_number').val(vendor.bank_acc_number);
                    }, State.delay_typing_fast);
                });
                let timeoutSearchDeviceID = null;
                $('#add-device_id').on('change', async function(e) {
                    TrxNew.hideCheckout();
                    // clearTimeout(timeoutSearchDeviceID);
                    // timeoutSearchDeviceID = setTimeout(async () => {
                        const respSearch = await Req.search_device_id({
                            device_id: $('#add-device_id :selected').data('device_id'),
                        }, false);
                        if (respSearch.type !== 'success') return true;
                        // if (respSearch.resp.data.length > 0) Helper.toast('Warning', 'just now', 'device id telah digunakan');
                        if (respSearch.resp.data.length < 1) return true;
                        const vhc = respSearch.resp.data[0];
                        $('#add-vhc_nopol').val(`${vhc.nopol1} ${vhc.nopol2} ${vhc.nopol3}`);
                        $('#add-vhc_stnk_taxexp').val(vhc.tax_exp);
                        $('#add-vhc_kir_exp').val(vhc.kir_exp);
                        return true;
                    // }, State.delay_typing_fast);
                });
                let timeoutSearchNopol = null;
                $('#add-vhc_nopol').on('keyup', async function(e) {
                    TrxNew.hideCheckout();
                    clearTimeout(timeoutSearchNopol);
                    timeoutSearchNopol = setTimeout(async () => {
                        const respSearch = await Req.search_nopol({
                            nopol: $('#add-vhc_nopol').val(),
                        }, false);
                        if (respSearch.type !== 'success') return true;
                        // if (respSearch.resp.data.length > 0) Helper.toast('Warning', 'just now', 'device id telah digunakan');
                        if (respSearch.resp.data.length < 1) return true;
                        const vhc = respSearch.resp.data[0];
                        $('#add-vhc_nopol').val(`${vhc.nopol1} ${vhc.nopol2} ${vhc.nopol3}`);
                        $('#add-vhc_stnk_taxexp').val(vhc.tax_exp);
                        $('#add-vhc_kir_exp').val(vhc.kir_exp);
                        return true;
                    }, State.delay_typing_fast);
                });
                let timeoutSearchDrvName = null;
                $('#add-drv_name').on('keyup', async function(e) {
                    TrxNew.hideCheckout();
                    clearTimeout(timeoutSearchDrvName);
                    timeoutSearchDrvName = setTimeout(async () => {
                        const respSearch = await Req.search_driver_name({
                            name: $('#add-drv_name').val(),
                        }, false);
                        if (respSearch.type !== 'success') return true;
                        const driver = respSearch.resp.data[0];
                        $('#add-drv_name').val(driver.fullname);
                        $('#add-drv_name2').val(driver.fullname2);
                        $('#add-drv_phone').val(driver.phone);
                        if (driver.phone2 == 0) {
                            $('#add-drv_phone2').val(driver.phone2);
                        } else {
                            $('#add-drv_phone2').val(driver.phone2);
                        }
                    }, State.delay_typing_fast);
                });
            },
            evInputBuyPrice: function() {
                $('#add-buyPrice').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let sell_price = (new Intl.NumberFormat('id-ID')).format($('#add-sellPrice').val().split('.').join(''));
                    $('#add-sellPrice').val(sell_price);
                    let buy_price = (new Intl.NumberFormat('id-ID')).format($('#add-buyPrice').val().split('.').join(''));
                    $('#add-buyPrice').val(buy_price);
                    Wrapper.calcMarginNPercentFtl($('#add-sellPrice').val().split('.').join(''), $('#add-buyPrice').val().split('.').join(''));
                    Wrapper.calcTerminBuyPrice(false);
                });
                // termin buy price // for vendor
                $('#add-termin1_amt_type').on('change', function() {
                    TrxNew.hideCheckout();
                    let termin1_amt_type = $('#add-termin1_amt_type').val();
                    if (termin1_amt_type == State.termin_amt_type.flat) {
                        $('.subTermin1').removeClass('d-none');
                        $('.subTermin1-calc_percent').addClass('d-none');
                    } else if (termin1_amt_type == State.termin_amt_type.percent) {
                        $('.subTermin1').removeClass('d-none');
                        $('.subTermin1-calc_percent').removeClass('d-none');
                    } else {
                        $('.subTermin1').addClass('d-none');
                    }
                    $('#add-termin2_amt_type').val(termin1_amt_type).trigger('change');
                    $('#add-termin1_amt').val('');
                });
                $('#add-termin1_amt').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let termin1_amt_type = $('#add-termin1_amt_type').val();
                    let termin1_amt = $('#add-termin1_amt').val().split('.').join('');
                    if (termin1_amt_type == State.termin_amt_type.flat) {
                        $('#add-termin1_amt').val((new Intl.NumberFormat('id-ID')).format(termin1_amt));
                    }
                    Wrapper.calcTerminBuyPrice(true);
                });
                $('#add-termin1_date').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-termin1_time').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-termin2_amt_type').on('change', function() {
                    TrxNew.hideCheckout();
                    let termin2_amt_type = $('#add-termin2_amt_type').val();
                    if (termin2_amt_type == State.termin_amt_type.flat) {
                        $('.subTermin2').removeClass('d-none');
                        $('.subTermin2-calc_percent').addClass('d-none');
                    } else if (termin2_amt_type == State.termin_amt_type.percent) {
                        $('.subTermin2').removeClass('d-none');
                        $('.subTermin2-calc_percent').removeClass('d-none');
                    } else {
                        $('.subTermin2').addClass('d-none');
                    }
                    $('#add-termin2_amt').val('');
                });
                $('#add-termin2_amt').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let termin2_amt_type = $('#add-termin2_amt_type').val();
                    let termin2_amt = $('#add-termin2_amt').val().split('.').join('');
                    if (termin2_amt_type == State.termin_amt_type.flat) {
                        $('#add-termin2_amt').val((new Intl.NumberFormat('id-ID')).format(termin2_amt));
                    }
                });
                $('#add-termin2_amt').on('change', function() {
                    TrxNew.hideCheckout();
                    let termin2_amt_type = $('#add-termin2_amt_type').val();
                    let termin2_amt = $('#add-termin2_amt').val().split('.').join('');
                    if (termin2_amt_type == State.termin_amt_type.flat) {
                        $('#add-termin2_amt').val((new Intl.NumberFormat('id-ID')).format(termin2_amt));
                    }
                });
            },
            evInputSellPrice: function() {
                $('#add-sellPrice').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let sell_price = (new Intl.NumberFormat('id-ID')).format($('#add-sellPrice').val().split('.').join(''));
                    $('#add-sellPrice').val(sell_price);
                    let buy_price = (new Intl.NumberFormat('id-ID')).format($('#add-buyPrice').val().split('.').join(''));
                    $('#add-buyPrice').val(buy_price);
                    Wrapper.calcMarginNPercentFtl($('#add-sellPrice').val().split('.').join(''), $('#add-buyPrice').val().split('.').join(''));
                    Wrapper.calcTerminSellPrice(false);
                });
                // termin sell price for client
                $('#add-sell_termin1_amt_type').on('change', function() {
                    TrxNew.hideCheckout();
                    let sell_termin1_amt_type = $('#add-sell_termin1_amt_type').val();
                    if (sell_termin1_amt_type == State.termin_amt_type.flat) {
                        $('.sell_subTermin1').removeClass('d-none');
                        $('.sell_subTermin1-calc_percent').addClass('d-none');
                    } else if (sell_termin1_amt_type == State.termin_amt_type.percent) {
                        $('.sell_subTermin1').removeClass('d-none');
                        $('.sell_subTermin1-calc_percent').removeClass('d-none');
                    } else {
                        $('.sell_subTermin1').addClass('d-none');
                    }
                    $('#add-sell_termin2_amt_type').val(sell_termin1_amt_type).trigger('change');
                    $('#add-sell_termin1_amt').val('');
                });
                $('#add-sell_termin1_amt').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let sell_termin1_amt_type = $('#add-sell_termin1_amt_type').val();
                    let sell_termin1_amt = $('#add-sell_termin1_amt').val().split('.').join('');
                    if (sell_termin1_amt_type == State.termin_amt_type.flat) {
                        $('#add-sell_termin1_amt').val((new Intl.NumberFormat('id-ID')).format(sell_termin1_amt));
                    }
                    Wrapper.calcTerminSellPrice(true);
                });
                $('#add-sell_termin1_date').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-sell_termin1_time').on('keyup', function() {
                    TrxNew.hideCheckout();
                });
                $('#add-sell_termin2_amt_type').on('change', function() {
                    TrxNew.hideCheckout();
                    let sell_termin2_amt_type = $('#add-sell_termin2_amt_type').val();
                    if (sell_termin2_amt_type == State.termin_amt_type.flat) {
                        $('.sell_subTermin2').removeClass('d-none');
                        $('.sell_subTermin2-calc_percent').addClass('d-none');
                    } else if (sell_termin2_amt_type == State.termin_amt_type.percent) {
                        $('.sell_subTermin2').removeClass('d-none');
                        $('.sell_subTermin2-calc_percent').removeClass('d-none');
                    } else {
                        $('.sell_subTermin2').addClass('d-none');
                    }
                    $('#add-sell_termin2_amt').val('');
                });
                $('#add-sell_termin2_amt').on('keyup', function() {
                    TrxNew.hideCheckout();
                    let sell_termin2_amt_type = $('#add-sell_termin2_amt_type').val();
                    let sell_termin2_amt = $('#add-sell_termin2_amt').val().split('.').join('');
                    if (sell_termin2_amt_type == State.termin_amt_type.flat) {
                        $('#add-sell_termin2_amt').val((new Intl.NumberFormat('id-ID')).format(sell_termin2_amt));
                    }
                });
                $('#add-sell_termin2_amt').on('change', function() {
                    TrxNew.hideCheckout();
                    let sell_termin2_amt_type = $('#add-sell_termin2_amt_type').val();
                    let sell_termin2_amt = $('#add-sell_termin2_amt').val().split('.').join('');
                    if (sell_termin2_amt_type == State.termin_amt_type.flat) {
                        $('#add-sell_termin2_amt').val((new Intl.NumberFormat('id-ID')).format(sell_termin2_amt));
                    }
                });
            },
            evDate: function() {
                let now = moment();
                $('#add-vhc_stnk_taxexp').attr('min', now.format('YYYY-MM-DD'));
                $('#add-vhc_stnk_taxexp').attr('max', now.add(6, 'month').format('YYYY-MM-DD'));
                now = moment();
                $('#add-vhc_kir_exp').attr('min', now.format('YYYY-MM-DD'));
                $('#add-vhc_kir_exp').attr('max', now.add(1, 'year').format('YYYY-MM-DD'));
                now = moment();
                $('#add-termin1_date').attr('min', now.format('YYYY-MM-DD'));
                $('#add-sell_termin1_date').attr('min', now.format('YYYY-MM-DD'));
            },
            getData: function() {
                let data = {};

                let clientSelected = $('#add-client :selected');
                data.uid = clientSelected.data('uid');

                data.pickup_date = $('#add-pickup_date').val().trim();
                // data.pickup_time = $('#add-pickup_time').val().trim();
                data.pickup_time_hour = $('#add-pickup_time_hour').val()?.trim();
                data.pickup_time_minute = $('#add-pickup_time_minute').val()?.trim();
                data.pickup_time = `${data.pickup_time_hour}:${data.pickup_time_minute}`;
                data.pickup_at = moment(data.pickup_date + ' ' + data.pickup_time).unix();

                let pickup_zone = $('#add-pickup_zone :selected');
                data.pickup_zone_id = pickup_zone.data('id');
                data.pickup_zone_title = pickup_zone.data('title')?.trim();
                data.pickup_zone_addr = pickup_zone.data('addr')?.trim();
                data.pickup_zone_pic_name = $('#add-pickup_pic_name').val();
                data.pickup_zone_pic_phone = $('#add-pickup_pic_phone').val();
                let drop_zone = $('#add-drop_zone :selected');
                data.drop_zone_id = drop_zone.data('id');
                data.drop_zone_title = drop_zone.data('title')?.trim();
                data.drop_zone_addr = drop_zone.data('addr')?.trim();
                data.drop_zone_pic_name = $('#add-drop_pic_name').val();
                data.drop_zone_pic_phone = $('#add-drop_pic_phone').val();

                // data.weight = Number($('#add-weight').val() || 'n');
                // data.cbm = Number($('#add-cbm').val() || 'n');
                // data.koli = Number($('#add-koli').val() || 'n');

                let truck_type = $('#add-truck_type :selected');
                data.truck_type_id = truck_type.data('type_id');
                data.truck_type_name = truck_type.data('name')?.trim();
                // data.truck_type_weight = truck_type.data('weight');
                // data.truck_type_cbm = truck_type.data('cbm');
                // data.truck_type_koli = truck_type.data('koli');

                data.is_insurance = $('#insuranceCheck').prop('checked');
                data.amt_beneficiary = $('#insuranceInput').val().split('.').join('');
                if ($('#checkout-insurance_value').data('id')) {
                    let insurance = $('#checkout-insurance_value');
                    data.insurance_id = insurance.data('id');
                    data.insurance_price = insurance.data('price');
                }

                data.vdr_id = $('#add-vdr_name').data('id');
                data.vdr_name = $('#add-vdr_name').val();
                data.vdr_phone = $('#add-vdr_phone').val();
                data.vdr_email = $('#add-vdr_email').val();
                data.vdr_addr = $('#add-vdr_addr').val();
                let selectedBank = $('#add-vdr_bank :selected');
                if (selectedBank && selectedBank.val() != '') {
                    data.vdr_bank_id = selectedBank.data('id');
                    data.vdr_bank_code = selectedBank.data('code');
                    data.vdr_bank_short = selectedBank.data('short');
                    data.vdr_bank_name = selectedBank.data('name');
                }
                data.vdr_bank_acc_number = $('#add-vdr_acc_number').val();
                data.vdr_bank_acc_name = $('#add-vdr_acc_name').val();

                // data.device_id = $('#add-device_id').val();
                let selected_device = $('#add-device_id :selected');
                data.dvc_id = selected_device.val();
                data.device_id = selected_device.data('device_id');
                data.simcard = selected_device.data('simcard');

                data.vhc_id = $('#add-vhc_nopol').data('id');
                data.vhc_nopol = $('#add-vhc_nopol').val();
                data.vhc_stnk_taxexp = $('#add-vhc_stnk_taxexp').val().trim();
                data.vhc_kir_exp = $('#add-vhc_kir_exp').val().trim();

                data.drv_id = $('#add-drv_name').data('id');
                data.drv_name = $('#add-drv_name').val();
                data.drv_name2 = $('#add-drv_name2').val();
                data.drv_phone = $('#add-drv_phone').val();
                data.drv_phone2 = $('#add-drv_phone2').val();

                data.sell_price = Number($('#add-sellPrice').val().split('.').join(''));
                data.buy_price = Number($('#add-buyPrice').val().split('.').join(''));
                data.margin_price = Number($('#add-sellPrice').data('margin').val()).split('.').join('');
                data.percent_price = $('#add-sellPrice').data('percent');
                data.lead_time = Number($('#add-leadTime').val());

                if ($('#checkOutView').attr('class').indexOf('d-none') < 0) {
                    for (let chooseFleet of $('input[name="chooseFleet"]')) {
                        if ($(chooseFleet).prop('checked')) {
                            data.lead_time_id = $(chooseFleet).data('lead_time_id');
                            data.lead_time = $(chooseFleet).data('lead_time');
                            data.price = $(chooseFleet).data('price');
                            data.real_price = $(chooseFleet).data('real_price');
                            data.disc_price = $(chooseFleet).data('disc_price');
                        }
                    }
                }

                // for vendor / buy price
                data.termin1_amt_type = $('#add-termin1_amt_type').val();
                if ($('#add-termin1_amt_type').val() == '') { } else {
                    data.termin1_amt = Number($('#add-termin1_amt').val().split('.').join(''));
                    data.termin1_date = $('#add-termin1_date').val().trim();
                    // data.termin1_time = $('#add-termin1_time').val().trim();
                    data.termin1_time_hour = $('#add-termin1_time_hour').val()?.trim();
                    data.termin1_time_minute = $('#add-termin1_time_minute').val()?.trim();
                    data.termin1_time = `${data.termin1_time_hour}:${data.termin1_time_minute}`;
                    data.termin1_at = moment(data.termin1_date + ' ' + data.termin1_time).unix();
                    data.termin1_calc_percent = Number($('#add-termin1_calc_percent').data('val'));
    
                    data.termin2_amt_type = $('#add-termin2_amt_type').val();
                    data.termin2_amt = Number($('#add-termin2_amt').val().split('.').join(''));
                    data.termin2_calc_percent = Number($('#add-termin2_calc_percent').data('val'));
                }

                // for client / sell price
                data.sell_termin1_amt_type = $('#add-sell_termin1_amt_type').val();
                if ($('#add-sell_termin1_amt_type').val() == '') { } else {
                    data.sell_termin1_amt = Number($('#add-sell_termin1_amt').val().split('.').join(''));
                    data.sell_termin1_date = $('#add-sell_termin1_date').val().trim();
                    // data.sell_termin1_time = $('#add-sell_termin1_time').val().trim();
                    data.sell_termin1_time_hour = $('#add-sell_termin1_time_hour').val()?.trim();
                    data.sell_termin1_time_minute = $('#add-sell_termin1_time_minute').val()?.trim();
                    data.sell_termin1_time = `${data.sell_termin1_time_hour}:${data.sell_termin1_time_minute}`;
                    data.sell_termin1_at = moment(data.sell_termin1_date + ' ' + data.sell_termin1_time).unix();
                    data.sell_termin1_calc_percent = Number($('#add-sell_termin1_calc_percent').data('val'));
    
                    data.sell_termin2_amt_type = $('#add-sell_termin2_amt_type').val();
                    data.sell_termin2_amt = Number($('#add-sell_termin2_amt').val().split('.').join(''));
                    data.sell_termin2_calc_percent = Number($('#add-sell_termin2_calc_percent').data('val'));
                }

                return data;
            },
            checkData: function(data, isAlert = false) {
                if (data.pickup_at < 1 || isNaN(data.pickup_at)) {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Pickup date / time not valid');
                    return false;
                }
                if (data.pickup_at < moment().unix()) {
                    if (isAlert) Helper.toast('Warning', 'just now',
                        'Pickup date / time is less than the current date / time');
                    return false;
                }
                if (data.pickup_at < moment().add(3, 'hours').unix()) {
                    if (isAlert) Helper.toast('Warning', 'just now',
                        'Pickup time minimum 3 hours from now');
                    return false;
                }
                if (typeof data.pickup_zone_id == 'undefined') {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Pickup zone not valid');
                    return false;
                }
                if (typeof data.drop_zone_id == 'undefined') {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Drop zone not valid');
                    return false;
                }
                if (Number(data.buy_price) > Number(data.sell_price)) {
                    if (isAlert) Helper.toast('Warning', 'just now', 'Biaya harus lebih rendah dari harga');
                    return false;
                }
                if (data.termin1_amt_type == State.termin_amt_type.flat) {
                } else if (data.termin1_amt_type == State.termin_amt_type.percent) {
                    if  (Number(data.termin1_amt) > 100) {
                        if (isAlert) Helper.toast('Warning', 'just now', 'Maksimal percent termin 1 adalah 100%');
                        return false;
                    }
                } else {
					if (isAlert) Helper.toast('Warning', 'just now', 'Termin 1 wajib diisi');
					return false;
				}
				if (moment.unix(data.pickup_at).add(data.lead_time, 'day').unix() >= moment(data.vhc_stnk_taxexp).unix() || moment.unix(data.pickup_at).add(data.lead_time, 'day').unix() >= moment(data.vhc_kir_exp).unix()) {
					if (isAlert) Helper.toast('Warning', 'just now', 'Tax / Kir Exp tidak boleh expired sebelum pengantaran selesai');
					return false;
				}
                // buy price / termin for vendor
                if (data.termin2_amt_type == State.termin_amt_type.flat) {
                } else if (data.termin2_amt_type == State.termin_amt_type.percent) {
                    if  (Number(data.termin2_amt) > 100) {
                        if (isAlert) Helper.toast('Warning', 'just now', 'Maksimal percent termin 2 adalah 100%');
                        return false;
                    }
                }
                if (data.termin1_amt_type == State.termin_amt_type.flat || data.termin1_amt_type == State.termin_amt_type.percent) {
                    if (data.termin1_date == '' || data.termin1_time == '') {
                        if (isAlert) Helper.toast('Warning', 'just now', 'tanggal dan waktu pada termin 1 wajib diisi');
                        return false;   
                    }
                }
                if (data.termin2_amt_type == State.termin_amt_type.flat || data.termin2_amt_type == State.termin_amt_type.percent) {
                    if (data.termin1_date == '' || data.termin1_time == '') {
                        if (isAlert) Helper.toast('Warning', 'just now', 'tanggal dan waktu pada termin 1 wajib diisi');
                        return false;   
                    }
                }

                // sell price / termin for client
				if (data.sell_termin1_amt_type == State.termin_amt_type.flat) {    
                } else if (data.sell_termin1_amt_type == State.termin_amt_type.percent) {
                    if  (Number(data.sell_termin1_amt) > 100) {
                        if (isAlert) Helper.toast('Warning', 'just now', 'Maksimal percent termin 1 adalah 100%');
                        return false;
                    }
                } else {
					if (isAlert) Helper.toast('Warning', 'just now', 'Termin 1 wajib diisi');
					return false;
				}
                if (data.sell_termin2_amt_type == State.termin_amt_type.flat) {    
                } else if (data.sell_termin2_amt_type == State.termin_amt_type.percent) {
                    if  (Number(data.sell_termin2_amt) > 100) {
                        if (isAlert) Helper.toast('Warning', 'just now', 'Maksimal percent termin 2 adalah 100%');
                        return false;
                    }
                }
                if (data.sell_termin1_amt_type == State.termin_amt_type.flat || data.sell_termin1_amt_type == State.termin_amt_type.percent) {
                    if (data.sell_termin1_date == '' || data.sell_termin1_time == '') {
                        if (isAlert) Helper.toast('Warning', 'just now', 'Jika termin 1 dipilih, maka tanggal dan waktu wajib diisi');
                        return false;   
                    }
                }
                if (data.sell_termin2_amt_type == State.termin_amt_type.flat || data.sell_termin2_amt_type == State.termin_amt_type.percent) {
                    if (data.sell_termin1_date == '' || data.sell_termin1_time == '') {
                        if (isAlert) Helper.toast('Warning', 'just now', 'Jika termin 2 diisi, maka termin 1 wajib diisi');
                        return false;   
                    }
                }

                // // start packing list data
                // if (isNaN(data.weight) && isNaN(data.cbm) && isNaN(data.koli)) {
                //     if (isAlert) Helper.toast('Warning', 'just now',
                //         'weight / volume / koli must be provided at least 1 of them');
                //     return false;
                // }
                // if (typeof data.truck_type_id != 'undefined') {
                //     if (!isNaN(data.weight) && data.truck_type_weight != 0 && data.weight > data
                //         .truck_type_weight) {
                //         if (isAlert) Helper.toast('Warning', 'just now',
                //             `maximum weight for type truck ${data.truck_type_name} is ${data.truck_type_weight} Kg`
                //         );
                //         return false;
                //     }
                //     if (!isNaN(data.cbm) && data.truck_type_cbm != 0 && data.cbm > data.truck_type_cbm) {
                //         if (isAlert) Helper.toast('Warning', 'just now',
                //             `maximum volume for type truck ${data.truck_type_name} is ${data.truck_type_cbm} m³`
                //         );
                //         return false;
                //     }
                //     if (!isNaN(data.koli) && data.truck_type_koli != 0 && data.koli > data.truck_type_koli) {
                //         if (isAlert) Helper.toast('Warning', 'just now',
                //             `maximum koli for type truck ${data.truck_type_name} is ${data.truck_type_koli} Pcs`
                //         );
                //         return false;
                //     }
                // }
                // if (State.checked_packing_list == 0) {
                //     if (isAlert) Helper.toast('Warning', 'just now', 'You haven\'t upload packing list');
                //     return false;
                // }
                // if (State.checked_packing_list == 1) {
                //     if (isAlert) Helper.toast('Warning', 'just now', 'Packing list in process of checking');
                //     return false;
                // }
                // if (State.checked_packing_list == 3 || State.input_packing_list.length < 1) {
                //     if (isAlert) Helper.toast('Warning', 'just now', 'Packing list not valid');
                //     return false;
                // }
                // // end packing list data

                return true;
            },
            showCheckoutSpinner: function() {
                $('#checkOutView').removeClass('d-none');
                $('#checkOutView').removeClass('d-block');

                $('#checkout-spinner').removeClass('d-none');
                $('.checkoutBody').addClass('d-none');
            },
            hideCheckoutSpinner: function() {
                $('#checkout-spinner').addClass('d-none');
                $('.checkoutBody').removeClass('d-none');
            },
            showCheckout: function(data) {
                $('#checkout-pickup_at').text(moment.unix(data.pickup_at).format('DD MMM YYYY HH:mm:ss'));
                $('#checkout-pickup_zone_title').text(data.pickup_zone_title);
                $('#checkout-pickup_zone_addr').text(data.pickup_zone_addr);
                $('#checkout-drop_zone_title').text(data.drop_zone_title);
                $('#checkout-drop_zone_addr').text(data.drop_zone_addr);

                // // start packing list data
                // $('#checkout-unit').html('');
                // if (!isNaN(data.weight)) {
                //     $('#checkout-unit').append(`
                //         <div class="col-sm-4 mb-0 d-flex align-items-center">
                //             <label for="" class="form-label">Berat</label>
                //             <div class="card bg-light border mb-0 w-100">
                //                 <div class="card-body">
                //                     <p class="mb-0">${data.weight} Kg</p>
                //                 </div>
                //             </div>
                //         </div>
                //     `);
                // }
                // if (!isNaN(data.cbm)) {
                //     $('#checkout-unit').append(`
                //         <div class="col-sm-4 mb-0 d-flex align-items-center">
                //             <label for="" class="form-label">Volume</label>
                //             <div class="card bg-light border mb-0 w-100">
                //                 <div class="card-body">
                //                     <p class="mb-0">${data.cbm} m&sup3;</p>
                //                 </div>
                //             </div>
                //         </div>
                //     `);
                // }
                // if (!isNaN(data.koli)) {
                //     $('#checkout-unit').append(`
                //         <div class="col-sm-4 mb-0 d-flex align-items-center">
                //             <label for="" class="form-label">Koli</label>
                //             <div class="card bg-light border mb-0 w-100">
                //                 <div class="card-body">
                //                     <p class="mb-0">${data.koli} Pcs</p>
                //                 </div>
                //             </div>
                //         </div>
                //     `);
                // }
                // // end packing list data

                $('#checkout-group_pricing').html('');
                for (let opt of data.pricing_options) {
                    let badge_best_service = '',
                        real_price = '',
                        disc_price = '';
                    if (opt.is_best) {
                        badge_best_service = '<span class="badge bg-success ms-2">Best services</span>';
                    }
                    if (opt.disc_type == State.discount_type.fix) {
                        real_price =
                            `<p class="text-bold m-0 text-danger"><strike>Rp${(new Intl.NumberFormat('id-ID')).format(opt.real_price)}</strike></p>`;
                        disc_price =
                            `<p class="text-bold m-0 text-dark">Discount -${(new Intl.NumberFormat('id-ID')).format(opt.disc_price)}</p>`;
                    } else if (opt.disc_type == State.discount_type.percent) {
                        real_price =
                            `<p class="text-bold m-0 text-danger"><strike>Rp${(new Intl.NumberFormat('id-ID')).format(opt.real_price)}</strike></p>`;
                        disc_price =
                            `<p class="text-bold m-0 text-dark">Discount ${(new Intl.NumberFormat('id-ID')).format(opt.disc_percent)}%</p>`;
                    }
                    $('#checkout-group_pricing').append(`
                        <div class="row mb-3 d-flex align-items-center">
                            <div class="col-6">
                                <div class="form-check">
                                    <input class="form-check-input" type="radio" name="chooseFleet" data-lead_time_id="${opt.lead_time_id}" data-lead_time="${opt.lead_time}" data-price="${opt.price}"
                                    data-real_price="${opt.real_price}" data-disc_price="${opt.disc_price}">
                                    <label class="form-check-label text-success">
                                        ${opt.lead_time} Day ${badge_best_service}
                                    </label>
                                </div>
                            </div>
                            <div class="col-6 text-end text-bold">
                                ${real_price}
                                ${disc_price}
                                <p class="text-bold m-0 text-success">Rp${(new Intl.NumberFormat('id-ID')).format(opt.price)}</p>
                            </div>
                        </div>
                    `);
                }

                if (data.is_insurance) {
                    $('#checkout-group_pricing').append(`
                        <hr>
                        <div class="row mb-3 d-flex align-items-center">
                            <div class="col-6">
                                <p class="mb-0">Biaya Asuransi</p>
                            </div>
                            <div class="col-6 text-end text-bold">
                                <p id="checkout-insurance_value" class="text-bold m-0 text-dark" data-id="${data.insurance?.id}" data-price="${data.insurance.premi_price}">Rp${(new Intl.NumberFormat('id-ID')).format(data.insurance.premi_price)}</p>
                            </div>
                        </div>
                    `);
                }

                $('#checkout-truck_type').text(data.truck_type_name);

                $('#checkOutView').removeClass('d-none');
                $('#checkOutView').addClass('d-block');
            },
            hideCheckout: function() {
                $('#checkOutView').addClass('d-none');
                $('#checkOutView').removeClass('d-block');
                $('#btnOrder').attr('disabled', true);
            },
            downloadTemplatePackingList: function() {
                const fileName = 'template_packing_list.xlsx';
                const sheetName = 'template_packing_list';
                downloadPackingList(fileName, sheetName);
            },
            handleUploadPacking: function() {
                const reader = new FileReader();
                reader.onload = async function(e) {
                    TrxNew.hideUploadPackingList();
                    State.input_packing_list = [];
                    State.checked_packing_list = 1;

                    // let {
                    //     weight,
                    //     cbm,
                    //     koli,
                    // } = TrxNew.getData();

                    let data = e.target.result;
                    let workbook = XLSX.read(data, {
                        type: 'binary'
                    });
                    let sheet = workbook.Sheets['template_packing_list'];
                    let rowBodyStart = 3;
                    for (let key in sheet) {
                        if (key.indexOf('!') > -1) continue;
                        let rowNumber = Number(key.slice(1));
                        if (rowNumber < 3) continue;
                        if (key.indexOf('A') > -1) {
                            if (!sheet[key].v) {
                                Helper.toast('Warning', 'just now', 'Item Code not valid on row ' +
                                    rowNumber);
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart] = {};
                            for (let checkUniqCode of State.input_packing_list) {
                                if (checkUniqCode['Item Code'] == sheet[key].v) {
                                    State.checked_packing_list = 3;
                                    State.input_packing_list = [];
                                    Helper.toast('Warning', 'just now', 'Item Code: ' + sheet[key].v +
                                        ' is not uniq');
                                    TrxNew.showUploadPackingList();
                                    return false;
                                }
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Item Code'] = sheet[key].v;
                        }
                        if (key.indexOf('B') > -1) {
                            if (!sheet[key].v) {
                                Helper.toast('Warning', 'just now', 'Description not valid on row' +
                                    rowNumber);
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Description'] = sheet[key]
                                .v;
                        }
                        if (key.indexOf('C') > -1) {
                            if (isNaN(Number(sheet[key].v))) {
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                Helper.toast('Warning', 'just now', 'Weight (kg) not numeric on row' +
                                    rowNumber);
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Weight (kg)'] = Number(
                                sheet[
                                    key].v);
                        }
                        // if (key.indexOf('D') > -1) {
                        //     if (Number(sheet[key].v) < 1) {
                        //         if (State.input_packing_list[rowNumber - rowBodyStart]['Item Code']
                        //             .toLowerCase()
                        //             .indexOf('note') >
                        //             -1 || State.input_packing_list[rowNumber - rowBodyStart]['Item Code']
                        //             .toLowerCase().indexOf(
                        //                 'catatan') > -1) {} else {
                        //             State.checked_packing_list = 3;
                        //             State.input_packing_list = [];
                        //             Helper.toast('Warning', 'just now',
                        //                 'Qty cannot be 0 minimum qty is 1 on row' + rowNumber);
                        //             TrxNew.showUploadPackingList();
                        //             return false;
                        //         }
                        //     }
                        //     State.input_packing_list[rowNumber - rowBodyStart]['Qty'] = Number(sheet[key]
                        //         .v);
                        // }
                        if (key.indexOf('D') > -1) {
                            if (isNaN(Number(sheet[key].v))) {
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                Helper.toast('Warning', 'just now', 'Length (cm) not numeric on row' +
                                    rowNumber);
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Length (cm)'] = Number(
                                sheet[
                                    key].v);
                        }
                        if (key.indexOf('E') > -1) {
                            if (isNaN(Number(sheet[key].v))) {
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                Helper.toast('Warning', 'just now', 'Wide (cm) not numeric on row' +
                                    rowNumber);
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Wide (cm)'] = Number(sheet[
                                    key]
                                .v);
                        }
                        if (key.indexOf('F') > -1) {
                            if (isNaN(Number(sheet[key].v))) {
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                Helper.toast('Warning', 'just now', 'Height (cm) not numeric on row' +
                                    rowNumber);
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Height (cm)'] = Number(
                                sheet[
                                    key].v);
                        }
                        if (key.indexOf('G') > -1) {
                            if (isNaN(Number(sheet[key].v))) {
                                State.checked_packing_list = 3;
                                State.input_packing_list = [];
                                Helper.toast('Warning', 'just now', 'Volume (m3) not numeric on row' +
                                    rowNumber);
                                TrxNew.showUploadPackingList();
                                return false;
                            }
                            State.input_packing_list[rowNumber - rowBodyStart]['Volume (m3)'] = Number(
                                sheet[
                                    key].v);
                            if (State.input_packing_list[rowNumber - rowBodyStart]['Item Code']
                                .toLowerCase()
                                .indexOf('note') >
                                -1 || State.input_packing_list[rowNumber - rowBodyStart]['Item Code']
                                .toLowerCase().indexOf(
                                    'catatan') > -1) {
                                // rereset array index
                                State.input_packing_list.splice(rowNumber - rowBodyStart);
                                // not rereset array index
                                // State.input_packing_list[rowNumber-rowBodyStart] = undefined;
                            }
                        }
                    }
                    // State.input_packing_list = State.input_packing_list.filter(() => true);
                    let check_packing_list = await Req.check_packing_list({
                        packing_list: State.input_packing_list,
                        // weight,
                        // cbm,
                        // koli,
                    }, true);
                    if (check_packing_list.type == 'success') {
                        State.checked_packing_list = 2;
                    } else {
                        State.checked_packing_list = 3;
                    }
                    TrxNew.showUploadPackingList();
                };
                reader.onerror = function(err) {
                    console.error(err);
                };
                reader.onabort = function(err) {
                    console.error(err);
                };
                $('#add-packinglist').on('change', function(e) {
                    if (browserBack()) {
                        return false;
                    }
                    let file = e.target.files[0];
                    let type = file.name.split('.');
                    if (type[1].indexOf('xlsx') < 0) {
                        Helper.toast('Warning', 'just now',
                            'Format file not supported. Supported format is .xlsx');
                        return false;
                    }
                    // let {
                    //     weight,
                    //     cbm,
                    //     koli,
                    // } = TrxNew.getData();
                    // if (isNaN(weight) && isNaN(cbm) && isNaN(koli)) {
                    //     Helper.toast('Warning', 'just now',
                    //         'weight / volume / koli must be provided at least 1 of them');
                    //     return false;
                    // }
                    reader.readAsBinaryString(file);
                });
                $('#add-packinglist').on('click', function() {
                    $('#add-packinglist').val('');
                });
            },
            hideUploadPackingList: function() {
                $('#add-group_upload_packinglist').addClass('d-none');
                $('#add-group_upload_packinglist_spinner').removeClass('d-none');
            },
            showUploadPackingList: function() {
                $('#add-group_upload_packinglist').removeClass('d-none');
                $('#add-group_upload_packinglist_spinner').addClass('d-none');
            },
            showBtnOrdSpinner: function() {
                $('#btnOrderSpinner').removeClass('d-none');
                $('#btnOrder').addClass('d-none');
            },
            hideBtnOrdSpinner: function() {
                $('#btnOrderSpinner').addClass('d-none');
                $('#btnOrder').removeClass('d-none');
            },
            submitData: async function(data) {
                return new Promise((resolve, reject) => {
                    if (typeof $('#btnOrder').attr('disabed') != 'undefined') {
                        resolve({
                            type: 'fail'
                        });
                        return false;
                    }
                    $('#btnOrder').attr('disabed', true);
                    $.ajax({
                        url: "{{ route('api_trx_spc_create_order_v1') }}",
                        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) => {
                            TrxNew.hideBtnOrdSpinner();
                            $('#btnOrder').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 create order');
                            window.location.href = State.route_list_trx;
                            resolve({
                                type: 'success'
                            });
                        },
                        error: (jqXHR, textStatus, error) => {
                            TrxNew.hideBtnOrdSpinner();
                            $('#btnOrder').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
