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

    public function index() {
        $this->data['halaman'] = "Pendaftaran Sample";
        $this->data['sub_halaman'] = '';
        $this->data['idGroup'] = $this->session->userdata("r_idGroup");

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

        //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_pendaftaran->getDaftar(false, $per_page, $page);
        //END CONFIG PAGINATION

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

    public function detail_uji_sampel(){
        $param = $this->input->get();
        $this->data['idUji'] = $param['id_uji'];
        $this->data['data'] = $this->m_pendaftaran->getSampel($param['id_uji']);
        $this->render('detail_uji_sampel');
    }

    public function create_edit() {
        $idUji = $this->input->get('id_uji');
        $idSampel = $this->input->get('id_sampel');
        $method = $this->input->get('method');
        $type = $this->input->get('type');

        $this->data['sub_halaman'] = '';
        $this->data['jenis_layanan'] = $this->m_master->getDataMaster('jenis_layanan');
        $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['jumlahUji'] =  intval($this->m_master->getCountUji()['jumlah']+1);
        $this->data['jenis_sampel'] = $this->m_master->getDataMaster('jenis_sampel');
        $this->data['gol_sampel'] = $this->m_master->getDataMaster('gol_jenis_sampel');

        if($this->input->post()){
            $data = $this->input->post();
            $this->proses_create_edit($type, $data);
        }

        if(isset($idUji)) $this->data['edit'] = $this->m_pendaftaran->detailData($idUji);

        if($type === 'uji') {
            $this->data['halaman'] = 'Tambah Data Uji';
            if(isset($idUji)) $this->data['halaman'] = 'Edit Data Uji';
            $this->render('create_data_uji');
        }
        if($type === 'sampel') {
            $this->data['halaman'] = 'Tambah Data Sampel';
            if($method == 'addHistory'){
              $this->data['add_history'] = true;
              $this->data['editSampel'] = $this->m_pendaftaran->detailSampel($idSampel);
            }
            if(isset($idSampel) && !isset($method)) {
                $this->data['halaman'] = 'Edit Data Sampel';
                $this->data['editSampel'] = $this->m_pendaftaran->detailSampel($idSampel);
            }
            $this->render('create_data_sampel');
        }
    }

    function proses_create_edit($type, $data){
        if($type === 'uji') $status = $this->m_pendaftaran->setUji($data);
        if($type === 'sampel') {
            $x = count($data['jenis_pengujian']);
            $jenis_pengujian = '';
            for($i=0;$i<$x;$i++) {
                if($data['jenis_pengujian'][$i] == '1|2|3|4|5'){
                  $proksimat = $data['jenis_pengujian'][$i];
                }
                $jenis_pengujian .= $data['jenis_pengujian'][$i].'|';
            }

            if(!empty($proksimat)){
              $array_uji = array('1|','2|','3|','4|','5|');
              foreach ($array_uji as $r) {
                if(strpos($jenis_pengujian, $r) !== false){
                  $jenis_pengujian = str_replace($r, '', $jenis_pengujian);
                }
              }
              $jenis_pengujian = $proksimat.'|'.$jenis_pengujian;
            }

            $data['jenis_pengujian'] = $jenis_pengujian;

            $status = $this->m_pendaftaran->setDaftar($data);
        }

        if($status){
            if(!isset($data['id_sampel_hidden']))$this->session->set_flashdata('msg', 'data '. $type. ' berhasil ditambahkan');
            else $this->session->set_flashdata('msg', 'data '. $type. ' berhasil diubah');
        }else{
            $this->session->set_flashdata('msg', 'data '. $type. ' gagal ditambahkan');
        }

        if($type === 'sampel') redirect('sampel/pendaftaran/detail_uji_sampel?id_uji='.$data['id_uji']);
        if($type === 'uji') redirect('sampel/pendaftaran/');
    }

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

    public function verifikasi(){

        $this->data['halaman'] = "Verifikasi Sampel";
        $this->data['sub_halaman'] = '';
        $this->data['id_uji'] = $id_uji = $this->input->get('id_uji');

        $this->data['data'] = $this->m_pendaftaran->getNpps($id_uji);


        if(!empty($this->input->get('status')) && $this->input->get('status') === 'ya'){
            $kaji_ulang = null;
            if(!empty($this->input->get('kaji_ulang'))) {
                $kaji_ulang = '';
                $x = count($this->input->get('kaji_ulang'));
                for ($i=0; $i < $x; $i++) {
                    $kaji_ulang .= $this->input->get('kaji_ulang')[$i].'|';
                }
            }
            $status = $this->m_master->updateStatus($id_uji, 3, 'mt1', $kaji_ulang); // (id_uji, id_status, type)

            if($status){
                $msg = 'Sampel berhasil diverifikasi';
                $this->session->set_flashdata('msg', $msg);
            }else{
                $msg = 'Sampel gagal diverifikasi';
                $this->session->set_flashdata('msgw', $msg);
            }
            redirect("sampel/pendaftaran");
        }else if (!empty($this->input->get('status')) && $this->input->get('status') === 'tidak'){
            $status = $this->m_master->updateStatus($id_uji, 14, 'ditolak'); // (id_uji, id_status, type)

            $msg = 'Sampel ditolak';
            $this->session->set_flashdata('msgw', $msg);

            redirect("sampel/pendaftaran");
        }

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

}
