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

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

      if(isset($data['id_uji'])){
          $this->data['data'] = $this->model->get_pelayuan($data['id_uji']);
          $this->data['id_uji'] = $data['id_uji'];
          $this->data['status'] = $data['status'];

          if(!empty($data['status'])){
              $this->data['idStatus'] = $data['status'];
          }
      }

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

    public function create_edit(){
      $this->data['halaman'] = "Form Data Pelayuan";
      $this->data['sub_halaman'] = '';
      $vget = $this->input->get();

      $this->data['edit'] = $this->model->detail($vget['id_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'] = date('Y-m-d H:i', strtotime($tgl_1)).':00';
        $data['tgl_pukul_setelah_dioven'] = date('Y-m-d H:i', strtotime($tgl_2)).':00';
        $data['last_modified_user'] = $this->idUser;

        //print('<pre>'.print_r($data, true).'</pre>'); die();

        $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($param_key, $data);

        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('sampel/pelayuan_sampel?id_uji='.$vget['id_uji'].'&status='.$vget['status']);

      }

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