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

class Mcu extends CI_Controller
{
    // CONSTRUCT
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('M_user');
        $this->load->model('M_Pendaftaran');
        $this->load->model('M_Mcu');
        $this->load->model('M_rekamMedis');
        $this->load->model('M_Utama');
        $this->load->model('M_BpjsID');
        $this->load->helper('url');

        if (!$this->session->userdata('username')) {
            redirect('login');
        }
    }

    public function index()
    {
        $data['judul'] = "Klinik";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        //==================================================================

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('Mcu/index', $data);
        $this->load->view('properti/footer', $data);
    }

    public function caripasien()
    {
        $this->load->model('M_anamnesa');
        $data['judul'] = "Klinik";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        //==================================================================
        $cari = $this->input->post('cari');
        $data['pasienmcu'] = $this->M_Mcu->PasienMcu($cari)->result();
        $data['poli'] = $this->M_anamnesa->poli()->result_array();
        $data['dokter'] = $this->M_anamnesa->dokter()->result_array();

        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('Mcu/pasienmcu', $data);
        $this->load->view('properti/footer', $data);
    }

    public function cetakmcu($id)
    {
        ob_start();
        $data['listDetail'] = $listDetail = $this->db->query("SELECT * FROM CaseDetail cd join Cases c on c.[Case ID] = cd.[Case ID] where c.[Case ID] ='$id' order by cd.jaminput DESC")->row_array();
        $data['mcu'] = $this->db->query("Select * From Cases c inner join Patient p on c.PatientCode = p.PatientCode where c.[Case ID] = '$id'")->row_array();
        $data['utama'] = $this->db->query("SELECT * FROM Utama")->row_array();

        $data['id'] = $this->db->query("SELECT C.[Case ID] AS ID, C.PatientCode AS MR, C.Description AS Nama, C.[Intake Date] AS Kunjungan, P.DOB AS TTL, D.[FirstName] AS Dokter, CA.Unit AS Poli, C.CompanyCode FROM Cases C, CaseAdmin CA, Doctor D, Patient P WHERE C.[Case ID] = '$id' AND C.[Case ID]=CA.[Case ID] AND CA.[DoctorID] = D.[DoctorID] AND C.[PatientCode]=P.[PatientCode]")->row_array();

        //Laborat
        //  ==========================================================================
        $data['viewlab'] = $this->db->query("
            SELECT 
                td.LabNo,
                td.ResultID,
                td.Bahasa,
                td.ResultValue,
                td.Normalvalue,
                LC.CategoryID,
                LC.Bahasa AS BahasaLC
            FROM tblLabResult tr
            JOIN tblLabResultDetail td ON tr.LabNo = td.LabNo
            JOIN tblLabCategory LC ON td.CategoryID = LC.CategoryID
            WHERE tr.caseid = '$id' and LC.CategoryID != 18
            ORDER BY LC.Bahasa, td.Bahasa
        ")->result_array();

        $data['viewlabnarkoba'] = $this->db->query("
            SELECT 
                td.LabNo,
                td.ResultID,
                td.Bahasa,
                td.ResultValue,
                td.Normalvalue,
                LC.CategoryID,
                LC.Bahasa AS BahasaLC
            FROM tblLabResult tr
            JOIN tblLabResultDetail td ON tr.LabNo = td.LabNo
            JOIN tblLabCategory LC ON td.CategoryID = LC.CategoryID
            WHERE tr.caseid = '$id' and LC.CategoryID = 18
            ORDER BY LC.Bahasa, td.Bahasa
        ")->result_array();

     $data['group'] = $this->db->query("
            SELECT DISTINCT LC.CategoryID, LC.Bahasa AS BahasaLC
            FROM tblLabResult LR
            JOIN tblLabResultDetail LRD ON LR.LabNo = LRD.LabNo
            JOIN tblLabCategory LC ON LRD.CategoryID = LC.CategoryID
            WHERE LR.caseid = '$id' and LC.CategoryID != 18
            ORDER BY LC.Bahasa
        ")->result_array();

    $data['groupnarkoba'] = $this->db->query("
            SELECT DISTINCT LC.CategoryID, LC.Bahasa AS BahasaLC
            FROM tblLabResult LR
            JOIN tblLabResultDetail LRD ON LR.LabNo = LRD.LabNo
            JOIN tblLabCategory LC ON LRD.CategoryID = LC.CategoryID
            WHERE LR.caseid = '$id' and LC.CategoryID = 18
            ORDER BY LC.Bahasa
        ")->result_array();

        $laporan1 = $this->load->view('Mcu/cetakmcu', $data, true);
        $laporan2 = $this->load->view('Mcu/laborat', $data, true);
        $laporan3 = $this->load->view('Mcu/cetaknarkoba', $data, true);
        // gabungkan, beri pemisah halaman
        $isi = $laporan1 . $laporan2 . $laporan3;

        require_once('./aset/html2pdf/html2pdf.class.php');

        try {
            $html2pdf = new HTML2PDF('P', 'A4', 'en', false, 'UTF-8', array(10, 10, 10, 10));
            $html2pdf->writeHTML($isi);
            $html2pdf->Output('MCU-' . $id . '.pdf');
        } catch (HTML2PDF_exception $e) {
            echo $e;
            exit;
        }
    }

    public function updatemcu()
    {
        $CaseID = $this->input->post('CaseID');
        $BeratBadan = $this->input->post('BeratBadan');
        $TinggiBadan = $this->input->post('TinggiBadan');
        $Tensi = $this->input->post('Tensi');
        $Suhu = $this->input->post('Suhu');
        $Keluhan = $this->input->post('Keluhan');
        $Diastole = $this->input->post('Diastole');
        $respRate = $this->input->post('respRate');
        $heartRate = $this->input->post('heartRate');
        $Unit = $this->input->post('Unit');
        $DoctorID = $this->input->post('DoctorID');
        $bmi = $this->input->post('bmi');
        $butawarna = $this->input->post('butawarna');
        $keluhansebelum = $this->input->post('keluhansebelum');
        $keluhansekarang = $this->input->post('keluhansekarang');

        $this->db->query("UPDATE CaseDetail SET BeratBadan = '$BeratBadan', TinggiBadan = '$TinggiBadan', Tensi = '$Tensi', Diastole = '$Diastole', Suhu = '$Suhu', Keluhan = '$Keluhan', Unit = '$Unit', DoctorID = '$DoctorID', respRate = '$respRate', heartRate = '$heartRate',bmi = '$bmi',ButaWarna = '$butawarna', RiwayatSebelum = '$keluhansebelum', RiwayatSekarang = '$keluhansekarang' WHERE [Case ID] = '$CaseID'");

        $this->session->set_flashdata('success', 'Data berhasil diperbarui.');
        redirect('mcu/index');
    }
}
