<?php

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

class M_master extends CI_Model {

    protected $idUser = null;

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

    function getDataMaster($table, $type = null) {
        $param = array();
        $query = "SELECT * FROM $table WHERE is_active = '1'";

        if($table === 'bentuk_sampel') {
            $param[] = 'Padat';
            $query .= " AND detail = ?";
        }

        if($table === 'jenis_penanganan_sampel') {
            $param[] = $type;
            $query .= " AND detail = ?";
        }

        if($table === 'jabatan'){
          $query .= " AND status = '0'";
        }

        return $this->db->query($query, $param)->result_array();
    }

    function getCountUji(){
        $tgl_mulai = date('Y-m-1 00:00:00');
        $tgl_selesai = date('Y-m-d 23:59:59');
        $query = $this->db->query("SELECT count(id_uji) as jumlah FROM permintaan_uji WHERE is_active = '1' AND tgl_pukul_registrasi BETWEEN ? AND ?", array($tgl_mulai, $tgl_selesai))->row_array();
        return $query;
    }


    function getMetodeUji($x=null){
        return $this->db->query("SELECT id_jenis, nama_jenis, detail FROM metode_uji WHERE id_jenis = ? AND is_active = '1'", $x)->result_array();
    }

    function getParameterUji($id) {
        return $this->db->query('SELECT nama_jenis FROM parameter_uji WHERE id_jenis = ? AND is_active = "1"', $id)->row_array()['nama_jenis'];
    }

    function updateStatus($id_uji, $status, $type = null, $data = null){
    	$this->db->trans_begin();
        if($type != 'ditolak'){
    	   $this->db->query("UPDATE permintaan_uji SET id_status = ?, kaji_ulang_permintaan = ? WHERE id_uji = ?", array($status, $data, $id_uji));
        }else {
          $data = null;
          $this->db->query("UPDATE permintaan_uji SET id_status = ?, kaji_ulang_permintaan = ? WHERE id_uji = ?", array($status, $data, $id_uji));
        }

    	if($type === 'mt1'){
	    	$query = $this->db->query("SELECT id_sampel
	    		FROM data_sampel WHERE id_uji = ? AND is_active = '1'", $id_uji)->result_array();

	    	if(isset($query)){
	    		foreach ($query as $q) {
	    			$this->db->query("UPDATE disposisi SET is_active = ? WHERE id_sampel = ?", array('1', $q['id_sampel']));
	    		}
	    	}
    	}

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


   function setDisposisi($id_uji){

   }

}
