<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Analis extends MY_Controller {
    protected $template = "app";
    protected $module = "uji_analis";

    protected $kode_batch = "";
    protected $param_uji = "";
    protected $id_parameter_uji = "";

    protected $per_page = 20;

    public function __construct() {
      parent::__construct();
      $this->load->model('m_disposisi');
      $this->load->model('m_analis');
      $this->load->model('m_master');

      $this->kode_batch = array('AIR','ABU','PK','LK','SK','CA','P','AF','UR','ANDF','TDN','MBM', 'CA', 'PKD');
      $this->param_uji = array('air', 'abu', 'protein_kasar', 'lemak_kasar', 'serat_kasar', 'kalsium', 'fosfor', 'af', 'ur', 'andf', 'tdn', 'mbm', 'ca', 'pkd');
      $this->id_parameter_uji = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14);
    }

    public function index() {
      $this->data['halaman'] = "Disposisi";
      $this->data['sub_halaman'] = '';
      $this->data['idGroup'] = $this->idGroup;

      $total_data = $this->m_disposisi->getDataUji(true);
      $this->data['total_data'] = $total_data;

      //CONFIG PAGINATION
      $this->load->library('pagination');

      $url = site_url('sampel/pendaftaran/index');
      $per_page = $this->per_page;
      $uri_segment = 3;

      $this->data['link_paging'] = $this->paging->set_paging2($url, $total_data, $per_page,$uri_segment);
      if ($this->uri->segment($uri_segment)) {
          $this->cur_page = $page = $this->uri->segment($uri_segment);
      } else {
          $this->cur_page = $page = 0;
      }

      $this->data['per_page']     = $per_page;
      $this->data['data']         = $this->m_disposisi->getDataUji(false, $per_page, $page);

      //END CONFIG PAGINATION

      $this->render('index');
    }

    public function form_uji($jenis_uji){
      //print('<pre>'.print_r($this->input->post(), true).'</pre>'); die();
      $this->data['halaman'] = "Analis";
      $j_uji = $jenis_uji;
      if($jenis_uji == 'af'){
        $j_uji = 'Aflatoksin Total';
      }
      if($jenis_uji == 'kalsium'){
        $j_uji = 'Ca Titrimetri';
      }
      if($jenis_uji == 'ca'){
        $j_uji = 'Ca AAS';
      }
      $this->data['sub_halaman'] = 'Uji '.$j_uji;
      $this->data['role_analis'] = $this->roleAnalis;

      if(!empty($this->input->post('filter_cari'))) {
        $this->data['filter'] = $this->input->post();
      }

      $method = !empty($this->input->post('method')) ? $this->input->post('method') : null;
      $id_param_uji = 'kosong';
      for($b=0;$b<count($this->param_uji);$b++){
        if($jenis_uji == $this->param_uji[$b]){
          $id_param_uji = $this->id_parameter_uji[$b];
        }
      }

      if(isset($method)){
        $data['id_sampel'] = $this->input->post('id_sampel[]');
        $data['id_kadar'] = $this->input->post('id_kadar[]');
        $data['method'] = $this->input->post('method');

        if(in_array($method, array('batch_sampel','kontrol_sampel'))) {
          $this->data['kontrol_sampel'] = '';
          if(!empty($this->input->get('kontrol_sampel'))){
            $this->data['kontrol_sampel'] = str_replace('|-|', '+', $this->input->get('kontrol_sampel'));
          }
          $this->data['nomor_batch'] = $this->input->post('nomor_batch');
          $this->data['url_batch'] = "?method=".$data['method']."&nomor_batch=".$this->data['nomor_batch']."&kontrol_sampel=".$this->data['kontrol_sampel'];
        }

        if($data['method'] != 'edit'){
          $data['nomor_batch'] = $this->input->post('nomor_batch');
          $data['kontrol_sampel'] = $this->input->post('kontrol_sampel');
        }

        $this->data['method'] = $data['method'];
        $this->data['data'] = $this->m_analis->get_uji_analis('kadar_'.$jenis_uji, $id_param_uji, $data);
        $this->data['alat_uji'] = $this->m_master->getDataMaster('peralatan');
        
        if(in_array($jenis_uji, array('fosfor', 'ur', 'af', 'ca')) && in_array($method, array('edit','form_deret'))) {
          $this->deret_uji($data, $jenis_uji, $method);
        }else {
          $this->render('form_uji_'.$jenis_uji.'_edit');
        }
      }else{
        if($this->input->post() && empty($this->input->post('filter_cari'))){
          $data = $this->input->post();
          $this->save_data_analis($data, $jenis_uji);
        }
        
        $this->data['data'] = $this->m_analis->get_uji_analis('kadar_'.$jenis_uji, $id_param_uji);

        $this->data['golongan_sampel'] = $this->m_master->getDataMaster('gol_jenis_sampel');
        $this->data['jenis_uji'] = $jenis_uji;

        $this->render('form_uji_'.$jenis_uji);
      }
    }

    //-- METHJOD UNTUK MENYIMPAN HASIL DARI DERET STANDAR
    function deret_uji($data, $jenis_uji, $method){
      if($method == 'form_deret'){
        $j_uji = $jenis_uji;
        if($jenis_uji == 'af'){
          $j_uji = 'Aflatoksin Total';
        }
        if($jenis_uji == 'kalsium'){
          $j_uji = 'Ca Titrimetri';
        }
        
        $this->data['sub_halaman'] = 'Deret Standar '.$j_uji;
      }
      $data = $this->input->post();

      if(!empty($data['uji_'.$jenis_uji])){
        
        $status = $this->m_disposisi->update_deret($data, $jenis_uji);

        $this->data['uji_'.$jenis_uji] = $data['uji_'.$jenis_uji];
        $this->render('form_uji_'.$jenis_uji.'_edit');

      }else if(!empty($data['deret_'.$jenis_uji])){
        $this->data['simpan_deret']= 1;
        $this->render('create_deret_'.$jenis_uji);

      }else if(!empty($data['simpan_deret']) && $data['simpan_deret'] == 1){
        if($jenis_uji == 'fosfor'){
          $status = $this->m_disposisi->set_deret_fosfor($data);
          $this->data['data'] = $this->m_disposisi->get_deret_fosfor($data['nomor_batch']);
        }
        if($jenis_uji == 'ur'){
          $status = $this->m_disposisi->set_deret_ur($data);
          $this->data['data'] = $this->m_disposisi->get_deret_ur($data['nomor_batch']);
        }
        if($jenis_uji == 'af'){
          $status = $this->m_disposisi->set_deret_af($data);
          $this->data['data'] = $this->m_disposisi->get_deret_af($data['nomor_batch']);
        }
        if($jenis_uji == 'ca'){
          $status = $this->m_disposisi->set_deret_ca($data);
          $this->data['data'] = $this->m_disposisi->get_deret_ca($data['nomor_batch']);
        }

        $data['simpan_deret'] = 2;
        if($status) {
          $this->session->set_flashdata('msg', 'Data berhasil disimpan');
        } else {
          $this->session->set_flashdata('msgw', 'Data gagal disimpan');
        };

        
        $this->render('deret_'.$jenis_uji);
      }else{
        $nomor_batch2 = $this->input->post('nomor_batch2');
        if($method == 'edit'){
          $batch = null;
          $i = 0;
          foreach($this->param_uji as $r){
            if($r == $jenis_uji){
              $batch = $this->kode_batch[$i].($nomor_batch2 ? '-'.date('dmY', strtotime($nomor_batch2)) : date('-dmY'));
              $cek_batch = $this->m_analis->cek_batch('kadar_'.$jenis_uji, $batch);
              $batch .= "-".$cek_batch;
            }
            $i++;
          }

          $this->session->set_userdata("nomor_batch", $batch);

          $autonobatch = $this->session->userdata("nomor_batch");

        } else {
          $autonobatch = $this->data['nomor_batch'];
        }

        if($jenis_uji == 'fosfor'){
          $this->data['grafik'] = $this->m_disposisi->get_deret_fosfor($autonobatch);
          $this->data['data'] = $this->m_disposisi->get_deret_fosfor($autonobatch);
        }
        if($jenis_uji == 'ur'){
          $this->data['grafik'] = $this->m_disposisi->get_deret_ur($autonobatch);
          $this->data['data'] = $this->m_disposisi->get_deret_ur($autonobatch);
        }
        if($jenis_uji == 'af'){
          $this->data['grafik'] = $this->m_disposisi->get_deret_af($autonobatch);
          $this->data['data'] = $this->m_disposisi->get_deret_af($autonobatch);
        }
        if($jenis_uji == 'ca'){
          $this->data['grafik'] = $this->m_disposisi->get_deret_ca($autonobatch);
          $this->data['data'] = $this->m_disposisi->get_deret_ca($autonobatch);
        }

        //print('<pre>'.print_r($this->data['data'], true).'</pre>'); die();
        
        $this->render('deret_'.$jenis_uji);
      }
    }

    //-- METHOD UNTUK MENYIMPAN HASIL UJI ANALIS
    function save_data_analis($data, $jenis_uji){
      $batch = null;
      if(!empty($this->session->userdata('nomor_batch'))){
        $batch = $this->session->userdata('nomor_batch');
        $this->session->unset_userdata('nomor_batch');
      }else{
        $nomor_batch2 = $this->input->post('nomor_batch2');
        $i = 0;
        foreach($this->param_uji as $r){
          if($r == $jenis_uji){
            $batch = $this->kode_batch[$i].($nomor_batch2 ? '-'.date('dmY', strtotime($nomor_batch2)) : date('-dmY'));
            $cek_batch = $this->m_analis->cek_batch('kadar_'.$jenis_uji, $batch);
            $batch .= "-".$cek_batch;
          }
          $i++;
        }
      }

      $data['last_modified_user'] = array();
      $data['nama_analis'] = array();
      $data['paraf_analis'] = array();
      $data['paraf_penyelia'] = array();

      if(empty($data['method_batch'][0]) && empty($data['nomor_batch'][0])){
        $data['nomor_batch'] = array();
      }

      $x = count($data['id_sampel']);
      $status = array();
      //print('<pre>'.print_r($data, true).'</pre>'); die();

      for($i=0;$i<$x;$i++){
        //$i = 2;
        $table = 'kadar_'.$jenis_uji; // NAMA TABLE BISA CUSTOM
        $id = $data['id_sampel'][$i];
        $kontrol_sampel2 = $data['kontrol_sampel2'][$i];
        if(!is_null($id) && $id!=""){
          $field_id = 'id_sampel=?';
        }else{
          $field_id = '';
        }
        $field_table = '';
        $field_table2 = '';

        $nama_analis = $this->m_analis->get_user_analis($this->idUser);

        $data['last_modified_user'][$i] = $this->idUser;
        $data['nama_analis'][$i] = !empty($nama_analis['nama']) && $nama_analis['nama'] != '' ? $nama_analis['nama'] : $nama_analis['username'];
        $data['paraf_analis'][$i] = null;
        $data['paraf_penyelia'][$i] = null;

        // -- INPUT JAM PADA PARAMETER FOSFOR CA DAN AF
        if(in_array($jenis_uji, array('fosfor', 'ca', 'af'))){
          if(!empty($data['mulai_uji'][0])){
            $selesai_uji = $data['mulai_uji'][0];
            $data['mulai_uji'][$i] = $data['mulai_uji'][0].' '.$data['jam_mulai'][0].':'.$data['menit_mulai'][0].':00';
            $data['selesai_uji'][$i] = $selesai_uji.' '.$data['jam_selesai'][0].':'.$data['menit_selesai'][0].':00';

          }
          if(!empty($data['persetujuan'][0])){
            $data['persetujuan'][$i] = $data['persetujuan'][0].' '.$data['jam_persetujuan'][0].':'.$data['menit_persetujuan'][0].':00';
          }
        }else {
          if(!empty($data['mulai_uji'][$i])){
            $data['mulai_uji'][$i] = $data['mulai_uji'][$i].' '.$data['jam_mulai'][$i].':'.$data['menit_mulai'][$i].':00';
          }
          if(!empty($data['selesai_uji'][$i])){
            $data['selesai_uji'][$i] = $data['selesai_uji'][$i].' '.$data['jam_selesai'][$i].':'.$data['menit_selesai'][$i].':00';
          }
          if(!empty($data['persetujuan'][$i])){
            $data['persetujuan'][$i] = $data['persetujuan'][$i].' '.$data['jam_persetujuan'][$i].':'.$data['menit_persetujuan'][$i].':00';
          }
        }
        
        //-- PENAMBAHAN KETERANGAN UNTUK PARAMETER Ca        
        if(in_array($jenis_uji, array('ur', 'fosfor'))){
          $data['ket_1'][$i] = ($data['ket_1'][0] ? $data['ket_1'][0] : null);
          $data['ket_2'][$i] = ($data['ket_2'][0] ? $data['ket_2'][0] : null);
          $data['ket_3'][$i] = ($data['ket_3'][0] ? $data['ket_3'][0] : null);
        }
        if(in_array($jenis_uji, array('ur'))){
          $data['ket_0'][$i] = ($data['ket_0'][0] ? $data['ket_0'][0] : null);
          $data['ket_4'][$i] = ($data['ket_4'][0] ? $data['ket_4'][0] : null);
          $data['ket_5'][$i] = ($data['ket_5'][0] ? $data['ket_5'][0] : null);
        }

        if(empty($data['method_batch'][$i]) && empty($data['nomor_batch'][$i])) {
          $data['nomor_batch'][$i] = $batch;
        }

        $data['suhu'][$i] = $data['suhu'][0];
        $data['rh'][$i] = $data['rh'][0];

        if(!empty($data['alat_uji'][0])){
          $data['alat_uji'][$i] = $data['alat_uji'][0];
        }

        $data['terbitan_revisi_ke'][$i] = $data['terbitan_revisi_ke'][0];
        $data['tanggal_terbitan'][$i] = $data['tanggal_terbitan'][0];
        $data['tanggal_revisi'][$i] = $data['tanggal_revisi'][0];

        if($i != 0 && !is_null($data['id_sampel'][$i]) && $data['id_sampel'][$i] !== ''){
          $data['kontrol_sampel'][$i] = $data['kontrol_sampel'][(intval((int)$i-1))];
        }else{
          if(is_null($data['id_sampel'][$i]) || $data['id_sampel'][$i] == ''){
            $data['kontrol_sampel'][$i] = $data['kontrol_sampel'][$i];
          }
        }

        if($i == 0 || is_null($data['id_sampel'][$i]) || $data['id_sampel'][$i] == ''){
          $data['kontrol_sampel'][$i] = $data['kontrol_sampel'][$i];
        }else{
          $data['kontrol_sampel'][$i] = $data['kontrol_sampel'][(intval((int)$i-1))];
        }
          
        $array_data = array_keys($data);

        $param = array();
        foreach($array_data as $r){
          if(!in_array($r, array('id_sampel', 'jam_mulai', 'menit_mulai', 'jam_selesai', 'menit_selesai', 'jam_persetujuan', 'menit_persetujuan', 'method_batch', 'kontrol_sampel2', 'letak_id', 'letak'))){

            if(in_array($r, array('suhu', 'terbitan_revisi_ke', 'rh', 'tanggal_terbitan', 'alat_uji', 'tanggal_revisi', 'nomor_batch'))){
              $param[$r] = $data[$r][0];
            }else{
              $param[$r] = $data[$r][$i];
            }
            if(!is_null($id) && $id != ""){
              $field_table .= $r.'=?,';
            }else{
              $field_table .= $r.',';
              $field_id .= '?,';

              $field_table2 .= $r.'=?,';
            }
            
          }
        }

        //print('<pre>'.print_r($param, true).'</pre>');
        
        if(is_null($id) || $id==''){
          $field_id = substr($field_id, 0, -1);
          $field_table2 = substr($field_table2, 0, -1);
        }
        $cf = intval(strlen($field_table) - 1);
        $field_table = substr($field_table, 0, -1);

        $status[] = $this->m_analis->save_analisis($table, $field_table, $param, $id, $field_id, $kontrol_sampel2, $field_table2);

      }

      $notif = $this->status_notif($status);

      if(isset($notif)){
        $msguji = strpos($jenis_uji, '_') !== false ? str_replace('_', ' ', $jenis_uji) : $jenis_uji;

        if(strpos($notif, 'gagal') !== false){
          $this->session->set_flashdata('msgw', 'Data uji '.$msguji.' gagal disimpan');
        }else{
          $this->session->set_flashdata('msg', 'Data uji '.$msguji.' berhasil disimpan');
        }
      }

      if(!empty($data['method_batch'][0])){
        if($this->input->get('method') == 'batch_sampel'){
          redirect("sampel/analis/batch/ca?nomor_batch=".$this->input->post('nomor_batch')[0]."&method=batch_sampel&kontrol_sampel=".$this->input->post('kontrol_sampel')[0]);
        }else{
          redirect('sampel/analis/batch/'.$jenis_uji);
        }
        
      }else {
        redirect('sampel/analis/form_uji/'.$jenis_uji);
      }
    }

    function hapus($jenis_uji){
      $data = $this->input->post();
      $status = $this->m_analis->hapus_sampel_batch('kadar_'.$jenis_uji, $data);

      redirect("sampel/analis/batch/ca?nomor_batch=".$data['nomor_batch']."&method=batch_sampel&kontrol_sampel=".$data['kontrol_sampel']);
    }

    function hapus_batch($jenis_uji){
      $data = $this->input->get(); 
      $status = $this->m_analis->hapus_batch($jenis_uji, $data);

      if($status){
        $this->session->set_flashdata('msg', 'Data batch berhasil disimpan');
      }else{
        $this->session->set_flashdata('msgw', 'Data batch gagal disimpan');
      }

      redirect('sampel/analis/batch/'.$jenis_uji);
    }

    function status_notif($status){
      if(isset($status)){
        $notif = '';
        foreach($status as $r){
          if($r){
            $notif .= 'sukses ';
          }else{
            $notif .= 'gagal ';
          }
        }
      }

      return $notif;
    }

    public function batch($jenis_uji = null){
      $j_uji = $jenis_uji;
      if($jenis_uji == 'af'){
        $j_uji = 'Aflatoksin Total';
      }
      if($jenis_uji == 'kalsium'){
        $j_uji = 'Ca Titrimetri';
      }
      if($jenis_uji == 'ca'){
        $j_uji = 'Ca AAS';
      }
      $this->data['halaman'] = "Batch ";
      $this->data['sub_halaman'] = 'Uji '.(!is_null($jenis_uji) ? $j_uji : '');
      $this->data['jenis_uji'] = !is_null($jenis_uji) ? $jenis_uji : null;

      if(!empty($this->input->get('kontrol_sampel'))){
        $this->data['kontrol_sampel'] = str_replace('|-|', '+', $this->input->get('kontrol_sampel'));
      }

      $i=0;
      foreach ($this->param_uji as $r) {
        if($r == $jenis_uji) {
          $data['kode_batch'] = $this->kode_batch[$i];
        }
        $i++;
      }

      $data['jenis_uji'] = $this->data['jenis_uji'];
      $data['table'] = 'kadar_'.$jenis_uji;

      if(!empty($this->input->get('method')) && $this->input->get('method') == 'batch_sampel') {
        $data['method'] = $this->input->get('method');
        $data['nomor_batch'] = $this->input->get('nomor_batch');
        $data['kontrol_sampel'] = $this->data['kontrol_sampel'];

        $this->data['method'] = $data['method'];
        $this->data['kontrol_sampel'] = $this->input->get('kontrol_sampel');
        $this->data['data'] = $this->m_analis->get_batch($data);

        $this->render('form_uji_'.$jenis_uji);
      }else{
        $this->data['data'] = $this->m_analis->get_batch($data);
        $this->render('batch');
      }
    }

    public function kontrol_sampel($jenis_uji = null) {
      $j_uji = $jenis_uji;
      if($jenis_uji == 'af'){
        $j_uji = 'Aflatoksin Total';
      }
      if($jenis_uji == 'kalsium'){
        $j_uji = 'Ca Titrimetri';
      }
      if($jenis_uji == 'ca'){
        $j_uji = 'Ca AAS';
      }
      $this->data['halaman'] = "Analis ";
      $this->data['sub_halaman'] = 'Kontrol Sampel '.(!is_null($jenis_uji) ? $j_uji : '');
      $this->data['jenis_uji'] = $data['jenis_uji'] = !is_null($jenis_uji) ? $jenis_uji : null;
      $data['table'] = 'kadar_'.$jenis_uji;
      $data['method'] = $this->input->get('method');
      $data['nomor_batch'] = $this->input->get('nomor_batch');
      $i=0;
      foreach ($this->param_uji as $r) {
        if($r == $jenis_uji) {
          $data['kode_batch'] = $this->kode_batch[$i];
        }
        $i++;
      }
      $this->data['data'] = $this->m_analis->get_batch($data);

      $this->render('kontrol_sampel');
    }

    function tambah_sampel_ks() {

    }

    function coba(){
      print('<pre>'.print_r($this->input->post(), true).'</pre>'); die();
    }

    public function form_uji2(){
      $this->data['halaman'] = "Analis";
      $this->data['sub_halaman'] = 'Uji '.$jenis_uji;
      $this->data['role_analis'] = $this->roleAnalis;
      
      $p_uji = $this->input->get('p_uji');
      $this->render("form_uji_".$p_uji);
    }

}
