<main id="main" class="main">

    <div class="pagetitle">
        <h1><i class="bi bi-truck"></i>&nbsp;Pengiriman FG</h1>
        <nav>
            <ol class="breadcrumb">

            </ol>
        </nav>
    </div><!-- End Page Title -->
    <script src="<?= base_url(); ?>assets/js/html5-qrcode.min.js"></script>
    <section class="section">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <br>
                        <!-- Table with stripped rows -->
                        <div class="datatable-wrapper datatable-loading no-footer sortable searchable fixed-columns">
                            <div class="row g-3">
                                <div class="col-md-3">
                                    <input type="date" class="form-control" id="mulai">
                                </div>
                                <div class="col-md-3">
                                    <input type="date" class="form-control" id="hingga">
                                </div>
                                <div class="col-md-4">
                                    <button onclick="tampildatakirim()" type="button" class="btn btn-primary">
                                        <i class="bi bi-search"></i>&nbsp;Lihat
                                    </button>
                                </div>

                            </div>
                            <br>
                            <div class="datatable-container">

                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>

    <ul class="nav nav-tabs" id="myTab" role="tablist">
        <li class="nav-item" role="presentation">
            <button class="nav-link active" id="home-tab" data-bs-toggle="tab" data-bs-target="#home" type="button" role="tab" aria-controls="home" aria-selected="true"><i class="bi bi-truck"></i>&nbsp;Pengiriman</button>
        </li>
        <li class="nav-item" role="presentation">
            <button class="nav-link" id="profile-tab" data-bs-toggle="tab" data-bs-target="#profile" type="button" role="tab" aria-controls="profile" aria-selected="false" tabindex="-1"><i class="bi bi-truck"></i>&nbsp;Expedisi / Kendaraan luar</button>
        </li>
    </ul>

    <div class="tab-content pt-2" id="myTabContent">
        <div class="tab-pane fade active show" id="home" role="tabpanel" aria-labelledby="home-tab">
            <button type="button" onclick="add_kirim()" class="btn btn-warning">
                <i class="bi bi-plus-circle"></i>&nbsp;Kendaraan
            </button><br><br>
            <div id="tbbjdelivery"></div>
        </div>
        <div class="tab-pane fade" id="profile" role="tabpanel" aria-labelledby="profile-tab">
            <button type="button" onclick="add_expedisi()" class="btn btn-secondary">
                <i class="bi bi-plus-circle"></i>&nbsp;Expedisi
            </button><br><br>
            <div id="tbbjexpedisi"></div>
        </div>
    </div>



    <script src="<?= base_url(); ?>assets/js/scanner_kirim.js"></script>
</main>

