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

class Perujuk_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('perujuk/perujuk_model');
		$this->load->model('admision/admision_panel_model');
		$this->load->model('inform_model');
		
		$this->data['tanggal']=date("Y-m-d", time());
		$this->data['setIDBaru']=$this->perujuk_model->getDIRegBaru();
		$this->data['setID']=$this->perujuk_model->getDIReg();
		$this->data['pintumasuk']=$this->perujuk_model->getPintuMasuk();
		$this->data['kecamatan']=$this->perujuk_model->getKecamatan();
		$this->data['asuransi']=$this->perujuk_model->getAsuransi();
		$this->data['perusahaan']=$this->perujuk_model->getPerusahaan();
		$this->data['penjamin']=$this->perujuk_model->getJenisPenjamin();
		$this->data['suku']=$this->perujuk_model->getSuku();
		$this->data['agama']=$this->perujuk_model->getAgama();
		$this->data['kelas']=$this->perujuk_model->getKelasKamar();
		
		$this->data['setpropinsi']=$this->perujuk_model->getPropinsi();
		
		foreach($this->data['setID'] as $dtid){
			$this->data['idreg']=$dtid->id;
		}
		
		
	}
	
	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.`Nama` LIKE '%".$this->data['nama']."%'";
			}else if($this->input->post('norm') <>''){
				$key="a.`NoRM`='".$this->data['norm']."'";
			}else if($this->input->post('nama') <>'' AND $this->input->post('norm') <>'')
			{
				$key="a.`Nama` LIKE '%".$this->data['nama']."%' OR a.`NoRM`='".$this->data['norm']."'";
			}else{
				$key="LEFT(a.`Tglsystem`,10)=LEFT(NOW(),10)";
			}
			$this->data['pasien']=$this->admision_panel_model->getCariDataPasien($key);
		}else{
			$this->data['pasien']=$this->admision_panel_model->getDataPasien();
		} */
		
		$this->load->view('perujuk/perujuk_tambah_pasien_baru',$this->data);
		
	}
	
	function saveRegistrasiPxBaruByPerujuk()
	{
		//print_r($_POST);exit();
		
		foreach($this->data['setIDBaru'] as $dtidb){
			$this->data['idregnew']=$dtidb->idn;
		}
		
		if($this->input->post('jenis_penjamin')=='1'){
			$perusahaanID='';
			$noAsuransi='';
		}else{
			$perusahaanID=$this->input->post('pers_penjamin');
			$noAsuransi=$this->input->post('no_asuransi');
		}
		/* $xdok=$this->input->post('dokter');
		$xxdok=explode("|",$xdok);
		$dokterid=$xxdok[0]; */
		
		$namanya=$this->input->post('nama').", ".$this->input->post('ket_nama');
		$baru=array(
			'ID' => $this->data['idregnew'],
			//'PasienID' => $this->input->post('pasienID'),
			'Nama' => $namanya,
			'JnsPenjamin' => $this->input->post('jenis_penjamin'),
			'JKel' => $this->input->post('jkel'),
			//'NoRM' => $this->input->post('no_rm'),
			'PershID' => $perusahaanID,//$this->input->post('pers_penjamin'),
			'NoAsuransi' => $noAsuransi, //$this->input->post('no_asuransi'),
			'TanggalMasuk' => $this->input->post('tgl_masuk'),
			'NIK' => $this->input->post('nik'),
			'NoTelp' => $this->input->post('hp'),
			'TmpLahir' => $this->input->post('tmp_lahir'),
			'TglLahir' => $this->input->post('tgl_lahir'),
			'Alamat' => $this->input->post('alamat'),
			'KategoriLamaBaru' => 'PASIEN BARU',//$this->input->post(''),
			'Kecamatan' => $this->input->post('kecamatan'),
			//'Dokter' => $dokterid, //$this->input->post('dokter'),
			//'jadwalDokter' => $this->input->post('jadwaldokter'),
			'PintuMasuk' => $this->input->post('poli'),
			'sts_daftar' => '1',
			'Kota' => $this->input->post('kota'),
			'Propinsi' => $this->input->post('propinsi'),
			'penanggung_jawab' => $this->input->post('pj'),			
			'hub_pj' => $this->input->post('hub_pj'),			
			'user' => $this->session->userdata('userid'),
			'Tglsystem' => date('Y-m-d H:i:s',time()),
			'anamnesia' => $this->input->post('anamnesia'),
			'terapi_perujuk' => $this->input->post('terapi_perujuk'),
			'diagnosa_perujuk' => $this->input->post('diagnosa_perujuk'),
			'periksa_perujuk' => $this->input->post('hasil_perujuk'),
			'kelas_kamar' => $this->input->post('kelas_kamar'),
		);
		//print_r($baru);exit();
		
		$save=$this->perujuk_model->simpanRegistrasiPx($baru);
		if($save){
			$this->session->set_flashdata('pesan', 'Registrasi Pasien Berhasil');
		}else{
			$this->session->set_flashdata('error', 'Gagal Mendaftarkan Pasien');
		}
		redirect(base_url('daftar-registrasi-byperujuk.html'));
	} 
	
	function cariPxByPerujuk()
	{
		$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.`Nama` LIKE '%".$this->data['nama']."%'";
			}else if($this->input->post('norm') <>''){
				$key="a.`NoRM`='".$this->data['norm']."'";
			}else if($this->input->post('nama') <>'' AND $this->input->post('norm') <>'')
			{
				$key="a.`Nama` LIKE '%".$this->data['nama']."%' OR a.`NoRM`='".$this->data['norm']."'";
			}else{
				$key="LEFT(a.`Tglsystem`,10)=LEFT(NOW(),10)";
			}
			$this->data['pasien']=$this->admision_panel_model->getCariDataPasien($key);
		}else{
			$this->data['pasien']=$this->admision_panel_model->getDataPasien();
		}
		
		$this->load->view('perujuk/perujuk_cari_pasien_lama',$this->data);
	}
	
	function regsitrasiPxByPerujuk()
	{
		//echo $this->input->get('key');
		$norm=base64_decode($this->input->get('key'));
		$this->data['pxbyrm']=$this->perujuk_model->getPxByNorm($norm);
		
		foreach($this->data['pxbyrm'] as $dtpx):
			$this->data['NoRM']=$dtpx->NoRM;
			$this->data['PasienID']=$dtpx->ID;
			$this->data['Nama']=$dtpx->Nama;
			$this->data['Alamat']=$dtpx->Alamat;
			$this->data['Kecamatan']=$dtpx->Kecamatan;
			$this->data['nmkecamatan']=$dtpx->nmkecamatan;
			$this->data['Kab_Kota']=$dtpx->Kab_Kota;
			$this->data['namakab']=$dtpx->namakab;
			$this->data['Propinsi']=$dtpx->Propinsi;
			$this->data['namaprop']=$dtpx->namaprop;
			$this->data['T4Lhr']=$dtpx->T4Lhr;
			$this->data['TglLhr']=$dtpx->TglLhr;
			$this->data['NIK']=$dtpx->NIK;
			$this->data['NoHP']=$dtpx->NoHP;
			$this->data['Item']=$dtpx->Item;
		endforeach;
		
		$this->load->view('perujuk/perujuk_registrasi_pasien_lama', $this->data);
	}
	
	function saveRegistrasiPxByPerujuk()
	{
		//print_r($_POST);exit();
		
		if($this->input->post('jenis_penjamin')=='1'){
			$perusahaanID='';
			$noAsuransi='';
		}else{
			$perusahaanID=$this->input->post('pers_penjamin');
			$noAsuransi=$this->input->post('no_asuransi');
		}
		
		$xdok=$this->input->post('dokter');
		$xxdok=explode("|",$xdok);
		$dokterid=$xxdok[0];
		
		$baru=array(
			'ID' => $this->data['idreg'],
			'PasienID' => $this->input->post('pasienID'),
			'Nama' => $this->input->post('nama'),
			'JnsPenjamin' => $this->input->post('jenis_penjamin'),
			'NoRM' => $this->input->post('no_rm'),
			'PershID' => $perusahaanID,//$this->input->post('pers_penjamin'),
			'NoAsuransi' => $noAsuransi, //$this->input->post('no_asuransi'),
			'TanggalMasuk' => $this->input->post('tgl_masuk'),
			'NIK' => $this->input->post('nik'),
			'NoTelp' => $this->input->post('hp'),
			'TmpLahir' => $this->input->post('tmp_lahir'),
			'TglLahir' => $this->input->post('tgl_lahir'),
			'Alamat' => $this->input->post('alamat'),
			'KategoriLamaBaru' => 'PASIEN LAMA',//$this->input->post(''),
			'Kecamatan' => $this->input->post('kecamatan'),
			//'Dokter' => $dokterid,//$this->input->post('dokter'),
			//'jadwalDokter' => $this->input->post('jadwaldokter'),
			'PintuMasuk' => $this->input->post('poli'),
			'sts_daftar' => '1',//$this->input->post('poli'),
			'Kota' => $this->input->post('kota'),
			'Propinsi' => $this->input->post('propinsi'),
			'penanggung_jawab' => $this->input->post('pj'),			
			'hub_pj' => $this->input->post('hub_pj'),			
			'user' => $this->session->userdata('userid'),
			'Tglsystem' => date('Y-m-d H:i:s',time()),
			'anamnesia' => $this->input->post('anamnesia'),
			'terapi_perujuk' => $this->input->post('terapi_perujuk'),
			'diagnosa_perujuk' => $this->input->post('diagnosa_perujuk'),
			'periksa_perujuk' => $this->input->post('hasil_perujuk'),
			'kelas_kamar' => $this->input->post('kelas_kamar'),
		);
		//print_r($baru);
		
		$save=$this->perujuk_model->simpanRegistrasiPx($baru);
		if($save){
			$this->session->set_flashdata('pesan', 'Registrasi Pasien Berhasil');
		}else{
			$this->session->set_flashdata('error', 'Gagal Mendaftarkan Pasien');
		}
		redirect(base_url('daftar-registrasi-byperujuk.html'));
	}
	
	function getDataRegistrasiPxByPerujuk()
	{
		$this->data['pasienr']=$this->perujuk_model->getDataRegPxByperujuk($this->session->userdata('userid'));
		
		$this->load->view('perujuk/perujuk_data_registrasi_pasien', $this->data);
	}
	
	//Kontrol
	function getDataRegistrasiAll()
	{
		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());
		}
		
		if($this->session->userdata('deptid')=='11'){
			$this->data['poli']='1';
		}else if($this->session->userdata('deptid')=='14'){
			$this->data['poli']='38';
		}else{
			$this->data['poli']='';
		}
		
		$this->data['tindakan']=$this->perujuk_model->getTindakan();
		$this->data['pasien']=$this->perujuk_model->getDataRegPxByperujukAll($this->data['tglaw'],$this->data['tglak'],$this->data['poli']);
		$this->data['pasienacc']=$this->perujuk_model->getDataRegPxByperujukAllAcc($this->data['tglaw'],$this->data['tglak'],$this->data['poli']);
		
		$this->load->view('perujuk/perujuk_data_registrasi_pasienbyadm', $this->data);
	}
	
	function simpanCekAdm()
	{
		//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'),
			'diagnosa_rs' => $this->input->post('diagnosa_rs'),
			'keterangan' => $this->input->post('ket'),
			'tgl_update' => date('Y-m-d H:i:s',time()),
			'petugas' => $this->session->userdata('userid'),
		);
		
		$update=$this->perujuk_model->updatePendaftaranByAdm($noreg,$updt);
		if($update){
			$this->session->set_flashdata('pesan', 'Update Status Registrasi Pasien Berhasil');
		}else{
			$this->session->set_flashdata('error', 'Gagal Update Status Pasien');
		}
		
		redirect(base_url('daftar-registrasi-all.html'));
	}
	
	//dummy
	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');
		
		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['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['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-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');
		
		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['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);
		}
	}
}
