<?php

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

class Pelayuan_sampel extends MY_Controller {

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

    protected $per_page = 20;

    public function __construct() {
        parent::__construct();
        $this->load->model('m_pelayuan_sampel', 'model');
        $this->load->model('m_disposisi');
    }

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

        $this->data['data'] = $this->model->get_pelayuan();

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

    public function create_edit($id_uji, $id_sampel, $id_status){
      $this->data['halaman'] = "Form Data Pelayuan";
      $this->data['sub_halaman'] = '';
      
      $this->data['edit'] = $this->model->detail($id_sampel);
      $this->data['user'] = $this->model->get_user_by_id($this->id_user);
      $this->data['berat_sampel'] = $this->m_disposisi->get_disposisi_by_id($id_sampel)['berat_sampel'];

      if($this->input->post()){
        $data = $this->input->post();
        $tgl_1 = date('d-m-Y H:i', strtotime(str_replace('/','-',$data['tgl_pukul_sebelum_dioven'])));
        $tgl_2 = date('d-m-Y H:i', strtotime(str_replace('/','-',$data['tgl_pukul_setelah_dioven'])));
        $data['tgl_pukul_sebelum_dioven'] = strtotime($tgl_1.':00');
        $data['tgl_pukul_setelah_dioven'] = strtotime($tgl_2.':00');
        $data['last_modified_user'] = $this->id_user;

        $data = array_reverse($data);
        $data_key = array_keys($data);

        $param_key = '';
        foreach ($data_key as $r) {
          if($r != 'id_sampel') $param_key .= $r.'= ?, ';
        }

        $param_key = substr($param_key,0,-2);

        $status = $this->model->set_data($data, $id_sampel);

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

        redirect('pelayuan_sampel/'.$id_uji.'/'.$id_status);

      }

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

    public function create_edit2()
    {
        $this->data['halaman'] = "Form Pelayuan Sampel";
        $ipost = $this->input->post();

        $this->data['id_uji'] = 1;
        $this->data['id_status'] = 4;

        $this->db->select('ps.*, ds.nomor_sampel');
        $this->db->join('data_sampel ds', 'ds.id = ps.id_sampel', 'left');
        $this->db->where_in('id_sampel', $ipost['check_pelayuan']);
        $this->data['data'] = $this->db->get('pelayuan_sampel ps')->result_array();

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

    public function update_pelayuan()
    {
        $ipost =$this->input->post();

        $tgl_pukul_sebelum_dioven = [];
        $tgl_pukul_setelah_dioven = [];
        foreach($ipost['tgl_pukul_sebelum_dioven'] as $tkey => $t)
        {
            if($tkey%2 == 1){
                $tgl_pukul_sebelum_dioven[] = $ipost['tgl_pukul_sebelum_dioven'][$tkey-1].' '.$t;
                $tgl_pukul_setelah_dioven[] = $ipost['tgl_pukul_setelah_dioven'][$tkey-1].' '.$t;
            }
        }

        $ipost['tgl_pukul_sebelum_dioven'] = $tgl_pukul_sebelum_dioven;
        $ipost['tgl_pukul_setelah_dioven'] = $tgl_pukul_setelah_dioven;

        $count = count($ipost['nomor_sampel']);
        $nomor_batch = strtotime(date('Y-m-d H:i:s'));

        for($i=0;$i<$count;$i++){
            $data = [
                'nomor_batch' => $nomor_batch,
                'status_pelayuan' => 1,
                'tgl_pukul_sebelum_dioven' => strtotime($ipost['tgl_pukul_sebelum_dioven'][$i]),
                'berat_segar' => $ipost['berat_segar'][$i],
                'tgl_pukul_setelah_dioven' => strtotime($ipost['tgl_pukul_setelah_dioven'][$i]),
                'berat_kering' => $ipost['berat_kering'][$i],
                'kadar_air' => $ipost['kadar_air'][$i],
                'nama_pelaksana' => $ipost['nama_pelaksana'][$i],
                'keterangan' => $ipost['keterangan'][$i],
            ];
            $status = $this->model->update_pelayuan($data, $ipost['id_sampel'][$i]);

            $this->db->where('id', $ipost['id_sampel'][$i]);
            $this->db->update('data_sampel', ['nomor_sampel'=>$ipost['nomor_sampel'][$i]]);
        }
        //
        redirect('pelayuan_sampel');
    }

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

        $this->data['data'] = $this->model->get_pelayuan_riwayat();
        $this->data['batch'] = $this->model->get_batch();
        $this->data['id_uji'] = 1;
        $this->data['id_status'] = 4;

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

    
  }
