<div class="container-fluid">
    <div class="row">
        <div class="col-md-12">
            <div class="card card-primary">
                <div id="tabel_loading" class="overlay d-flex justify-content-center align-items-center">
                    <i class="fas fa-2x fa-sync fa-spin"></i>
                </div>
                <div class="card-header">
                    <h3 class="card-title">KATEGORI PDAM</h3>

                    <div class="card-tools">
                        <button type="button" class="btn btn-tool" onclick="aksi_reload()"><i class="fas fa-sync-alt"></i></button>
                    </div>
                </div>
                <div class="card-body">
                    <a class="btn btn-primary" onclick="aksi_tambah_modal()">Tambah</a> &nbsp;
                    <div style="margin-bottom: 10px;"></div>
                    <div id="tabel" class="table-responsive">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<div class="modal fade" id="modal_pdam">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div id="modal_loading" class="overlay d-flex justify-content-center align-items-center">
                <i class="fas fa-2x fa-sync fa-spin"></i>
            </div>
            <form enctype="multipart/form-data" id="submit">
                <div class="modal-header">
                    <h4 id="modal_title" class="modal-title">TAMBAH DATA</h4>
                    <button type="button" class="close" onclick="aksi_tutup_modal()" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-row">
                                <label for="input1" class="col-lg-3 control-label">Kode</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" id="ktg_pdam_kode" name="ktg_pdam_kode" placeholder="Kode kategori">
                                </div>
                            </div>
                            <br>
                            <div class="form-row">
                                <label for="input1" class="col-lg-3 control-label">Nama</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" id="ktg_pdam_nama" name="ktg_pdam_nama" placeholder="Nama kategori">
                                </div>
                            </div>
                            <br>
                            <div class="form-row">
                                <label for="input1" class="col-lg-3 control-label">Icon</label>
                                <div class="col-lg-9">
                                    <input type="file" class="form-control" id="ktg_pdam_icon" name="ktg_pdam_icon">
                                </div>
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-row">
                                <label for="input1" class="col-lg-3 control-label">Urutan</label>
                                <div class="col-lg-9">
                                    <input type="number" class="form-control" id="ktg_pdam_urutan" name="ktg_pdam_urutan" placeholder="Urutan tampil">
                                </div>
                            </div>
                            <br>
                            <div class="form-row">
                                <label for="input1" class="col-lg-3 control-label">Keterangan</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" id="ktg_pdam_keterangan" name="ktg_pdam_keterangan" placeholder="Keterangan">
                                </div>
                            </div>
                            <br>
                            <div class="form-row">
                                <label for="input1" class="col-lg-3 control-label">Notasi</label>
                                <div class="col-lg-9">
                                    <input type="text" class="form-control" id="ktg_pdam_notasi" name="ktg_pdam_notasi" placeholder="Notasi khusus (promo, cara pakai dll)">
                                </div>
                            </div>
                        </div>
                    </div>

                </div>

                <div class="modal-footer justify-content-between">
                    <button type="button" class="btn btn-default" onclick="aksi_tutup_modal()">TUTUP</button>
                    <button type="submit" id="btn_simpan" class="btn btn-primary">SIMPAN</button>
                </div>
            </form>
        </div>
        <!-- /.modal-content -->
    </div>
    <!-- /.modal-dialog -->
</div>


<script src="<?php echo base_url(); ?>assets/plugins/jquery/jquery.min.js"></script>

