<script type="text/javascript">
	$(document).ready(function() {
        list_data();
	});

    function list_data(){
        $("#list_data").load("<?= route('master.karyawan.akun.data').'/'.$id_karyawan ?>", function(){
            $(".btn_edit_akun").click(function(){
                var id_karyawan = $(this).attr('id-karyawan');
                var id_user = $(this).attr('id-user');
                $('#tampil_form').load("<?= route('master.karyawan.akun.form.edit').'/' ?>"+id_karyawan+"/"+id_user,function(){
                    $('#modal_form').modal('show');
                    $("#modal_form .title").text("Form Edit Data");
                    $("#modal_form .btn_save_akun").addClass("btn_save_akun btn btn-warning");
                    $("#modal_form .btn_save_akun").append('<i class="fa fa-edit (alias)"></i> ');
                    $("#modal_form .btn_save_akun").append('PERBARUI');
                    $("#modal_form #btn_new_input").hide();
                    simpan_data()
                });
            });

            $(".btn_show_facerecognition").click(function(){
                    var image = $(this).attr("url-image");
                    $("#modal_image #image_facerecognition").attr("src",image);
                    $('#modal_image').modal('show');
            });

            $('.btn_reset_deviceid').click(function(){
                var id_user = $(this).attr("id");
                $('#modal_resetdeviceid #id_user').val(id_user);
                $('#modal_resetdeviceid').modal('show');
                reset_deviceid();
            });

            $('.btn_reset_facerecognition').click(function(){
                var id_user = $(this).attr("id");
                $('#modal_resetfacerecognition #id_user').val(id_user);
                $('#modal_resetfacerecognition').modal('show');
                reset_facerecognition();
            });
        });
    }

    function simpan_data(){
        $(".btn_save_akun").click(function(e){
            e.preventDefault();
            $('#message').html("");
            var loading = Ladda.create(this);

            if($("#username").val()==""){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Username belum diisi</font>");
                $("#username").focus();
                return false;
            }else if($("#level").val()==""){
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> Level User belum diisi</font>");
                $("#level").focus();
                return false;
            }
            loading.start();
            // var lok = JSON.stringify($("#id_lok").val());
            //console.log(lok);
                var param = new FormData();
                param.append('username',$("#username").val());
                param.append('password_baru',$("#password_baru").val());
                param.append('lock_facerecognition',$("#lock_facerecognition").val());
                param.append('lock_device',$("#lock_device").val());
                param.append('lock_lokasi',$("#lock_lokasi").val());
                param.append('id_lokasi',$("#id_lokasi").val());
                param.append('id_user',$("#id_user").val());
                param.append('id_karyawan',$("#id_karyawan").val());

            $.ajax({
                type: 'POST',
                url: "<?= route('master.karyawan.akun.save') ?>",
                data: param,
                dataType:'JSON',
                contentType: false,
                processData: false,
                cache: false,
                success: function(result) {
                    if(result.success == true){
                        list_data();
                    }
                    loading.stop();
                    $('#message').show();
                    $('#message').html(result.message);
                },
                error:function(){
                    loading.stop();
                    $('#message').show();
                    $('#message').html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong>Gagal : Terjadi Kesalahan</strong></font>');
                }
            });
        });
        //btn_save
    }

    function reset_deviceid(){
        $(".btn_proses_reset_deviceid").click(function(e){
            e.preventDefault();
            $('#modal_resetdeviceid #message').html("");
            var loading = Ladda.create(this);
            loading.start();
            var param = new FormData();
            param.append('id_user',$("#modal_resetdeviceid #id_user").val());

            $.ajax({
                type: 'POST',
                url: "<?= route('master.karyawan.akun.reset.deviceid') ?>",
                data: param,
                dataType:'JSON',
                contentType: false,
                processData: false,
                cache: false,
                success: function(result) {
                    if(result.success == true){
                        list_data();
                    }
                    loading.stop();
                    $('#modal_resetdeviceid #message').show();
                    $('#modal_resetdeviceid #message').html(result.message);
                },
                error:function(){
                    loading.stop();
                    $('#modal_resetdeviceid #message').show();
                    $('#modal_resetdeviceid #message').html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong>Gagal : Terjadi Kesalahan</strong></font>');
                }
            });
        });
        //btn_save
    }

    function reset_facerecognition(){
        $(".btn_proses_reset_facerecognition").click(function(e){
            e.preventDefault();
            $('#modal_resetfacerecognition #message').html("");
            var loading = Ladda.create(this);
            loading.start();
            var param = new FormData();
            param.append('id_user',$("#modal_resetfacerecognition #id_user").val());

            $.ajax({
                type: 'POST',
                url: "<?= route('master.karyawan.akun.reset.facerecognition') ?>",
                data: param,
                dataType:'JSON',
                contentType: false,
                processData: false,
                cache: false,
                success: function(result) {
                    if(result.success == true){
                        list_data();
                    }
                    loading.stop();
                    $('#modal_resetfacerecognition #message').show();
                    $('#modal_resetfacerecognition #message').html(result.message);
                },
                error:function(){
                    loading.stop();
                    $('#modal_resetfacerecognition #message').show();
                    $('#modal_resetfacerecognition #message').html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong>Gagal : Terjadi Kesalahan</strong></font>');
                }
            });
        });
        //btn_save
    }

    function radio_camera(item){
        $('#modal_camera #message').html("");
        var id_user = item.getAttribute("iduser");
        var value = item.value;
        var idcamera = $("#camera_default").val();
        $('#modal_camera #id_user').val(id_user);
        $('#modal_camera #camera').val(value);
        if(value == "0"){
            var title = "Jika Anda memilih kamera HP maka fitur face recognition tidak bisa digunakan, apakah Anda setuju?";
        }else{
            var title = "Apakah Anda ingin mengganti ke kamera ABSENKU?"
        }
        $('#modal_camera .title').html(title);
        $('#modal_camera #btn_close_camera').hide();
        $('#modal_camera #btn_unconfrim_camera').show();
        $('#modal_camera #btn_confrim_camera').show();
        $("#modal_camera #btn_unconfrim_camera").attr("idcamera",idcamera)
        $('#modal_camera').modal('show');
        confrim_camera();
    }

    function btn_unconfrim_camera(item){
        var idcamera = item.getAttribute("idcamera");
        if(idcamera == "1"){
            $("#c_plugin").prop("checked", true);
        }else{
            $("#c_default").prop("checked", true);
        }
        $('#modal_camera').modal('hide');
    }

    function confrim_camera(){
        $("#btn_confrim_camera").click(function(e){
            e.preventDefault();
            $('#modal_camera #message').html("");
            var loading = Ladda.create(this);
            loading.start();
            var param = new FormData();
            param.append('id_user',$("#modal_camera #id_user").val());
            param.append('id_camera',$("#modal_camera #camera").val());

            $.ajax({
                type: 'POST',
                url: "<?= route('master.karyawan.akun.kamera.save') ?>",
                data: param,
                dataType:'JSON',
                contentType: false,
                processData: false,
                cache: false,
                success: function(result) {
                    if(result.success == true){
                        $('#modal_camera #btn_close_camera').show();
                        $('#modal_camera #btn_unconfrim_camera').hide();
                        $('#modal_camera #btn_confrim_camera').hide();
                        list_data();
                    }
                    loading.stop();
                    $('#modal_camera #message').show();
                    $('#modal_camera #message').html(result.message);
                },
                error:function(){
                    loading.stop();
                    $('#modal_camera #message').show();
                    $('#modal_camera #message').html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong>Gagal : Terjadi Kesalahan</strong></font>');
                }
            });
        });
    }
