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

class Formkomunikasi 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_rekamMedis');
        $this->load->helper('url');

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

    public function cetakformkomunikasi($id)
    {
        ob_start();
        $this->load->model('M_Utama');
        $data['abdulradjak'] = $this->M_Utama->abdulradjak()->row_array();
        $abdulradjak = $this->M_Utama->abdulradjak()->row_array();
        $pageAbdulRadjak = $abdulradjak['NamaPerusahaan'];
        $data['judul'] = $pageAbdulRadjak;

        $data['hasilLaboratory'] = $this->M_rekamMedis->hasilLaboratory($id)->row_array();
        $data['HeaderLab'] = $this->M_rekamMedis->HeaderLab($id)->row_array();
        $data['viewlab'] = $this->db->query("SELECT td.LabNo,td.ResultID,td.Bahasa,td.ResultValue,td.Normalvalue
        from tblLabResult tr 
        JOIN tblLabResultDetail td on tr.LabNo = td.LabNo
        where tr.caseid = '$id'")->result_array();
        $data['hasil'] = $this->db->query("Select c.PatientCode,c.[Case ID],c.Description,c.[Intake Date],c.[Intake Time],p.DOB,
        p.DOBPlace,p.HomeAddress1,er.poli,er.keluhan_utama,er.diagnosa_primer,i.DESCRPTION,er.pemeriksaan_fisik,i.ICD_CODE,
        ep.Pengkajian,ep.Recomendasi,ep.Tulislengkapadvisdokter,ep.Bacaulangdokter,ep.Kofirmasi,er.tindak_lanjut,ep.Tanggal,ep.Jam
        from Cases c Inner Join Patient p on c.patientcode = p.PatientCode
        inner join erm_pemeriksaan_fisik er on er.case_id = c.[Case ID]
        inner join ICD9 i on i.ICD_CODE = er.diagnosa_primer
        inner join erm_Pengkajiansbar ep on ep.CaseID = er.case_id
        where ep.CaseID = '$id'")->result_array();

        $data['ttd'] = $this->db->query("Select * From erm_ttdkomunikasi where caseID = '$id'")->row_array();

        $this->load->view('form/cetakformkomunikasi', $data);

    }

    public function resume($id)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $data['judul'] = "Monitoring Tidakan Anestesi dan Bedah";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['unit'] = $this->db->query("SELECT * FROM Utama")->row_array();
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('form/Komunikasiefektif', $data);
        $this->load->view('properti/footer', $data);
    }

    public function proses($id) 
    {
        $pengkajian = $this->input->post('pengkajian');
        $recomendation = $this->input->post('recomendation');
        $tulisdokter = $this->input->post('tulisdokter');
        $bacaulang = $this->input->post('bacaulang');
        $konfirmasi = $this->input->post('konfirmasi');
        $tgl = date('Y-m-d');
        $jam = date('H:i');

        $this->db->query(
            "INSERT INTO erm_Pengkajiansbar (
            CaseID, 
            Pengkajian,
            Recomendasi,
            Tulislengkapadvisdokter,
            Bacaulangdokter,
            Kofirmasi,
            Tanggal,
            Jam
            )VALUES (
                '$id', 
                '$pengkajian',
                '$recomendation',
                '$tulisdokter',
                '$bacaulang',
                '$konfirmasi',
                '$tgl',
                '$jam'
            )"
        );

        $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert"> Data Berhasil Tersimpan.</div>');
        redirect(base_url('Formkomunikasi/resume/' . $id));
    }

    public function signature_1()
    {
        $data['judul'] = "Form Asesment";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $this->load->view('Form/signaturekomunikasi1', $data);
    }

    public function simpansignatur1()
    {
        date_default_timezone_set("Asia/Jakarta");
        $data_uri = $_POST['foto'];
        $CaseID   = $_POST['caseid'];
        $namajelas   = $_POST['namajelas'];
        $tgl1 = date('Y-m-d');
        $waktu1 = date('H:i');
        $image_parts_1 = explode(",", $data_uri)[1];
        $image_type_aux_1 = explode("image/", $image_parts_1[0]);
        $decoded_image = base64_decode($image_parts_1);
        file_put_contents("./images/uploadsign/komunikasi/sig-".$CaseID."-1.png", $decoded_image);
        $ttd_1 = "sig-".$CaseID."-1.png";
       
        $this->db->query("INSERT INTO erm_ttdkomunikasi (caseID, namajelas_1, tandatangan_1, tgl1, waktu1) VALUES ('$CaseID', '$namajelas', '$ttd_1','$tgl1','$waktu1')");
    }

    public function signature_2()
    {
        $data['judul'] = "Form Asesment";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $this->load->view('Form/signaturekomunikasi2', $data);
    }

    public function simpansignatur2()
    {
        date_default_timezone_set("Asia/Jakarta");
        $data_uri = $_POST['foto'];
        $CaseID   = $_POST['caseid'];
        $namajelas   = $_POST['namajelas'];
        $tgl2 = date('Y-m-d');
        $waktu2 = date('H:i');
        $image_parts_1 = explode(",", $data_uri)[1];
        $image_type_aux_1 = explode("image/", $image_parts_1[0]);
        $decoded_image = base64_decode($image_parts_1);
        file_put_contents("./images/uploadsign/komunikasi/sig-".$CaseID."-2.png", $decoded_image);
        $ttd_2 = "sig-".$CaseID."-2.png";
       
        $this->db->query("UPDATE erm_ttdkomunikasi SET namajelas_2 = '$namajelas',tandatangan_2 = '$ttd_2',tgl2 = '$tgl2',waktu2 = '$waktu2' WHERE caseID = '$CaseID'");
    }

}

?>