<?php

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

class Worklist_Model extends CI_Model
{
    public function cariPasien($field)
    {
        return $this->db->query("SELECT l.*, d.FirstName FROM tblLabResult l LEFT JOIN Doctor d ON l.DoctorID=d.DoctorID WHERE $field ORDER BY l.ReceiptDate DESC, l.ReceiptTime DESC");
    }

    public function getPasien()
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        return $this->db->query("SELECT l.*, d.FirstName FROM tblLabResult l LEFT JOIN Doctor d ON l.DoctorID=d.DoctorID WHERE l.ReceiptDate = '2025-05-19' ORDER BY l.ReceiptDate DESC, l.ReceiptTime DESC");
    }

    public function dataLabHeader($noLab)
    {
        return $this->db->query("SELECT l.*, d.FirstName FROM tblLabResult l LEFT JOIN Doctor d ON l.DoctorID=d.DoctorID WHERE l.LabNo = '$noLab'");
    }

    public function dataLabHeaderCetak($noLab)
    {
        return $this->db->query("SELECT l.*, pt.CompanyName, d.FirstName, d2.FirstName dokterSpk, c.[Intake Date] tanggalKunjungan, c.[Intake Time] jamKunjungan,l.Penjamin FROM tblLabResult l LEFT JOIN Doctor d ON l.DoctorID=d.DoctorID LEFT JOIN Doctor d2 ON l.DoctorLab=d2.DoctorID LEFT JOIN Cases c ON l.caseid=c.[Case ID] LEFT JOIN Company pt ON c.CompanyCode=pt.CompanyCode WHERE l.LabNo = '$noLab'");
    }

    public function dataLabDetail($noLab)
    {
        return $this->db->query("SELECT l.*, c.Bahasa kategoriLab FROM tblLabResultDetail l LEFT JOIN tblLabCategory c ON l.CategoryID=c.CategoryID WHERE l.LabNo = '$noLab'");
    }

    public function loopNormalValue($resultID, $umurHariAja, $jk)
    {
        return $this->db->query("SELECT
            ResultID,
            UmurMin,
            UmurMax,
            REPLACE(NormalUp, ',', '.') AS NormalUp,
            REPLACE(NormalBottom, ',', '.') AS NormalBottom 
            FROM
            tblLabAge 
            WHERE
            ResultID = '$resultID' 
            AND Sex = '$jk'
            AND $umurHariAja BETWEEN UmurMin 
            AND UmurMax
        ");
    }

    public function getPilihan()
    {
        return $this->db->query("SELECT * FROM tblLabPilihResult");
    }

    public function updateHasilLab($dataSend, $id)
    {
        $this->db->where("ID", $id);
        $this->db->update("tblLabResultDetail", $dataSend);
    }

    public function updateHeader($dataHeader, $noLab)
    {
        $this->db->where("LabNo", $noLab);
        $this->db->update("tblLabResult", $dataHeader);
    }

    public function kategoriLab($noLab)
    {
        return $this->db->query("SELECT r.CategoryID, c.Category FROM tblLabResultDetail r LEFT JOIN tblLabCategory c ON r.CategoryID=c.CategoryID WHERE LabNo = '$noLab' GROUP BY r.CategoryID, c.Category");
    }

    public function simpanEditNormal($edit, $id)
    {
        $this->db->where("ID", $id);
        $this->db->update('tblLabResultDetail', $edit);
    }

    public function cekHasilListLab($userID)
    {
        return $this->db->query("EXEC SP_NotifikasiHasilLab '" . $userID . "'");
    }

    public function getHasilListLab($userID)
    {
        return $this->db->query("EXEC SP_ListPasienHasilLab '" . $userID . "'");
    }
}