</script>
<div class="row">
	<div id="list_data" class="col-md-12"></div>
</div>

<!-- START TAMPIL MODAL IMAGE-->
<div id="modal_image" class="modal fade in">
    <div class="modal-dialog modal-md text-center">
        <img class="modal-content rounded mx-auto d-block" id="image_facerecognition" style="max-height:500px; width:auto">
    </div>
</div>
<!-- END TAMPIL MODAL IMAGE-->

<!-- START TAMPIL MODAL RESET DEVICE ID-->
<div id="modal_resetdeviceid" class="modal fade in" data-keyboard="false" data-backdrop="static" aria-hidden="true">
    <div class="modal-dialog modal-danger modal-md">
        <div class="modal-content">
            <div class="card-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h3 class="title modal-title">Apakah Anda ingin mereset device id?</h3>
                <input type="hidden" id="id_user" value="">
            </div>
            <div class="card-footer">
                <div class="row">
                    <div class="col-md-7 text-left">
                        <div id="message"></div>
                    </div>
                    <div class="col-md-5 text-right">
                        <button class="btn_proses_reset_deviceid btn btn-danger ladda-button" data-style="expand-left"><i class="fa fa-refresh">&nbsp;</i> RESET</button>
                    </div>
                </div><!-- ./row -->
            </div>
        </div>
    </div>
</div>
<!-- END TAMPIL MODAL RESET DEVICE ID-->


<!-- START TAMPIL MODAL RESET FACE RECOGNITION-->
<div id="modal_resetfacerecognition" class="modal fade in" data-keyboard="false" data-backdrop="static" aria-hidden="true">
    <div class="modal-dialog modal-danger modal-md">
        <div class="modal-content">
            <div class="card-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h3 class="title modal-title">Apakah Anda ingin mereset data face recognition?</h3>
                <input type="hidden" id="id_user" value="">
            </div>
            <div class="card-footer">
                <div class="row">
                    <div class="col-md-7 text-left">
                        <div id="message"></div>
                    </div>
                    <div class="col-md-5 text-right">
                        <button class="btn_proses_reset_facerecognition btn btn-danger ladda-button" data-style="expand-left"><i class="fa fa-refresh">&nbsp;</i> RESET</button>
                    </div>
                </div><!-- ./row -->
            </div>
        </div>
    </div>
</div>
<!-- END TAMPIL MODAL RESET FACE RECOGNITION -->

<!-- START TAMPIL MODAL RESET FACE RECOGNITION-->
<div id="modal_camera" class="modal fade in" data-keyboard="false" data-backdrop="static" aria-hidden="true">
    <div class="modal-dialog modal-danger modal-md">
        <div class="modal-content">
            <div class="card-header">
                <!-- <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> -->
                <h3 class="title modal-title"></h3>
                <input type="hidden" id="id_user">
                <input type="hidden" id="camera">
            </div>
            <div class="card-footer">
                <div class="row">
                    <div class="col-md-4 text-left">
                        <div id="message"></div>
                    </div>
                    <div class="col-md-8 text-right">
                        <button id="btn_close_camera" onclick="btn_close_camera()" class="btn btn-secondary" data-dismiss="modal" aria-label="Close" style="display:none"><i class="fa fa-close">&nbsp;</i> TUTUP</button>
                        <button id="btn_unconfrim_camera" idcamera="" onclick="btn_unconfrim_camera(this)" class="btn btn-danger"><i class="fa fa-close">&nbsp;</i> BATAL</button>
                        <button id="btn_confrim_camera" class="btn btn-primary ladda-button" data-style="expand-left"><i class="fa fa-check">&nbsp;</i> SETUJU</button>
                    </div>
                </div><!-- ./row -->
            </div>
        </div>
    </div>
</div>
<!-- END TAMPIL MODAL RESET FACE RECOGNITION -->


