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

        //btn_tambah
        $('.btn_tambah_file').click(function() {
            $('#tampil_form').load("<?= route('master.karyawan.kelengkapanfile.form.tambah') . '/' . $id_karyawan_md5 ?>", function() {
                $('#modal_form').modal('show');
                $(".title").text("<?= $this->lang->line('title_form_tambah_data') ?>");
                $(".btn_save_file").addClass("btn_save_file btn btn-primary");
                $(".btn_save_file").append('<i class="fa fa-save"></i> ');
                $(".btn_save_file").append('<?= $this->lang->line('button_form_simpan') ?>');
                $("#btn_new_input").hide();
                simpan_data();
            });
        });
        //btn_tambah
    });

    function list_data() {
        $("#list_data").load("<?= route('master.karyawan.kelengkapanfile.data') . '/' . $id_karyawan_md5 ?>", function() {
            //btn_edit
            $('.btn_edit_file').click(function() {
                $('#tampil_form').load("<?= route('master.karyawan.kelengkapanfile.form.edit') . '/' . $id_karyawan_md5 . '/' ?>" + $(this).attr('id'), function() {
                    $('#modal_form').modal('show');
                    $(".title").text("<?= $this->lang->line('title_form_edit_data') ?>");
                    $(".btn_save_file").addClass("btn_save_file btn btn-warning");
                    $(".btn_save_file").append('<i class="fa fa-edit (alias)"></i> ');
                    $(".btn_save_file").append('<?= $this->lang->line('button_form_perbarui') ?>');
                    $("#btn_new_input").hide();
                    simpan_data();
                });
            });
            //btn_edit

            //btn_hapus
            $('.btn_delete_file').click(function() {
                $('#tampil_form').load("<?= route('master.karyawan.kelengkapanfile.form.edit') . '/' . $id_karyawan_md5 . '/' ?>" + $(this).attr('id'), function() {
                    $('#modal_form').modal('show');
                    $(".title").text("<?= $this->lang->line('title_form_hapus_data') ?>");
                    $('#form_input input[type=text]').prop("disabled", true);
                    $('#form_input input[type=file]').prop("disabled", true);
                    $(".btn_save_file").append('<i class="fa fa-trash"></i> ');
                    $(".btn_save_file").append('<?= $this->lang->line('button_form_hapus') ?>');
                    $(".btn_save_file").removeClass("btn_save_file").addClass("btn_hapus_file btn btn-danger");
                    hapus_data();
                });
            });
            //btn_hapus
        });
    }

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

            if ($("#nama_dokumen").val() == "") {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_nama_dokumen') ?></font>");
                $("#nama_sertifikat").focus();
                return false;
            } else if (document.getElementById("file").files.length == 0 && $("#file_lama").val() == "") {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_file_upload') ?></font>");
                $('#file').focus();
                return false;
            }

            loading.start();

            var param = new FormData();
            param.append('nama_dokumen', $("#nama_dokumen").val());
            param.append('file', $('#file').prop('files')[0]);
            param.append('file_lama', $("#file_lama").val());
            param.append('id_kelengkapan_file', $("#id_kelengkapan_file").val());
            param.append('id_karyawan', $("#id_karyawan").val());

            $.ajax({
                type: 'POST',
                url: "<?= route('master.karyawan.kelengkapanfile.save') ?>",
                data: param,
                dataType: 'JSON',
                contentType: false,
                processData: false,
                cache: false,
                success: function(result) {
                    if (result.success == true) {
                        if (result.type == 'save') {
                            btn_new_input();
                            $("#form_input :input").prop("disabled", true);
                            $('#form_input input[type=file]').prop("disabled", true);
                            $('.btn_save_file').hide();
                            $('#btn_new_input').show();
                        } else {
                            $("#form_input #file_lama").val(result.file);
                        }
                        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><?= $this->lang->line('error') ?></strong></font>');
                }
            });
        });
    }

    function hapus_data() {
        $('.btn_hapus_file').click(function(e) {
            var loading = Ladda.create(this);
            loading.start();
            $.ajax({
                type: 'POST',
                url: "<?= route('master.karyawan.kelengkapanfile.delete') ?>",
                data: 'id_kelengkapan_file=' + $("#id_kelengkapan_file").val(),
                dataType: 'JSON',
                success: function(result) {
                    $("#modal_form .card-body").hide();
                    $("#modal_form .card-footer").hide();
                    $("#modal_form .title").html(result.message);
                    if (result.success == true) {
                        list_data();
                    }
                },
                error: function() {
                    loading.stop();
                    $("#modal_form .card-body").hide();
                    $("#modal_form .card-footer").hide();
                    $("#modal_form .title").html('<font color="#eb3a28"><i class="fa fa-close(alias)">&nbsp;</i><strong><?= $this->lang->line('error') ?></strong></font>');
                }
            });
        });
    }

    function btn_new_input() {
        $('#btn_new_input').click(function() {
            var file = $('#form_input #file').dropify();
            file = file.data('dropify');
            file.resetPreview();
            file.clearElement();

            $('#form_input input[type=file]').prop("disabled", false);
            $("#form_input input[type=file]").val('');

            $('#form_input input[type=text]').prop("disabled", false);
            $("#form_input input[type=text]").val('');

            $('.btn_save_file').show();
            $('#btn_new_input').hide();
            $('#message').hide();
        });
    }
</script>
<?php if ($this->session->userdata('level_user') == 1) {
    echo $tab . "<br>";
} ?>

<div class="row">
    <div class="col-md-12">
        <div class="card-header">
            <h4 class="card-title" id="heading-labels">
                <?= strtoupper($this->lang->line('kelengkapan_file')) ?>
                <button type="button" class="btn btn_tambah_file btn-primary pull-right">
                    <i class="fa fa-plus"></i> <?= $this->lang->line('button_tambah_file') ?>
                </button>
            </h4>

        </div>
    </div>
</div>

<div id="list_data" class="row mt-3"></div>
