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

class GeneralConsent 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 index()
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $data['judul'] = "General Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $data['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 [Intake Date] ='$tgl'")->result_array();
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('generalConsent/index', $data);
        $this->load->view('properti/footer', $data);
    }

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

        $data['judul'] = "General 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('generalConsent/ttd', $data);
        $this->load->view('properti/footer', $data);
    }

    public function simpan($caseID)
    {
        date_default_timezone_set('Asia/Jakarta');
        $folderPath = "./images/uploadsign/generalConsent/";
        $yang_menjelaskan = $_POST['yang_menjelaskan'];
        $pasien = $_POST['pasien'];
        date_default_timezone_set('Asia/Jakarta');
        $tgl = date('Y-m-d');
        $jam = date('H:i');
        $penanggungjawab = $_POST['penanggungjawab'];

        // yang menjelaskan
       
        $this->db->query("INSERT INTO GeneralConsent (caseID, yang_menjelaskan, ttd_yang_menjelaskan, pasien, ttd_pasien, tgl_buat, jam_buat,penanggungjawab,ttdpenanggungjawab) VALUES ('$caseID', '', '', '', '', '$tgl', '$jam', '', '')");

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

    public function detail($caseID)
    {
        $data['judul'] = "General 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 GeneralConsent g JOIN [TandatanganGeneralConsent] td On g.caseID = td.CaseID
        WHERE td.CaseID = '$caseID'")->row_array();
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('generalConsent/detail', $data);
        $this->load->view('properti/footer', $data);
    }

    public function print($caseID)
    {
        $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 GeneralConsent g JOIN [TandatanganGeneralConsent] td On g.caseID = td.CaseID
        WHERE td.CaseID = '$caseID'")->row_array();
        $this->load->view('generalConsent/print', $data);
    }

    public function signature()
    {
        $data['judul'] = "General Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $this->load->view('generalConsent/signature', $data);
    }

    public function simpansignature()
    {
        $data_uri = $_POST['foto'];
        $CaseID   = $_POST['caseid'];
        $namajelas   = $_POST['namajelas'];
        $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/generalConsent/sig-".$CaseID.".png", $decoded_image);
        $ttd_1 = "sig-".$CaseID.".png";
       
        $this->db->query("INSERT INTO TandatanganGeneralConsent (CaseID, namajelas_1, tandatangan_1, namajelas_2, tandatangan_2,namajelas_3, tandatangan_3) VALUES ('$CaseID', '$namajelas', '$ttd_1', '', '', '','')");
    }

    public function signaturepasien()
    {
        $data['judul'] = "General Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $this->load->view('generalConsent/signaturepasien', $data);
    }

    public function simpansignaturepasien()
    {
        $data_uri = $_POST['foto'];
        $CaseID   = $_POST['caseid'];
        $namajelas   = $_POST['namajelas'];
        $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/generalConsent/sig-".$CaseID."-2.png", $decoded_image);
        $ttd_2 = "sig-".$CaseID."-2.png";
       
        $this->db->query("UPDATE TandatanganGeneralConsent SET namajelas_2 = '$namajelas',tandatangan_2 = '$ttd_2' WHERE CaseID = '$CaseID'");
    }

    public function signaturepenanggungjawab()
    {
        $data['judul'] = "General Consent";
        $data['user'] = $this->M_user->getUserSession()->row_array();
        $this->load->view('generalConsent/signaturepenanggungjawab', $data);
    }

    public function simpansignaturepenanggungjawab()
    {
        $data_uri = $_POST['foto'];
        $CaseID   = $_POST['caseid'];
        $namajelas   = $_POST['namajelas'];
        $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/generalConsent/sig-".$CaseID."-3.png", $decoded_image);
        $ttd_3 = "sig-".$CaseID."-3.png";
       
        $this->db->query("UPDATE TandatanganGeneralConsent SET namajelas_3 = '$namajelas',tandatangan_3 = '$ttd_3' WHERE CaseID = '$CaseID'");
    }

}
