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

class M_Pendaftaran extends CI_Model
{

    public function suku()
    {
        return $this->db->query("SELECT * FROM SukuBangsa");
    }
    public function religi()
    {
        return $this->db->query("SELECT * FROM Religion");
    }
    public function edu()
    {
        return $this->db->query("SELECT * FROM Education");
    }
    public function prof()
    {
        return $this->db->query("SELECT * FROM Profesi");
    }
    public function tipe()
    {
        return $this->db->query("SELECT * FROM PatientType");
    }

    public function bahasa()
    {
        return $this->db->query("SELECT * FROM BahasaPasien");
    }

    public function asuransi()
    {
        return $this->db->query("SELECT * FROM Company WHERE [Status] = 'T'");
    }

    public function perusahaan()
    {
        return $this->db->query("SELECT * FROM Perusahaan");
    }

    public function provinsi()
    {
        return $this->db->query("SELECT * FROM Provinsi ORDER BY PROVINCE ASC");
    }

    public function cariNamaDepan($pencarian)
    {
        return $this->db->query("SELECT * FROM Patient WHERE [FirstName] LIKE '%$pencarian%'");
    }

    public function cariNik($pencarian)
    {
        return $this->db->query("SELECT * FROM Patient WHERE [NIK] LIKE '%$pencarian%'");
    }

    public function cariBpjs($pencarian)
    {
        return $this->db->query("SELECT * FROM Patient WHERE [EmployeeID] LIKE '%$pencarian%'");
    }

    public function cariNamaBelakang($pencarian)
    {
        return $this->db->query("SELECT * FROM Patient WHERE [LastName] LIKE '%$pencarian%'");
    }

    public function cariTanggal($pencarian)
    {
        return $this->db->query("SELECT * FROM Patient WHERE [DOB] BETWEEN '$pencarian' AND '$pencarian' ");
    }

    public function detail($id)
    {
        return $this->db->query("SELECT * FROM Patient WHERE PatientCode = '$id'");
    }

    public function mr($nomr)
    {
        return $this->db->query("SELECT * FROM Patient WHERE PatientCode = '$nomr'");
    }

    public function doctor()
    {
        return $this->db->query("SELECT * FROM Doctor Where Active ='T'");
    }

    public function Company()
    {
        $dataCompany = $this->db->get("Company")->result();
        $bb = json_encode($dataCompany);
        return json_decode($bb, TRUE);
    }

    public function daftarPoli()
    {
        return $this->db->query("SELECT * FROM [dbo].[Category] WHERE  [Status] = 'Y'");
    }

    public function daftarPerujuk()
    {
        return $this->db->query("SELECT * FROM RefferedFrom where Active = 'Y'");
    }

    public function diagnosa()
    {
        $dataDiagnosa = $this->db->get("ICD9")->result();
        $bb = json_encode($dataDiagnosa);
        return json_decode($bb, TRUE);
    }

    public function detailCaseUmum($case)
    {
        return $this->db->query("SELECT * FROM Cases c, Patient p WHERE c.PatientCode=p.PatientCode AND  [Case ID] = '$case'");
    }

    public function list()
    {
        extract($_GET);
        if (isset($pelayanan)) {
            $def = "IP";
            return $this->db->query("SELECT * FROM Cases c, Doctor d WHERE c.DoctIncharge=d.DoctorID AND [Case Status] = 'OPEN' AND Category = '$def'");
        } else if (isset($ob)) {
            $def = "OB";
            return $this->db->query("SELECT * FROM Cases WHERE Category = '$def' and [Intake Date] = '$tgl'");
        } else {
            $def = "OP";
            return $this->db->query("SELECT * FROM Cases c, Doctor d WHERE c.DoctIncharge=d.DoctorID AND [Intake Date] = '$tgl' AND Category = '$def' ORDER BY [Case ID] ASC");
        }
        date_default_timezone_set("Asia/Jakarta");
        $companyBpjs = $this->db->query("SELECT CompanyCode FROM BPJS_ID")->row_array();
        $companyCode = $companyBpjs['CompanyCode'];

        // return $this->db->query("SELECT * FROM Cases c, Company n, Doctor d WHERE c.CompanyCode=n.CompanyCode AND c.DoctIncharge=d.DoctorID AND [Intake Date] = '$tgl' AND Category='OP' AND c.CompanyCode='$companyCode' AND [Case Status] = 'OPEN' AND [FRNo] <> '' AND [task] IS NULL ORDER BY NoSEP DESC");

    }

    public function pasien($id)
    {
        return $this->db->query("SELECT * FROM Patient WHERE PatientCode = '$id'");
    }

    // Ini Perubahan Pencarian byNoKartu
    public function peserta($nobpjs)
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        $dataA = array(
            "noKartuPeserta" => $nobpjs
        );
        $payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/peserta/getpeserta");
        $result = curl_exec($ch);
        curl_close($ch);

        //echo $result;exit();

        return json_decode($result, true);
    }

    // Ini Perubahan Pencarian NIK
    public function pesertaNik($nik)
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        $dataA = array(
            "jenisKartu" => "nik",
            "nikAtauNoka" => "$nik"
        );
        $payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/peserta/getpesertabyjeniskartu");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    public function apigetpulang()
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        $dataA = array(
            'isRawatInap' => false
        );
        $payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/statuspulang/getstatuspulang");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    public function getSubspesialis($kdspesialis)
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        $dataA = array(
            "kodeSpesialis" => $kdspesialis
        );
        $payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/spesialis/getreferensisubspesialis");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    public function datapeserta($nobpjs)
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        $dataA = array(
            "noKartuPeserta" => $nobpjs
        );
        $payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/peserta/getpeserta");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    public function getNobpjs()
    {
        extract($_GET);
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        $dataA = array(
            "jenisKartu" => "nik",
            "nikAtauNoka" => "$nik"
        );
        $payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "wsbpjspeserta/nokartubpjs");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    public function carirujukanfaskes()
    {
        $kodeSubSpesialis = $this->input->post('subspesialis');
        $kodeSarana = $this->input->post('sarana');
        $tanggalEstimasiRujuk = $this->input->post('TglRencana');

        $results = $this->M_Pendaftaran->get_faskes_rujukan($kodeSubSpesialis, $kodeSarana, $tanggalEstimasiRujuk);

        echo json_encode($results);
    }

    public function get_faskes_rujukan($kodeSubSpesialis, $kodeSarana, $tanggalEstimasiRujuk)
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $apiUrl = $abdulradjak['IP_WS_Layanan'] . 'pcare/spesialis/getFaskesrujukansubspesialis';

        // Convert the date to the required format if needed

        $formattedDate = date("d-m-Y", strtotime($tanggalEstimasiRujuk));
        // print_r($formattedDate);exit;
        $params = array(
            "kodeSubSpesialis" => $kodeSubSpesialis,
            "kodeSarana" => $kodeSarana,
            "tanggalEstimasiRujuk" => $formattedDate
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

        $response = curl_exec($ch);
        curl_close($ch);

        return json_decode($response, true);
    }

    public function apigetsarana()
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        //$payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/spesialis/getreferensisarana");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }

    public function apigetspesialis()
    {
        $abdulradjak = $this->db->query("SELECT TOP 1 * FROM BPJS_ID")->row_array();
        $ch = curl_init();
        //$payload = json_encode($dataA);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "");
        curl_setopt($ch, CURLOPT_URL, $abdulradjak['IP_WS_Layanan'] . "pcare/spesialis/getreferensispesialis");
        $result = curl_exec($ch);
        curl_close($ch);
        return json_decode($result, true);
    }
}
