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

class DiscardPlaning 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 DiscardPlanning WHERE caseID = '$caseID'")->num_rows();
        if ($cek > 0) {
            redirect('DiscardPlaning/detail/' . $caseID);
        }

        $data['judul'] = "Discard Planning";
        $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, p.DOB, p.Sex
        from Cases c
        join Company co on co.CompanyCode = c.CompanyCode 
        join Patient p on p.PatientCode = c.PatientCode
        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('discardPlaning/ttd', $data);
        $this->load->view('properti/footer', $data);
    }

    public function simpan($caseID)
    {
        $NoMr = $this->input->post('NoMr');
        $planning = $this->input->post('planning');
        $diagnosaUtama = $this->input->post('diagnosaUtama');
        $diagnosaTambahan = $this->input->post('diagnosaTambahan');
        $bantuan = $this->input->post('bantuan');
        $edukasiGizi = $this->input->post('edukasiGizi');
        $penangananKronis = $this->input->post('penangananKronis');
        $tujuan = $this->input->post('tujuan');
        $penataLaksana = $this->input->post('penataLaksana');
        $tindakan = $this->input->post('tindakan');
        $tempat = $this->input->post('tempat');
        $kebutuhanLainnya = $this->input->post('kebutuhanLainnya');
        $konsultasiKe = $this->input->post('konsultasiKe');
        $lainya1 = $this->input->post('lainya1');
        $edukasilanya = $this->input->post('edukasilanya');
        $penangananlainya = $this->input->post('penangananlainya');

        $this->db->query(
            "INSERT INTO DiscardPlanning (
            CaseID, 
            NoMr,
            planning,
            diagnosaUtama,
            diagnosaTambahan,
            bantuan,
            edukasiGizi,
            penangananKronis,
            tujuan,
            penataLaksana,
            tindakan,
            tempat,
            kebutuhanLainnya,
            konsultasiKe,
            bantuanlainya,
            edukasigizilainya,
            penangananlainya
            )VALUES (
                '$caseID', 
                '$NoMr',
                '$planning',
                '$diagnosaUtama',
                '$diagnosaTambahan',
                '$bantuan',
                '$edukasiGizi',
                '$penangananKronis',
                '$tujuan',
                '$penataLaksana',
                '$tindakan',
                '$tempat',
                '$kebutuhanLainnya',
                '$konsultasiKe',
                '$lainya1',
                '$edukasilanya',
                '$penangananlainya'
            )"
        );

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

    public function simpanTtdDokter($caseID)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $namaDokter = $this->input->post('namaDokter');

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

        $ttdDokter = $this->input->post('ttdDokter');

        $filename_1 = $caseID . '-DOKTER';
        $image_parts_1 = explode(";base64,", $ttdDokter);
        $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);

        $this->db->query("UPDATE DiscardPlanning SET namaDokter = '$namaDokter', ttdDokter = '$filename_1', tglTtdDokter = '$tgl' WHERE CaseID = '$caseID'");
        redirect($_SERVER['HTTP_REFERER']);
    }

    public function simpanTtdPerawat($caseID)
    {
        date_default_timezone_set("Asia/Jakarta");
        $tgl = date('Y-m-d');
        $namaPerawat = $this->input->post('namaPerawat');

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

        $ttdPerawat = $this->input->post('ttdPerawat');

        $filename_2 = $caseID . '-PERAWAT';
        $image_parts_2 = explode(";base64,", $ttdPerawat);
        $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);

        $this->db->query("UPDATE DiscardPlanning SET namaPerawat = '$namaPerawat', ttdPerawat = '$filename_2', tglTtdPerawat = '$tgl' WHERE CaseID = '$caseID'");
        redirect($_SERVER['HTTP_REFERER']);
    }

    public function detail($caseID)
    {
        $data['judul'] = "Discard Planning";
        $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, p.DOB, p.Sex
        from Cases c 
        join Company co on co.CompanyCode = c.CompanyCode
        join Patient p on p.PatientCode = c.PatientCode
        where c.[Case ID] = '$caseID'")->row_array();

        $data['unit'] = $this->db->query("SELECT * FROM Utama")->row_array();
        $data['ttd'] = $this->db->query("SELECT * FROM DiscardPlanning WHERE caseID = '$caseID'")->row_array();
        $this->load->view('properti/header', $data);
        $this->load->view('properti/menu', $data);
        $this->load->view('discardPlaning/detail', $data);
        $this->load->view('properti/footer', $data);
    }

    public function print($caseID)
    {
        $data['judul'] = "Discard Planning";
        $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, p.DOB, p.Sex
        from Cases c 
        join Company co on co.CompanyCode = c.CompanyCode
        join Patient p on p.PatientCode = c.PatientCode
        where c.[Case ID] = '$caseID'")->row_array();

        $data['unit'] = $this->db->query("SELECT * FROM Utama")->row_array();
        $data['ttd'] = $this->db->query("SELECT * FROM DiscardPlanning WHERE caseID = '$caseID'")->row_array();
        $this->load->view('discardPlaning/print', $data);
    }
}
