<?php namespace App\Controllers;

use App\Controllers\BaseController ;
use CodeIgniter\I18n\Time;
use App\Models\PatientModel ;
use App\Models\VisiteModel ;
use App\Models\PrescriptionModel ;
use App\Models\LPrescriptionActeModel ;
use App\Models\ActeModel ;
use App\Models\VilleModel ;
use App\Models\RdvModel ;
use App\Models\RdvFrequenceModel ;


class Agenda extends BaseController
{

	public function index()
	{
    $data = [] ;

		$data['aff'] = ((null!==$this->request->getVar('aff') && $this->request->getVar('aff')!="undefined" && $this->request->getVar('aff')!="")?$this->request->getVar('aff'):"j") ;
    $data['date'] = ((null!==$this->request->getVar('date') && $this->request->getVar('date')!="undefined" && $this->request->getVar('date')!="")?$this->request->getVar('date'):date('Y-m-d')) ;
    $tmp = explode("-",$data['date']) ;
		$data['affDate'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;

    $data['footer_js'] = "agenda" ;
    echo view('header',$data);
		echo view('agenda/index',$data);
		echo view('footer',$data);
	}

	public function getStartAndEndDate($year, $week)
{
   return [
      (new \DateTime())->setISODate($year, $week)->format('Y-m-d'), //start date
      (new \DateTime())->setISODate($year, $week, 7)->format('Y-m-d') //end date
   ];
}


	public function grille()
	{
		$mPA = new PatientModel() ;
		$mRDV = new RdvModel() ;
		$mVI = new VisiteModel() ;
		$mPR = new PrescriptionModel() ;
		$mLPA = new LPrescriptionActeModel() ;
		$mAC = new ActeModel() ;
		$data = [] ;
		$data['rech_voir'] = ((null!==$this->request->getVar('rech_voir') && $this->request->getVar('rech_voir')!="undefined" && $this->request->getVar('rech_voir')!="")?$this->request->getVar('rech_voir'):"") ;
    $data['mois'] = ["","janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre"] ;
    $data['jours'] = ["","lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"] ;
    $data['aff'] = $this->request->getVar('aff') ;
    $tmp = explode("-",$this->request->getVar('date')) ;
		$data['date'] = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
		$dat['tab_jour'] = [] ;
		$data['firstDecal'] = 0 ;

		if($data['aff']=="j"){
			$num_jour = $data['date']->getDayOfWeek() ;
			$data['entete'] = [0=>['date'=>$data['date'],'jour'=>$data['jours'][$num_jour]]] ;
		  $data['du'] = $data['au'] = $data['date'] ;
		}
		else if($data['aff']=="s"){
			// trouver la semaine en cours
			$num_sem = $data['date']->getWeekOfYear() ;
			$StartAndEnd = $this->getStartAndEndDate($data['date']->getYear(),$data['date']->getWeekOfYear()) ;
			$tmpdu = explode("-",$StartAndEnd[0]) ;
			$tmpau = explode("-",$StartAndEnd[1]) ;
			$data['du'] = Time::createFromDate($tmpdu[0],$tmpdu[1],$tmpdu[2]) ;
			$data['au'] = Time::createFromDate($tmpau[0],$tmpau[1],$tmpau[2]) ;
			$dateRef = $data['du'] ;
			$data['entete'] = [] ;
			$cpt=0;
			$fin = $data['au']->toDateString() ;
	    while($dateRef->toDateString()<=$fin){
				$cpt++;
				$data['entete'][] = ['date'=>$dateRef,'jour'=>$data['jours'][$cpt]] ;
				$dateRef = $this->date_apres($dateRef) ;
			}
		}
		else if($data['aff']=="m"){
      $annee = $data['date']->getYear() ;
      $mois = $data['date']->getMonth() ;
			$data['du'] = Time::createFromDate($annee,$mois,1) ;
			$au = new \DateTime('last day of '.$data['date']->getYear().'-'.$data['date']->getMonth());
			$data['au'] = Time::createFromDate($annee,$mois,$au->format('d')) ;
			$data['entete'] = [] ;
			for ($cpt=1; $cpt <= 7 ; $cpt++) {
				$data['entete'][] = ['date'=>'','jour'=>$data['jours'][$cpt]] ;
			}
			$data['firstDecal'] = $data['du']->getDayOfWeek() ;
		}

		$dateRef = $data['du'] ;
		$cpt=0;
		while($dateRef->toDateString()<=$data['au']->toDateString()){
			$cpt++;
			// $data['tab_jour'][$dateRef->toDateString()] = $mVI->where('date',$dateRef->toDateString())->orderBy('ordre')->findAll() ;
			if($data['rech_voir']=="1") $mRDV->where('petit_matin',1) ;
			if($data['rech_voir']=="2") $mRDV->where('petit_matin',0) ;
			if($data['rech_voir']=="3") $mRDV->where('mms',0) ;
			$data['tab_jour'][$dateRef->toDateString()] = $mRDV->where('date',$dateRef->toDateString())->orderBy('id')->findAll() ;
			$dateRef = $this->date_apres($dateRef) ;
		}

		/*
		foreach ($data['tab_jour'] as $key => $tab_visite) {
			foreach ($tab_visite as $key2 => $visite) {
				$tab_visite[$key2]['patient'] = $mPA->find(intval($visite['id_patient'])) ;
				$tab_visite[$key2]['patient']['prescription'] = $mPR->where('id_patient',$visite['id_patient'])->orderBy('id DESC')->first() ;
        $mAC->join('l_prescription_acte lpa','lpa.id_acte = acte.id','inner') ;
				$mAC->where('lpa.id_prescription',$tab_visite[$key2]['patient']['prescription']['id']) ;
				$tab_visite[$key2]['patient']['prescription']['actes'] = $mAC->findAll() ;

			}
			$data['tab_jour'][$key] = $tab_visite ;
		}
		*/

		// AVANT / APRES
    $data['dateAvant'] = $this->date_avant($data['du']) ;
    $data['dateApres'] = $this->date_apres($data['au']) ;
		/*
    $dateRef = $data['du'] ;
    // $dateStop = Time::createFromDate($tmpau[0],$tmpau[1],$tmpau[2]) ;
		$data['tab_jour'] = [] ;
		while($dateRef->toDateString()<=$data['du']->toDateString()){
			$data['tab_jour'][] = $dateRef ;
			$dateRef = $dateRef->tomorrow() ;
		}
		*/
		echo view("agenda/grille",$data) ;
	}

	public function grille_restant()
	{
		$mPA = new PatientModel() ;
		$mVI = new VisiteModel() ;
		$mRDV = new RdvModel() ;
		$mRDVF = new RdvFrequenceModel() ;
		$mPR = new PrescriptionModel() ;
		$mLPA = new LPrescriptionActeModel() ;
		$mAC = new ActeModel() ;
		$mVILLE = new VilleModel() ;
		$data = [] ;
		$data['rech_voir'] = ((null!==$this->request->getVar('rech_voir') && $this->request->getVar('rech_voir')!="undefined" && $this->request->getVar('rech_voir')!="")?$this->request->getVar('rech_voir'):"") ;
    $data['mois'] = ["","janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre"] ;
    $data['jours'] = ["","lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"] ;
    $data['aff'] = $this->request->getVar('aff') ;
    $tmp = explode("-",$this->request->getVar('date')) ;
		$data['date'] = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
		$dat['tab_jour'] = [] ;
		$data['firstDecal'] = 0 ;

		if($data['aff']=="j"){
			$num_jour = $data['date']->getDayOfWeek() ;
			$data['entete'] = [0=>['date'=>$data['date'],'jour'=>$data['jours'][$num_jour]]] ;
		  $data['du'] = $data['au'] = $data['date'] ;

		$dateRef = $data['du'] ;
		$cpt=0;
		while($dateRef->toDateString()<=$data['au']->toDateString()){
			$cpt++;
			// $data['tab_jour'][$dateRef->toDateString()] = $mVI->where('effectuee',0)->where('date',$dateRef->toDateString())->orderBy('ordre')->findAll() ;
			if($data['rech_voir']=="1") $mRDV->where('petit_matin',1) ;
			if($data['rech_voir']=="2") $mRDV->where('petit_matin',0) ;
			if($data['rech_voir']=="3") $mRDV->where('mms',1) ;
			$data['tab_jour'][$dateRef->toDateString()] = $mRDV->where("effectue",0)->where('date',$dateRef->toDateString())->orderBy('petit_matin DESC, matin DESC, midi DESC, soir DESC, id')->findAll() ;
			$dateRef = $this->date_apres($dateRef) ;
		}

		foreach ($data['tab_jour'] as $key => $tab_rdv) {
			foreach ($tab_rdv as $key2 => $rdv) {
				$tab_rdv[$key2]['prescription'] = $mPR->find(intval($rdv['id_prescription'])) ;
				$tab_rdv[$key2]['patient'] = $mPA->find(intval( $tab_rdv[$key2]['prescription']['id_patient'] )) ;
				$tab_rdv[$key2]['patient']['ville'] = $mVILLE->find(intval($tab_rdv[$key2]['patient']['id_ville'])) ;
				$tab_rdv[$key2]['patient']['prescription'] = $mPR->find(intval($rdv['id_prescription'])) ;
				$tab_rdv[$key2]['patient']['prescription']['acte'] = $mAC->find(intval($rdv['id_acte'])) ;
				$tab_rdv[$key2]['frequence'] = $mRDVF->find(intval($rdv['id_frequence'])) ;
				// if($rdv['petit_matin']>0) $tab_rdv[$key2]['bg_color'] = "#42f2f5" ;
				// else if($rdv['matin']>0) $tab_rdv[$key2]['bg_color'] = "#42b9f5" ;
				// else if($rdv['midi']>0) $tab_rdv[$key2]['bg_color'] = "#9ec2e8" ;
				// else if($rdv['soir']>0) $tab_rdv[$key2]['bg_color'] = "#e4cdf7" ;
				// else $tab_rdv[$key2]['bg_color'] = "#FFFFFF" ;
				$tab_rdv[$key2]['bg_color'] = "#FFFFFF" ;
			}
			$data['tab_jour'][$key] = $tab_rdv ;
		}

	  }

		echo view("agenda/grille_restant",$data) ;
	}
	public function grille_effectuee()
	{
		$mPA = new PatientModel() ;
		$mVI = new VisiteModel() ;
		$mVILLE = new VilleModel() ;
		$mPR = new PrescriptionModel() ;
		$mLPA = new LPrescriptionActeModel() ;
		$mAC = new ActeModel() ;
		$mRDV = new RdvModel() ;
		$mRDVF = new RdvFrequenceModel() ;
		$data = [] ;
		$data['rech_voir'] = ((null!==$this->request->getVar('rech_voir') && $this->request->getVar('rech_voir')!="undefined" && $this->request->getVar('rech_voir')!="")?$this->request->getVar('rech_voir'):"") ;
    $data['mois'] = ["","janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre"] ;
    $data['jours'] = ["","lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"] ;
    $data['aff'] = $this->request->getVar('aff') ;
    $tmp = explode("-",$this->request->getVar('date')) ;
		$data['date'] = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
		$dat['tab_jour'] = [] ;
		$data['firstDecal'] = 0 ;

		if($data['aff']=="j"){
			$num_jour = $data['date']->getDayOfWeek() ;
			$data['entete'] = [0=>['date'=>$data['date'],'jour'=>$data['jours'][$num_jour]]] ;
		  $data['du'] = $data['au'] = $data['date'] ;

		$dateRef = $data['du'] ;
		$cpt=0;
		while($dateRef->toDateString()<=$data['au']->toDateString()){
			$cpt++;
			if($data['rech_voir']=="1") $mRDV->where('petit_matin',1) ;
			if($data['rech_voir']=="2") $mRDV->where('petit_matin',0) ;
			if($data['rech_voir']=="3") $mRDV->where('mms',0) ;
			$data['tab_jour'][$dateRef->toDateString()] = $mRDV->where('effectue > ',0)->where('date',$dateRef->toDateString())->orderBy('petit_matin DESC, matin DESC, midi DESC, soir DESC, id')->findAll() ;
			$dateRef = $this->date_apres($dateRef) ;
		}

		foreach ($data['tab_jour'] as $key => $tab_rdv) {
			foreach ($tab_rdv as $key2 => $rdv) {
				$tab_rdv[$key2]['prescription'] = $mPR->find(intval($rdv['id_prescription'])) ;
				$tab_rdv[$key2]['patient'] = $mPA->find(intval( $tab_rdv[$key2]['prescription']['id_patient'] )) ;
				$tab_rdv[$key2]['patient']['ville'] = $mVILLE->find(intval($tab_rdv[$key2]['patient']['id_ville'])) ;
				$tab_rdv[$key2]['patient']['prescription'] = $mPR->find(intval($rdv['id_prescription'])) ;
				$tab_rdv[$key2]['patient']['prescription']['acte'] = $mAC->find(intval($rdv['id_acte'])) ;
				$tab_rdv[$key2]['frequence'] = $mRDVF->find(intval($rdv['id_frequence'])) ;
				// $tab_rdv[$key2]['bg_color'] = "#000000" ;
				// if($rdv['petit_matin']>0) $tab_rdv[$key2]['bg_color'] = "#42f2f5" ;
				// else if($rdv['matin']>0) $tab_rdv[$key2]['bg_color'] = "#42b9f5" ;
				// else if($rdv['midi']>0) $tab_rdv[$key2]['bg_color'] = "#9ec2e8" ;
				// else if($rdv['soir']>0) $tab_rdv[$key2]['bg_color'] = "#e4cdf7" ;
				// else $tab_rdv[$key2]['bg_color'] = "#FFFFFF" ;
				$tab_rdv[$key2]['bg_color'] = "#FFFFFF" ;
			}
			$data['tab_jour'][$key] = $tab_rdv ;
		}

	  }

		echo view("agenda/grille_effectuee",$data) ;
	}

	public function print_liste_jour($date)
	{
		$mPA = new PatientModel() ;
		$mVI = new VisiteModel() ;
		$mVILLE = new VilleModel() ;
		$mPR = new PrescriptionModel() ;
		$mLPA = new LPrescriptionActeModel() ;
		$mAC = new ActeModel() ;
		$mRDV = new RdvModel() ;
		$data = [] ;
		$data['date_sql'] = $date ;
		$tmpd = explode("-",$date) ;
		$data['date'] = $tmpd[2]."/".$tmpd[1]."/".$tmpd[0] ;

		$data['rdvs'] = $mRDV->where('date',$date)->orderBy('petit_matin DESC, matin DESC, midi DESC, soir DESC, id')->findAll() ;

		foreach ($data['rdvs'] as $key2 => $rdv) {
			$data['rdvs'][$key2]['prescription'] = $mPR->find(intval($rdv['id_prescription'])) ;
			$data['rdvs'][$key2]['patient'] = $mPA->find(intval( $data['rdvs'][$key2]['prescription']['id_patient'] )) ;
			$data['rdvs'][$key2]['patient']['ville'] = $mVILLE->find(intval($data['rdvs'][$key2]['patient']['id_ville'])) ;
			$data['rdvs'][$key2]['patient']['prescription'] = $mPR->find(intval($rdv['id_prescription'])) ;
			$data['rdvs'][$key2]['patient']['prescription']['acte'] = $mAC->find(intval($rdv['id_acte'])) ;
			// $data['rdvs'][$key2]['bg_color'] = "#000000" ;
			if($rdv['petit_matin']>0) $data['rdvs'][$key2]['bg_color'] = "#42f2f5" ;
			else if($rdv['matin']>0) $data['rdvs'][$key2]['bg_color'] = "#42b9f5" ;
			else if($rdv['midi']>0) $data['rdvs'][$key2]['bg_color'] = "#9ec2e8" ;
			else if($rdv['soir']>0) $data['rdvs'][$key2]['bg_color'] = "#e4cdf7" ;
			else $data['rdvs'][$key2]['bg_color'] = "#FFFFFF" ;
		}

		echo view("agenda/print_liste_jour",$data) ;
	}

	public function date_apres($dateRef)
	{
    if($dateRef->toDateString()=='2021-10-31') return Time::createFromDate(2021,11,1) ;
		else{
		  $time = $dateRef->getTimestamp() ;
		  $time+=(60*60*24) ;
		  return Time::createFromTimestamp($time) ;
	  }
	}

	public function date_avant($dateRef)
	{
		$time = $dateRef->getTimestamp() ;
		$time-=(60*60*24) ;
		return Time::createFromTimestamp($time) ;
	}


}
