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

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

  var service_name = $('input[name=service_name]').val();

  if(service_name == 'encounter') $('#content-tabel-1').html('');

  tabel_eror(service_name);

  var data = {
    service_name: service_name
  };

  $.ajax({
    url: url_page,
    data: data,
    type: 'POST',
    dataType:'json',
    success: function(response)
    {    
      var length = response.length;

      if(data.service_name == 'observation'){ waktu = 3000; } else waktu = 1000;

      setTimeout(function()
      {       
        $('#judul_'+service_name).append(' (Total: '+length+')');
      }, waktu/10);

      if(length > 0)
      {
        for (let j = 0; j < length; j++)
        {          
          setTimeout(function()
          {
            kirim(j, response[j], service_name); 

            if((j+1) == length)
            {
              setTimeout(function()
              {       
                ganti_service_name(service_name);
              }, waktu);
            } 

          }, waktu*(j+1));
        }
      }
      else  {
        setTimeout(function()
        {       
          ganti_service_name(service_name);
        }, 10000);
      }
    },
    error: function (xhr, ajaxOptions, thrownError) {
      console.log(xhr.responseText);
    }
  });
}

function ganti_service_name(service_name)
{ 
  switch (service_name)
  {
    case 'encounter': 
    $('input[name=service_name]').val('condition');
    break;

    case 'condition': 
    $('input[name=service_name]').val('observation');
    break;

    case 'observation': 
    $('input[name=service_name]').val('procedure');
    break;

    case 'procedure': 
    $('input[name=service_name]').val('composition');
    break;

    case 'composition': 
    $('input[name=service_name]').val('encounter');
    break;
  }

  load_tabel();
}

function tabel_eror(service_name)
{
  var data = {
    service_name: service_name
  };

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

function kirim(urut, data_satusehat, service_name)
{  
  var data = {
    no_rawat: data_satusehat.no_rawat
  };

  $.ajax({
    url: base_url+'satusehat_data_'+service_name+'/kirim',
    type: 'POST',
    data: data,
    dataType:'json',
    success: function(response)
    {   
      if(service_name == 'observation')
      {          
        var html = '<tr id="'+data_satusehat.no_rawat+'"><td style="text-align: center;" rowspan="2">'+(urut+1)+'</td><td rowspan="2">'+data_satusehat.nama+'</td><td style="text-align: center;" rowspan="2">'+data_satusehat.tgl_daftar+'</td><td>'+data_satusehat.sistole+'</td><td>'+data_satusehat.diastole+'</td><td>'+data_satusehat.heart_rate+'</td><td>'+data_satusehat.lingkar_perut+'</td><td>'+data_satusehat.berat_badan+'</td><td>'+data_satusehat.tinggi_badan+'</td><td>'+data_satusehat.respirasi_rate+'</td><td>'+data_satusehat.kesadaran+'</td><td>'+data_satusehat.suhu+'</td></tr><tr>'+response.pesan+'</tr>';
      }
      else var html = '<tr><td style="text-align: center;">'+(urut+1)+'</td><td>'+data_satusehat.nama+'</td><td style="text-align: center;">'+data_satusehat.no_rawat+'</td><td style="text-align: center;">'+data_satusehat.tgl_daftar+'</td><td style="text-align: center;">'+data_satusehat.jam_daftar+'</td><td style="text-align: center;">'+data_satusehat.pasien_satusehat_id+'</td><td style="text-align: center;">'+data_satusehat.debitur_value+'</td><td style="text-align: center;">'+response.pesan+'</td></tr>';

      $('#data_eror_'+service_name).append(html); 
    },
    error: function (xhr, ajaxOptions, thrownError) { 
      console.log(xhr.responseText);
    }
  });

  var card = document.querySelector('.card');
  card.scrollIntoView(false);      
}