<?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','TDN','UR','AF','ANDF','MBM', 'CAAS', 'PKD', 'Proksimat');
      $this->param_uji = array('air', 'abu', 'protein_kasar', 'lemak_kasar', 'serat_kasar', 'kalsium', 'fosfor', 'tdn', 'ur', 'af', 'andf', 'mbm', 'ca', 'pkd', 'proksimat');
      $this->id_parameter_uji = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);
    }

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

      $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');
    }

    function hapus($jenis_uji){
      $data = $this->input->post();
      $status = $this->m_analis->hapus_sampel_batch('kadar_'.$jenis_uji, $data);
        
      redirect("batch_sampel_list/$jenis_uji/".$data['nomor_batch']."/".$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 dihapus');
      }else{
        $this->session->set_flashdata('msgw', 'Data batch gagal dihapus');
      }

      redirect('batch_list/'.$jenis_uji);
    }


//-----------------------------------------------------------------------------
//            CONTROLLER BARU
//-----------------------------------------------------------------------------
    public function uji_analis($jenis_uji='')
    {
        $this->data['halaman'] = 'ANALIS '.strtoupper($jenis_uji);
        
        for($i=0;$i<count($this->param_uji);$i++){
            if($this->param_uji[$i] == $jenis_uji){
                $id_uji = $this->id_parameter_uji[$i];
                $this->data['jenis_uji']= $this->param_uji[$i];
                $this->data['id_uji']   = $id_uji;
            }
        }

        $this->data['data']       = $this->m_analis->get_analis($jenis_uji, $id_uji)->result_array();
        $this->data['role_analis']= $this->role_analis;

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

    public function buat_batch($jenis_uji, $deret='')
    {
        $this->data['halaman'] = 'UJI ANALIS '.strtoupper($jenis_uji);

        // SET VARIABEL POKOK
        for($i=0;$i<count($this->param_uji);$i++){
            if($this->param_uji[$i] == $jenis_uji){
                $id_uji     = $this->id_parameter_uji[$i];
                $kode_batch = $this->kode_batch[$i];
                $this->data['jenis_uji']= $this->param_uji[$i];
                $this->data['id_uji']   = $id_uji;
            }
        }

        // SET NOMOR BATCH
        if(empty($this->input->get('nomor_batch'))){
            $tgl_batch    = date('dmY', strtotime($this->input->post('tgl_batch')));
            $tgl_kode     = $kode_batch.'-'.$tgl_batch;
            $count_batch  = $this->m_analis->get_data_batch($jenis_uji, $tgl_batch)->row_array();
            $nomor_batch  = $tgl_kode.'-'.((int)$count_batch+1);
            $this->data['nomor_batch'] = $nomor_batch;
        }else {
            $this->data['nomor_batch'] = $this->input->get('nomor_batch');
        }

        // MENENTUKAN ID KADAR UNTUK PAGE DERET ATAU LANGSUNG
        if($this->input->post('id')){
            $id_param = $this->input->post('id');
        }else {
            $id_param = $this->session->userdata('id_param');
        }
        
        // JIKA DERET = '' KARENA TIDAK SEMUA PARAMETER UJI MEMILIKI DERET
        if($deret != ''){
            $this->session->set_userdata('id_param', $id_param);
            redirect("deret_uji_list/$jenis_uji/$nomor_batch");
        }

        // KHUSUS UNTUK PAREMETER YANG MEMILIKI DERET
        if($this->input->get()){
            $i_get = $this->input->get();
            $this->data['slope'] = $i_get['slope'];
            $this->data['intercept'] = $i_get['intercept'];
            if(!empty($i_get['abs0'])){
                $this->data['abs0'] = $i_get['abs0'];
            }
        }

        if(in_array($jenis_uji, ['fosfor', 'ca', 'ur', 'af'])){
            switch ($jenis_uji) {
                case 'ur':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_ur($this->input->get('nomor_batch'));
                    break;
                case 'af':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_af($this->input->get('nomor_batch'));
                    break;
                case 'ca':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_ca($this->input->get('nomor_batch'));
                    break;
                case 'fosfor':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_fosfor($this->input->get('nomor_batch'));
                    break;
                default:
                    # code...
                    break;
            }
            $this->data['is_grafik'] = 1;
        }

        //$this->data['time_css'] = "onfocus='this.size=3;' onblur='this.size=1;'";
        $this->data['alat_uji']    = $this->m_analis->get_alat_uji($id_uji)->result_array();
        $this->data['digestor']    = $this->m_analis->get_alat_uji($id_uji, "Digestor")->result_array();
        $this->data['kjeltec']    = $this->m_analis->get_alat_uji($id_uji, "Kjeltec")->result_array();
        $this->data['kode_fibertec']    = $this->m_analis->get_alat_uji($id_uji, "Kode Fibertec")->result_array();
        $this->data['oven']    = $this->m_analis->get_alat_uji($id_uji, "Oven")->result_array();
        $this->data['furnace']    = $this->m_analis->get_alat_uji($id_uji, "Furnace")->result_array();

        if(in_array($jenis_uji, ['fosfor', 'ca', 'kalsium'])){
            $data1 = [];
            $abu_ks = $this->m_analis->get_abu_ks($jenis_uji, $id_param);
            foreach($abu_ks as $ak){
                $ak['c_range'] = null;
                $data1[] = $ak;
            }

            $data = $this->m_analis->get_analis($jenis_uji, false, $id_param)->result_array();
            foreach($data as $d){
                $data1[] = $d;
            }

            //print('<pre>'.print_r($data1, true).'</pre>'); die();
            $this->data['data'] = $data1;

        }else{
            $this->data['data'] = $this->m_analis->get_analis($jenis_uji, false, $id_param)->result_array();
        }

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

    public function edit_batch_uji($jenis_uji, $nomor_batch='', $kontrol_sampel='')
    {
        $this->data['halaman'] = 'EDIT UJI ANALIS '.strtoupper($jenis_uji);

        // SET VARIABEL POKOK
        for($i=0;$i<count($this->param_uji);$i++){
            if($this->param_uji[$i] == $jenis_uji){
                $id_uji     = $this->id_parameter_uji[$i];
                $kode_batch = $this->kode_batch[$i];
                $this->data['jenis_uji']= $this->param_uji[$i];
                $this->data['id_uji']   = $id_uji;
            }
        }

        if(in_array($jenis_uji, ['fosfor', 'ca', 'ur', 'af'])){
            switch ($jenis_uji) {
                case 'ur':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_ur($nomor_batch);
                    break;
                case 'af':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_af($nomor_batch);
                    break;
                case 'ca':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_ca($nomor_batch);
                    break;
                case 'fosfor':
                    $this->data['grafik'] = $this->m_disposisi->get_deret_fosfor($nomor_batch);
                    break;
                default:
                    # code...
                    break;
            }
            $this->data['is_grafik'] = 1;
        }

        $this->data['alat_uji']    = $this->m_analis->get_alat_uji($id_uji)->result_array();
        $this->data['kontrol_sampel'] = $kontrol_sampel;
        $this->data['nomor_batch'] = $nomor_batch;
        $this->data['is_edit']     = 1;
        //$this->data['time_css']    = "onfocus='this.size=3;' onblur='this.size=1;' onchange='this.size=1; this.blur();'";
        $this->data['digestor']    = $this->m_analis->get_alat_uji($id_uji, "Digestor")->result_array();
        $this->data['kjeltec']    = $this->m_analis->get_alat_uji($id_uji, "Kjeltec")->result_array();
        $this->data['kode_fibertec']    = $this->m_analis->get_alat_uji($id_uji, "Kode Fibertec")->result_array();
        $this->data['oven']    = $this->m_analis->get_alat_uji($id_uji, "Oven")->result_array();
        $this->data['furnace']    = $this->m_analis->get_alat_uji($id_uji, "Furnace")->result_array();
        $data = $this->m_analis->get_edit_analis($jenis_uji, $nomor_batch, $kontrol_sampel)->result_array();

        if($jenis_uji == 'serat_kasar'){
            $data1 = [];
            foreach($data as $key => $d) if($d['id_sampel'] != 0) $data1[] = $d;
            foreach($data as $key => $d) if($d['id_sampel'] == 0) $data1[] = $d;
            
            $this->data['data'] = $data1;
        }else{
            $this->data['data'] = $data;
        }

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

    public function save_batch_uji($jenis_uji)
    {
        // MENDENIFISIKAN INPUT POST KE DALAM VARIABLE IPOST
        $i_post = $this->input->post();

        // KHUSUS SERAT KASAR, KONTROL SAMPEL BERADA DI BAWAH
        if($jenis_uji == 'serat_kasar'){
            $post_key = array_keys($i_post);
            foreach($post_key as $pk_key => $pk){
                if(!in_array($pk_key, [0,1,2,3,4,5,6,7,8,9,10,11,12])){
                    $i_post[$pk] = array_reverse($i_post[$pk]);
                }
            }
        }
        //-----------------------------------------------------

        // MENGATUR IPOST YANG DENGAN NILAI TUNGGAL / [0]  
        $i_post = $this->parameter_ipost($jenis_uji, $i_post);
        //  MENGATUR FIELD-FIELD YANG SAMA DARI INPUT DG YG DI DB
        $field_db  = $this->m_master->get_field('kadar_'.$jenis_uji);
        $field_uji = $this->samakan_variable($i_post, $field_db);
        // MELENGKAPI VARIABLE-VARIALBE DG ISI VARIABLE TUNGGAL SEBELUBM DI SIMPAN DI DB
        $field_uji = $this->table_field($jenis_uji, $field_uji);

        if(in_array($jenis_uji, ['serat_kasar'])){
            $field_uji = array_reverse($field_uji);
        }

        // SAVE DAN MENGATUR KONTROL SAMPEL
        for($i=0;$i<count($field_uji);$i++){
            $save = $this->m_analis->save_batch_uji($jenis_uji, $field_uji[$i]);
        }
        
        $this->session->set_flashdata('msg', 'Sampel berhasil diuji');
        redirect('sampel_uji/'.$jenis_uji);
    }

    public function update_batch_uji($jenis_uji, $nomor_batch, $kontrol_uji)
    {
        $i_post = $this->input->post();
        $i_post = $this->parameter_ipost($jenis_uji, $i_post);
        //  MENGATUR FIELD-FIELD YANG SAMA DARI INPUT DG YG DI DB
        $field_db  = $this->m_master->get_field('kadar_'.$jenis_uji);
        $field_uji = $this->samakan_variable($i_post, $field_db);
        // MELENGKAPI VARIABLE-VARIALBE DG ISI VARIABLE TUNGGAL SEBELUBM DI SIMPAN DI DB
        $field_uji = $this->table_field($jenis_uji, $field_uji);

        // UPDATE DAN MENGATUR KONTROL SAMPEL
        for($i=0;$i<count($field_uji);$i++){
            if($i != 0){
              $field_uji[$i]['kontrol_sampel'] = $field_uji[$i-1]['kontrol_sampel'];
            }
            $save[] = $this->m_analis->update_batch_uji($jenis_uji, $field_uji[$i], $i_post['id'][$i]);
        }

        $this->session->set_flashdata('msg', 'Sampel dan batch berhasil diubah');

        $kontrol_sampel = $field_uji[0]['kontrol_sampel'];
        redirect("batch_sampel_list/$jenis_uji/$nomor_batch/$kontrol_sampel");
    }

    // MENGISI VARIABLE DENGAN ISIAN VARIABLE SEBELUMNYA (YANG SAMA) & MENAMBAH VARIABLE YANG KURANG
    public function table_field($jenis_uji, $field_uji){
        //$nama_analis = $this->m_analis->get_nama_analis($this->id_user);
        for($i=0;$i<count($field_uji);$i++){
            //$field_uji[$i]['nama_analis'] = $nama_analis;
            $nama_kontrol_sampel = $this->compress_field($field_uji, $i, 'kontrol_sampel');
            $nama_kontrol_sampel = str_replace(' ', '_s_', $nama_kontrol_sampel);
            $nama_kontrol_sampel = str_replace('+', '_p_', $nama_kontrol_sampel);
            $field_uji[$i]['kontrol_sampel'] = $nama_kontrol_sampel;
            $field_uji[$i]['mulai_uji']      = $this->compress_field($field_uji, $i, 'mulai_uji');
            $field_uji[$i]['selesai_uji']    = $this->compress_field($field_uji, $i, 'selesai_uji');
            $field_uji[$i]['metode_uji'] = $this->compress_field($field_uji, $i, 'metode_uji');
            $field_uji[$i]['nama_analis'] = $this->compress_field($field_uji, $i, 'nama_analis');
            if(!in_array($jenis_uji, ['fosfor', 'ca', 'af', 'ur', 'kalsium', 'lemak_kasar', 'protein_kasar', 'andf', 'serat_kasar'])) {
                $field_uji[$i]['alat_uji']   = $this->compress_field($field_uji, $i, 'alat_uji');
                $field_uji[$i]['alat_uji']  = json_encode($field_uji[$i]['alat_uji']);
            }
            $field_uji[$i]['suhu']           = $this->compress_field($field_uji, $i, 'suhu');
            $field_uji[$i]['rh']             = $this->compress_field($field_uji, $i, 'rh');
            $field_uji[$i]['terbitan_revisi_ke'] = $this->compress_field($field_uji, $i, 'terbitan_revisi_ke');
            $field_uji[$i]['nomor_bagian'] = $this->compress_field($field_uji, $i, 'nomor_bagian');
            $field_uji[$i]['halaman'] = $this->compress_field($field_uji, $i, 'halaman');
            $field_uji[$i]['tanggal_terbitan']   = $this->compress_field($field_uji, $i, 'tanggal_terbitan');
            $field_uji[$i]['tanggal_revisi'] = $this->compress_field($field_uji, $i, 'tanggal_revisi');
            if(in_array($jenis_uji, ['fosfor', 'ur'])){
                $field_uji[$i]['ket_1']      = $this->compress_field($field_uji, $i, 'ket_1');
                $field_uji[$i]['ket_2']      = $this->compress_field($field_uji, $i, 'ket_2');
                $field_uji[$i]['ket_3']      = $this->compress_field($field_uji, $i, 'ket_3');
                if($jenis_uji == 'ur'){
                    $field_uji[$i]['ket_4']  = $this->compress_field($field_uji, $i, 'ket_4');
                    $field_uji[$i]['ket_5']  = $this->compress_field($field_uji, $i, 'ket_5');
                    $field_uji[$i]['ket_0']  = $this->compress_field($field_uji, $i, 'ket_0');
                }
            }
            $field_uji[$i]['last_modified_user'] = $this->id_user;
        }
        return $field_uji;
    }

    public function compress_field($field, $x, $nama_field){
        $field_hasil = $field[$x][$nama_field];
        if(empty($field[$x][$nama_field]) || !isset($field[$x][$nama_field])){
            if($x != 0){
                $field_hasil = $field[$x-1][$nama_field];
            }
        }
        return $field_hasil;
    }

    // DIGUNAKAN UNTUK NEYAMAKAN VARIABLE DARI IPOST DAN DB
    public function samakan_variable($i_post, $field_db)
    {
        $param_key  = array_keys($i_post);
        $field_uji_key  = [];
        $field_uji = [];
        foreach($field_db as $f){
            if(in_array($f, $param_key)){
                $field_uji_key[$f] = $i_post[$f];
                $x = 0;
                foreach($i_post[$f] as $ip){
                    $field_uji[$x][$f] = $ip;
                    $x++;  
                }             
            }
        }
        return $field_uji;
    }

    public function parameter_ipost($jenis_uji, $i_post)
    {
        // MENGGABUNG TANGGAL DENGAN JAM DAN MENIT
        for($i=0;$i<count($i_post['id_sampel']);$i++){
            $waktu_mulai   = $this->compress_ipost($i_post, 'waktu_mulai', $i);
            $mulai_uji   = $this->compress_ipost($i_post, 'mulai_uji', $i);
            $selesai_uji   = $this->compress_ipost($i_post, 'selesai_uji', $i);

            $waktu_selesai   = $this->compress_ipost($i_post, 'waktu_selesai', $i);
            
            $i_post['mulai_uji'][$i]        = strtotime("$mulai_uji $waktu_mulai");
            $i_post['selesai_uji'][$i]      = strtotime("$selesai_uji $waktu_selesai");

            $i_post['suhu'][$i]             = $this->compress_ipost($i_post, 'suhu', $i);
            $i_post['rh'][$i]               = $this->compress_ipost($i_post, 'rh', $i);
            $i_post['tanggal_terbitan'][$i] = strtotime($this->compress_ipost($i_post, 'tanggal_terbitan', $i));
            $i_post['terbitan_revisi_ke'][$i] = $this->compress_ipost($i_post, 'terbitan_revisi_ke', $i);
            $i_post['nomor_bagian'][$i] = $this->compress_ipost($i_post, 'nomor_bagian', $i);
            $i_post['halaman'][$i] = $this->compress_ipost($i_post, 'halaman', $i);
            $i_post['tanggal_revisi'][$i]   = strtotime($this->compress_ipost($i_post, 'tanggal_revisi', $i));
            $i_post['metode_uji'][$i]       = $this->compress_ipost($i_post, 'metode_uji', $i);
            $i_post['nama_analis'][$i]       = $this->compress_ipost($i_post, 'nama_analis', $i);
            if(!in_array($jenis_uji, ['fosfor', 'ca', 'af', 'ur', 'kalsium', 'lemak_kasar', 'protein_kasar', 'andf', 'serat_kasar'])){
                $i_post['alat_uji'][$i]     = $this->compress_ipost($i_post, 'alat_uji', $i);
            }
            if(in_array($jenis_uji, ['ur', 'fosfor'])){
                $i_post['ket_1'][$i]        = strtotime($this->compress_ipost($i_post, 'ket_1', $i));
                $i_post['ket_2'][$i]        = strtotime($this->compress_ipost($i_post, 'ket_2', $i));
                if($jenis_uji == 'fosfor'){
                    $i_post['ket_3'][$i]    = $this->compress_ipost($i_post, 'ket_3', $i);
                }
                if($jenis_uji == 'ur'){
                    $i_post['ket_3'][$i]    = strtotime($this->compress_ipost($i_post, 'ket_3', $i));
                    $i_post['ket_4'][$i]    = strtotime($this->compress_ipost($i_post, 'ket_4', $i));
                    $i_post['ket_5'][$i]    = strtotime($this->compress_ipost($i_post, 'ket_5', $i));
                    $i_post['ket_0'][$i]    = $this->compress_ipost($i_post, 'ket_0', $i);
                }     
            }
        }
        return $i_post;
    }

    public function compress_ipost($i_post, $nama_ipost, $x)
    {   
        $field_hasil = null;
        if(!empty($i_post[$nama_ipost][$x])){
            if(!isset($i_post[$nama_ipost][$x])){
                $field_hasil = $i_post[$nama_ipost][$x-1];
            }else {
                $field_hasil = $i_post[$nama_ipost][$x];
            }
        }

        if(is_null($field_hasil) && !empty($i_post[$nama_ipost][0])){
            $field_hasil = $i_post[$nama_ipost][0];
        }

        return $field_hasil;
    }

    public function batch_list($jenis_uji)
    {   
        $this->data['halaman']   = 'BATCH LIST '.strtoupper($jenis_uji);
        $this->data['jenis_uji'] = $jenis_uji;
        $this->data['data']      = $this->m_analis->get_batch_list($jenis_uji)->result_array();
        $this->render('batch');
    }

    public function kontrol_sampel_list($jenis_uji, $nomor_batch)
    {
        $this->data['halaman']     = 'KONTROL SAMPEL LIST '.$nomor_batch;
        $this->data['jenis_uji']   = $jenis_uji;
        $this->data['nomor_batch'] = $nomor_batch;
        $this->data['data']        =  $this->m_analis->get_kontrol_sampel_list($jenis_uji, $nomor_batch)->result_array();

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

    public function batch_sampel_list($jenis_uji, $nomor_batch, $kontrol_sampel)
    {
        $this->data['halaman']        = 'BATCH: '.$nomor_batch.' ('.val_ks($kontrol_sampel).')';
        $this->data['jenis_uji']      = $jenis_uji;
        $this->data['nomor_batch']    = $nomor_batch;
        $this->data['kontrol_sampel'] = $kontrol_sampel;
        $this->data['role_analis']    = $this->role_analis;

        $data = $this->m_analis->get_batch_sampel_list($jenis_uji, $nomor_batch, $kontrol_sampel);
        $data = $data->result_array();

        if($jenis_uji == 'serat_kasar'){
            $data1 = [];
            foreach($data as $key => $d) if($d['id_sampel'] != 0) $data1[] = $d;
            foreach($data as $key => $d) if($d['id_sampel'] == 0) $data1[] = $d;

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

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

    public function deret_uji_list($jenis_uji, $nomor_batch, $kontrol_sampel='')
    {
        $this->data['halaman']        = 'DERET UJI '.strtoupper($jenis_uji);
        $this->data['jenis_uji']      = $jenis_uji;
        $this->data['id_kadar']       = $this->session->userdata('id_param');
        $this->data['nomor_batch']    = $nomor_batch;
        if($kontrol_sampel != '') {
            $this->data['kontrol_sampel'] = $kontrol_sampel;
        }
        $this->render('deret_'.$jenis_uji);
    }

    public function deret_uji_grafik($jenis_uji, $nomor_batch, $is_grafik = 0)
    {
        $this->data['halaman']        = 'GRAFIK DERET UJI '.strtoupper($jenis_uji);
        $this->data['jenis_uji']      = $jenis_uji;
        $this->data['id_kadar']       = $this->session->userdata('id_param');
        $this->data['nomor_batch']    = $nomor_batch;
        if($jenis_uji == 'fosfor'){
            $this->data['grafik'] = $this->m_disposisi->get_deret_fosfor($nomor_batch);
            $this->data['data'] = $this->data['grafik'];
        }
        if($jenis_uji == 'ur'){
            $this->data['grafik'] = $this->m_disposisi->get_deret_ur($nomor_batch);
            $this->data['data'] = $this->data['grafik'];
        }
        if($jenis_uji == 'af'){
            $this->data['grafik'] = $this->m_disposisi->get_deret_af($nomor_batch);
            $this->data['data'] = $this->data['grafik'];
        }
        if($jenis_uji == 'ca'){
            $this->data['grafik'] = $this->m_disposisi->get_deret_ca($nomor_batch);
            $this->data['data'] = $this->data['grafik'];
        }

        if($is_grafik != 0){
            $this->data['is_grafik'] = 1;
        }

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

    public function save_deret_uji($jenis_uji, $nomor_batch)
    {
        $field  = $this->m_master->get_field('deret_'.$jenis_uji);
        $i_post = $this->input->post();

        // MENAMBAH FIELD YANG KURANG DI IPOST DG DI DB
        if($jenis_uji == 'fosfor'){
            $i_post['deret_std_p'] = $i_post['deret_level'];
            $i_post['c_std_p'] = $i_post['c_std'];
        }
        if($jenis_uji == 'af'){
            $i_post['level_standart'] = $i_post['deret_level'];
            $i_post['total_af'] = $i_post['c_std'];
        }
        if($jenis_uji == 'ur'){
            $i_post['deret_std_urea'] = $i_post['deret_level'];
            $i_post['c_std_urea'] = $i_post['c_std'];
        }
        if($jenis_uji == 'ca'){
            $i_post['deret_std_ca'] = $i_post['deret_level'];
            $i_post['c_std_ca'] = $i_post['c_std'];
        }

        // D_DERET
        //print('<pre>'.print_r($i_post, true).'</pre>');

        // MENYAMAKAN KOLOM FIELD-FIELD DENGAN I POST
        $param_key  = array_keys($i_post);
        $field_uji_key  = [];
        $field_uji = [];
        foreach($field as $f){
            if(in_array($f, $param_key)){
                $field_uji_key[$f] = $i_post[$f];
                $x = 0;
                foreach($i_post[$f] as $ip){
                    $field_uji[$x][$f] = $ip;
                    $x++;  
                }             
            }
        }
        
        // D_DERET
        //print('<pre>'.print_r($field_uji, true).'</pre>'); die();

        // MEMBERI NILAI FIELD YANG KOSONG DENGAN NILAI FIELD SEBELUMNYA & SAVE DATA
        for($i=0;$i<count($field_uji);$i++){
            if(empty($field_uji[$i]['nomor_batch'])){
                $field_uji[$i]['nomor_batch'] = $field_uji[$i-1]['nomor_batch'];
            }    
            $this->m_analis->save_deret_uji($jenis_uji, $field_uji[$i]);
        }
        $intercept   = $field_uji[0]['intercept'];
        $slope       = $field_uji[0]['slope'];
        $nomor_batch = $field_uji[0]['nomor_batch'];
        $i_abs0        = '';
        if($jenis_uji == 'af'){
            $i_abs0        = '&abs0='.$i_post['abs'][0];
        }
        redirect("batch_uji/$jenis_uji?nomor_batch=$nomor_batch&intercept=$intercept&slope=$slope".$i_abs0);

    }

}
