$(document).ready(function(){
  load_tabel();
})  

$('.cari_data').keyup(function(){ 
  load_tabel();
});

function load_tabel(url_page)
{ 
  if(url_page == null) var url_page = controller_url+'/tabel';

  var data = {
    range_tgl: $('input[name=range_tgl]').val(),
    keyword: $('input[name=keyword]').val()
  };

  $.ajax({
    url: url_page,
    type: 'POST',
    data: data,
    success: function(response){    
      $('#content-tabel-1').html(response);
    },
    error: function (xhr, ajaxOptions, thrownError) { 
     console.log(xhr.responseText);
   }
 });
}

function kirim(no_rawat)
{
  proses_loading();
  var data = {no_rawat: no_rawat};

  $.ajax({
    url: controller_url+'/kirim',
    type: 'POST',
    data: data,
    dataType:'json',
    success: function(response)
    {    
      notif(response.eror,response.pesan);      
      load_tabel(page_curr);

      if(response.eror == 'warning') console.log(response);
    },
    error: function (xhr, ajaxOptions, thrownError) { 
     console.log(xhr.responseText);
   }
 });
}

function kirim_semua(no_rawat)
{  
  proses_loading();
  $('#modal-view-xl').html('');

  var data = {
    range_tgl: $('input[name=range_tgl]').val(),
    keyword: $('input[name=keyword]').val()
  };

  $.ajax({
    url: controller_url+'/kirim_semua',
    type: 'POST',
    data: data,
    success: function(response)
    {      
      notif('success', 'Kirim data selesai');  
      load_tabel(page_curr);
      $('#modal-view-xl').html(response);
      $('#modal-title').html("<i class='fas fa-upload'></i> | Kirim Data SATU SEHAT");

      if(response.eror == 'warning') console.log(response);
    },
    error: function (xhr, ajaxOptions, thrownError) { 
     console.log(xhr.responseText);
   }
 });
}

function hapus_data(id, value_1, value_2)
{
  var capcha = Math.floor((Math.random() * 100));

  Swal.fire({
    title: 'Apakah data ini akan dihapus?',
    html: '<strong>'+value_1+'</strong><br>'+value_2+'<br><br><table width="100%"><tr><td width="150px">Ketik angka <br><b>'+capcha+
    '</b></b></td><td width="20px">:</td><td align="left"><input type="text" id="kode" class="swal2-input" placeholder="Kode konfirmasi"></td></tr></table>',
    icon: 'warning',
    showCancelButton: true,
    confirmButtonText: 'Hapus',
    cancelButtonText: 'Batal',
    preConfirm: () => {
      const kode = Swal.getPopup().querySelector('#kode').value
      if (kode != capcha || !kode) {
        Swal.showValidationMessage('Kode konfirmasi salah')
      }
      return { capcha: kode }
    }
  }).then((result) => {
    if(result.value.capcha){
      delete_data(id);
    } 
  })
}

function delete_data(id)
{  
  var data = {id: id};

  $.ajax({
    url: controller_url+'/delete',
    type: 'POST',
    data: data,
    dataType:'json',
    success: function(response)
    {
      notif(response.eror, response.pesan);

      if(response.eror == 'success')
      {
        load_tabel();
      } 
      else console.log(response);

      $('.add_data').attr('disabled',false);
    },
    error: function (xhr, ajaxOptions, thrownError) { 
      console.log(xhr.responseText);
    }
  });
}