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

    <div class="pagetitle">
        <h1>Employee</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 g-3">

                            </div>
                            <br>
                            <div class="datatable-container">
                                <div id="tb_emp"></div>
                            </div>
                        </div>
                        <!-- End Table with stripped rows -->
                    </div>
                </div>
            </div>
        </div>
    </section>
</main>

<div class="modal fade" id="f_add_emp" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Add Employee</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" class="form-control form-control-sm" id="id_atasan">
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">ID</label>
                        <input type="text" class="form-control form-control-sm" id="id_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">Nama</label>
                        <input type="text" class="form-control form-control-sm" id="nama_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Email</label>
                        <input type="email" class="form-control form-control-sm" id="email_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Password</label>
                        <input type="text" class="form-control form-control-sm" id="pass_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Divisi</label>
                        <input type="text" class="form-control form-control-sm" id="divisi_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputAddress" class="form-label">Level</label>
                        <select class="form-control form-control-sm" id="level_emp" name="level_emp">
                            <!--
                            <option value="2">MANAGER</option>
                            <option value="3">SPV</option>
                            <option value="4">ADMIN</option>
-->
                        </select>
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Jabatan</label>
                        <input type="text" class="form-control form-control-sm" id="jabatan_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">Approval Line</label>
                        <input type="text" class="form-control form-control-sm" id="atasan" readonly>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" onclick="simpan_emp()" class="btn btn-primary">Save</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="f_edit_emp" tabindex="-1" style="display: none;" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Update Employee</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">
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">ID</label>
                        <input type="text" class="form-control form-control-sm" id="ed_id_emp" readonly>
                    </div>
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">Nama</label>
                        <input type="text" class="form-control form-control-sm" id="ed_nama_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputEmail4" class="form-label">Email</label>
                        <input type="email" class="form-control form-control-sm" id="ed_email_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Password</label>
                        <input type="text" class="form-control form-control-sm" id="ed_pass_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Divisi</label>
                        <input type="text" class="form-control form-control-sm" id="ed_divisi_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputAddress" class="form-label">Level</label>
                        <select class="form-control form-control-sm" id="ed_level_emp" onchange="ubah_apprv(this.value)">
                            <option value="2">MANAGER</option>
                            <option value="3">SPV</option>
                            <option value="4">ADMIN</option>
                        </select>
                    </div>
                    <div class="col-6">
                        <label for="inputPassword4" class="form-label">Jabatan</label>
                        <input type="text" class="form-control form-control-sm" id="ed_jabatan_emp">
                    </div>
                    <div class="col-6">
                        <label for="inputNanme4" class="form-label">Approval Line</label>
                        <select class="form-control form-control-sm" id="ed_atasan">
                        </select>
                    </div>
                    <input type="hidden" class="form-control form-control-sm" id="ed_password_emp">
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                <button type="button" onclick="update_emp()" class="btn btn-success">Update</button>
            </div>
        </div>
    </div>
