<?php

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

class Pendaftaran extends MY_Controller {

    protected $template = "app";
    protected $module = "pendaftaran";

    protected $per_page = 20;

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

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

        $total_data = $this->m_pendaftaran->getDaftar(true);
        $this->data['total_data'] = $total_data;

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

        // PAGINATION
        $config['base_url'] = base_url() . '/permintaan_uji/';
		$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_pendaftaran->getDaftar(false, $config["per_page"], $data['page']);
		$this->data["link_paging"] = $this->pagination->create_links();
        // END PAGINATION

        $this->data['data_filter']['status'] = $this->m_master->getDataMaster('status_sampel');
        $this->data['data_filter']['jenis_layanan'] = $this->m_master->getDataMaster('jenis_layanan');
        //$this->data['data']     = $this->m_pendaftaran->getDaftar(false, $per_page, $page);

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

    public function detail_uji_sampel($id_uji){
        $this->data['halaman']  = 'Daftar Sampel';
        $this->data['id_uji']   = $id_uji;
        $this->data['data']     = $this->m_pendaftaran->getSampel($id_uji);
        $this->data['data_uji']       = $this->m_pendaftaran->detailData($id_uji);
        $this->data['back_url'] = 'permintaan_uji';

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

    function set_data_uji($id=null)
    {
        $this->data['halaman']      = 'Tambah Data Uji';
        $this->data['jenis_layanan']= $this->m_master->getDataMaster('jenis_layanan');
        $this->data['jumlah_uji']   =  $this->m_pendaftaran->count_permintaan_uji(date('Y'), date('m'))+1;

        if($id){
            $this->data['halaman']  = 'Ubah Data Uji';
            $this->data['edit']     = $this->m_pendaftaran->detailData($id);
        }

        if(!empty($this->input->get('id_ttpp'))){
            $id_uji = $this->input->get('id_ttpp');
            $this->data['id_ttpp'] = $id_uji;
            $data_uji = $this->m_pendaftaran->detailData($id_uji);
            $this->data['edit']  = $data_uji;
            
            // mmebuat nomor registrasi laboratorium
            $noreg = $data_uji['registrasi_laboratorium'];
            $ex_noreg = explode('.', $noreg);
            $noreg = $ex_noreg[1].'.'.$ex_noreg[2];
            $c_puji = $this->data['jumlah_uji'] + 1;
            $c_puji = (strlen($c_puji) < 2) ? '0'.$c_puji : $c_puji;
            $noreg = $c_puji.'.'.$noreg;

            $this->data['noreg'] = $noreg;
            $this->session->set_userdata('data_uji', $data_uji);
        }

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

    function set_uji($id = null)
    {
        $data = $this->input->post();
        if($id){
            $this->m_pendaftaran->set_uji($data, $id);
            $this->session->set_flashdata('msg', 'Data uji berhasil diubah');

        }elseif((!empty($this->session->userdata('data_uji')))){
            $data2 = $this->session->userdata('data_uji');
            $data2['registrasi_laboratorium'] = $data['registrasi_laboratorium'];
            $data3 = [];
            foreach($data2 as $key => $d){
                if(!in_array($key, ['id', 'last_modified_time', 'jenis_layanan', 'status_sampel', 'detail_status', 'singkatan_layanan'])){
                    $data3[$key] = $d;
                }
            }

            $this->m_pendaftaran->set_uji($data3);
            $this->session->unset_userdata('data_uji');
            $this->session->set_flashdata('msg', 'Data uji berhasil ditambahkan');
        }else{
            $data['penerima_sampel'] = $this->session->userdata('id_user');
            $this->m_pendaftaran->set_uji($data);
            $this->session->set_flashdata('msg', 'Data uji berhasil ditambahkan');
        }
        redirect('permintaan_uji');
    }

    function delete_uji($id)
    {
        $this->m_pendaftaran->delete('permintaan_uji', $id);
        redirect('permintaan_uji');
    }

    public function set_data_sampel($id_uji=null, $id=null)
    {
        $this->data['penanganan_sampel']= $this->m_master->getDataMaster('penanganan_sampel');
        $this->data['berat_sampel']     = $this->m_master->getDataMaster('penanganan_sampel');
        $this->data['sampel_kerja']     = $this->m_master->getDataMaster('penyimpanan_sampel');
        $this->data['sampel_arsip']     = $this->m_master->getDataMaster('penyimpanan_sampel');
        $this->data['jenis_pengujian']  = $this->m_master->getDataMaster('parameter_uji');
        $this->data['klasifikasi']      = $this->m_master->getDataMaster('klasifikasi');
        $this->data['jenis_sampel']     = $this->m_master->getDataMaster('jenis_sampel');
        $this->data['gol_sampel']       = $this->m_master->getDataMaster('gol_jenis_sampel');
        $this->data['detail_uji']       = $this->m_pendaftaran->detailData($id_uji);
        $this->data['id_uji']           = $id_uji;
        $cek_sampel                     = $this->m_pendaftaran->cek_sampel($id_uji);
        
        if(count($cek_sampel) < 1){
            $cek_sampel =  $this->m_pendaftaran->cek_sampel($this->data['detail_uji']['id_ttpp']);
        }

        if($cek_sampel && !$id){
            $this->data['temporarySampel'] = $d = $this->m_pendaftaran->detailSampel($cek_sampel[0]['id']);
        }

        $d['id_uji'] = $id_uji;
        if($id){
            $this->data['editSampel'] = $d = $this->m_pendaftaran->detailSampel($id);
            $this->data['halaman'] = 'Edit Data Sampel';
        }else {
            $this->data['halaman'] = 'Tambah Data Sampel';
        }

        $this->data['data_nomor_uji']= $this->m_pendaftaran->get_nomor_uji($d['id_uji']);

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

    function set_sampel($id_uji=null, $id=null){
        $data = $this->input->post();
        $data['id_uji']          = !empty($this->input->post('id_uji')) ? $this->input->post('id_uji') : $id_uji;
        $data['jenis_pengujian'] = str_replace('|','', json_encode($data['jenis_pengujian']));

        if($id){
            $this->m_pendaftaran->set_sampel($data, $id);
            $this->session->set_flashdata('msg', 'Data uji berhasil diubah');
        }else{
            $this->m_pendaftaran->set_sampel($data);
            $this->session->set_flashdata('msg', 'Data uji berhasil ditambahkan');
        }
        redirect('permintaan_uji/sampel/'.$id_uji);
    }

    function delete_sampel($id_uji, $id)
    {
        $this->m_pendaftaran->delete('data_sampel', $id);
        redirect('permintaan_uji/sampel/'.$id_uji);
    }

    public function detail() {
        $this->data['halaman'] = "Detail Data Sampel";
        $this->render('detail');
    }

    public function verifikasi($id, $status=null){
        $this->data['halaman']      = "Verifikasi Sampel";
        $this->data['sub_halaman']  = '';
        $this->data['id_uji']       = $id;
        $this->data['data']         = $this->m_pendaftaran->getNpps($id);
        $this->data['data_uji']     = $this->m_pendaftaran->detailData($id);

        if($status == 'ya'){
            $kaji_ulang = str_replace('|', '', json_encode($this->input->post('kaji_ulang')));
            $hasil = $this->m_master->updateStatus($id, 3, 'mt1', $kaji_ulang);
            $msg = 'Permintaan uji dan sampel berhasil diverifikasi';

            $this->session->set_flashdata('msg', $msg);
            redirect("permintaan_uji");
        }
        if($status == 'tidak'){
            $this->db->where('id', $id);
            $this->db->update('permintaan_uji', ['note_verifikasi'=>$this->input->post('note_verifikasi')]);
            $hasil = $this->m_master->updateStatus($id, 1);
            $msg = 'Permintaan uji dan sampel ditolak, mohon cek catatan verifikasi';  
            
            $this->session->set_flashdata('msg', $msg);
            redirect("permintaan_uji");
        }

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

    public function ajukan_sampel(){
        $this->data['id_uji'] = $id_uji = $this->input->get('id_uji');

        if(!empty($this->input->get('status'))){
            $status = $this->input->get('status');
            $status = $this->m_master->updateStatus($id_uji, $status); // (id_uji, id_status, type)

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

    function get_jenis_sampel(){
        $data = $this->input->post();

        echo $this->m_pendaftaran->getJenisSampel($data);
    }

    function get_jenis_pengujian(){
        $id = $this->input->post('id_pengujian[]');
        echo $this->m_pendaftaran->getJenisPengujian($id);
    }

    function cek_nomor_lab()
    {
        $nomor_lab = $this->input->post('nomor_lab');
        $result = $this->db->get_where('permintaan_uji', ['registrasi_laboratorium'=>$nomor_lab, 'is_active'=> '1'])->result_array();
        if(count($result) > 0){
            echo 1;
        }else {
            echo 0;
        }
    }

    function cek_nomor_sampel()
    {
        $nomor_sampel = $this->input->post('nomor_sampel');
        $tahun = $this->input->post('tahun');
        $nomor_sampel_edit = $this->input->post('nomor_sampel_edit');
        $result = $this->db->get_where('data_sampel', ['nomor_sampel'=>$nomor_sampel, 'is_active'=> '1', 'tahun'=>$tahun]);
        $result = $result->result_array();
        if(count($result) > 0){
            if($nomor_sampel_edit == $result[0]['nomor_sampel']){
                echo 0;
            }else{
                echo 1;
            }
        }else {
            echo 0;
        }
    }

    function sampel_sudah_diterima($id_uji, $is_receive){
        $this->db->where('id', $id_uji);
        $this->db->update('permintaan_uji', ['is_receive'=> $is_receive]);
        redirect('sampel/pengajuan/siapkan_sampel/'.$id_uji.'/13');
    }
}
