<?php namespace App\Controllers;

use App\Controllers\BaseController ;
use App\Models\PatientModel ;
use App\Models\PrescriptionModel ;
use App\Models\ActeModel ;
use App\Models\BoUserModel ;
use App\Models\RdvModel ;
use App\Models\LPrescriptionActeModel ;


class Prescription extends BaseController
{
	public function patient_liste()
	{
		$mPA = new PatientModel() ;
		$mPR = new PrescriptionModel() ;
		$mRDV = new RdvModel() ;
		$mACT = new ActeModel() ;
		$mBU = new BoUserModel() ;
		$data = [] ;

		$data['actes'] = $mACT->orderBy('libel')->findAll() ;
    $data['bo_users'] = $mBU->orderBy('pseudo')->findAll() ;
		$data['start'] = ((null!==$this->request->getVar('start') && $this->request->getVar('start')!="undefined")?$this->request->getVar('start'):0) ;
    $data['nb'] = ((null!==$this->request->getVar('nb') && $this->request->getVar('nb')!="undefined")?$this->request->getVar('nb'):100) ;
    $data['order_by'] = (($this->request->getVar('order_by'))?$this->request->getVar('order_by'):'rdv.date') ;
    $data['order_sens'] = (($this->request->getVar('order_sens'))?$this->request->getVar('order_sens'):'DESC') ;

		$data['rech_acte'] = ((null!==$this->request->getVar('rech_acte') && $this->request->getVar('rech_acte')!="0" && $this->request->getVar('rech_acte')!="undefined")?$this->request->getVar('rech_acte'):0) ;
		$data['rech_effectue_par'] = ((null!==$this->request->getVar('rech_effectue_par') && $this->request->getVar('rech_effectue_par')!="0" && $this->request->getVar('rech_effectue_par')!="undefined")?$this->request->getVar('rech_effectue_par'):0) ;
		$data['rech_effectue'] = ((null!==$this->request->getVar('rech_effectue') && $this->request->getVar('rech_effectue')!="undefined")?$this->request->getVar('rech_effectue'):"") ;
		$data['rech_petit_matin'] = ((null!==$this->request->getVar('rech_petit_matin') && $this->request->getVar('rech_petit_matin')!="undefined")?$this->request->getVar('rech_petit_matin'):"") ;
		$data['rech_mms'] = ((null!==$this->request->getVar('rech_mms') && $this->request->getVar('rech_mms')!="undefined")?$this->request->getVar('rech_mms'):"") ;
		$data['rech_du'] = ((null!==$this->request->getVar('rech_du') && $this->request->getVar('rech_du')!="undefined")?$this->request->getVar('rech_du'):"") ;
		$data['rech_au'] = ((null!==$this->request->getVar('rech_au') && $this->request->getVar('rech_au')!="undefined")?$this->request->getVar('rech_au'):"") ;
		$data["patient"] = $mPA->find(intval($this->request->getVar('id'))) ;

    $mRDV->select('rdv.*') ;
    $mRDV->distinct() ;
    $mRDV->join("prescription pre","pre.id=rdv.id_prescription","inner") ;
		$mRDV->where('pre.id_patient',$data['patient']['id']) ;

		if($data['rech_effectue']!=""){
			if($data['rech_effectue']==1) $mRDV->where('rdv.effectue > ',0) ;
			else if($data['rech_effectue']==0) $mRDV->where('rdv.effectue',0) ;
		}
		if($data['rech_petit_matin']!="") $mRDV->where('rdv.petit_matin',$data['rech_petit_matin']) ;
		if($data['rech_mms']!="") $mRDV->where('rdv.mms',$data['rech_mms']) ;
		if($data['rech_du']!=""){
      $tmp = explode("/",$data['rech_du']) ;
			$du = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
			$mRDV->where('rdv.date >=',$du) ;
		}
		if($data['rech_au']!=""){
      $tmp = explode("/",$data['rech_au']) ;
			$au = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
			$mRDV->where('rdv.date <=',$au) ;
		}
		if(intval($data['rech_acte'])>0){
			$mRDV->where('id_acte',$data['rech_acte']) ;
		}
		if(intval($data['rech_effectue_par'])>0){
			$mRDV->where('rdv.effectue',$data['rech_effectue_par']) ;
		}

		$data['nbres'] = $mRDV->countAllResults(false) ;
		$mRDV->orderBy($data['order_by']." ".$data['order_sens']) ;
		$data['rdvs'] = $mRDV->findAll($data['nb'], $data['start']) ;
		$data['nbpages'] = ceil($data['nbres']/$data['nb']) ;
		$data['page'] = (($data['start']/$data['nb'])+1) ;
		// echo $mPR->getLastQuery() ;

		foreach ($data['rdvs'] as $key => $rdv) {
			$data['rdvs'][$key]['acte'] = $mACT->find(intval($rdv['id_acte'])) ;
			$tmp = explode("-",$rdv['date']) ;
			$data['rdvs'][$key]['aff_date'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
			$data['rdvs'][$key]['bo_user'] = $mBU->find(intval($rdv['effectue'])) ;
			// $nb_avant = $mPR->where('id_patient',$prescription['id_patient'])->where('id <',$prescription['id'])->countAllResults() ;
			// $data['rdvs'][$key]['libel'] = "PR-".$data['patient']['id']."-".($nb_avant+1) ;
			// $data['rdvs'][$key]['rdvs'] = $mRDV->where('id_prescription',$prescription['id'])->orderBy('id')->findAll() ;
			// foreach ($data['rdvs'][$key]['rdvs'] as $key2 => $rdv) {

			// }
		}
		echo view("prescription/patient_liste",$data) ;
	}

	public function trait_form()
	{
		$mPR = new PrescriptionModel() ;
		$tmp = explode("/",$this->request->getVar('date')) ;
		$date = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
		$tmp = explode("/",$this->request->getVar('du')) ;
		$du = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
		$tmp = explode("/",$this->request->getVar('au')) ;
		$au = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
		$pr = [
			'id'=>$this->request->getVar('id'),
			'date'=>$date,
			'du'=>$du,
			'au'=>$au,
			'details'=>$this->request->getVar('details')
			] ;
		$mPR->save($pr) ;
	}

	public function form()
	{
		$mPA = new PatientModel() ;
		$mPR = new PrescriptionModel() ;
		$data = [] ;
		$data["patient"] = $mPA->find(intval($this->request->getVar('id_patient'))) ;
		$data['prescription'] = $mPR->find(intval($this->request->getVar('id'))) ;

    if($data['prescription']){
			$nb_avant = $mPR->where('id_patient',$data['patient']['id'])->where('id <',$data['prescription']['id'])->countAllResults() ;
		}
		else{
			$nb_avant = $mPR->where('id_patient',$data['patient']['id'])->countAllResults() ;
			$pr = ['id_patient'=>$data['patient']['id']] ;
			$mPR->insert($pr) ;
			$id_new = $mPR->getInsertID() ;
			$data['prescription'] = $mPR->find($id_new) ;
		}
		if(null!==$data['prescription']['date']){
			$tmp = explode("-",$data['prescription']['date']) ;
			$data['prescription']['aff_date'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
		}
		else $data['prescription']['aff_date'] = "" ;

		if(null!==$data['prescription']['du']){
			$tmp = explode("-",$data['prescription']['du']) ;
			$data['prescription']['aff_du'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
		}
		else $data['prescription']['aff_du'] = "" ;

		if(null!==$data['prescription']['au']){
			$tmp = explode("-",$data['prescription']['au']) ;
			$data['prescription']['aff_au'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
		}
		else $data['prescription']['aff_au'] = "" ;

		$data['libel_prescription'] = "PR-".$data['patient']['id']."-".($nb_avant+1) ;

		echo view("prescription/form",$data) ;
	}

	public function new_acte()
	{
		$mLPA = new LPrescriptionActeModel() ;
		$lpa = ['id_prescription'=>$this->request->getVar('id'),'id_acte'=>$this->request->getVar('id_acte'),'nb_acte'=>$this->request->getVar('nb_acte')] ;
		$mLPA->insert($lpa) ;
	}

	public function liste_acte()
	{
		$mPA = new PatientModel() ;
		$mAC = new ActeModel() ;
		$mPR = new PrescriptionModel() ;
		$mLPA = new LPrescriptionActeModel() ;
		$data = [] ;
		$data['tab_actes'] = $mAC->orderBy('libel')->findAll() ;
		$data['prescription'] = $mPR->find(intval($this->request->getVar('id'))) ;
		$data['actes'] = $mLPA->where('id_prescription',$data['prescription']['id'])->orderBy('id')->findAll() ;
		foreach ($data['actes'] as $key => $acte) {
			$data['actes'][$key]['acte'] = $mAC->find($acte['id_acte']) ;
		}

		echo view("prescription/liste_acte",$data) ;
		// $data["patient"] = $mPA->find(intval($this->request->getVar('id_patient'))) ;

	}

}
