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

class AddCpptNonDokter extends CI_Controller
{

    // CONSTRUCT
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->model('M_rekamMedis');
        $this->load->model('M_apotek');
        $this->load->model('M_anamnesa');
        $this->load->helper('url');

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

    public function index()
    {
        date_default_timezone_set("Asia/Jakarta");
        $tanggal = date('Y-m-d');
        $data['judul'] = "Form CPPT";
        $data['pasien'] = $this->db->query("SELECT *, d.FirstName as namaDokter , d.LastName as lasNameDokter FROM Cases c join company cp on cp.CompanyCode = c.CompanyCode 
        join CaseDetail cd on cd.[Case ID] = c.[Case ID] 
        join CaseAdmin ca on ca.[Case ID] = c.[Case ID] 
        join Doctor d on d.DoctorID = ca.DoctorID 
        WHERE  [Intake Date] = '$tanggal'  AND CaseDetail = '1'")->result_array();

        $data['user'] = $this->db->get_where('userV2', ['username' => $this->session->userdata('username')])->row_array();
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('formCppt/addCppt', $data);
        $this->load->view('properti/footer', $data);
    }

    public function listAndForm($mr, $case)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tanggal = date('Y-m-d');
        $data['judul'] = "Form CPPT";
        $data['pasien'] = $this->db->query("SELECT *, d.FirstName as namaDokter , d.LastName as lasNameDokter FROM Cases c join company cp on cp.CompanyCode = c.CompanyCode 
        join CaseDetail cd on cd.[Case ID] = c.[Case ID] 
        join CaseAdmin ca on ca.[Case ID] = c.[Case ID] 
        join Doctor d on d.DoctorID = ca.DoctorID 
        WHERE  [Intake Date] = '$tanggal' AND CaseDetail = '1' AND c.[Case ID] = '$case'")->row_array();
         $data['datapasien'] = $this->db->query("SELECT *, d.FirstName as namaDokter , d.LastName as lasNameDokter FROM Cases c join company cp on cp.CompanyCode = c.CompanyCode 
         join CaseDetail cd on cd.[Case ID] = c.[Case ID] 
         join CaseAdmin ca on ca.[Case ID] = c.[Case ID] 
         join Doctor d on d.DoctorID = ca.DoctorID 
         WHERE c.[Case ID] = '$case'")->row_array();
        $data['user'] = $this->db->get_where('userV2', ['username' => $this->session->userdata('username')])->row_array();
        $data['countHeader'] = $this->db->query("SELECT * FROM HeaderCppt WHERE Mr = '$mr' AND Note = '1'")->num_rows();
        $data['cpptHeader'] = $this->db->query("SELECT * FROM HeaderCppt WHERE Mr = '$mr' ORDER BY IdHeader DESC")->result_array();
        extract($_GET);
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('formCppt/listAndForm', $data);
        $this->load->view('properti/footer', $data);
    }

    public function simpanCpptLama($case)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $jam = date('H:i');
        $user = $this->db->get_where('userV2', ['username' => $this->session->userdata('username')])->row_array();
        $username = $user['username'];
        $idHeader = $_POST['idHeader'];
        $dept = $user['dept'];
        $note = $_POST['note'];
        $description = $_POST['description'];
        $Subjek    = $this->input->post('Subjek');
        $Objek     = $this->input->post('Objek');
        $Asesment  = $this->input->post('Asesment');
        $Penilaian = $this->input->post('Penilaian');
        $Asuhan  = $this->input->post('Asuhan');
        $alias = $user['deskripsi'];
        $iduser    = $user['IdAlias'];

        $folderPath = "./images/uploadsign/cppt/";
        $ttd = $this->input->post('ttd');

         // 1
         $filename_1 = $case . ' - TTD1 '. $iduser;
         $image_parts_1 = explode(";base64,", $ttd);
         $image_type_aux_1 = explode("image/", $image_parts_1[0]);
         $image_type_1 = $image_type_aux_1[1];
         $image_base64_1 = base64_decode($image_parts_1[1]);
         $file_1 = $folderPath . $filename_1 . '.' . $image_type_1;
         file_put_contents($file_1, $image_base64_1);

        // --- insert detail cppt
        $usercppt = $username.'-'.$alias;
        $this->db->query("INSERT INTO DetailCppt (IdHeader, CaseId, DateDetail, TimeDetail, UserCategory, UserCreated, Description, Subjek, Objek, Asesment, Penilaian, ttd, Asuhankeperawatan) VALUES ('$idHeader', '$case', '$tgl', '$jam', '$dept', '$usercppt', '$description', '$Subjek','$Objek','$Asesment','$Penilaian','$filename_1','$Asuhan')");

        // --- update header cppt
        $this->db->query("UPDATE HeaderCppt SET Note = '$note' WHERE IdHeader = '$idHeader'");
        $this->session->set_flashdata("success", "CPPT berhasil disimpan !");
        redirect($_SERVER['HTTP_REFERER']);
    }

    public function simpanCpptBaru($case, $mr)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $jam = date('H:i');
        $user = $this->db->get_where('userV2', ['username' => $this->session->userdata('username')])->row_array();

        $username = $user['username'];
        $dept = $user['dept'];
        $note = $_POST['note'];
        $description = $_POST['description'];
        $Subjek    = $this->input->post('Subjek');
        $Objek     = $this->input->post('Objek');
        $Asesment  = $this->input->post('Asesment');
        $Penilaian = $this->input->post('Penilaian');
        $Asuhan  = $this->input->post('Asuhan');
        $alias = $user['deskripsi'];
        $iduser    = $user['IdAlias'];

        $folderPath = "./images/uploadsign/cppt/";
        $ttd = $this->input->post('ttd');

         //1
         $cek = $this->db->query("Select * from DetailCppt where CaseId = '$case' ")->num_rows();
         if ($cek > 0){
            $filename_1 = $case . ' - TTD1 '. ' - ' . $cek;
         }else{
            $filename_1 = $case . ' - TTD1 ';  
         }
         
         //$filename_1 = $case . ' - TTD1 '. $iduser;
         $image_parts_1 = explode(";base64,", $ttd);
         $image_type_aux_1 = explode("image/", $image_parts_1[0]);
         $image_type_1 = $image_type_aux_1[1];
         $image_base64_1 = base64_decode($image_parts_1[1]);
         $file_1 = $folderPath . $filename_1 . '.' . $image_type_1;
         file_put_contents($file_1, $image_base64_1);

        // --- tambah header cppt baru
        $getNomor = $this->db->query("SELECT NoUrut FROM Penomoran WHERE ModulAktivitas = 'CpptKlinik'")->row_array();
        $setId = $getNomor['NoUrut'];
        $upId = $getNomor['NoUrut'] + 1;
        $this->db->query("UPDATE Penomoran SET NoUrut = '$upId' WHERE ModulAktivitas = 'CpptKlinik'");

        // insert header cppt baru
        $this->db->query("INSERT INTO HeaderCppt (IdHeader, Mr, DateHeader, TimeHeader, Note) VALUES ('$setId' ,'$mr', '$tgl', '$jam', '$note')");

        $usercppt = $username.'-'.$alias;
        // --- insert detail cppt baru
        $this->db->query("INSERT INTO DetailCppt (IdHeader, CaseId, DateDetail, TimeDetail, UserCategory, UserCreated, Description, Subjek, Objek, Asesment, Penilaian, ttd, Asuhankeperawatan) VALUES ('$setId', '$case', '$tgl', '$jam', '$dept', '$usercppt', '$description', '$Subjek','$Objek','$Asesment','$Penilaian','$filename_1','$Asuhan')");

        $this->session->set_flashdata("success", "CPPT berhasil disimpan !");
        redirect($_SERVER['HTTP_REFERER']);
    }