</div>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        $.get("<?= base_url('Masterdata/tb_emp') ?>", function(data, status) {
            $("#tb_emp").html(data);
        });
    });

    function add_emp(id, nama, lvl) {
        $("#id_atasan").val(id);
        $("#atasan").val(nama);
        if (lvl == 1) {
            $("#level_emp").html("<option value='2'>MANAGER</option><option value='3'>SPV</option><option value='4'>ADMIN</option>");
        }
        if (lvl == 2) {
            $("#level_emp").html("<option value='3'>SPV</option><option value='4'>ADMIN</option>");
        }
        if (lvl == 3) {
            $("#level_emp").html("<option value='4'>ADMIN</option>");
        }
        $("#f_add_emp").modal("show");
    }

    function simpan_emp() {
        var id_atasan = $("#id_atasan").val();
        var id_emp = $("#id_emp").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();

        if (id_emp == '') {
            pesan("ID Employee harap diisi", "error");
        } else {
            if (nama_emp == '') {
                pesan("Nama Employee harap diisi", "error");
            } else {
                if (email_emp == '') {
                    pesan("Email harap diisi", "error");
                } else {
                    if (pass_emp == '') {
                        pesan("Password harap diisi", "error");
                    } else {
                        $.ajax({
                            url: "<?= base_url('Masterdata/simpan_emp'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                id_atasan: id_atasan,
                                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
                            },
                            success: function() {
                                $("#f_add_emp").modal("hide");
                                $.get("<?= base_url('Masterdata/tb_emp') ?>", function(data, status) {
                                    $("#tb_emp").html(data);
                                });
                                clear();
                            }
                        });

                        var strTable = 'employee';
                        var keterangan = 'Telah menambahkan user dengan email ' + email_emp;
                        $.ajax({
                            url: "<?= base_url('Pengaturan/log'); ?>",
                            type: 'POST',
                            cache: false,
                            data: {
                                strTable: strTable,
                                keterangan: keterangan
                            },
                            success: function() {
                                // begin
                                //end
                            }
                        });
                    }
                }
            }
        }
    }

    function clear() {
        document.getElementById('id_emp').value = '';
        document.getElementById('nama_emp').value = '';
        document.getElementById('email_emp').value = '';
        document.getElementById('pass_emp').value = '';
        document.getElementById('divisi_emp').value = '';
        document.getElementById('level_emp').value = '';
        document.getElementById('jabatan_emp').value = '';
    }

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

    function add_unit() {
        alert("Ok");
    }

    function disable_user(id, nama) {
        Swal.fire({
            title: "Non-Aktifkan User ini ?" + id,
            text: "Nama : " + nama,
            icon: "question",
            showCancelButton: true,
            confirmButtonColor: "#3085d6",
            cancelButtonColor: "#d33",
            confirmButtonText: "Ya, Non-Aktifkan"
        }).then((result) => {
            if (result.isConfirmed) {
                //Begin
                $.ajax({
                    url: "<?= base_url('Masterdata/disable_user'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        id: id
                    },
                    success: function() {
                        // begin
                        $.get("<?= base_url('Masterdata/tb_emp') ?>", function(data, status) {
                            $("#tb_emp").html(data);
                        });
                        //end
                    }
                });

                var strTable = 'employee';
                var keterangan = 'Telah melakukan disable user dengan nama ' + nama;
                $.ajax({
                    url: "<?= base_url('Pengaturan/log'); ?>",
                    type: 'POST',
                    cache: false,
                    data: {
                        strTable: strTable,
                        keterangan: keterangan
                    },
                    success: function() {
                        // begin
                        //end
                    }
                });
                //End
            }
        });
    }

    function edit_emp(id, nama, email, pass, divisi, lvl, jabatan, bos) {

        $.get("<?= base_url('Masterdata/get_apprv?lvl=') ?>" + lvl + "&bos=" + bos, function(data, status) {
            $("#ed_atasan").html(data);
        });

        $("#ed_id_emp").val(id);
        $("#ed_nama_emp").val(nama);
        $("#ed_email_emp").val(email);
        $("#ed_password_emp").val(pass);
        $("#ed_divisi_emp").val(divisi);
        $("#ed_level_emp").val(lvl);
        $("#ed_jabatan_emp").val(jabatan);
        document.getElementById("ed_atasan").value = bos;
        $("#f_edit_emp").modal("show");
    }

    function ubah_apprv(lvl) {
        $.get("<?= base_url('Masterdata/get_apprv?lvl=') ?>" + lvl, function(data, status) {
            $("#ed_atasan").html(data);
        });
    }

    function update_emp() {
        var id = $("#ed_id_emp").val();
        var nama = $("#ed_nama_emp").val();
        var email = $("#ed_email_emp").val();
        var divisi = $("#ed_divisi_emp").val();
        var lvl = $("#ed_level_emp").val();
        var jabatan = $("#ed_jabatan_emp").val();
        var bos = $("#ed_atasan").val();
        if (document.getElementById('ed_pass_emp').value == '') {
            var sandi = $("#ed_password_emp").val();
        } else {
            var sandi = $("#ed_pass_emp").val();
        }
        $.ajax({
            url: "<?= base_url('Masterdata/update_emp'); ?>",
            type: 'POST',
            cache: false,
            data: {
                id: id,
                nama: nama,
                email: email,
                divisi: divisi,
                lvl: lvl,
                jabatan: jabatan,
                bos: bos,
                sandi: sandi
            },
            success: function() {
                // begin
                $.get("<?= base_url('Masterdata/tb_emp') ?>", function(data, status) {
                    $("#tb_emp").html(data);
                });
                $("#f_edit_emp").modal("hide");
                //end
            }
        });
    }
</script>