<div class="modal fade" id="f_add_kirim" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Persiapan Pengiriman</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No POL</label>
                    <div class="col-sm-6">
                        <select class="form-control form-control-sm" id="no_pol"></select>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Driver</label>
                    <div class="col-sm-6">
                        <select class="form-control form-control-sm" id="sopir"></select>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Tgl Kirim</label>
                    <div class="col-sm-6">
                        <input type="datetime-local" class="form-control form-control-sm" id="tgl_pergi">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Kilometer</label>
                    <div class="col-sm-6">
                        <input type="number" class="form-control form-control-sm" id="km_pergi" step="3600">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea class="form-control form-control-sm" id="keterangan"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="simpan_do()">Simpan</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_kembali" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Rit Kembali</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="id_rit_balik">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Tgl Kembali</label>
                    <div class="col-sm-6">
                        <input type="datetime-local" class="form-control form-control-sm" id="tgl_kembali">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">KM Kembali</label>
                    <div class="col-sm-6">
                        <input type="number" class="form-control form-control-sm" id="km_kembali" step="3600">
                    </div>
                </div>
                <input type="hidden" id="pergi_km">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="simpan_rit_balik()">Simpan</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_update_kirim" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update Persiapan Pengiriman</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="ed_id_rit">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No POL</label>
                    <div class="col-sm-6">
                        <select class="form-control form-control-sm" id="ed_no_pol"></select>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Driver</label>
                    <div class="col-sm-6">
                        <select class="form-control form-control-sm" id="ed_sopir"></select>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Tgl Kirim</label>
                    <div class="col-sm-6">
                        <input type="datetime-local" class="form-control form-control-sm" id="ed_tgl_pergi">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Kilometer</label>
                    <div class="col-sm-6">
                        <input type="number" class="form-control form-control-sm" id="ed_km_pergi" step="3600">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea class="form-control form-control-sm" id="ed_keterangan"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-success" onclick="update_do()">Update</button>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="f_add_sj" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Surat Jalan</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="id_rit_sj">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No SJ</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="no_sj">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">ID Cust</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="customer">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Kota</label>
                    <div class="col-sm-9">
                        <input type="text" id="kota" name="strDeskripsi" data-parsley-trigger="change" required="" placeholder="Cari kota" autocomplete="off" class="form-control form-control-sm" list="list_kota">
                        <datalist id="list_kota">
                            <?php
                            foreach ($kota as $k) {
                                echo "<option value='" . $k['id_kota'] . "'>   (" . $k['kota'] . ")</option>";
                            }
                            ?>
                        </datalist>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">DK / LK</label>
                    <div class="col-sm-9">
                        <select class="form-control form-control-sm" id="dklk">
                            <option value="LK">LK</option>
                            <option value="DK">DK</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea id="ket_sj" class="form-control form-control-sm"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="simpan_sj()">Simpan</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_update_sj" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update Surat Jalan</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="ed_id_rit_sj">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No SJ</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="ed_no_sj">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">ID Cust</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="ed_customer">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Kota</label>
                    <div class="col-sm-9">
                        <input type="text" id="ed_kota" name="strDeskripsi" data-parsley-trigger="change" required="" placeholder="Cari kota" autocomplete="off" class="form-control form-control-sm" list="ed_list_kota">
                        <datalist id="ed_list_kota">
                            <?php
                            foreach ($kota as $k) {
                                echo "<option value='" . $k['id_kota'] . "'>   (" . $k['kota'] . ")</option>";
                            }
                            ?>
                        </datalist>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">DK / LK</label>
                    <div class="col-sm-9">
                        <select class="form-control form-control-sm" id="ed_dklk">
                            <option value="LK">LK</option>
                            <option value="DK">DK</option>
                        </select>
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea id="ed_ket_sj" class="form-control form-control-sm"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="update_sj()">Simpan</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_pindah_fg" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Pindah Barang SJ</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="pindah_sku">
                <input type="hidden" id="pindah_sj_asal">
                <div class="row mb-3">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Barang</label>
                    <div class="col-sm-7">
                        <input type="text" id="nama_brg" class="form-control form-control-sm" readonly>
                    </div>
                    <div class="col-sm-2">
                        <input type="text" id="qty_brg" class="form-control form-control-sm" readonly>
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No SJ</label>
                    <div class="col-sm-7">
                        <input type="text" id="sj_brg" class="form-control form-control-sm" readonly>
                    </div>
                </div>
                <div class="row mb-3">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Pindah ke No SJ</label>
                    <div class="col-sm-7">
                        <select class="form-control form-control-sm" id="pindah_no_sj"></select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="pindah_fg()">Pindah</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_add_exp" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Expedisi / Pick-up</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No POL</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="exp_no_pol">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Tanggal</label>
                    <div class="col-sm-9">
                        <input type="datetime-local" class="form-control form-control-sm" id="exp_tgl">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No RESI</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="exp_no_resi">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Cust</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="exp_customer">
                    </div>
                </div>

                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea id="exp_ket_sj" class="form-control form-control-sm"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="simpan_exp()">Simpan</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_add_sj_exp" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Surat Jalan Expedisi / Kendaraan Luar</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="id_rit_sj_exp">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No SJ</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="no_sj_exp">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea id="ket_sj_exp" class="form-control form-control-sm"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="simpan_sj_exp()">Simpan SJ</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_update_sj_exp" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Surat Jalan Expedisi / Kendaraan Luar</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <input type="hidden" id="ed_id_rit_sj_exp">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No SJ</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="ed_no_sj_exp">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea id="ed_ket_sj_exp" class="form-control form-control-sm"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-success" onclick="update_sj_exp()">Update SJ</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_update_exp" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Expedisi / Pick-up</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <input type="hidden" id="ed_id_exp">
            <div class="modal-body">
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No POL</label>
                    <div class="col-sm-4">
                        <input type="text" class="form-control form-control-sm" id="ed_exp_no_pol">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Tanggal</label>
                    <div class="col-sm-9">
                        <input type="datetime-local" class="form-control form-control-sm" id="ed_exp_tgl">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">No RESI</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="ed_exp_no_resi">
                    </div>
                </div>
                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Cust</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control form-control-sm" id="ed_exp_customer">
                    </div>
                </div>

                <div class="row mb-6">
                    <label for="inputEmail3" class="col-sm-3 col-form-label">Keterangan</label>
                    <div class="col-sm-9">
                        <textarea id="ed_exp_ket_sj" class="form-control form-control-sm"></textarea>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-success" onclick="update_exp()">Update</button>
            </div>
        </div>
    </div>
