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

class Beranda 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('masuk_model');
		$this->load->model('inform_model');
		$this->load->model('mutu/indikator_model');	
		
		function setTglSql($str_tgl){
			$tglaja=strtotime($str_tgl);
			$bulan=date('m', $tglaja);
			$tahun=date('Y', $tglaja);
			$tglok=$tahun.'-'.$bulan.'-01';
			
			return $tglok;
		}
		
		function setTglAkhir($str_tgl){
			$tglaja=strtotime($str_tgl);
			$bulan=date('m', $tglaja);
			$tahun=date('Y', $tglaja);
			$tglok=$tahun.'-'.$bulan.'-31';
			
			return $tglok;
		}
		
		$date=date_create(date('Y-m-d'));
		date_modify($date, '-1 day');
		$tglkemarin=date_format($date, 'Y-m-d');
		
		//$this->data['enddate']=$tglkemarin;
		$this->data['enddate']=date('Y-m-d');
		
		$this->data['qtyxdokter']=$this->inform_model->getQtyXByDokterNow($this->session->userdata('userid'));
		//$this->data['pxvisitdokter']=$this->inform_model->getRekapPasienRanapByDOkter($this->session->userdata('dokterid'));
		
		$this->data['tanggal']=setTglSql(date('Y-m-d'));
		$this->data['tglakhir']=setTglAkhir(date('Y-m-d'));
		$this->data['result']=$this->indikator_model->getDataBorOK($this->data['tanggal'],$this->data['tglakhir']);
		
		$trawat=0;	$tlos=0; $tkeluar=0;
		$start_date = new DateTime($this->data['tanggal']);
		$end_date = new DateTime($this->data['enddate']);
		$interval = $start_date->diff($end_date);
		
		$tahunx = date('Y'); //Mengambil tahun saat ini
		$bulanx = date('m'); //Mengambil bulan saat ini
		$jmlbulanini = cal_days_in_month(CAL_GREGORIAN, $bulanx, $tahunx);

		//echo "-".$jmlbulanini."-";
		//echo $interval->days+1;
		
		if(date('m',strtotime($this->data['tanggal']))==date('m')){
			$jmlhari=$interval->days+1;
		}else{
			$jmlhari=$jmlbulanini;
		}
		
		//$this->data['jmhari']=$jmlhari;
		foreach($this->data['result'] as $dtrs):
			$trawat=$trawat + $dtrs->jml_dirawat;
			$tlos=$tlos + $dtrs->los;
			$tkeluar=$tkeluar + $dtrs->jml_keluar;
		endforeach;
		$this->data['bor']=round($trawat/(100*$jmlhari)*100,2);
		if($tkeluar > 0){
			$this->data['toi']=round(((100 * $jmlhari) - $trawat)/ $tkeluar,2);
		}else{
			$this->data['toi']=0;
		}
		$this->data['los']=round($tlos/$jmlhari,2);
		$this->data['bto']=round($tkeluar/100,2);
	}
	
	public function index()
	{
		$not_an_array=null;
		//if(!is_array($not_an_array) === false && (in_array('1',$this->session->userdata('grup')) || in_array('4',$this->session->userdata('grup')) || in_array('6',$this->session->userdata('grup')) || in_array('8',$this->session->userdata('grup')) || in_array('9',$this->session->userdata('grup')))):
		 if(in_array('1',$this->session->userdata('grup')) || in_array('4',$this->session->userdata('grup')) || in_array('6',$this->session->userdata('grup')) || in_array('8',$this->session->userdata('grup')) || in_array('9',$this->session->userdata('grup'))):
			$this->load->view('beranda_m',$this->data);
		else: 
			$this->load->view('beranda',$this->data);
		endif;
	}
	
	/* function getPasienByDokterNow()
	{
		$this->data['dokterid']=base64_decode($this->input->get('dok'));
		$this->data['jadwaldok']=base64_decode($this->input->get('jadwal'));
		$this->data['pxbydok']=$this->inform_model->getPasienByDokterNow($this->data['dokterid'],$this->data['jadwaldok']);
		$this->load->view('pasien_by_dokter',$this->data);
	}
	
	function getPasienByDokterNowDiagnosa()
	{
		$this->data['idpx']=base64_decode($this->input->get('key'));
		$this->data['diagnosapasien']=$this->inform_model->getDiagnosaByPasienDokter($this->data['idpx']);
		$this->load->view('pasien_by_dokter_diagnosa',$this->data);
	}
	
	function getPxVisitByDokter()
	{
		$this->data['dokid']=base64_decode($this->input->get('dok'));		
		$this->data['pxvisitbydok']=$this->inform_model->getPasienVisitByDokter($this->data['dokid']);
		$this->load->view('pasien_visit_by_dokter',$this->data);
	} */
}
