<?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->id_user = $this->session->userdata("id_group");
    }

    public function get_field($table = '', $data = [])
    {
        $except_data = ['id', 'is_active', 'last_modified_time'];
        if($data){
            foreach($data as $d){
                $except_data[] = $d;
            }
        }

        if($table != ''){
            $query1 = $this->db->query("DESC ".$table)->result_array();
            $query2 = [];
            $x=0;
            for($i=0; $i<count($query1); $i++){
                if(!in_array($query1[$i]['Field'],  $except_data)){
                    $query2[$x] = $query1[$i]['Field'];
                    $x++;
                }
            }
            return $query2;

        }else{
            return 'Not Table';
        }
    }

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

        if($table === 'bentuk_sampel') {
            if($type != 'all') {
                $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) 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, nama, detail FROM metode_uji WHERE id = ? AND is_active = '1'", $x)->result_array();
    }

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

    function updateStatus($id, $id_status, $type = null, $kaji_ulang = null){
        $param = [];
        $param['id_status'] = $id_status;
        if($kaji_ulang){
            $param['kaji_ulang_permintaan'] = $kaji_ulang;
        }
        $id_ttpp= $this->db->get_where('permintaan_uji', ['id'=>$id])->row_array()['id_ttpp'];

        $this->db->where('id_ttpp', $id_ttpp);
        $this->db->update('permintaan_uji', $param);

    	if($type === 'mt1'){
            $this->db->select('id');
            $data_sampel = $this->db->get_where('data_sampel', ['id_uji'=>$id, 'is_active'=>'1'])->result_array();

            if($data_sampel){
                foreach($data_sampel as $ds){
                    $this->db->where('id', $ds['id']);
                    $this->db->update('disposisi', ['is_active'=>'1']);
                }
            }
        }

        return 1;
    }

}
