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

class Admision_panel extends MBELINK_Controller {
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->library(array('session'));
		$this->load->helper(array('rsonline'));
				 
		// User Login Information
		$this->data['user'] = user_info();
		$this->data['instansi'] = orgInfo();
		
		// Cek Sesi
		 if(! $this->session->userdata('userid')):
			redirect(base_url('login-system.html'), 'refresh');
		endif;
		//Ngaeng-ngaeng
		$this->load->model('admision/admision_panel_model');
		$this->load->model('inform_model');
		
		$this->data['tanggal']=date("Y-m-d", time());
		$this->data['tglaw']=$this->data['tanggal'];
		$this->data['tglak']=$this->data['tanggal'];
	}
	
	function rekapPxMCUByAct()
	{
		$this->data['tglawal']=$this->input->post('tglawal');
		$this->data['tglakhir']=$this->input->post('tglakhir');
		
		$this->data['result']=$this->admision_panel_model->getPxMCUByAct($this->data['tglawal'],$this->data['tglakhir']);
		$this->load->view('admision/mcu_rekapbyact',$this->data);
	}
	
	public function index()
	{
		$this->data['nama']=$this->input->post('nama');
		$this->data['norm']=$this->input->post('norm');
		
		if(isset($_POST['bCari'])){
			if($this->input->post('nama') <>''){
				$key="a.`nm_pasien` LIKE '%".$this->data['nama']."%'";
			}else if($this->input->post('norm') <>''){
				$key="a.`no_rkm_medis`='".$this->data['norm']."'";
			}else if($this->input->post('nama') <>'' AND $this->input->post('norm') <>'')
			{
				$key="a.`nm_pasien` LIKE '%".$this->data['nama']."%' OR a.`no_rkm_medis`='".$this->data['norm']."'";
			}else{
				$key="a.`tgl_daftar`=LEFT(NOW(),10)";
			}
			$this->data['pasien']=$this->admision_panel_model->getCariDataPasien($key);
		}else{
			$this->data['pasien']=$this->admision_panel_model->getDataPasien();
		}
		
		$this->load->view('admision/daftar_pasien',$this->data);
		
	}
	
	function cetakKartuPasien()
	{
		$dataps=$this->admision_panel_model->getPasienByRM($this->input->get('key'));
		foreach($dataps as $dtps):
			$this->data['norm']=$dtps->no_rkm_medis;
			$this->data['nama']=$dtps->nm_pasien;
			$this->data['nik']=$dtps->no_ktp;
			$this->data['tgllahir']=$dtps->tgl_lahir;
		endforeach;
		
		$this->load->view('admision/cetak_kartu_pasien',$this->data);
	}
	
	function listPendaftaran()
	{
		if(isset($_POST['bCari'])){
			$this->data['tanggal']=$this->input->post('tanggal');
		}else{
			$this->data['tanggal']=date("Y-m-d", time());
		}
		
		$this->data['registrasi']=$this->admision_panel_model->getDataPendaftaranBPJS($this->data['tanggal']);
		$this->load->view('admision/data_pendaftaran',$this->data);
	}
	
	function saveUpdateNoSEP()
	{
		print_r($_POST);
		
		$dtupdate=array(
			'NoSEP_BPJS' =>$this->input->post('nosep'),
		);
		$updt=$this->admision_panel_model->simpanUbdapeSEP($dtupdate,$this->input->post('idreg'),$this->input->post('tglmasuk'));
		
		if($updt){		
			$insert=array(
				'idreg'=>$this->input->post('idreg'),
				'nama'=>$this->input->post('nama'),
				'no_sep'=>$this->input->post('nosep'),
				'tgl_masuk'=>$this->input->post('tglmasuk'),
				'tgl_entry'=>date("Y-m-d H:i:s", time()),
			);
			$save=$this->admision_panel_model->insertLogSEP($insert);
			
			$this->session->set_flashdata('pesan', 'Update SEP Berhasil');
			
		}else{
			$this->session->set_flashdata('error', 'Gagal Melakukan Update SEP');
		}
		redirect(base_url('list-pendaftaran.html'));
	}
	
	function daftarPasienPenjamin()
	{
		$this->data['followup']=$this->input->post('followup');
		$this->data['statuspx']=$this->input->post('statuspx');
		$this->data['tglawal']=$this->input->post('tglawal');
		$this->data['tglakhir']=$this->input->post('tglakhir');
		//$this->data['filter']=$this->input->post('filter');
		
		if(! $this->data['followup']==''){
			$followupx="a.`status_lanjut`='".$this->data['followup']."' AND";
		}else{
			$followupx="";
		}
		if($this->data['statuspx']==''){
			$status_pasien="";
		}else if($this->data['statuspx']=='customer'){
			$status_pasien="a.`stts`<>'Batal' AND";
		}else{
			$status_pasien="a.`stts`='".$this->data['statuspx']."' AND";
		}
		/* if(! $this->data['statuspx']==''){
			$status_pasien="a.`stts`='".$this->data['statuspx']."' AND";
		}else{
			$status_pasien="";
		} */
		
		if($this->data['tglakhir'] < $this->data['tglawal']){
			$this->session->set_flashdata('error', 'Tanggal Akhir yang di masukkan Harus Lebih Besar atau Sama dengan Tanggal Awal');	
			redirect(base_url('admision-pasien-penjamin.html'));
		}else{
			$this->data['result']=$this->admision_panel_model->getPasienJaminan($followupx,$status_pasien,$this->data['tglawal'],$this->data['tglakhir']);
		
			$this->load->view('admision/pasien_penjamin',$this->data);
		}
	}
	
	function daftarJenisPasien()
	{
		$this->data['followup']=$this->input->post('followup');
		$this->data['statuspx']=$this->input->post('statuspx');
		$this->data['tglawal']=$this->input->post('tglawal');
		$this->data['tglakhir']=$this->input->post('tglakhir');
		$this->data['filter']=$this->input->post('filter');
		//$this->data['lamabaru']=$this->input->post('lamabaru');
		
		if(! $this->data['followup']==''){
			$followupx="a.`status_lanjut`='".$this->data['followup']."' AND";
		}else{
			$followupx="";
		}
		
		if($this->data['statuspx']==''){
			$status_pasien="";
		}else if($this->data['statuspx']=='customer'){
			$status_pasien="a.`stts`<>'Batal' AND";
		}else{
			$status_pasien="a.`stts`='".$this->data['statuspx']."' AND";
		}
		
		/* if(! $this->data['statuspx']==''){
			$status_pasien="a.`stts`='".$this->data['statuspx']."' AND";
		}else{
			$status_pasien="";
		}	 */	
		
		
		/* if(! $this->data['lamabaru']==''){
			$status_lama="a.`KategoriLamaBaru`='".$this->data['lamabaru']."' AND";
		}else{
			$status_lama="";
		} */
		
		if($this->data['tglakhir'] < $this->data['tglawal']){
			$this->session->set_flashdata('error', 'Tanggal Akhir yang di masukkan Harus Lebih Besar atau Sama dengan Tanggal Awal');	
			redirect(base_url('admision-byjenis-pasien.html'));
		}else{
			//$this->data['result']=$this->admision_panel_model->getPasienLamaBaru($followupx,$status_pasien,$status_lama,$this->data['tglawal'],$this->data['tglakhir']);
			$this->data['result']=$this->admision_panel_model->getPasienLamaBaru($followupx,$status_pasien,$this->data['tglawal'],$this->data['tglakhir']);
		
			$this->load->view('admision/pasien_jenis',$this->data);
		}
	}
	
	function daftarJenisPasienPenunjang()
	{
		$this->data['followup']=$this->input->post('followup');
		$this->data['statuspx']=$this->input->post('statuspx');
		$this->data['tglawal']=$this->input->post('tglawal');
		$this->data['tglakhir']=$this->input->post('tglakhir');
		//$this->data['lamabaru']=$this->input->post('lamabaru');
		
		if(! $this->data['followup']==''){
			$followupx="a.`FollowUp`='".$this->data['followup']."' AND";
		}else{
			$followupx="";
		}
		
		if(! $this->data['statuspx']==''){
			$status_pasien="a.`Pulang`='".$this->data['statuspx']."' AND";
		}else{
			$status_pasien="";
		}
		
		/* if(! $this->data['lamabaru']==''){
			$status_lama="a.`KategoriLamaBaru`='".$this->data['lamabaru']."' AND";
		}else{
			$status_lama="";
		} */
		
		if($this->data['tglakhir'] < $this->data['tglawal']){
			$this->session->set_flashdata('error', 'Tanggal Akhir yang di masukkan Harus Lebih Besar atau Sama dengan Tanggal Awal');	
			redirect(base_url('admision-pasien-penunjang-byjenis.html'));
		}else{
			//$this->data['result']=$this->admision_panel_model->getPasienLamaBaruPenunjang($followupx,$status_pasien,$status_lama,$this->data['tglawal'],$this->data['tglakhir']);
			$this->data['result']=$this->admision_panel_model->getPasienLamaBaruPenunjang($followupx,$status_pasien,$this->data['tglawal'],$this->data['tglakhir']);
		
			$this->load->view('admision/pasien_jenis_penunjang',$this->data);
		}
	}
	
	function daftarRekapPasienByPerujuk()
	{
		$this->data['followup']=$this->input->post('followup');
		$this->data['statuspx']=$this->input->post('statuspx');
		$this->data['tglawal']=$this->input->post('tglawal');
		$this->data['tglakhir']=$this->input->post('tglakhir');
		//$this->data['filter']=$this->input->post('filter');
		
		if(! $this->data['followup']==''){
			$followupx="a.`status_lanjut`='".$this->data['followup']."' AND";
		}else{
			$followupx="";
		}
		
		/* if(! $this->data['statuspx']==''){
			$status_pasien="a.`stts`='".$this->data['statuspx']."' AND";
		}else{
			$status_pasien="";
		} */
		if($this->data['statuspx']==''){
			$status_pasien="";
		}else if($this->data['statuspx']=='customer'){
			$status_pasien="a.`stts`<>'Batal' AND";
		}else{
			$status_pasien="a.`stts`='".$this->data['statuspx']."' AND";
		}
		
		/* if($this->data['filter']=='2'){
			$filter="a.`TanggalKeluar`";
		}else{
			$filter="a.`TanggalMasuk`";
		} */
		
		if($this->data['tglakhir'] < $this->data['tglawal']){
			$this->session->set_flashdata('error', 'Tanggal Akhir yang di masukkan Harus Lebih Besar atau Sama dengan Tanggal Awal');	
			redirect(base_url('admision-pasien-byrujukan.html'));
		}else{
			$this->data['result']=$this->admision_panel_model->getPasienRekapByPerujuk($followupx,$status_pasien,$this->data['tglawal'],$this->data['tglakhir']);
		
			$this->load->view('admision/pasien_rekap_rukujan',$this->data);
		}
	}
	
	//perujuk Kontrol
	function getDaftarPasienByPerujuk()
	{
		if(isset($_POST['bProses'])){
			$this->data['tglaw']=$this->input->post('tglawal');
			$this->data['tglak']=$this->input->post('tglakhir');
		}else{
			$this->data['tglaw']=date("Y-m-d", time());
			$this->data['tglak']=date("Y-m-d", time());
		}
		
		
		$this->data['pasienr']=$this->admision_panel_model->getDataPxDaftarByPerujuk($this->data['tglaw'],$this->data['tglak']);
		$this->data['pasienterdaftar']=$this->admision_panel_model->getDataPxDaftarByPerujukTerdaftar($this->data['tglaw'],$this->data['tglak']);
		
		$this->load->view('admision/perujuk_data_pasien_terdaftar',$this->data);
	}
	
	function getLihatPxPerujukByID()
	{
		$idpx=base64_decode($this->input->get('key'));
		$datapx=$this->admision_panel_model->getPxRujukanByID($idpx);
		
		foreach($datapx as $dtpx){
			$this->data['ID']=$dtpx->ID;
			$this->data['idreg']=$dtpx->idreg;
			$this->data['nama']=$dtpx->Nama;
			$this->data['alamat']=$dtpx->Alamat;
			$this->data['propinsi']=$dtpx->namaprop;
			$this->data['kecamatan']=$dtpx->nmkecamatan;
			$this->data['kota']=$dtpx->namakab;
			$this->data['jkel']=$dtpx->JKel;
			$this->data['tmp_lhr']=$dtpx->TmpLahir;
			$this->data['tgl_lhr']=$dtpx->TglLahir;
			$this->data['no_telp']=$dtpx->NoTelp;
			$this->data['nik']=$dtpx->NIK;
			$this->data['tgl_masuk']=$dtpx->TanggalMasuk;
			$this->data['idpoli']=$dtpx->PintuMasuk;
			$this->data['poli']=$dtpx->poli;
			$this->data['pj']=$dtpx->penanggung_jawab;
			$this->data['hub_pj']=$dtpx->hub_pj;
			$this->data['nmdokter']=$dtpx->nmdokter;
			$this->data['dokterid']=$dtpx->Dokter;
			$this->data['idjadwakdok']=$dtpx->jadwalDokter;
			$this->data['jadwaldok']=$dtpx->hari." ".$dtpx->JamStart."-".$dtpx->JamFinish;			
			$this->data['idpenjamin']=$dtpx->JnsPenjamin;			
			$this->data['penjamin']=$dtpx->penjamin;			
			$this->data['idperusahaan']=$dtpx->PershID;			
			$this->data['perusahaan']=$dtpx->perusahaan;			
			$this->data['no_asuransi']=$dtpx->NoAsuransi;			
			$this->data['diagnosars']=$dtpx->diagnosa_rs;			
			
		}
		$this->load->view('admision/perujuk_lihat_pasien',$this->data);
	}
	
	function cetakDataPxRujukanByAdmision()
	{
		$idp=base64_decode($this->input->get('key'));
		$datap=$this->admision_panel_model->getPxRujukanByID($idp);
		
		foreach($datap as $dtpx){
			$this->data['idreg']=$dtpx->idreg;
			$this->data['nama']=$dtpx->Nama;
			$this->data['alamat']=$dtpx->Alamat;
			$this->data['propinsi']=$dtpx->namaprop;
			$this->data['kecamatan']=$dtpx->nmkecamatan;
			$this->data['kota']=$dtpx->namakab;
			$this->data['jkel']=$dtpx->JKel;
			$this->data['tmp_lhr']=$dtpx->TmpLahir;
			$this->data['tgl_lhr']=$dtpx->TglLahir;
			$this->data['no_telp']=$dtpx->NoTelp;
			$this->data['nik']=$dtpx->NIK;
			$this->data['tgl_masuk']=$dtpx->TanggalMasuk;
			$this->data['idpoli']=$dtpx->PintuMasuk;
			$this->data['poli']=$dtpx->poli;
			$this->data['pj']=$dtpx->penanggung_jawab;
			$this->data['hub_pj']=$dtpx->hub_pj;
			$this->data['nmdokter']=$dtpx->nmdokter;
			$this->data['dokterid']=$dtpx->Dokter;
			$this->data['idjadwakdok']=$dtpx->jadwalDokter;
			$this->data['jadwaldok']=$dtpx->hari." ".$dtpx->JamStart."-".$dtpx->JamFinish;			
			$this->data['idpenjamin']=$dtpx->JnsPenjamin;			
			$this->data['penjamin']=$dtpx->penjamin;			
			$this->data['idperusahaan']=$dtpx->PershID;			
			$this->data['perusahaan']=$dtpx->perusahaan;			
			$this->data['no_asuransi']=$dtpx->NoAsuransi;			
						
			
		}
		
		$this->load->view('admision/cetak_datapasien_rujukan',$this->data);
	}
	
	function updStatusPxPerujukByAdmision()
	{
		//print_r($_POST);exit();
		$noreg=$this->input->post('noreg');		
		$updt=array(
			'sts_daftar' => $this->input->post('sts_daftar'),
			//'tindakan' => $this->input->post('tindakan'),
			//'reward' => $this->input->post('reawrd'),
			'keterangan' => $this->input->post('ket'),
			'tgl_update' => date('Y-m-d H:i:s',time()),
			'petugas_admision' => $this->session->userdata('userid'),
		);
		
		$update=$this->admision_panel_model->updatePxRujukanByAdmision($noreg,$updt);
		if($update){
		$this->session->set_flashdata('pesan', 'Update Status Registrasi Pasien Berhasil[By Admision]');
		}else{
			$this->session->set_flashdata('error', 'Gagal Update Status Pasien[By Admision]');
		}
		
		redirect(base_url('perujuk-daftar-pasien.html'));
	}
	
	function exsportPxPerujuk()
	{
		$idp=base64_decode($this->input->get('key'));
		$datapx=$this->admision_panel_model->getPxRujukanByID($idp);
		$dtrm=$this->admision_panel_model->getRMPx();
		
		//echo $dtrm->norm."<br>";
		
		foreach($datapx as $dtin){
			$idreg=$dtin->ID;
			$namax=explode(',',$dtin->Nama);
			$nama=$namax[0];
			$nik=$dtin->NIK;
			$kelamin=$dtin->JKel;
			$jnspenjamin=$dtin->JnsPenjamin;
			$pershID=$dtin->PershID;
			$noasuransi=$dtin->NoAsuransi;
			$notelp=$dtin->NoTelp;
			$tmplahir=$dtin->TmpLahir;
			$tgllahir=$dtin->TglLahir;
			$alamat=$dtin->Alamat;
			$kecamatan=$dtin->Kecamatan;
			$kota=$dtin->Kota;
			$propinsi=$dtin->Propinsi;			
		}
		
		$dtpxbaru=array(		
			'NoRM' =>$dtrm->norm,
			'Nama' =>$nama,
			'NIK' =>$nik,
			//'ketNamaID' =>$,
			'Alamat' =>$alamat,
			'T4Lhr' =>$tmplahir,
			'TglLhr' =>$tgllahir,
			'Kecamatan' =>$kecamatan,
			'Kab_Kota' =>$kota,
			'Propinsi' =>$propinsi,		
			'NoHP' =>$notelp,   
			'Kelamin' =>$kelamin,
			'JnsTransk' =>$jnspenjamin,	
			'PershID' =>$pershID,
			'NoAsuransi' =>$noasuransi,
			'user' =>$this->session->userdata('userid'),   
		);
		//print_r($dtpxbaru);
		
		$export=$this->admision_panel_model->insertToHospiPx($dtpxbaru);
		if($export){
			$updp=array(
				'sts_export'=>'1',
				);
			$this->admision_panel_model->updatePxRujukanByAdmision($idreg, $updp);
			$this->session->set_flashdata('pesan', 'Update Pasien ke SIMRS Berhasil[By Admision]');
		}else{
			$this->session->set_flashdata('error', 'Gagal Update Pasien ke SIMRS[By Admision]');
		}
		
		redirect(base_url('perujuk-daftar-pasien.html'));
	}
}
