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

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

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

            $('.btn_delete_pekerjaan').click(function() {
                $('#tampil_form').load("<?php echo site_url('master/karyawan/karir-pekerjaan/form/delete/' . $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);
                    $('#jabatan, #cabang, #departemen, #status').prop("disabled", true);
                    $(".btn_save_pekerjaan").append('<i class="fa fa-trash"></i> ');
                    $(".btn_save_pekerjaan").append('<?= $this->lang->line('button_form_hapus') ?>');
                    $(".btn_save_pekerjaan").removeClass("btn_save_pekerjaan").addClass("btn_hapus_pekerjaan btn btn-danger");
                    $("#btn_new_input").hide();
                    hapus_data();
                });
            });

            $('#btn_new_input').click(function() {
                $('.btn_save_pekerjaan').show();
                $('#btn_new_input').hide();
                $('#message').hide();

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

                $('#form_input #div_departemen').show();

                $('#form_input #jabatan, #cabang, #departemen, #status').prop("disabled", false);
                $('#form_input #status').val("0");

                $('#form_input select').select2("val", "0");


            });
        });
    }

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

            if ($("#nomor_surat_keputusan").val() == "") {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_nomor_surat_keputusan') ?></font>");
                $("#nomor_surat_keputusan").focus();
                return false;
            } else if ($("#jabatan").val() == null) {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_pilih_jabatan') ?></font>");
                $("#jabatan").focus();
                return false;
            } else if ($("#cabang").val() == null) {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_pilih_cabang') ?></font>");
                $("#cabang").focus();
                return false;
            } else if ($("#jabatan").select2('data')[0].leveluser == "3" || $("#jabatan").select2('data')[0].leveluser == "7") {
                if ($("#departemen").val() == null) {
                    $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_pilih_departemen') ?></font>");
                    $("#departemen").focus();
                    return false;
                }
            }

            if ($("#tgl_mulai").val() == "") {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_tgl_mulai') ?></font>");
                $("#tgl_mulai").focus();
                return false;
            } else if ($("#status").val() == "0") {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_status') ?></font>");
                $("#status").focus();
                return false;
            } else if ($("#keterangan_status").val() == "") {
                $('#message').html("<font style='color:Crimson'><i class='fa fa-exclamation-triangle'></i> <?= $this->lang->line('error_keterangan_status') ?></font>");
                $("#keterangan_status").focus();
                return false;
            }

            loading.start();
            if (typeof $('#jabatan option:selected').attr('namajabatan') === "undefined") {
                if ($("#jabatan").select2('data')[0].leveluser == "3" || $("#jabatan").select2('data')[0].leveluser == "7") {
                    var kode_departemen = $("#departemen").select2('data')[0].kodedepartemen;
                    var nama_departemen = $("#departemen").select2('data')[0].namadepartemen;
                } else {
                    var kode_departemen = "";
                    var nama_departemen = "";
                }
            } else {
                if (typeof $('#departemen option:selected').attr('namadepartemen') === "undefined") {
                    var kode_departemen = "";
                    var nama_departemen = "";
                } else {
                    var kode_departemen = $('#departemen option:selected').attr('kodedepartemen');
                    var nama_departemen = $('#departemen option:selected').attr('namadepartemen');
                }

            }

            if (typeof $('#jabatan option:selected').attr('namajabatan') === "undefined") {
                var kode_jabatan = $("#jabatan").select2('data')[0].kodejabatan;
                var nama_jabatan = $("#jabatan").select2('data')[0].namajabatan;
            } else {
                var kode_jabatan = $('#jabatan option:selected').attr('kodejabatan');
                var nama_jabatan = $('#jabatan option:selected').attr('namajabatan');
            }

            if (typeof $('#cabang option:selected').attr('namacabang') === "undefined") {
                var kode_cabang = $("#cabang").select2('data')[0].kodecabang;
                var nama_cabang = $("#cabang").select2('data')[0].namacabang;
            } else {
                var kode_cabang = $('#cabang option:selected').attr('kodecabang');
                var nama_cabang = $('#cabang option:selected').attr('namacabang');
            }

            var param = 'nomor_surat_keputusan=' + $("#nomor_surat_keputusan").val() +
                '&kode_jabatan=' + kode_jabatan +
                '&nama_jabatan=' + nama_jabatan +
                '&kode_cabang=' + kode_cabang +
                '&nama_cabang=' + nama_cabang +
                '&kode_departemen=' + kode_departemen +
                '&nama_departemen=' + nama_departemen +
                '&tgl_mulai=' + $("#tgl_mulai").val() +
                '&tgl_akhir=' + $("#tgl_akhir").val() +
                '&status=' + $("#status").val() +
                '&keterangan_status=' + $("#keterangan_status").val() +
                '&id_karyawan=' + $("#id_karyawan").val() +
                '&id_karir_pekerjaan=' + $("#id_karir_pekerjaan").val();

            $.ajax({
                type: 'POST',
                url: "<?= site_url('master/karyawan/karir-pekerjaan/save') ?>",
                data: param,
                dataType: 'JSON',
                success: function(result) {
                    if (result.success == true) {
                        if (result.type == 'save') {
                            $('#form_input input[type=text]').prop("disabled", true);
                            $('#jabatan, #cabang, #departemen, #status').prop("disabled", true);
                            $('.btn_save_pekerjaan').hide();
                            $('#btn_new_input').show();
                        }

                        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_pekerjaan').click(function() {
            var loading = Ladda.create(this);
            loading.start();
            $.ajax({
                type: 'POST',
                url: "<?php echo site_url('master/karyawan/karir-pekerjaan/delete') ?>",
                data: 'id_karir_pekerjaan=' + $("#id_karir_pekerjaan").val(),
                dataType: 'JSON',
                success: function(result) {
                    if (result.success == true) {
                        list_data();
                    }
                    loading.stop();
                    $("#modal_form .card-body").hide();
                    $("#modal_form .card-footer").hide();
                    $("#modal_form .title").html(result.message.replace("009900", "FFFFFF"));
                },
                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>');
                }
            });
        });
    }
</script>
<div class="row">
    <div class="col-md-12">
        <div class="text-right">
            <button type="button" class="btn_tambah_pekerjaan btn btn-biru btn-min-width mb-1" id="tambah_pekerjaan">
                <i class="fa fa-plus"></i> <?= $this->lang->line('button_tambah_karir_pekerjaan') ?>
            </button>
        </div>
    </div>
</div>
<div class="row">
    <div id="list_data" class="col-md-12"></div>
</div>