<script type='text/javascript'>
    var submit_status;
    $(document).ready(function() {
        aksi_data();
    });

    $('#submit').submit(function(e) {
        e.preventDefault();
        var ktg_pdam_kode = $("#ktg_pdam_kode").val();
        var ktg_pdam_nama = $("#ktg_pdam_nama").val();
        var ktg_pdam_icon = $("#ktg_pdam_icon").val();
        var ktg_pdam_urutan = $("#ktg_pdam_urutan").val();
        var ktg_pdam_keterangan = $("#ktg_pdam_keterangan").val();
        var ktg_pdam_notasi = $("#ktg_pdam_notasi").val();

        //EDIT
        if (submit_status == 'Edit') {
            if (ktg_pdam_kode.length < 3) {
                toastr.error('Data belum lengkap');
            } else if (ktg_pdam_nama.length < 3) {
                toastr.error('Data belum lengkap');
            } else if (ktg_pdam_urutan.length < 1) {
                toastr.error('Data belum lengkap');
            } else {
                document.getElementById("modal_loading").style.visibility = "visible";

                $.ajax({
                    url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_edit',
                    type: "POST",
                    data: new FormData(this),
                    processData: false,
                    contentType: false,
                    cache: false,
                    async: false,
                    success: function(data) {
                        toastr.success('Kategori berhasil di edit');

                        //SET VALUE
                        document.getElementById("ktg_pdam_kode").value = '';
                        document.getElementById("ktg_pdam_nama").value = '';
                        document.getElementById("ktg_pdam_icon").value = '';
                        document.getElementById("ktg_pdam_urutan").value = '';
                        document.getElementById("ktg_pdam_keterangan").value = '';
                        document.getElementById("ktg_pdam_notasi").value = '';

                        //LOADING
                        document.getElementById("modal_loading").style.visibility = "hidden";

                        //MODAL
                        $("#modal_pdam").modal('hide');

                        aksi_data();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        toastr.error('Kategori gagal di edit');
                        document.getElementById("modal_loading").style.visibility = "hidden";
                    }
                });
            }
        }

        //TAMBAH
        if (submit_status == 'Tambah') {
            if (ktg_pdam_kode.length < 3) {
                toastr.error('Data belum lengkap');
            } else if (ktg_pdam_nama.length < 3) {
                toastr.error('Data belum lengkap');
            } else if (ktg_pdam_icon.length < 1) {
                toastr.error('Data belum lengkap');
            } else if (ktg_pdam_urutan.length < 1) {
                toastr.error('Data belum lengkap');
            } else {
                document.getElementById("modal_loading").style.visibility = "visible";

                $.ajax({
                    url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_tambah',
                    type: "POST",
                    data: new FormData(this),
                    processData: false,
                    contentType: false,
                    cache: false,
                    async: false,
                    success: function(data) {
                        var obj = JSON.parse(data);
                        var tambah_status = obj.tambah_status;

                        if (tambah_status == '0') {
                            toastr.success('Kategori berhasil di tambahkan');

                            //SET VALUE
                            document.getElementById("ktg_pdam_kode").value = '';
                            document.getElementById("ktg_pdam_nama").value = '';
                            document.getElementById("ktg_pdam_icon").value = '';
                            document.getElementById("ktg_pdam_urutan").value = '';
                            document.getElementById("ktg_pdam_keterangan").value = '';
                            document.getElementById("ktg_pdam_notasi").value = '';

                            //LOADING
                            document.getElementById("modal_loading").style.visibility = "hidden";

                            aksi_data();
                        } else {
                            toastr.error('Kategori gagal di tambahkan karena kode kategori sudah pernah ditambahkan sebelumnya!');
                            document.getElementById("modal_loading").style.visibility = "hidden";
                        }

                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        toastr.error('Kategori gagal di tambahkan');
                        document.getElementById("modal_loading").style.visibility = "hidden";
                    }
                });
            }
        }

    });

    function aksi_data() {
        document.getElementById("tabel_loading").style.visibility = "visible";
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_data',
            success: function(html) {
                document.getElementById("tabel_loading").style.visibility = "hidden";
                $("#tabel").html(html);
            }
        })
    }

    function aksi_reload() {
        document.getElementById("tabel_loading").style.visibility = "visible";
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_reload',
            success: function() {
                document.getElementById("tabel_loading").style.visibility = "hidden";
                aksi_data();
            }
        })
    }

    function aksi_edit_modal(element) {
        document.getElementById("modal_loading").style.visibility = "hidden";

        var $row = $(element).closest("tr");
        var ktg_pdam_kode = $row.find("#tbl_ktg_pdam_kode").text();;
        var ktg_pdam_nama = $row.find("#tbl_ktg_pdam_nama").text();
        var ktg_pdam_urutan = $row.find("#tbl_ktg_pdam_urutan").text();
        var ktg_pdam_keterangan = $row.find("#tbl_ktg_pdam_keterangan").text();
        var ktg_pdam_notasi = $row.find("#tbl_ktg_pdam_notasi").text();

        //SET VALUE
        document.getElementById("ktg_pdam_kode").value = ktg_pdam_kode;
        document.getElementById("ktg_pdam_nama").value = ktg_pdam_nama;
        document.getElementById("ktg_pdam_urutan").value = ktg_pdam_urutan;
        document.getElementById("ktg_pdam_keterangan").value = ktg_pdam_keterangan;
        document.getElementById("ktg_pdam_notasi").value = ktg_pdam_notasi;

        //DISABLE VALUE
        document.getElementById("ktg_pdam_kode").readOnly = true;

        //SET STATUS
        submit_status = 'Edit';

        //SET TITLE
        $("#modal_title").html('EDIT PDAM');

        //SHOW MODAL
        $('#modal_pdam').modal({
            backdrop: 'static',
            keyboard: false
        }, 'show');
    }

    function aksi_tambah_modal() {
        document.getElementById("modal_loading").style.visibility = "hidden";

        //SET TITLE
        $("#modal_title").html('TAMBAH PDAM');

        //ENABLE VALUE
        document.getElementById("ktg_pdam_kode").readOnly = false;

        //SET VALUE
        document.getElementById("ktg_pdam_kode").value = '';
        document.getElementById("ktg_pdam_nama").value = '';
        document.getElementById("ktg_pdam_icon").value = '';
        document.getElementById("ktg_pdam_urutan").value = '';
        document.getElementById("ktg_pdam_keterangan").value = '';
        document.getElementById("ktg_pdam_notasi").value = '';

        //SET STATUS
        submit_status = 'Tambah';

        //SHOW MODAL
        $('#modal_pdam').modal({
            backdrop: 'static',
            keyboard: false
        }, 'show');
    }

    function aksi_tidak_aktif(ktg_pdam_kode) {
        $.ajax({
            url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_tidak_aktif/' + ktg_pdam_kode,
            type: "POST",
            success: function() {
                toastr.success('Berhasil mengubah status kategori menjadi tidak aktif');
                aksi_data();
            },
            error: function(xhr, ajaxOptions, thrownError) {
                toastr.error('Gagal mengubah status kategori menjadi tidak aktif');
            }
        });
    }

    function aksi_aktif(ktg_pdam_kode) {
        $.ajax({
            url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_aktif/' + ktg_pdam_kode,
            type: "POST",
            success: function() {
                toastr.success('Berhasil mengubah status kategori menjadi aktif');
                aksi_data();
            },
            error: function(xhr, ajaxOptions, thrownError) {
                toastr.error('Gagal mengubah status kategori menjadi tidak aktif');
            }
        });
    }

    function aksi_hapus(ktg_pdam_kode) {
        Swal.fire({
            title: 'Yakin ingin menghapus kategori?',
            text: "Setelah menghapus, data tidak bisa dikembalikan lagi!",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Hapus!'
        }).then((result) => {
            if (result.isConfirmed) {
                $.ajax({
                    url: '<?php echo base_url() ?>kategori/kategori_pdam/aksi_hapus/' + ktg_pdam_kode,
                    type: "POST",
                    success: function() {
                        toastr.success('Berhasil menghapus kategori');
                        aksi_data();
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        toastr.error('Gagal menghapus kategori');
                    }
                });
            }
        })


    }

    function aksi_tutup_modal() {
        aksi_data();
        document.getElementById("modal_loading").style.visibility = "hidden";
        $("#modal_pdam").modal('hide');
    }
</script>