</div>

<script>
    function tampildatakirim() {
        var mulai = $("#mulai").val();
        var hingga = $("#hingga").val();
        $.get("<?= base_url('Scan/tb_do?s='); ?>" + mulai + "&e=" + hingga, function(data, status) {
            $("#tbbjdelivery").html(data);
        });

        $.get("<?= base_url('Scan/tb_expedisi?s='); ?>" + mulai + "&e=" + hingga, function(data, status) {
            $("#tbbjexpedisi").html(data);
        });
    }



    function pesankirim() {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 2000,
            timerProgressBar: false,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "success",
            title: "Delivery telah dibuat"
        });
    }

    function pesansukses(txt) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 2000,
            timerProgressBar: false,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "success",
            title: txt
        });
    }

    function pesan_error(txt) {
        const Toast = Swal.mixin({
            toast: true,
            position: "top-end",
            showConfirmButton: false,
            timer: 2000,
            timerProgressBar: false,
            didOpen: (toast) => {
                toast.onmouseenter = Swal.stopTimer;
                toast.onmouseleave = Swal.resumeTimer;
            }
        });
        Toast.fire({
            icon: "error",
            title: txt
        });
    }

    function add_kirim() {
        $.get("<?= base_url('Scan/available_truck'); ?>", function(data, status) {
            $("#no_pol").html(data);
        });

        $.get("<?= base_url('Scan/available_driver'); ?>", function(data, status) {
            $("#sopir").html(data);
        });

        $("#f_add_kirim").modal("show");
    }

    function simpan_do() {
        var no_pol = $("#no_pol").val();
        var sopir = $("#sopir").val();
        var km_pergi = $("#km_pergi").val();
        var tgl_pergi = $("#tgl_pergi").val();
        var keterangan = $("#keterangan").val();
        if (no_pol == 0) {
            pesan_error('Kendaraan mohon dipilih');
        } else {
            if (sopir == 0) {
                pesan_error('Mohon pilih sopir untuk pengiriman ini');
            } else {
                if (km_pergi == '') {
                    pesan_error('Kilometer keberangkatan mohon diisi');
                } else {
                    if (tgl_pergi == '') {
                        pesan_error('Kilometer keberangkatan mohon diisi');
                    } else {
                        $.ajax({
                            url: "<?= base_url('Scan/simpan_do'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                no_pol: no_pol,
                                sopir: sopir,
                                tgl_pergi: tgl_pergi,
                                km_pergi: km_pergi,
                                keterangan: keterangan
                            },
                            success: function() {
                                tampildatakirim();
                                document.getElementById('no_pol').value = '';
                                document.getElementById('sopir').value = '';
                                document.getElementById('km_pergi').value = '';
                                document.getElementById('tgl_pergi').value = '';
                                document.getElementById('keterangan').value = '';
                                //log
                                var strTable = 'tb_ritase';
                                var keterangan = 'Telah membuat dokumen delivery untuk kendaraan : ' + no_pol;
                                $.ajax({
                                    url: "<?= base_url('Pengaturan/log'); ?>",
                                    type: 'POST',
                                    cache: false,
                                    data: {
                                        strTable: strTable,
                                        keterangan: keterangan
                                    },
                                    success: function() {}
                                });
                                pesankirim();
                                $("#f_add_kirim").modal("hide");
                            }
                        });
                    }
                }
            }
        }
    }

    function hapus_sj(id) {
        Swal.fire({
            title: "Hapus pengiriman ini ?",
            text: "SJ yang berada pada kendaraan ini juga akan dihapus beserta barang yang telah discan",
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
        }).then((result) => {
            if (result.isConfirmed) {
                //Begin                
                $.ajax({
                    url: "<?= base_url('Scan/hapus_sj'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id: id
                    },
                    success: function() {
                        tampildatakirim();
                    }
                });
                //End
            }
        });
    }

    function rit_kembali(id, tgl, pergi) {
        $("#id_rit_balik").val(id);
        $("#tgl_kembali").attr("min", tgl);
        $("#pergi_km").val(pergi);
        $("#f_kembali").modal("show");
    }


    function simpan_rit_balik() {
        var id_rit = $("#id_rit_balik").val();
        var tgl_kembali = $("#tgl_kembali").val();
        var km_kembali = $("#km_kembali").val();
        var km_pergi = $("#pergi_km").val();

        if (tgl_kembali == "") {
            pesan_error('Tanggal kembali harus diisi');
        } else {
            if (km_kembali == "") {
                pesan_error('Kilometer kembali harus diisi');
            } else {
                if (parseInt(km_kembali) <= parseInt(km_pergi)) {
                    pesan_error('Kilometer tidak sesuai');
                } else {
                    $.ajax({
                        url: "<?= base_url('Scan/rit_balik'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            id_rit: id_rit,
                            tgl_kembali: tgl_kembali,
                            km_kembali: km_kembali
                        },
                        success: function() {
                            document.getElementById('tgl_kembali').value = '';
                            document.getElementById('km_kembali').value = '';
                            $("#f_kembali").modal("hide");
                            tampildatakirim();
                            pesansukses('Data disimpan');
                        }
                    });
                }
            }
        }
    }


    function edit_sj(id, tgl, km, ket) {
        $("#ed_id_rit").val(id);
        $("#ed_tgl_pergi").val(tgl);
        $("#ed_km_pergi").val(km);
        $("#ed_keterangan").val(ket);
        $.get("<?= base_url('Scan/available_truck'); ?>", function(data, status) {
            $("#ed_no_pol").html(data);
        });

        $.get("<?= base_url('Scan/available_driver'); ?>", function(data, status) {
            $("#ed_sopir").html(data);
        });
        $("#f_update_kirim").modal("show");
    }

    function update_do() {
        var id_rit = $("#ed_id_rit").val();
        var no_pol = $("#ed_no_pol").val();
        var sopir = $("#ed_sopir").val();
        var tgl_pergi = $("#ed_tgl_pergi").val();
        var km_pergi = $("#ed_km_pergi").val();
        var keterangan = $("#ed_keterangan").val();
        if (tgl_pergi == '') {
            pesan_error('No SJ harus diisi');
        } else {
            if (km_pergi == '') {
                pesan_error('No SJ harus diisi');
            } else {
                $.ajax({
                    url: "<?= base_url('Scan/update_pengiriman'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id_rit: id_rit,
                        no_pol: no_pol,
                        sopir: sopir,
                        tgl_pergi: tgl_pergi,
                        km_pergi: km_pergi,
                        keterangan: keterangan
                    },
                    success: function() {
                        $("#f_update_kirim").modal("hide");
                        tampildatakirim();
                        pesansukses('Data telah update');
                    }
                });
            }
        }
    }

    function berangkat(id) {
        Swal.fire({
            title: "Kirim ke customer ?",
            //text: "You won't be able to revert this!",
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Ya,Kirim"
        }).then((result) => {
            if (result.isConfirmed) {
                //begin
                $.ajax({
                    url: "<?= base_url('Scan/kirim_customer'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id: id
                    },
                    success: function() {
                        tampildatakirim();
                    }
                });
                //end
            }
        });
    }

    function add_sj(id) {
        $("#id_rit_sj").val(id);
        $("#f_add_sj").modal("show");
    }

    function simpan_sj() {
        var id_rit_sj = $("#id_rit_sj").val();
        var no_sj = $("#no_sj").val();
        var customer = $("#customer").val();
        var kota = $("#kota").val();
        var dklk = $("#dklk").val();
        var ket_sj = $("#ket_sj").val();
        if (kota == '') {
            pesan_error('Informasi Kota mohon diisi');
        } else {
            $.ajax({
                url: "<?= base_url('Scan/simpan_sj'); ?>",
                type: 'POST',
                cache: false,
                data: {
                    id_rit_sj: id_rit_sj,
                    no_sj: no_sj,
                    customer: customer,
                    kota: kota,
                    dklk: dklk,
                    ket_sj: ket_sj
                },
                success: function() {
                    document.getElementById('id_rit_sj').value = '';
                    document.getElementById('no_sj').value = '';
                    document.getElementById('customer').value = '';
                    document.getElementById('kota').value = '';
                    document.getElementById('dklk').value = '';
                    document.getElementById('ket_sj').value = '';
                    tampildatakirim();
                    pesansukses('SJ disimpan');
                    $("#f_add_sj").modal("hide");
                }
            });
        }
    }

    function edit_sjln(id, sj, cst, kota, dklk, ket) {
        $("#ed_id_rit_sj").val(id);
        $("#ed_no_sj").val(sj);
        $("#ed_customer").val(cst);
        $("#ed_kota").val(kota);
        $("#ed_dklk").val(dklk);
        $("#ed_ket_sj").val(ket);
        $("#f_update_sj").modal("show");
    }

    function update_sj() {
        var doc_sj = $("#ed_id_rit_sj").val();
        var no_sj = $("#ed_no_sj").val();
        var customer = $("#ed_customer").val();
        var kota = $("#ed_kota").val();
        var dklk = $("#ed_dklk").val();
        var ket_sj = $("#ed_ket_sj").val();
        $.ajax({
            url: "<?= base_url('Scan/update_sj'); ?>",
            type: 'POST',
            cache: false,
            data: {
                doc_sj: doc_sj,
                no_sj: no_sj,
                customer: customer,
                kota: kota,
                dklk: dklk,
                ket_sj: ket_sj
            },
            success: function() {
                pesansukses('SJ diupdate');
                tampildatakirim();
                $("#f_update_sj").modal("hide");
            }
        });
    }

    function hapus_sjln(id) {
        Swal.fire({
            title: "Hapus SJ ?",
            text: "Barang yang sudah discan juga kan ikut terhapus ",
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "<?= base_url('Scan/hapus_surat_jalan'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id: id
                    },
                    success: function() {
                        tampildatakirim();
                        $("#f_update_sj").modal("hide");
                    }
                });
            }
        });
    }

    function add_expedisi() {
        $("#f_add_exp").modal("show");
    }

    function hapus_fg_scan(sku, sj) {
        Swal.fire({
            title: "Hapus FG ?",
            //text: "You won't be able to revert this!",
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
        }).then((result) => {
            if (result.isConfirmed) {
                //begin
                $.ajax({
                    url: "<?= base_url('Scan/hapus_fg_pengiriman'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        sku: sku,
                        sj: sj
                    },
                    success: function() {
                        tampildatakirim();
                    }
                });
                //end
            }
        });
    }

    function pindah_fg_scan(sku, sj, brg, qty, nosj) {
        $("#pindah_sku").val(sku);
        $("#pindah_sj_asal").val(sj);
        $("#nama_brg").val(brg);
        $("#qty_brg").val(qty);
        $("#sj_brg").val(nosj);
        $.get("<?= base_url('Scan/get_available_sj?sj='); ?>" + sj, function(data, status) {
            $("#pindah_no_sj").html(data);
        });
        $("#f_pindah_fg").modal("show");
    }

    function pindah_fg() {
        var sku = $("#pindah_sku").val();
        var sj = $("#pindah_sj_asal").val();
        var pindah_sj = $("#pindah_no_sj").val();
        $.ajax({
            url: "<?= base_url('Scan/pindah_fg_pengiriman'); ?>",
            type: 'POST',
            cache: false,
            data: {
                sku: sku,
                sj: sj,
                pindah_sj: pindah_sj
            },
            success: function() {
                $("#f_pindah_fg").modal("hide");
                tampildatakirim();
            }
        });
    }

    function simpan_exp() {
        var no_pol = $("#exp_no_pol").val();
        var no_resi = $("#exp_no_resi").val();
        var customer = $("#exp_customer").val();
        var ket_sj = $("#exp_ket_sj").val();
        var tgl = $("#exp_tgl").val();
        if (tgl == '') {
            pesan_error('Tanggal mohon diisi');
        } else {
            $.ajax({
                url: "<?= base_url('Scan/simpan_expedisi'); ?>",
                type: 'POST',
                cache: false,
                data: {
                    no_pol: no_pol,
                    no_resi: no_resi,
                    customer: customer,
                    ket_sj: ket_sj,
                    tgl: tgl
                },
                success: function() {
                    document.getElementById('exp_no_pol').value = '';
                    document.getElementById('exp_no_resi').value = '';
                    document.getElementById('exp_customer').value = '';
                    document.getElementById('exp_ket_sj').value = '';
                    document.getElementById('exp_tgl').value = '';
                    $("#f_add_exp").modal("hide");
                    tampildatakirim();
                }
            });
        }
    }

    function add_sj_exp(id) {
        $("#id_rit_sj_exp").val(id);
        $("#f_add_sj_exp").modal("show");
    }

    function simpan_sj_exp() {
        var id_rit_sj = $("#id_rit_sj_exp").val();
        var no_sj = $("#no_sj_exp").val();
        var ket_sj = $("#ket_sj_exp").val();
        $.ajax({
            url: "<?= base_url('Scan/simpan_sj_exp'); ?>",
            type: 'POST',
            cache: false,
            data: {
                id_rit_sj: id_rit_sj,
                no_sj: no_sj,
                ket_sj: ket_sj
            },
            success: function() {
                document.getElementById('no_sj').value = '';
                document.getElementById('ket_sj').value = '';
                tampildatakirim();
                pesansukses('SJ disimpan');
                $("#f_add_sj_exp").modal("hide");
            }
        });
    }

    function edit_sjln_exp(id, sj, ket) {
        $("#ed_id_rit_sj_exp").val(id);
        $("#ed_no_sj_exp").val(sj);
        $("#ed_ket_sj_exp").val(ket);
        $("#f_update_sj_exp").modal("show");
    }

    function update_sj_exp() {
        var doc_sj = $("#ed_id_rit_sj_exp").val();
        var no_sj = $("#ed_no_sj_exp").val();
        var ket_sj = $("#ed_ket_sj_exp").val();
        $.ajax({
            url: "<?= base_url('Scan/update_sj_exp'); ?>",
            type: 'POST',
            cache: false,
            data: {
                doc_sj: doc_sj,
                no_sj: no_sj,
                ket_sj: ket_sj
            },
            success: function() {
                tampildatakirim();
                pesansukses('SJ disimpan');
                $("#f_update_sj_exp").modal("hide");
            }
        });
    }

    function edit_exp(id, nopol, tgl, resi, cust, ket) {
        $("#ed_id_exp").val(id);
        $("#ed_exp_no_pol").val(nopol);
        $("#ed_exp_tgl").val(tgl);
        $("#ed_exp_no_resi").val(resi);
        $("#ed_exp_customer").val(cust);
        $("#ed_exp_ket_sj").val(ket);
        $("#f_update_exp").modal("show");
    }

    function update_exp() {
        var id = $("#ed_id_exp").val();
        var nopol = $("#ed_exp_no_pol").val();
        var tgl = $("#ed_exp_tgl").val();
        var resi = $("#ed_exp_no_resi").val();
        var cust = $("#ed_exp_customer").val();
        var ket = $("#ed_exp_ket_sj").val();
        $.ajax({
            url: "<?= base_url('Scan/update_exp'); ?>",
            type: 'POST',
            cache: false,
            data: {
                id: id,
                nopol: nopol,
                tgl: tgl,
                resi: resi,
                cust: cust,
                ket: ket
            },
            success: function() {
                tampildatakirim();
                pesansukses('Disimpan');
                $("#f_update_exp").modal("hide");
            }
        });
    }
    
    function cetak_sjln(sj) {
        window.open("<?= base_url('gudang/cetak_sj?sj='); ?>" + sj);
    }
</script>