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

class InformedConsent 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->helper('url');

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

    public function ttd($caseID)
    {
        // cek
        $cek = $this->db->query("SELECT caseID FROM InformedConsent WHERE caseID = '$caseID'")->num_rows();
        if ($cek > 0) {
            redirect('informedConsent/detail/' . $caseID);
        }

        $data['judul'] = "Informed Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['cases'] = $cases = $this->db->query("select c.[Case ID],c.PatientCode,c.Description,c.Category,
        c.[Case Status],c.[Intake Date],c.[Intake Time],co.CompanyName
        from Cases c 
        join Company co on co.CompanyCode = c.CompanyCode
        where c.[Case ID] = '$caseID'")->row_array();
        $mr = $cases['PatientCode'];
        $data['pasien'] = $this->db->query("SELECT DOB, HomeAddress1, CellPhone FROM Patient WHERE PatientCode = '$mr'")->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('informedConsent/ttd', $data);
        $this->load->view('properti/footer', $data);
    }

    public function simpan($caseID)
    {
        $jenis_form = $this->input->post('jenis_form');
        $DokterPelaksana = $this->input->post('DokterPelaksana');
        $PemberiInformasi = $this->input->post('PemberiInformasi');
        $PenerimaInformasi = $this->input->post('PenerimaInformasi');
        $Diagnosa = $this->input->post('Diagnosa');
        $cek_Diagnosa = $this->input->post('cek_Diagnosa');
        $DasarDiagnosa = $this->input->post('DasarDiagnosa');
        $cek_DasarDiagnosa = $this->input->post('cek_DasarDiagnosa');
        $TindakanKedokteran = $this->input->post('TindakanKedokteran');
        $cek_TindakanKedokteran = $this->input->post('cek_TindakanKedokteran');
        $IndikasiTindakan = $this->input->post('IndikasiTindakan');
        $cek_IndikasiTindakan = $this->input->post('cek_IndikasiTindakan');
        $TataCara = $this->input->post('TataCara');
        $cek_TataCara = $this->input->post('cek_TataCara');
        $Tujuan = $this->input->post('Tujuan');
        $cek_Tujuan = $this->input->post('cek_Tujuan');
        $Risiko = $this->input->post('Risiko');
        $cek_Risiko = $this->input->post('cek_Risiko');
        $Komplikasi = $this->input->post('Komplikasi');
        $cek_Komplikasi = $this->input->post('cek_Komplikasi');
        $Prognosis = $this->input->post('Prognosis');
        $cek_Prognosis = $this->input->post('cek_Prognosis');
        $Alternatif = $this->input->post('Alternatif');
        $cek_Alternatif = $this->input->post('cek_Alternatif');
        $LainLain = $this->input->post('LainLain');
        $cek_LainLain = $this->input->post('cek_LainLain');

        $folderPath = "./images/uploadsign/informedConsent/";

        // upload ttd 1 & 2
        $Ttd1 = $this->input->post('Ttd1');
        $Ttd2 = $this->input->post('Ttd2');

        // 1
        $filename_1 = $caseID . ' - TTD1 ';
        $image_parts_1 = explode(";base64,", $Ttd1);
        $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);
        // 2
        $filename_2 = $caseID . ' - TTD2 ';
        $image_parts_2 = explode(";base64,", $Ttd2);
        $image_type_aux_2 = explode("image/", $image_parts_2[0]);
        $image_type_2 = $image_type_aux_2[1];
        $image_base64_2 = base64_decode($image_parts_2[1]);
        $file_2 = $folderPath . $filename_2 . '.' . $image_type_2;
        file_put_contents($file_2, $image_base64_2);

        $Nama = $this->input->post('Nama');
        $Umur = $this->input->post('Umur');
        $Jk = $this->input->post('Jk');
        $Alamat = $this->input->post('Alamat');
        $Tindakan = $this->input->post('Tindakan');
        $Terhadap = $this->input->post('Terhadap');
        $NamaYangDitindak = $this->input->post('NamaYangDitindak');
        $UmurYangDitindak = $this->input->post('UmurYangDitindak');
        $JkYangDitindak = $this->input->post('JkYangDitindak');
        $AlamatYangDitindak = $this->input->post('AlamatYangDitindak');
        $TempatDitindak = $this->input->post('TempatDitindak');
        $TanggalBuat = $this->input->post('TanggalBuat');
        $JamBuat = $this->input->post('JamBuat');

        // 3
        $TtdYangMenyatakan = $this->input->post('TtdYangMenyatakan');
        $YangMenyatakan = $this->input->post('YangMenyatakan');
        $filename_3 = $caseID . ' - YangMenyatakan ' . url_title($YangMenyatakan, '-', true);
        $image_parts_3 = explode(";base64,", $TtdYangMenyatakan);
        $image_type_aux_3 = explode("image/", $image_parts_3[0]);
        $image_type_3 = $image_type_aux_3[1];
        $image_base64_3 = base64_decode($image_parts_3[1]);
        $file_3 = $folderPath . $filename_3 . '.' . $image_type_3;
        file_put_contents($file_3, $image_base64_3);

        // 4
        $TtdSaksi1 = $this->input->post('TtdSaksi1');
        $Saksi1 = $this->input->post('Saksi1');
        $filename_4 = $caseID . ' - Saksi1 ' . url_title($Saksi1, '-', true);
        $image_parts_4 = explode(";base64,", $TtdSaksi1);
        $image_type_aux_4 = explode("image/", $image_parts_4[0]);
        $image_type_4 = $image_type_aux_4[1];
        $image_base64_4 = base64_decode($image_parts_4[1]);
        $file_4 = $folderPath . $filename_4 . '.' . $image_type_4;
        file_put_contents($file_4, $image_base64_4);

        // 5
        $TtdSaksi2 = $this->input->post('TtdSaksi2');
        $Saksi2 = $this->input->post('Saksi2');
        $filename_5 = $caseID . ' - Saksi2 ' . url_title($Saksi2, '-', true);
        $image_parts_5 = explode(";base64,", $TtdSaksi2);
        $image_type_aux_5 = explode("image/", $image_parts_5[0]);
        $image_type_5 = $image_type_aux_5[1];
        $image_base64_5 = base64_decode($image_parts_5[1]);
        $file_5 = $folderPath . $filename_5 . '.' . $image_type_5;
        file_put_contents($file_5, $image_base64_5);

        $this->db->query(
            "INSERT INTO InformedConsent (
            caseID, 
            DokterPelaksana,
            PemberiInformasi,
            PenerimaInformasi,
            Diagnosa,
            DasarDiagnosa,
            TindakanKedokteran,
            IndikasiTindakan,
            TataCara,
            Tujuan,
            Risiko,
            Komplikasi,
            Prognosis,
            Alternatif,
            LainLain,
            Ttd1,
            Ttd2,
            Nama,
            Umur,
            Jk,
            Alamat,
            Tindakan,
            Terhadap,
            NamaYangDitindak,
            UmurYangDitindak,
            JkYangDitindak,
            AlamatYangDitindak,
            TempatDitindak,
            TanggalBuat,
            JamBuat,
            YangMenyatakan,
            TtdYangMenyatakan,
            Saksi1,
            Saksi2,
            TtdSaksi1,
            TtdSaksi2,
            cek_Diagnosa,
            cek_DasarDiagnosa,
            cek_TindakanKedokteran,
            cek_IndikasiTindakan,
            cek_TataCara,
            cek_Tujuan,
            cek_Risiko,
            cek_Komplikasi,
            cek_Prognosis,
            cek_Alternatif,
            cek_LainLain,
            jenis_form
            )VALUES (
                '$caseID', 
                '$DokterPelaksana',
                '$PemberiInformasi',
                '$PenerimaInformasi',
                '$Diagnosa',
                '$DasarDiagnosa',
                '$TindakanKedokteran',
                '$IndikasiTindakan',
                '$TataCara',
                '$Tujuan',
                '$Risiko',
                '$Komplikasi',
                '$Prognosis',
                '$Alternatif',
                '$LainLain',
                '$filename_1',
                '$filename_2',
                '$Nama',
                '$Umur',
                '$Jk',
                '$Alamat',
                '$Tindakan',
                '$Terhadap',
                '$NamaYangDitindak',
                '$UmurYangDitindak',
                '$JkYangDitindak',
                '$AlamatYangDitindak',
                '$TempatDitindak',
                '$TanggalBuat',
                '$JamBuat',
                '$YangMenyatakan',
                '$filename_3',
                '$Saksi1',
                '$Saksi2',
                '$filename_4',
                '$filename_5',
                '$cek_Diagnosa',
                '$cek_DasarDiagnosa',
                '$cek_TindakanKedokteran',
                '$cek_IndikasiTindakan',
                '$cek_TataCara',
                '$cek_Tujuan',
                '$cek_Risiko',
                '$cek_Komplikasi',
                '$cek_Prognosis',
                '$cek_Alternatif',
                '$cek_LainLain',
                '$jenis_form'
            )"
        );

        $this->session->set_flashdata('message', '<div class="alert alert-success" role="alert"> TTD Tersimpan.</div>');
        redirect(base_url('informedConsent/detail/' . $caseID));
    }

    public function detail($caseID)
    {
        $data['judul'] = "Informed Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['cases'] = $cases = $this->db->query("select c.[Case ID],c.PatientCode,c.Description,c.Category,
        c.[Case Status],c.[Intake Date],c.[Intake Time],co.CompanyName
        from Cases c 
        join Company co on co.CompanyCode = c.CompanyCode
        where c.[Case ID] = '$caseID'")->row_array();
        $mr = $cases['PatientCode'];
        $data['pasien'] = $this->db->query("SELECT DOB, HomeAddress1, CellPhone FROM Patient WHERE PatientCode = '$mr'")->row_array();
        $data['unit'] = $this->db->query("SELECT * FROM Utama")->row_array();
        $data['ttd'] = $this->db->query("SELECT * FROM InformedConsent WHERE caseID = '$caseID'")->row_array();
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('informedConsent/detail', $data);
        $this->load->view('properti/footer', $data);
    }

    public function print($caseID)
    {
        $data['judul'] = "Informed Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['cases'] = $cases = $this->db->query("select c.[Case ID],c.PatientCode,c.Description,c.Category,
        c.[Case Status],c.[Intake Date],c.[Intake Time],co.CompanyName
        from Cases c 
        join Company co on co.CompanyCode = c.CompanyCode
        where c.[Case ID] = '$caseID'")->row_array();
        $mr = $cases['PatientCode'];
        $data['pasien'] = $this->db->query("SELECT DOB, HomeAddress1, CellPhone FROM Patient WHERE PatientCode = '$mr'")->row_array();
        $data['unit'] = $this->db->query("SELECT * FROM Utama")->row_array();
        $data['ttd'] = $this->db->query("SELECT * FROM InformedConsent WHERE caseID = '$caseID'")->row_array();
        $this->load->view('informedConsent/print', $data);
    }
}
