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

    function get_pelayuan(){
        $selectSampel = $this->db->get_where('disposisi', ['id_penanganan_sampel' => 3])->result_array();

        $this->db->trans_begin();

        $id = array();
        foreach ($selectSampel as $r) {
            $cek = $this->db->query("SELECT id FROM pelayuan_sampel WHERE id_sampel = ? AND is_active = '1'", $r['id'])->row_array();
            if(empty($cek['id_penyiapan']) && !isset($cek['id'])){
                $this->db->query("INSERT INTO pelayuan_sampel (id_sampel) VALUES(?)", $r['id']);
                $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 as nama_grinder, jl.nama as nama_layanan, ds.nomor_sampel
              FROM data_sampel ds
                LEFT JOIN pelayuan_sampel py ON py.id_sampel = ds.id AND py.is_active = '1'
                LEFT JOIN penyiapan_sampel ps ON ps.id_sampel = ds.id AND ps.is_active = '1'
                LEFT JOIN jenis_grinder jg ON jg.id = ps.id_grinder
                LEFT JOIN permintaan_uji pu ON pu.id = ds.id_uji
                LEFT JOIN jenis_layanan jl ON jl.id = pu.id_layanan
                WHERE 1 AND ds.is_active = '1' AND py.status_pelayuan = 0")->result_array();

            return $query;
        }
    }

    function get_pelayuan_riwayat($nomor_batch = 0){
        $query = $this->db->query("
            SELECT py.*, ds.bentuk_jenis, pu.tgl_penyiapan_sampel, jg.nama as nama_grinder, jl.nama as nama_layanan, ds.nomor_sampel
            FROM data_sampel ds
            LEFT JOIN pelayuan_sampel py ON py.id_sampel = ds.id AND py.is_active = '1'
            LEFT JOIN penyiapan_sampel ps ON ps.id_sampel = ds.id AND ps.is_active = '1'
            LEFT JOIN jenis_grinder jg ON jg.id = ps.id_grinder
            LEFT JOIN permintaan_uji pu ON pu.id = ds.id_uji
            LEFT JOIN jenis_layanan jl ON jl.id = pu.id_layanan
            WHERE 1 AND ds.is_active = '1' AND py.status_pelayuan = 1 AND py.nomor_batch = ?", [$nomor_batch])->result_array();

        return $query;
    }

    function get_batch()
    {
        $query = $this->db->query("
            SELECT DISTINCT nomor_batch
            FROM pelayuan_sampel
            WHERE is_active = '1' AND status_pelayuan = 1
        ");
        $result = $query->result_array();
        return $result;
    }

    function detail($id_sampel){
      $param = array();
      $query = "SELECT ps.*, ds.nomor_sampel FROM pelayuan_sampel ps LEFT JOIN data_sampel ds ON ds.id = ps.id_sampel 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($data, $id_sampel){
      $this->db->where(['id_sampel'=>$id_sampel]);
      $this->db->update('pelayuan_sampel',$data);

      return 1;
    }

    function get_user_by_id($id = ''){
      return $this->db->get_where('user', ['id'=>$id])->row_array();
    }

    function update_pelayuan($data, $id_sampel)
    {
        $this->db->where('id_sampel', $id_sampel);
        $this->db->update('pelayuan_sampel', $data);
        return 1;
    }
}
