text: var save_method;
  var table;
  
  $(document).ready(function() {
  
      //force integer input in textfield
      $('input.numberinput').bind('keypress', function (e) {
          return (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) ? false : true;
      });
  
  	//datatables
  	table = $('#tabel_tindakan').DataTable({
  		responsive: true,
          searchDelay: 500,
          processing: true,
          serverSide: true,
  		ajax: {
  			url  : base_url + "master_tindakan/list_tindakan",
  			type : "POST" 
  		},
  
  		//set column definition initialisation properties
  		columnDefs: [
  			{
  				targets: [-1], //last column
  				orderable: false, //set not orderable
  			},
  		],
      });
      
  
      //change menu status
      $(document).on('click', '.btn_edit_status', function(){
          var id = $(this).attr('id');
          var status = $(this).val();
          swalConfirmDelete.fire({
              title: 'Ubah Status Data Pegawai ?',
              text: "Apakah Anda Yakin ?",
              type: 'warning',
              showCancelButton: true,
              confirmButtonText: 'Ya, Ubah Status!',
              cancelButtonText: 'Tidak, Batalkan!',
              reverseButtons: true
            }).then((result) => {
              if (result.value) {
                  $.ajax({
                      url : base_url + 'master_pegawai/edit_status_pegawai',
                      type: "POST",
                      dataType: "JSON",
                      data : {status : status, id : id},
                      success: function(data)
                      {
                          swalConfirm.fire('Berhasil Ubah Status Pegawai!', data.pesan, 'success');
                          table.ajax.reload();
                      },
                      error: function (jqXHR, textStatus, errorThrown)
                      {
                          Swal.fire('Terjadi Kesalahan');
                      }
                  });
              } else if (
                /* Read more about handling dismissals below */
                result.dismiss === Swal.DismissReason.cancel
              ) {
                swalConfirm.fire(
                  'Dibatalkan',
                  'Aksi Dibatalakan',
                  'error'
                )
              }
          });
      });
  
      $(".modal").on("hidden.bs.modal", function(){
          reset_modal_form();
          reset_modal_form_import();
      });
  });	
  
  function add_tindakan()
  {
      reset_modal_form();
      save_method = 'add';
  	$('#modal_pegawai_form').modal('show');
  	$('#modal_title').text('Entry data tindakan'); 
  }
  
  function edit_tindakan(id)
  {
      // alert('tes'); exit;
      reset_modal_form();
      save_method = 'update';
      //Ajax Load data from ajax
      $.ajax({
          url : base_url + 'master_tindakan/edit_tindakan',
          type: "POST",
          dataType: "JSON",
          data : {id:id},
          success: function(data)
          {
              $('[name="id_tindakan"]').val(data.old_data.id_tindakan);
              $('[name="kode"]').val(data.old_data.kode_tindakan);
              $('[name="nama"]').val(data.old_data.nama_tindakan);
              $('[name="harga"]').val(data.old_data.harga);
              $('#modal_pegawai_form').modal('show');
  	        $('#modal_title').text('Edit Data Tindakan'); 
  
          },
          error: function (jqXHR, textStatus, errorThrown)
          {
              alert('Error get data from ajax');
          }
      });
  }
  
  function reload_table()
  {
      table.ajax.reload(null,false); //reload datatable ajax 
  }
  
  function save()
  {
      var url;
      var txtAksi;
  
      if(save_method == 'add') {
          url = base_url + 'master_tindakan/add_data_tindakan';
          txtAksi = 'Tambah Diagnosa';
      }else{
          url = base_url + 'master_tindakan/update_data_tindakan';
          txtAksi = 'Edit Diagnosa';
      }
      
      var form = $('#form-pegawai')[0];
      var data = new FormData(form);
      
      $("#btnSave").prop("disabled", true);
      $('#btnSave').text('Menyimpan Data'); //change button text
      swalConfirmDelete.fire({
          title: 'Perhatian !!',
          text: "Apakah anda yakin menambah data ini ?",
          type: 'warning',
          showCancelButton: true,
          confirmButtonText: 'Ya',
          cancelButtonText: 'Tidak',
          reverseButtons: true
        }).then((result) => {
          if (result.value) {
              $.ajax({
                  type: "POST",
                  enctype: 'multipart/form-data',
                  url: url,
                  data: data,
                  dataType: "JSON",
                  processData: false, // false, it prevent jQuery form transforming the data into a query string
                  contentType: false, 
                  cache: false,
                  timeout: 600000,
                  success: function (data) {
                      if(data.status) {
                          swal.fire("Sukses!!", "Aksi "+txtAksi+" Berhasil", "success");
                          $("#btnSave").prop("disabled", false);
                          $('#btnSave').text('Simpan');
                          
                          reset_modal_form();
                          $(".modal").modal('hide');
                          
                          reload_table();
                      }else {
                          for (var i = 0; i < data.inputerror.length; i++) 
                          {
                              if (data.inputerror[i] != 'jabatans') {
                                  $('[name="'+data.inputerror[i]+'"]').addClass('is-invalid');
                                  $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]).addClass('invalid-feedback'); //select span help-block class set text error string
                              }else{
                                  $($('#jabatans').data('select2').$container).addClass('has-error');
                              }
                          }
          
                          $("#btnSave").prop("disabled", false);
                          $('#btnSave').text('Simpan');
                      }
                  },
                  error: function (e) {
                      console.log("ERROR : ", e);
                      $("#btnSave").prop("disabled", false);
                      $('#btnSave').text('Simpan');
          
                      reset_modal_form();
                      $(".modal").modal('hide');
                  }
              });
          } else if (
            /* Read more about handling dismissals below */
            result.dismiss === Swal.DismissReason.cancel
          ) {
            swalConfirm.fire(
              'Dibatalkan',
              'Aksi Dibatalakan',
              'error'
            )
          }
        })
      
  }
  
  function delete_tindakan(id){
      swalConfirmDelete.fire({
          title: 'Hapus Data Tindakan ?',
          text: "Data Akan dihapus permanen ?",
          type: 'warning',
          showCancelButton: true,
          confirmButtonText: 'Ya, Hapus Data !',
          cancelButtonText: 'Tidak, Batalkan!',
          reverseButtons: true
        }).then((result) => {
          if (result.value) {
              $.ajax({
                  url : base_url + 'master_tindakan/delete_tindakan',
                  type: "POST",
                  dataType: "JSON",
                  data : {id:id},
                  success: function(data)
                  {
                      swalConfirm.fire('Berhasil Hapus data tindakan!', data.pesan, 'success');
                      table.ajax.reload();
                  },
                  error: function (jqXHR, textStatus, errorThrown)
                  {
                      Swal.fire('Terjadi Kesalahan');
                  }
              });
          } else if (
            /* Read more about handling dismissals below */
            result.dismiss === Swal.DismissReason.cancel
          ) {
            swalConfirm.fire(
              'Dibatalkan',
              'Aksi Dibatalakan',
              'error'
            )
          }
      });
  }
  
  function reset_modal_form()
  {
      $('#form-pegawai')[0].reset();
      $('.append-opt').remove(); 
      $('div.form-group').children().removeClass("is-invalid invalid-feedback");
      $('span.help-block').text('');
      $('#div_pass_lama').css("display","none");
  }
  
  function reset_modal_form_import()
  {
      $('#form_import_excel')[0].reset();
      $('#label_file_excel').text('Pilih file excel yang akan diupload');
  }
  
  function import_excel(){
      $('#modal_import_excel').modal('show');
  	$('#modal_import_title').text('Import data pegawai'); 
  }
  
  function import_data_excel(){
      var form = $('#form_import_excel')[0];
      var data = new FormData(form);
      
      $("#btnSaveImport").prop("disabled", true);
      $('#btnSaveImport').text('Import Data');
      $.ajax({
          type: "POST",
          enctype: 'multipart/form-data',
          url: base_url + 'master_pegawai/import_data_master',
          data: data,
          dataType: "JSON",
          processData: false, // false, it prevent jQuery form transforming the data into a query string
          contentType: false, 
          success: function (data) {
              if(data.status) {
                  swal.fire("Sukses!!", data.pesan, "success");
                  $("#btnSaveImport").prop("disabled", false);
                  $('#btnSaveImport').text('Simpan');
              }else {
                  swal.fire("Gagal!!", data.pesan, "error");
                  $("#btnSaveImport").prop("disabled", false);
                  $('#btnSaveImport').text('Simpan');
              }
  
              reset_modal_form_import();
              $(".modal").modal('hide');
              table.ajax.reload();
          },
          error: function (e) {
              console.log("ERROR : ", e);
              $("#btnSaveImport").prop("disabled", false);
              $('#btnSaveImport').text('Simpan');
  
              reset_modal_form_import();
              $(".modal").modal('hide');
              table.ajax.reload();
          }
      });
  }