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

    <div class="pagetitle">
        <h1>Pengaturan User</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>
                        <!-- Table with stripped rows -->
                        <div class="datatable-wrapper datatable-loading no-footer sortable searchable fixed-columns">
                            <div class="row">
                                <div class="col-lg-9">
                                    <input type="text" id="cari" name="cari" class="form-control" placeholder="ID or User name">
                                </div>
                                <div class="col-lg-3">
                                    <div class="btn-group" role="group" aria-label="Basic outlined example">
                                        <button onclick="tampildata()" type="button" class="btn btn-outline-primary"><i class="bi bi-search"></i>&nbsp;Cari</button>
                                        <button type="button" class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#f_add_emp"><i class="bi bi-plus-lg"></i>&nbsp;Tambah</button>
                                    </div>
                                </div>
                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tb_usr"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<div class="modal fade" id="f_akses" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Akses User</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div id="tbakses"></div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_unit" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <form id="frm_unit" name="frm_unit" action="" method="">
                <div class="modal-header">
                    <h5 class="modal-title">Unit Kerja</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_emp" name="id_emp">
                    <div id="tbunit"></div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Simpan</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal fade" id="f_add_emp" name="f_add_emp" tabindex="-1" aria-hidden="true" style="display: none;">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">New Employee</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="row g-3">
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">ID karyawan</label>
                        <input type="text" class="form-control" id="id_karyawan">
                    </div>
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">Nama Karyawan</label>
                        <input type="text" class="form-control" id="nama_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Email</label>
                        <input type="email" class="form-control" id="email_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Password</label>
                        <input type="text" class="form-control" id="pass_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Divisi</label>
                        <input type="text" class="form-control" id="divisi_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Level</label>
                        <select class="form-control" id="level_emp" onchange="cariapprove(this.value)">
                            <option value="1">DIREKTUR</option>
                            <option value="2">MANAGER</option>
                            <option value="3">SPV</option>
                            <option value="4">STAFF</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Jabatan</label>
                        <select class="form-control" id="jabatan_emp">
                            <option value="SUPER ADMIN">SUPER ADMIN</option>
                            <option value="MANAGER">MANAGER</option>
                            <option value="SPV">SPV</option>
                            <option value="ADMIN">ADMIN</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Approval</label>
                        <select class="form-control" id="aproval_emp"></select>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
                <button type="button" class="btn btn-primary" onclick="simpan_emp()">Simpan</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        var cari = $("#cari").val();
        $.get("<?= base_url('Pengaturan/tb_usr?key='); ?>" + cari, function(data, status) {
            $("#tb_usr").html(data);
        });
    });

    function tampildata() {
        var cari = $("#cari").val();
        $.get("<?= base_url('Pengaturan/tb_usr?key='); ?>" + cari, function(data, status) {
            $("#tb_usr").html(data);
        });
    }

    function tbakses(id) {
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function buka_akses(id, menu) {
        $.get("<?= base_url('Pengaturan/buka_akses?id='); ?>" + id + "&menu=" + menu, function(data, status) {});
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function tutup_akses(id, menu) {
        $.get("<?= base_url('Pengaturan/tutup_akses?id='); ?>" + id + "&menu=" + menu, function(data, status) {});
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function buka_dml(id, menu) {
        $.get("<?= base_url('Pengaturan/buka_dml?id='); ?>" + id + "&menu=" + menu, function(data, status) {});
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function tutup_dml(id, menu) {
        $.get("<?= base_url('Pengaturan/tutup_dml?id='); ?>" + id + "&menu=" + menu, function(data, status) {});
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function buka_unduh(id, menu) {
        $.get("<?= base_url('Pengaturan/buka_unduh?id='); ?>" + id + "&menu=" + menu, function(data, status) {});
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function tutup_unduh(id, menu) {
        $.get("<?= base_url('Pengaturan/tutup_unduh?id='); ?>" + id + "&menu=" + menu, function(data, status) {});
        $.get("<?= base_url('Pengaturan/tb_akses?id='); ?>" + id, function(data, status) {
            $("#tbakses").html(data);
        });
    }

    function hapuslokasi(emp) {
        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('Pengaturan/hapus_unit_kerja'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        emp: emp
                    },
                    success: function() {
                        // begin
                        tampildata();
                        //end
                    }
                });
            }
        });
    }

    function tambahunit(emp) {
        $("#id_emp").val(emp);
        $.get("<?= base_url('Pengaturan/tb_unit_kerja?emp='); ?>" + emp, function(data, status) {
            $("#f_unit").modal("show");
            $("#tbunit").html(data);
        });
    }

    function cariapprove(value) {
        $.get("<?= base_url('Pengaturan/get_approvalline?lvl='); ?>" + value, function(data, status) {
            $("#aproval_emp").html(data);
        });
    }

    function simpan_emp() {
        var id_emp = $("#id_karyawan").val();
        var nama_emp = $("#nama_emp").val();
        var email_emp = $("#email_emp").val();
        var pass_emp = $("#pass_emp").val();
        var divisi_emp = $("#divisi_emp").val();
        var level_emp = $("#level_emp").val();
        var jabatan_emp = $("#jabatan_emp").val();
        var aproval_emp = $("#aproval_emp").val();

        if (id_emp == "") {
            pesan("ID Karyawan harus diisi", "error");
        } else {
            if (nama_emp == "") {
                pesan("nama Karyawan harus diisi", "error");
            } else {
                if (email_emp == "") {
                    pesan("Email Karyawan harus diisi", "error");
                } else {
                    if (pass_emp == "") {
                        pesan("Password Karyawan harus diisi", "error");
                    } else {
                        $.get("<?= base_url('pengaturan/get_id_emp?id='); ?>" + id_emp, function(data, status) {
                            if (data > 0) {
                                pesan("ID Karyawan ditemukan", "error");
                            }
                            if (data == 0) {
                                $.get("<?= base_url('pengaturan/get_email_emp?email_emp='); ?>" + email_emp, function(data, status) {
                                    if (data > 0) {
                                        pesan("Email Karyawan ditemukan", "error");
                                    }
                                    if (data == 0) {
                                        $.ajax({
                                            url: "<?= base_url('Pengaturan/add_emp'); ?>",
                                            type: 'POST',
                                            cache: false,
                                            data: {
                                                id_emp: id_emp,
                                                nama_emp: nama_emp,
                                                email_emp: email_emp,
                                                pass_emp: pass_emp,
                                                divisi_emp: divisi_emp,
                                                level_emp: level_emp,
                                                jabatan_emp: jabatan_emp,
                                                aproval_emp: aproval_emp
                                            },
                                            success: function() {
                                                // begin
                                                $("#f_add_emp").modal("hide");
                                                pesan("data tersimpan", "success");
                                                tampildata();
                                                document.getElementById('id_karyawan').value = '';
                                                document.getElementById('nama_emp').value = '';
                                                document.getElementById('email_emp').value = '';
                                                document.getElementById('pass_emp').value = '';
                                                document.getElementById('divisi_emp').value = '';
                                                // Menyimpan log 
                                                var strTable = 'employee';
                                                var keterangan = 'Telah menambahkan user dengan ID ' + id_emp + ' nama ' + nama_emp;
                                                $.ajax({
                                                    url: "<?= base_url('Pengaturan/log'); ?>",
                                                    type: 'POST',
                                                    cache: false,
                                                    data: {
                                                        strTable: strTable,
                                                        keterangan: keterangan
                                                    },
                                                    success: function() {
                                                        // begin
                                                        //end
                                                    }
                                                });
                                                //end
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                }
            }
        }
    }

    function pesan(txt, icon) {
        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: icon,
            title: txt
        });
    }
</script>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        var searchInput = document.getElementById('cari');

        searchInput.addEventListener('keyup', function(event) {
            if (event.keyCode === 13) {
                tampildata();
            }
        });
    });
</script>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $('form[name="frm_unit"]').on('submit', function(event) {
            event.preventDefault();
            var id_emp = $("#id_emp").val();
            var formData = new FormData(this);
            $.ajax({
                url: '<?= base_url('Pengaturan/add_unit_kerja'); ?>',
                type: 'POST',
                data: formData,
                processData: false,
                contentType: false,
                success: function(response) {
                    $("#f_unit").modal("hide");
                    tampildata();
                    // Menyimpan log 
                    var strTable = 'tb_brg_unit';
                    var keterangan = 'Telah menambahkan unit pada kode barang ' + kd;
                    $.ajax({
                        url: "<?= base_url('Pengaturan/log'); ?>",
                        type: 'POST',
                        cache: false,
                        data: {
                            strTable: strTable,
                            keterangan: keterangan
                        },
                        success: function() {
                            // begin
                            //end
                        }
                    });
                },
                error: function(xhr, status, error) {
                    Swal.fire({
                        title: 'Terjadi kesalahan',
                        text: error,
                        icon: 'error'
                    });
                }
            });
        });
    });
</script>