<?php

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

class M_pelayuan_sampel extends CI_Model {

    protected $idGroup = null;
    protected $iduser = null;

    public function __construct() {
        parent::__construct();
        $this->idGroup = $this->session->userdata("r_idGroup");
        $this->idUser = $userId = $this->session->userdata('r_userId');
    }

    function get_pelayuan($id_uji){
        $selectSampel = $this->db->query("SELECT id_sampel FROM data_sampel WHERE id_uji = ? AND is_active = '1'", $id_uji)->result_array();
        //$cek_tgl_penyiapan = $this->db->query("SELECT tgl_penyiapan_sampel FROM permintaan_uji WHERE id_uji = ? AND is_active = '1'", $id_uji)->row_array()['tgl_penyiapan_sampel'];

        $this->db->trans_begin();
        /*
        if(!isset($cek_tgl_penyiapan)){
          $this->db->query("UPDATE permintaan_uji SET tgl_penyiapan_sampel = ? WHERE id_uji = ?", array(date('Y-m-d'), $id_uji));
        }*/

        $id = array();
        foreach ($selectSampel as $r) {
            $cek = $this->db->query("SELECT id FROM pelayuan_sampel WHERE id_sampel = ? AND is_active = '1'", $r['id_sampel'])->row_array();
            if(empty($cek['id_penyiapan']) && !isset($cek['id'])){
                $this->db->query("INSERT INTO pelayuan_sampel (id_sampel) VALUES(?)", $r['id_sampel']);
                $id[] = $this->db->insert_id();
            }else{
                $id[] = $cek['id'];
            }
        }

        if($this->db->trans_status() === FALSE){
            $this->db->trans_rollback();
            return FALSE;
        }else {
            $this->db->trans_commit();
            $query = $this->db->query("SELECT py.*, ds.bentuk_jenis, pu.tgl_penyiapan_sampel, jg.nama_jenis as nama_grinder, jl.nama_jenis as nama_layanan
              FROM data_sampel ds
                LEFT JOIN pelayuan_sampel py ON py.id_sampel = ds.id_sampel AND py.is_active = '1'
                LEFT JOIN penyiapan_sampel ps ON ps.id_sampel = ds.id_sampel AND ps.is_active = '1'
                LEFT JOIN jenis_grinder jg ON jg.id_jenis = ps.id_jenis_grinder
                LEFT JOIN permintaan_uji pu ON pu.id_uji = ds.id_uji
                LEFT JOIN jenis_layanan jl ON jl.id_jenis = pu.id_jenis_layanan
                WHERE ds.id_uji = ? AND ds.is_active = '1'", $id_uji)->result_array();

            return $query;
        }
    }

    function detail($id_sampel){
      $param = array();
      $query = "SELECT ps.* FROM pelayuan_sampel ps WHERE ps.id_sampel = ? AND ps.is_active = '1'";
      $param[] = $id_sampel;

      $result = $this->db->query($query, $param);
      return $result->row_array();
    }

    function set_data($param_key, $param){
      $this->db->trans_begin();

      $query = "UPDATE pelayuan_sampel SET ".$param_key." WHERE id_sampel = ?";
      $this->db->query($query, $param);

      if($this->db->trans_status() == false){
        $this->db->trans_rollback();
        return false;
      }else{
        $this->db->trans_commit();
        return true;
      }
    }
}