    public function cetakcppt($mr,$id)
    {
        $this->load->model('M_Utama');
        $this->load->model('M_rekamMedis');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        ob_start();
        $idheader = $this->uri->segment(5);
        $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();
        $data['listDetail'] = $this->db->query("SELECT * FROM DetailCppt d WHERE IdHeader = '$idheader'")->result_array();
        $this->load->view('cetak/cetakCppt', $data);
        // $isi = ob_get_clean();

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

        // try {
        //     $width_in_mm = '80';
        //     $height_in_mm = '190';

        //     $html2pdf = new HTML2PDF('P', 'A4', 'en', false, 'ISO-8859-15', array(5, 2, 5, 2));
        //     // $html2pdf = new HTML2PDF('P', array($width_in_mm, $height_in_mm), 'en', false, 'ISO-8859-15', array(2, 10, 2, 10));
        //     $html2pdf->writeHTML($isi, isset($_GET['vuehtml']));
        //     $html2pdf->Output('CatatanCppt.pdf');
        // } catch (HTML2PDF_exception $e) {
        //     echo $e;
        //     exit;
        // }
    }

    public function cetakcpptdokter()
    {
        $this->load->model('M_Utama');
        $this->load->model('M_rekamMedis');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        ob_start();
        $id = $this->uri->segment(3);
        $idheader = $this->uri->segment(5);
        $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();
        $data['listDetail'] = $this->db->query("SELECT * FROM DetailCppt WHERE IdHeader = '$idheader'")->result_array();
        $this->load->view('cetak/cetakCppt', $data);

    }

    public function cari()
    {
        extract($_GET);
		// ---------------------------------------------------------------------
        date_default_timezone_set("Asia/Jakarta");
        $tanggal = date('Y-m-d');
       
        $data['user'] = $this->db->get_where('userV2', ['username' => $this->session->userdata('username')])->row_array();
        $data['judul'] = 'CPPT';
        // =================================================================================
        $data['pasien'] = $this->db->query("SELECT *, d.FirstName as namaDokter , d.LastName as lasNameDokter FROM Cases c join company cp on cp.CompanyCode = c.CompanyCode 
        join CaseDetail cd on cd.[Case ID] = c.[Case ID] 
        join CaseAdmin ca on ca.[Case ID] = c.[Case ID] 
        join Doctor d on d.DoctorID = ca.DoctorID 
        WHERE  cd.[Case ID] = '$src'  AND CaseDetail = '1'")->result_array();
        // =================================================================================
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('formCppt/addCppt', $data);
        $this->load->view('properti/footer', $data);
    }
}

