<?php

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

    require './vendor/autoload.php';

class Pengajuan extends MY_Controller {

    protected $template = "app";
    protected $module = "pengajuan";
    protected $per_page = 20;
    protected $url_uji = 'uji_air';

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

    }

    public function index() {
        $this->data['halaman'] = "Disposisi";
        $this->data['sub_halaman'] = '';
        $this->data['idGroup'] = $this->id_group;
        $this->data['parameter_uji'] = $this->m_master->getDataMaster('parameter_uji');
        $this->data['keterangan'] = $this->m_master->getDataMaster('gol_jenis_sampel');
        $this->data['penanganan_sampel'] = $this->m_master->getDataMaster('penanganan_sampel');
        $this->data['role_analis'] = $this->role_analis;

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

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

        // PAGINATION
        $this->load->library('pagination');
        $config['base_url'] = base_url() . '/disposisi/';
		$config['total_rows'] = $total_data;
		$config["per_page"] = 10;

        // Membuat Style pagination untuk BootStrap v4
        $config['first_link']       = 'First';
        $config['last_link']        = 'Last';
        $config['next_link']        = 'Next';
        $config['prev_link']        = 'Prev';
        $config['full_tag_open']    = '<div class="pagging text-center"><nav><ul class="pagination justify-content-center">';
        $config['full_tag_close']   = '</ul></nav></div>';
        $config['num_tag_open']     = '<li class="page-item"><span class="page-link">';
        $config['num_tag_close']    = '</span></li>';
        $config['cur_tag_open']     = '<li class="page-item active"><span class="page-link">';
        $config['cur_tag_close']    = '<span class="sr-only">(current)</span></span></li>';
        $config['next_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['next_tagl_close']  = '<span aria-hidden="true">&raquo;</span></span></li>';
        $config['prev_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['prev_tagl_close']  = '</span>Next</li>';
        $config['first_tag_open']   = '<li class="page-item"><span class="page-link">';
        $config['first_tagl_close'] = '</span></li>';
        $config['last_tag_open']    = '<li class="page-item"><span class="page-link">';
        $config['last_tagl_close']  = '</span></li>';
		$this->pagination->initialize($config);

        $data['page'] = ($this->uri->segment(2)) ? $this->uri->segment(2) : 0;
        
        $this->data['data']     = $this->m_disposisi->getDisposisi(false, $config["per_page"], $data['page']);
		$this->data["link_paging"] = $this->pagination->create_links();
        // END PAGINATION
        */
        $this->data['data']     = $this->m_disposisi->getDisposisi();
        $this->data["link_paging"] = '';

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

    public function penyiapan_sampel($id_uji = '', $id_sampel = '', $id_status = '') {
        $this->data['halaman'] = "Penyiapan Sampel";
        $this->data['sub_halaman'] = '';
        $this->data['bentuk_sampel'] = $this->m_master->getDataMaster('bentuk_sampel');
        $this->data['penanganan_arsip'] =$this->m_master->getDataMaster('jenis_penanganan_sampel', 'arsip');
        $this->data['penanganan_kerja'] = $this->m_master->getDataMaster('jenis_penanganan_sampel', 'kerja');
        $this->data['sampel_kerja'] = $this->m_master->getDataMaster('penyimpanan_sampel');
        $this->data['sampel_arsip'] = $this->m_master->getDataMaster('penyimpanan_sampel');
        $this->data['grinder'] = $this->m_master->getDataMaster('jenis_grinder');

        $this->data['nomor_sampel'] = $this->m_disposisi->get_sampel_by_id($id_sampel)['id'];
        $this->data['bentuk_jenis_sampel'] = $this->m_disposisi->get_sampel_by_id($id_sampel)['bentuk_jenis'];
        $this->data['berat_sampel'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['berat_sampel'];
        $this->data['satuan_berat'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['satuan_berat'];
        $this->data['kemasan'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['kemasan'];
        $this->data['id_sampel_kerja'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['id_sampel_kerja'];
        $this->data['id_sampel_arsip'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['id_sampel_arsip'];

        $this->data['edit'] = $this->m_disposisi->getEditSampel($id_sampel);

        $data = $this->input->post();
        if($data){
            if(in_array($this->id_group, array('1', '8'))){
                $data['pukul_mulai'] = $data['pukul_mulai'];
                $data['pukul_selesai'] = toi_time01($data['pukul_selesai']);

                $id_bentuk_sampel = null;
                if($data['id_bentuk_sampel'] == '1' || $data['id_bentuk_sampel'] == 'Cair') $id_bentuk_sampel = 1;
                if($data['id_bentuk_sampel'] == '2' || $data['id_bentuk_sampel'] == 'Padat'){
                    if(!empty($data['bentuk_padat']) && isset($data['bentuk_padat'])){
                        $id_bentuk_sampel = '';
                        $x = count($data['bentuk_padat']);
                        for ($i=0; $i < $x; $i++) {
                            $id_bentuk_sampel .= $data['bentuk_padat'][$i].'|';
                        }
                    }
                    $data['id_bentuk_sampel'] = $id_bentuk_sampel;
                }

                if(isset($data['grinder_baru']) && $data['grinder_baru'] != ''){
                    $this->db->insert('jenis_grinder', ['nama'=>$data['grinder_baru']]);

                    $this->db->where('id', $this->db->insert_id());
                    $this->db->update('jenis_grinder', ['urutan'=>$this->db->insert_id()]);
                    $data['id_grinder'] = $this->db->insert_id();
                }

                $status = $this->m_disposisi->penyiapan_sampel($data, $id_sampel);

                if($status){
                    $msg = 'Sampel berhasil disiapkan';
                    $this->session->set_flashdata('msg', $msg);
                }else{
                    $msg = 'Sampel gagal disiapkan';
                    $this->session->set_flashdata('msgw', $msg);
                }
                redirect("penyiapan_uji/".$id_uji.'/'.$id_status);
            }
        }

        $this->render("penyiapan_sampel");
    }

    public function penyiapan_uji(){
        $this->data['halaman'] = "Penyiapan Uji Sampel";
        $this->data['sub_halaman'] = '';

        $this->data['data'] = $this->m_disposisi->penyiapanUji();

        $this->render("penyiapan_uji");
    }

    function siapkan_sampel($id_uji = '', $id_status = 0, $is_daftar = 0){
        if($id_status == 8){
        $this->m_disposisi->set_tgl_persetujuan($id_uji);
        }
        if($id_status != 0){
            $param = array(
                'id_uji' => $id_uji,
                'id_status' => $id_status,
                'id_user'=> $this->id_user
            );
            $status = $this->m_disposisi->siapkanSampel($param);

            if($id_status == 8){
                $ket_lhu = trim(str_replace("\r\n", '||', $this->input->post('ket_lhu')));
                //print($ket_lhu);
                //die();
                $this->db->where('id', $id_uji);
                $this->db->update('permintaan_uji', ['ket_lhu'=>$ket_lhu]);
            }

            if($id_status == 9){
                $this->db->where('id', $id_uji);
                $this->db->update('permintaan_uji', ['tgl_acc_lhu'=>strtotime(date('Y-m-d H:i:s'))]);
            }

            if($id_status == 10){
                $this->db->where('id', $id_uji);
                $this->db->update('permintaan_uji', ['tgl_acc_lhp'=>strtotime(date('Y-m-d H:i:s'))]);
            }

            if($id_status == 13){
                redirect('permintaan_uji');
            }
            $msg = 'Data sampel berhasil diupdate';
        }else if($data['status'] == '0'){
            $status = $this->m_disposisi->deletePenyiapanSampel($data['id_uji']);
            $msg = 'Penyiapan Sampel Dihapus';
        }

        if($status){
            $this->session->set_flashdata('msg', $msg);
        }else{
            $msg = 'Sampel gagal disiapkan';
            $this->session->set_flashdata('msgw', $msg);
        }

        if($is_daftar == 1){
            redirect('permintaan_uji');
        }
        redirect("disposisi");
    }

    public function mulai_uji_sampel($id_uji = '', $id_status = '') {
        $this->module               = 'uji_analis';
        $this->data['halaman']      = "Uji Analis";
        $this->data['sub_halaman']  = '';
        $this->data['id_uji']       = $id_uji;
        $this->data['id_status']    = $id_status;
        $this->data['is_uji_analis']= 1;
        $this->data['data']         = $this->m_disposisi->getAnalis($id_uji);
        //tampil_array($this->data['data']); die();
        $this->render('index');
    }

    public function verifikasi_penyelia($id_uji='', $id_status='') 
    {
        $this->module = 'uji_analis';
        $this->data['halaman'] = "Uji Analis";
        $this->data['sub_halaman'] = '';
        $this->data['id_group'] = $this->id_group;
        $this->data['id_status'] = $id_status;
        $this->data['id_uji'] = $id_uji;
        $this->data['is_penyelia'] = 1;
            $this->data['data'] = $this->m_disposisi->getAnalis($id_uji);
        $this->render('index');
    }

    public function lhp($id_uji, $id_status){
        $this->module = "lhu_lhp";
        $this->data['halaman'] = 'LHP';
        $this->data['sub_halaman'] = '';
        $this->data['id_uji'] = $id_uji;
        $this->data['id_status'] = $id_status;
        $this->data['data'] = $this->m_disposisi->getAnalis($id_uji);
        //tampil_array($this->data['data']);
        $this->render('lhp');
    }

    function save_lhp($id_uji, $id_status)
    {
        $ipost = $this->input->post();
        $ipost_key = array_keys($ipost);
        
        foreach($ipost['id_sampel'] as $ikey => $is){
            foreach($ipost_key as $pkey => $p){
                $param[$p] = $ipost[$p][$ikey];
            }

            $status = $this->m_disposisi->save_lhp($param);
        }

        if($status == 1){
            $this->session->set_flashdata('msg', 'Data LHP berhasil disimpan');
        }
        if($status == 2){
            $this->session->set_flashdata('msg', 'Data LHP berhasil diupdate');
        }

        redirect('lhp/'.$id_uji.'/'.$id_status);
    }

    public function lhu($id_uji, $id_status){
        $this->module = "uji_analis";
        $this->data['halaman'] = 'LHU';
        $this->data['sub_halaman'] = '';
        $this->data['id_status'] = $id_status;
        $this->data['id_uji'] = $id_uji;
        $this->data['is_lhu'] = 1;
        $this->data['data'] = $this->m_disposisi->getAnalis($id_uji);
        $this->render('index');
    }

    public function tdn($id_uji = '', $id_status = ''){
        $this->module = "lhu_lhp";
        $this->data['halaman'] = 'TDN';
        $this->data['sub_halaman'] = '';
        $this->data['id_uji'] = $id_uji;
        $this->data['id_status'] = $id_status;
        $this->data['get'] = $dget = array('id_uji'=>$id_uji, 'status'=>$id_status);
        $this->data['data'] = $this->m_disposisi->get_tdn($dget);

        if($this->input->post()){
            $data = $this->input->post();
            $data_key = array_keys($data);

            $param = $this->tdn_perulangan1($data, $data_key);//mengubah sturktur array dari brdsrkan jenis k id
            //print('<pre>'.print_r($param, true).'</pre>'); die();
            $param = $this->tdn_perulangan2($param, $data_key);// digunakan untuk menampilkan 2 angka d blkng koma(,)

            $status = '';
            foreach($param as $p){
            $p = array_reverse($p);
            $key = array_keys($p);
            $pkey['insert'] =  $pkey['insert_p']= $pkey['update'] = '';

            foreach($key as $k){
                $pkey['insert'] .= $k.',';
                $pkey['insert_p'] .= '?,';

                if($k != 'id_sampel'){
                $pkey['update'] .= $k.'=?,';
                }
            }

            $pkey['insert'] = substr($pkey['insert'],0,-1);
            $pkey['insert_p'] = substr($pkey['insert_p'],0,-1);
            $pkey['update'] = substr($pkey['update'],0,-1);
            $pkey['update_p'] = 'id_sampel = ?';

            //print("<pre>".print_r($pkey, true)."</pre?"); die();
            $status .=  $this->m_disposisi->set_tdn($p, $pkey);
            }

            if(strpos($status, 'gagal') == false){
            $msg = 'TDN berhasil diupdate';
            $this->session->set_flashdata('msg', $msg);
            }else{
            $msg = 'TDN gagal diupdate';
            $this->session->set_flashdata('msg', $msg);
            }
            redirect("tdn/".$dget['id_uji']."/".$dget['status']);
        }

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

    function tdn_perulangan1($data, $data_key){
        $param = array();
        $x = count($data['id_sampel']);
        for ($i=0; $i < $x; $i++) {
            foreach($data_key as $dk){
            $tdn = (float)$data[$dk][$i];

            if($dk == 'id_sampel'){
                $param[$i][$dk] = $tdn;
            }else{
                if($dk == 'air') $tdn = 100-$tdn;
                else $tdn = 100/$param[$i]['bk_air']*$tdn;

                $param[$i]['bk_'.$dk] = round($tdn, 2);
            }
            }

            $p = $param[$i];
            $p['bk_betn'] = 100-($p['bk_abu']+$p['bk_protein_kasar']+$p['bk_lemak_kasar']+$p['bk_serat_kasar']);
            $p['bk_tdn'] = 5.31 + (0.412 * $p['bk_protein_kasar']) + (0.249 * $p['bk_serat_kasar']) + (1.444 * $p['bk_lemak_kasar']) + (0.937 * $p['bk_betn']);
            //5,31 + 0,412 x PK + 0,249 x SK + 1,444 x LK + 0,937 x BETN

            $param[$i] = $p;
        }

        return $param;
    }

    function tdn_perulangan2($param, $data_key){
        $data_key[] = 'betn';
        $data_key[] = 'tdn';
        $s = 0;
        foreach($param as $pa){
            foreach ($data_key as $k) {
            if($k != 'id_sampel'){
                if(strpos($pa['bk_'.$k], '.') != false){
                $pa['bk_'.$k] = explode('.', $pa['bk_'.$k]);
                $pa['bk_'.$k][1] = substr($pa['bk_'.$k][1],0,2);
                $pa['bk_'.$k] = $pa['bk_'.$k][0].'.'.$pa['bk_'.$k][1];

                $param[$s]['bk_'.$k] = $pa['bk_'.$k];
                }
            }
            }
            $s++;
        }

        return $param;
    }

    public function kadar_air_total($id_uji, $id_status){
        $this->module = "lhu_lhp";
        $this->data['halaman'] = 'Kadar Air';
        $this->data['sub_halaman'] = '';
        $this->data['id_uji'] = $id_uji;
        $this->data['id_status'] = $id_status;
        $this->data['get'] = $dget = array('id_uji'=>$id_uji, 'status'=>$id_status);
        $this->data['data'] = $this->m_disposisi->get_kadar_air_total($dget);

        if($this->input->post()){
            $data = $this->input->post();
            for($i=0; $i < count($data['id_sampel']); $i++){
            if(isset($data['kadar_air1'][$i]) && isset($data['kadar_air2'][$i])){
                $ka1 = isset($data['kadar_air1'][$i]) && $data['kadar_air1'][$i] != '' ? (float)$data['kadar_air1'][$i] : 0;
                $ka2 = isset($data['kadar_air2'][$i]) && $data['kadar_air2'][$i] != '' ? (float)$data['kadar_air2'][$i] : 0;

                $ka3 = $ka1+(((100-$ka1)/100)*$ka2);

                $param['kadar_air3'] = $ka3;
                $param['pj_air3'] = $this->uname;
                $param['id_sampel'] = $data['id_sampel'][$i];

                $status = $this->m_disposisi->set_kat($param);
            }
            }
            redirect("kadar_air_total/".$dget['id_uji']."/".$dget['status']);
        }

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

    public function deret_fosfor() {
        $this->module = "uji_analis";
        $this->data['halaman'] = 'Deret Fosfor';
        $this->data['sub_halaman'] = '';
        $this->data['vget'] = $vget = $this->input->get();
        $this->data['data'] = $this->m_disposisi->get_deret_fosfor();
        $this->render('deret_fosfor');
        }

        public function create_deret_fosfor() {
        $this->module = "uji_analis";
        $this->data['halaman'] = 'Form Deret Fosfor';
        $this->data['sub_halaman'] = '';
        $this->data['vpost'] = $vpost = $this->input->post();

        if($this->input->post()){
            $data = $this->input->post();
            $status = $this->m_disposisi->set_deret_fosfor($data);

            if($status) {
            $this->session->set_flashdata('msg', 'Data berhasil disimpan');
            } else {
            $this->session->set_flashdata('msgw', 'Data gagal disimpan');
            }
            redirect('sampel/pengajuan/deret_fosfor');
        }

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

    function verifikasi_analis(){
        $data = $this->input->get();
        if($data['status'] !== '0'){
            $param = array(
                'id_uji' => $data['id_uji'],
                'id_status' => $data['status'],
                'id_user'=> $this->id_user
            );
            $status = $this->m_disposisi->verifikasi_analis($param);
            $msg = 'Sampel Berhasil Dianalisis';
        }

        if($status){
            $this->session->set_flashdata('msg', $msg);
        }else{
            $msg = 'Sampel gagal disiapkan';
            $this->session->set_flashdata('msgw', $msg);
        }
        redirect("sampel/pengajuan");
    }

    function tolak_deputi() {
        $data = $this->input->post();
        $status = $this->m_disposisi->tolak_deputi($data);
        if($status){
            $msg = 'Sampel Berhasil Ditolak';
            $this->session->set_flashdata('msg', $msg);
        }else{
            $msg = 'Sampel gagal ditolak';
            $this->session->set_flashdata('msgw', $msg);
        }
        redirect('sampel/pengajuan/siapkan_sampel/'.$data['id_uji2'].'/2');
        redirect("sampel/pengajuan");
    }

    function save_uji_sampel(){
        $data = $this->input->post();
        $x = count($data['id_sampel']);
        $tgl = [];
        for($i = 0; $i < $x; $i++){
            $param['id_sampel'] = $data['id_sampel'][$i];
            $param['air'] = $data['air'][$i];
            $param['abu'] = $data['abu'][$i];
            $param['protein_kasar'] = $data['protein_kasar'][$i];
            $param['lemak_kasar'] = $data['lemak_kasar'][$i];
            $param['serat_kasar'] = $data['serat_kasar'][$i];
            $param['kalsium'] = $data['kalsium'][$i];
            $param['fosfor'] = $data['fosfor'][$i];

            $param['tdn'] = $data['tdn'][$i];
            $param['ur'] = $data['ur'][$i];
            $param['af'] = $data['af'][$i];
            $param['andf'] = $data['andf'][$i];
            $param['mbm'] = $data['mbm'][$i];
            $param['betn'] = $data['betn'][$i];

            $jam_mulai = '09:00:00';
            $jam_selesai = '15:00:00';
            $tgl[] = $param['mulai_uji_air'] = strtotime($data['mulai_uji_air'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_abu'] = strtotime($data['mulai_uji_abu'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_protein_kasar'] = strtotime($data['mulai_uji_protein_kasar'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_lemak_kasar'] = strtotime($data['mulai_uji_lemak_kasar'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_serat_kasar'] = strtotime($data['mulai_uji_serat_kasar'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_kalsium'] = strtotime($data['mulai_uji_kalsium'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_fosfor'] = strtotime($data['mulai_uji_fosfor'].' '.$jam_mulai);

            $tgl[] = $param['mulai_uji_tdn'] = strtotime($data['mulai_uji_tdn'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_ur'] = strtotime($data['mulai_uji_ur'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_af'] = strtotime($data['mulai_uji_af'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_andf'] = strtotime($data['mulai_uji_andf'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_mbm'] = strtotime($data['mulai_uji_mbm'].' '.$jam_mulai);
            $tgl[] = $param['mulai_uji_betn'] = strtotime($data['mulai_uji_betn'].' '.$jam_mulai);

            $tgl[] = $param['selesai_uji_air'] = strtotime($data['selesai_uji_air'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_abu'] = strtotime($data['selesai_uji_abu'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_protein_kasar'] = strtotime($data['selesai_uji_protein_kasar'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_lemak_kasar'] = strtotime($data['selesai_uji_lemak_kasar'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_serat_kasar'] = strtotime($data['selesai_uji_serat_kasar'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_kalsium'] = strtotime($data['selesai_uji_kalsium'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_fosfor'] = strtotime($data['selesai_uji_fosfor'].' '.$jam_selesai);

            $tgl[] = $param['selesai_uji_tdn'] = strtotime($data['selesai_uji_tdn'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_ur'] = strtotime($data['selesai_uji_ur'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_af'] = strtotime($data['selesai_uji_af'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_andf'] = strtotime($data['selesai_uji_andf'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_mbm'] = strtotime($data['selesai_uji_mbm'].' '.$jam_selesai);
            $tgl[] = $param['selesai_uji_betn'] = strtotime($data['selesai_uji_betn'].' '.$jam_selesai);

            $param['last_modified_user'] = $this->id_user;

            //$status[] = $this->m_disposisi->saveAnalis($param);
            $this->db->where('id_sampel', $param['id_sampel']);
            $status[] = $this->db->update('lhu', $param);
        }

        sort($tgl);

        $tgl_mulai = $this->m_analis->update_tgl_mulai($data['id_sampel'][0], $tgl[0]);
        $tgl_selesai = $this->m_analis->update_tgl_selesai($data['id_sampel'][0], end($tgl));

        if($status){
            $msg = 'Sampel uji berhasi disimpan';
            $this->session->set_flashdata('msg', $msg);
        }else{
            $msg = 'Sampel uji gagal disimpan';
            $this->session->set_flashdata('msgw', $msg);
        }

        redirect("uji_analis/".$data['id_uji']."/".$data['status']);
    }

    function get_penanganan_sampel_arsip() {
        $type= $this->input->post('type');
        echo $this->m_disposisi->getPenangananSampelArsip($type);
    }


    function ket_lhu(){
        $data = $this->input->get();
        $status = $this->m_disposisi->set_intruksi($data);
        redirect('sampel/pengajuan/verifikasi_penyelia/'.$data['id_uji'].'/'.$data['status']);
    }

    function create_penyiapan_sampel()
    {
        $id_param = $this->input->post('check_penyiapan');
        $this->db->select("ps.*, ks.nama as kondisi_sampel, jp.nama as arsip_penanganan, ks.nama as kerja_penanganan, jg.nama as nama_grinder, d.id_sampel_kerja, d.id_sampel_arsip, d.kemasan, d.satuan_berat, d.berat_sampel, ds.bentuk_jenis, jl.nama as nama_layanan, ds.nomor_sampel");
        $this->db->where_in('ps.id_sampel', $id_param);
        //$this->db->where('d.id_penanganan_sampel', 3);
        $this->db->join('bentuk_sampel ks', 'ks.id = ps.id_bentuk_sampel', 'left');
        $this->db->join('jenis_penanganan_sampel jp', 'jp.id = ps.id_penanganan_arsip', 'left');
        $this->db->join('jenis_penanganan_sampel js', 'js.id = ps.id_penanganan_kerja', 'left');
        $this->db->join('jenis_grinder jg', 'jg.id = ps.id_grinder', 'left');
        $this->db->join('disposisi d', 'd.id_sampel = ps.id_sampel', 'left');
        $this->db->join('data_sampel ds', 'ds.id = ps.id_sampel', 'left');
        $this->db->join('permintaan_uji pu', 'pu.id = ds.id_uji');
        $this->db->join('jenis_layanan jl', 'jl.id = pu.id_layanan');
        $query  = $this->db->get('penyiapan_sampel ps');
        $result = $query->result_array();

        $this->data['bentuk_sampel'] = $this->m_master->getDataMaster('bentuk_sampel', 'all');
        $this->data['penanganan_arsip'] =$this->m_master->getDataMaster('jenis_penanganan_sampel', 'arsip');
        $this->data['penanganan_kerja'] = $this->m_master->getDataMaster('jenis_penanganan_sampel', 'kerja');
        $this->data['sampel_kerja'] = $this->m_master->getDataMaster('penyimpanan_sampel');
        $this->data['sampel_arsip'] = $this->m_master->getDataMaster('penyimpanan_sampel');
        $this->data['grinder'] = $this->m_master->getDataMaster('jenis_grinder');

        // $this->data['nomor_sampel'] = $this->m_disposisi->get_sampel_by_id($id_sampel)['id'];
        // $this->data['bentuk_jenis_sampel'] = $this->m_disposisi->get_sampel_by_id($id_sampel)['bentuk_jenis'];
        // $this->data['berat_sampel'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['berat_sampel'];
        // $this->data['satuan_berat'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['satuan_berat'];
        // $this->data['kemasan'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['kemasan'];
        // $this->data['id_sampel_kerja'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['id_sampel_kerja'];
        // $this->data['id_sampel_arsip'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['id_sampel_arsip'];

        $this->data['data'] = $result;
        $this->render('create_penyiapan_sampel');
    }

    public function simpan_penyiapan_sampel()
    {
        $ipost = $this->input->post();
        $nomor_batch = strtotime(date('Y-m-d H:i:s'));
        tampil_array($ipost);
        foreach($ipost['id_sampel'] as $ikey => $id){
            $data1 = [
                'nomor_batch' => $nomor_batch,
                'tanggal_penyiapan' => $ipost['tanggal_penyiapan'][0],
                'jenis_sampel' => $ipost['jenis_sampel'][0],
                'pukul_mulai' => $ipost['pukul_mulai'][$ikey],
                'pukul_selesai' => $ipost['pukul_selesai'][$ikey],
                'nomor_sampel' => $ipost['nomor_sampel'][$ikey],
                'id_bentuk_sampel' => $ipost['id_bentuk_sampel'][$ikey],
                'id_penanganan_arsip' => json_encode($ipost['id_penanganan_arsip'][$ikey]),
                'id_penanganan_kerja' => json_encode($ipost['id_penanganan_kerja'][$ikey]),
                'id_grinder' => $ipost['id_grinder'][$ikey],
                'keterangan' => $ipost['keterangan'][$ikey],
                'bentuk_jenis' => $ipost['bentuk_jenis'][$ikey],
                'status_penyiapan' => 0,
                'is_acc' => 0
            ];

            if($ipost['grinder_baru'][$ikey] != ''){
                $this->db->insert('jenis_grinder', ['nama'=>$ipost['grinder_baru'][$ikey]]);
                $id_grinder_baru = $this->db->insert_id();
                $this->db->where('id', $id_grinder_baru);
                $this->db->update('jenis_grinder', ['urutan'=>$id_grinder_baru]);
                $data1['id_grinder'] = $id_grinder_baru;
            }

            $id_uji = $this->db->get_where('data_sampel', ['id'=>$id])->row_array()['id_uji'];
            $this->db->where('id', $id_uji);
            $this->db->update('permintaan_uji', ['id_status'=>5]);
            
            $status = $this->m_disposisi->penyiapan_sampel($data1, $id);
        }
        redirect('penyiapan_uji');
    }

    public function riwayat_penyiapan()
    {
        $this->data['halaman'] = "Riwayat Penyiapan Sampel";
        $this->data['sub_halaman'] = '';

        $this->data['batch'] = $this->m_disposisi->get_batch_penyiapan(false, 1);

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

    public function verifikasi_penyiapan_sampel()
    {
        $this->data['halaman'] = "ACC Penyiapan Sampel";

        $is_acc = '0';
        $this->data['batch'] = $this->m_disposisi->get_batch_penyiapan($is_acc);
        $this->render('verifikasi_penyiapan_sampel');
    }

    public function acc_penyiapan()
    {
        $ipost = $this->input->post();
        $data = [
            'is_acc' => $ipost['is_acc'],
            'ket_koreksi' => $ipost['ket_koreksi']
        ];
        $this->db->where('nomor_batch', $ipost['nomor_batch']);
        $this->db->update('penyiapan_sampel', $data);

        $this->db->select('pu.id as id_uji');
        $this->db->join('data_sampel ds', 'ds.id = ps.id_sampel', 'left');
        $this->db->join('permintaan_uji pu', 'pu.id = ds.id_uji', 'left');
        $this->db->where('ps.nomor_batch', $ipost['nomor_batch']);
        $data = $this->db->get('penyiapan_sampel ps')->result_array();

        $id_uji = [];
        if(count($data) > 0) {
            if($ipost['is_acc'] == 2) {
                $id_status = 6;
            }elseif($ipost['is_acc'] == 1){
                $id_status = 4;
            }
            foreach($data as $r){
                $id_uji[] = $r['id_uji'];
            }
            $this->db->where_in('id', $id_uji);
            $this->db->update('permintaan_uji', ['id_status' => $id_status]);
        }            

        redirect('sampel/pengajuan/verifikasi_penyiapan_sampel');
    }

}
