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

    <div class="pagetitle">
        <h1>Master Kendaraan</h1>
        <nav>
            <ol class="breadcrumb">

            </ol>
        </nav>
    </div><!-- End Page Title -->

    <section class="section">
        <div class="row">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body"><br>
                        <button type="button" class="btn btn-primary" onclick="tampildata()">Reload</button>
                        <br>
                        <!-- Table with stripped rows -->
                        <div class="datatable-wrapper datatable-loading no-footer sortable searchable fixed-columns">
                            <div class="row g-3">
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tb_kendaraan"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<div class="modal fade" id="f_edit_kendaraan" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update Kendaraan</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form class="row g-3">
                    <input type="hidden" id="ed_id_kendaraan">
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">No Pol</label>
                        <input type="text" class="form-control" id="ed_nopol">
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Merk</label>
                        <select id="ed_merk" class="form-control">
                            <option value="0">- Merk kendaraan-</option>
                            <option value="Mitsubishi">Mitsubishi</option>
                            <option value="Isuzu">Isuzu</option>
                            <option value="Toyota">Toyota</option>
                            <option value="Hino">Hino</option>
                            <option value="Nissan">Nissan</option>
                            <option value="UD">UD</option>
                            <option value="Daihatsu">Daihatsu</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Jenis</label>
                        <select id="ed_jenis" class="form-control">
                            <option value="0">- Jenis kendaraan-</option>
                            <option value="Pickup">Pickup</option>
                            <option value="Minibus">Minibus</option>
                            <option value="Truk">Truk</option>
                            <option value="Truk boks">Truk boks</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label for="inputAddress" class="form-label">Kapasitas</label>
                        <input type="text" class="form-control" id="ed_kapasitas" placeholder="1234 Main St">
                    </div>
                    <div class="col-12">
                        <label for="inputAddress" class="form-label">Tanggal Berlaku STNK</label>
                        <input type="date" class="form-control" id="ed_tgl_stnk" placeholder="1234 Main St">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="updatekendaraan()">Save changes</button>
            </div>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $.get("<?= base_url('Masterdata/tb_kendaraan'); ?>", function(data, status) {
            $("#tb_kendaraan").html(data);
        });
    });

    function tampildata() {
        $.get("<?= base_url('Masterdata/tb_kendaraan'); ?>", function(data, status) {
            $("#tb_kendaraan").html(data);
        });
    }

    function simpan() {
        var nopol = $("#no_pol").val();
        var merk = $("#merk").val();
        var jenis = $("#jenis").val();
        var kapasitas = $("#kapasitas").val();
        var tgl_stnk = $("#tgl_stnk").val();
        if (nopol == "") {
            document.getElementById('no_pol').focus();
            pesan_warning("No Pol harus diisi");
        } else {
            if (merk == 0) {
                document.getElementById('merk').focus();
                pesan_warning("Merk kendaraan harus diisi");
            } else {
                if (jenis == 0) {
                    document.getElementById('jenis').focus();
                    pesan_warning("Jenis kendaraan harus diisi");
                } else {
                    if (tgl_stnk == "") {
                        document.getElementById('jenis').focus();
                        pesan_warning("Tanggal Berlaku STNK harus diisi");
                    } else {
                        $.ajax({
                            url: "<?= base_url('Masterdata/simpan_kendaraan'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                nopol: nopol,
                                merk: merk,
                                jenis: jenis,
                                kapasitas: kapasitas,
                                tgl_stnk: tgl_stnk
                            },
                            success: function() {
                                tampildata();
                                pesan();
                                //log
                                var strTable = 'tb_kendaraan';
                                var keterangan = 'Telah menambahkan data kendaraan dengan Nopol : ' + nopol;
                                $.ajax({
                                    url: "<?= base_url('Pengaturan/log'); ?>",
                                    type: 'POST',
                                    cache: false,
                                    data: {
                                        strTable: strTable,
                                        keterangan: keterangan
                                    },
                                    success: function() {}
                                });
                            }
                        });
                    }
                }
            }
        }
    }

    function pesan_warning(txt) {
        Swal.fire({
            title: txt,
            icon: "warning"
        });
    }

    function pesan() {
        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: "data disimpan"
        });
    }

    function hapus(id, nopol) {
        Swal.fire({
            title: "Hapus ?",
            //text: "You won't be able to revert this!",
            icon: "warning",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Yes, delete it!"
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: "<?= base_url('Masterdata/hapus_kendaraan'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id: id
                    },
                    success: function() {
                        tampildata();
                        //log
                        var strTable = 'tb_kendaraan';
                        var keterangan = 'Telah menghapus data kendaraan dengan Nopol : ' + nopol;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {}
                        });
                    }
                });
            }
        });
    }

    function editkendaraan(id, nopol, merk, jenis, cc, tgl) {
        $("#ed_id_kendaraan").val(id);
        $("#ed_nopol").val(nopol);
        $("#ed_merk").val(merk);
        $("#ed_jenis").val(jenis);
        $("#ed_kapasitas").val(cc);
        $("#ed_tgl_stnk").val(tgl);
    }

    function updatekendaraan() {
        var id_kendaraan = $("#ed_id_kendaraan").val();
        var no_pol = $("#ed_nopol").val();
        var merk = $("#ed_merk").val();
        var jenis = $("#ed_jenis").val();
        var cc = $("#ed_kapasitas").val();
        var tgl_stnk = $("#ed_tgl_stnk").val();
        if (no_pol == "") {
            document.getElementById('ed_nopol').focus();
            pesan_warning("No Pol harus diisi");
        } else {
            if (merk == 0) {
                document.getElementById('ed_merk').focus();
                pesan_warning("Merk kendaraan harus diisi");
            } else {
                if (jenis == 0) {
                    document.getElementById('ed_jenis').focus();
                    pesan_warning("Jenis kendaraan harus diisi");
                } else {
                    if (tgl_stnk == "") {
                        document.getElementById('ed_tgl_stnk').focus();
                        pesan_warning("Tanggal Berlaku STNK harus diisi");
                    } else {
                        $.ajax({
                            url: "<?= base_url('Masterdata/update_kendaraan'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                id_kendaraan: id_kendaraan,
                                no_pol: no_pol,
                                merk: merk,
                                jenis: jenis,
                                cc: cc,
                                tgl_stnk: tgl_stnk
                            },
                            success: function() {
                                $("#f_edit_kendaraan").modal("hide");
                                tampildata();
                                //log
                                var strTable = 'tb_kendaraan';
                                var keterangan = 'Telah mengupdate data kendaraan dengan Nopol : ' + no_pol;
                                $.ajax({
                                    url: "<?= base_url('Pengaturan/log'); ?>",
                                    type: 'POST',
                                    cache: false,
                                    data: {
                                        strTable: strTable,
                                        keterangan: keterangan
                                    },
                                    success: function() {}
                                });
                            }
                        });
                    }
                }
            }
        }
    }
</script>