<?php namespace App\Controllers;

use App\Controllers\BaseController ;
use CodeIgniter\I18n\Time;
use App\Models\BoUserModel ;
use App\Models\PatientModel ;
use App\Models\ActeModel ;
use App\Models\PrescriptionModel ;
use App\Models\RdvModel ;
use App\Models\RdvFrequenceModel ;

class Rdv extends BaseController
{

	public function new()
	{
		$mR = new RdvModel() ;
		$mRF = new RdvFrequenceModel() ;
		$mB = new BoUserModel() ;
		$mP = new PatientModel() ;
		$mA = new ActeModel() ;
    $data = [] ;

		$data['frequences'] = $mRF->orderBy('id')->findAll() ;

		$data['mois'] = ["","janvier","fevrier","mars","avril","mai","juin","juillet","aout","septembre","octobre","novembre","decembre"] ;
    $data['jours'] = ["","lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"] ;

		$tmp = explode("-",$this->request->getVar('date')) ;
		$data['date'] = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
		$data['bu'] = $mB->find(intval(session('id'))) ;

		// $tab_deja = $mVI->where('date',$data['date']->toDateString())->findColumn('id_patient') ;
		// if($tab_deja) $mPA->whereNotIn('id',$tab_deja) ;




		$num_jour = $data['date']->getDayOfWeek() ;
		$data['jour'] = $data['jours'][$num_jour] ;

    echo view('rdv/new',$data);

	}

	public function liste_patient()
	{
		$mR = new RdvModel() ;
		$mB = new BoUserModel() ;
		$mP = new PatientModel() ;
		$mA = new ActeModel() ;
    $data = [] ;
		$data['patients'] = $mP->orderBy('nom, prenom')->findAll() ;
    // $data["patient"] = $mP->find(intval($this->request->getVar('id_patient'))) ;
		// $data['actes'] = $mA->orderBy('libel')->findAll() ;

		echo view('rdv/liste_patient',$data);
	}
	public function liste_acte()
	{
		$mR = new RdvModel() ;
		$mB = new BoUserModel() ;
		$mP = new PatientModel() ;
		$mA = new ActeModel() ;
    $data = [] ;
    $data["patient"] = $mP->find(intval($this->request->getVar('id_patient'))) ;
		$data['actes'] = $mA->orderBy('libel')->findAll() ;

		echo view('rdv/liste_acte',$data);
	}

	public function acte_details()
	{
		$mR = new RdvModel() ;
		$mB = new BoUserModel() ;
		$mP = new PatientModel() ;
		$mA = new ActeModel() ;
    $data = [] ;
    $data["patient"] = $mP->find(intval($this->request->getVar('id_patient'))) ;
    $data["acte"] = $mA->find(intval($this->request->getVar('id_acte'))) ;

		echo view('rdv/acte_details',$data);
	}

	public function acte_details_multiple()
	{
		$mR = new RdvModel() ;
		$mB = new BoUserModel() ;
		$mP = new PatientModel() ;
		$mA = new ActeModel() ;
    $data = [] ;

    $data["patient"] = $mP->find(intval($this->request->getVar('id_patient'))) ;
    $data["acte"] = $mA->find(intval($this->request->getVar('id_acte'))) ;
    $data['type_acte'] = $this->request->getVar('type_acte') ;
    /*
		$data['jours'] = [
			['num'=>1,'jour'=>'lundi','abrev'=>'L'],
			['num'=>2,'jour'=>'mardi','abrev'=>'M'],
			['num'=>3,'jour'=>'mercredi','abrev'=>'M'],
			['num'=>4,'jour'=>'jeudi','abrev'=>'J'],
			['num'=>5,'jour'=>'vendredi','abrev'=>'V'],
			['num'=>6,'jour'=>'samedi','abrev'=>'S'],
			['num'=>7,'jour'=>'dimanche','abrev'=>'D'],
		] ;
		*/

		echo view('rdv/acte_details_multiple',$data);
	}

	public function details_repete()
	{
		$mR = new RdvModel() ;
		$mB = new BoUserModel() ;
		$mP = new PatientModel() ;
		$mA = new ActeModel() ;
    $data = [] ;
		$data['jours'] = ["","lundi","mardi","mercredi","jeudi","vendredi","samedi","dimanche"] ;

    $data["new_date"] = $this->request->getVar('new_date') ;
    $data["id_bo_user"] = $this->request->getVar('id_bo_user') ;
    $data["patient"] = $mP->find(intval($this->request->getVar('id_patient'))) ;
    $data["acte"] = $mA->find(intval($this->request->getVar('id_acte'))) ;
    $data['type_acte'] = $this->request->getVar('type_acte') ;
    $data['diff_repete'] = $this->request->getVar('diff_repete') ;
    $data['type_repete'] = $this->request->getVar('type_repete') ;


		$tmp = explode("-",$this->request->getVar('new_date')) ;
		$dd = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;

		switch ($data['type_repete']) {
			case 'j':
				$df = $dd->addMonths(1);
				$tmp = explode("-",$df->toDateString()) ;
				$data['df'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
				$diff = $dd->difference($df);
				$data['nbo'] = $diff->getDays() ;
			break;
			case 's':
			  $data['num_jour'] = $dd->getDayOfWeek() ;
			  $df = $dd->addDays(7*13) ;
				$tmp = explode("-",$df->toDateString()) ;
				$data['df'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
				$diff = $dd->difference($df);
				$data['nbo'] = $diff->getWeeks() ;
			break;
			case 'm':
			  $data['libel_choix_1'] = "Tous les mois le ".$dd->getDay() ;
			  $data['num_jour'] = $dd->getDayOfWeek() ;
				switch ($dd->getWeekOfMonth()) {
					case '1' :
						$tmp = "premier" ;
					break;
					case '2' :
						$tmp = "deuxième" ;
					break;
					case '3' :
						$tmp = "troisième" ;
					break;
					case '4' :
						$tmp = "quatrième" ;
					break;
					case '5' :
						$tmp = "cinquième" ;
					break;
					case '6' :
						$tmp = "sixième" ;
					break;
				}
			  $data['num_sem'] = $dd->getWeekOfMonth() ;
				$data['libel_choix_2'] = "Tous les  mois le ".$tmp." ". $data['jours'][intval($dd->getDayOfWeek())] ;
			  $df = $dd->addMonths(12) ;
			  $tmp = explode("-",$df->toDateString()) ;
			  $data['df'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
			  $diff = $dd->difference($df);
			  $data['nbo'] = $diff->getMonths() ;
			break;
			case 'a':
			  $df = $dd->addYears(5) ;
			  $tmp = explode("-",$df->toDateString()) ;
			  $data['df'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
			  $diff = $dd->difference($df);
			  $data['nbo'] = $diff->getYears() ;
			break;
		}

		$data['jours'] = [
			['num'=>1,'jour'=>'lundi','abrev'=>'L'],
			['num'=>2,'jour'=>'mardi','abrev'=>'M'],
			['num'=>3,'jour'=>'mercredi','abrev'=>'M'],
			['num'=>4,'jour'=>'jeudi','abrev'=>'J'],
			['num'=>5,'jour'=>'vendredi','abrev'=>'V'],
			['num'=>6,'jour'=>'samedi','abrev'=>'S'],
			['num'=>7,'jour'=>'dimanche','abrev'=>'D'],
		] ;

		echo view('rdv/details_repete',$data);
	}

	public function details_rdv()
	{
		$mPRE = new PrescriptionModel() ;
		$mPAT = new PatientModel() ;
		$mACT = new ActeModel() ;
		$mRDV = new RdvModel() ;
		$mRDVF = new RdvFrequenceModel() ;

		$data = [] ;
		$data['frequences'] = $mRDVF->orderBy('id')->findAll() ;
		$data['rdv'] = $mRDV->find(intval($this->request->getVar('id'))) ;
		$data['actes'] = $mACT->orderBy('libel')->findAll() ;
		$tmp = explode("-",$data['rdv']['date']) ;
		$data['rdv']['aff_date'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
		$data['acte'] = $mACT->find(intval($data['rdv']['id_acte'])) ;
		$data['prescription'] = $mPRE->find(intval($data['rdv']['id_prescription'])) ;
		$data['patient'] = $mPAT->find(intval($data['prescription']['id_patient'])) ;

		echo view('rdv/details_rdv',$data) ;
	}

  public function modal_make_effectue()
  {
  	$mRDV = new RdvModel() ;
		$mPRE = new PrescriptionModel() ;
		$mP = new PatientModel() ;
		$data = [] ;
		$data['id'] = $this->request->getVar("id") ;
		$rdv = $mRDV->where("id",$this->request->getVar("id"))->first() ;
		$pre = $mPRE->find($rdv['id_prescription']) ;
		$data['rdv'] = $rdv ;
	  $data['rdv']['patient'] = $mP->find($pre['id_patient']) ;
		$tmp = explode("-",$rdv['date']) ;
		$data['rdv']['aff_date'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
		echo view('rdv/modal_make_effectue',$data) ;
  }

	public function make_effectuee()
	{
		$mRDV = new RdvModel() ;
		$mRDV->where('id',$this->request->getVar('id')) ;
		$mRDV->set('effectue',intval(session('id'))) ;
		$mRDV->set('com_effectue',$this->request->getVar('com_effectue')) ;
		$mRDV->update() ;
	}
	public function make_not_effectuee()
	{
		$mRDV = new RdvModel() ;
		$mRDV->where('id',$this->request->getVar('id')) ;
		$mRDV->set('effectue',0) ;
		$mRDV->update() ;
	}

	public function trait_details_rdv()
	{
		$mRDV = new RdvModel() ;
		$mRDV->where('id',$this->request->getVar('id')) ;
		$tmp = explode("/",$this->request->getVar('date')) ;
		$mRDV->set('date',$tmp[2]."-".$tmp[1]."-".$tmp[0]) ;
		$mRDV->set('id_acte',$this->request->getVar('id_acte')) ;
    $mRDV->set('petit_matin',0) ;
		$mRDV->set('matin',0) ;
		$mRDV->set('midi',0) ;
		$mRDV->set('soir',0) ;
		$mRDV->set('id_frequence',$this->request->getVar('frequence')) ;
		// $mRDV->set('mms',$this->request->getVar('mms')) ;
		$mRDV->update() ;
		$mRDV->where('id',$this->request->getVar('id')) ;
		$mRDV->set($this->request->getVar('type'),1) ;
		$mRDV->update() ;
	}

	public function supprime()
	{
		$mPRE = new PrescriptionModel() ;
		$mRDV = new RdvModel() ;

		$rdv = $mRDV->find(intval($this->request->getVar('id'))) ;
		$pre = $mPRE->find(intval($rdv['id_prescription'])) ;

		$nbrdv = $mRDV->where('id_prescription',$rdv['id_prescription'])->countAllResults() ;
		if(!$nbrdv>1) $mPRE->whre('id',$pre['id'])->delete() ;
		$mRDV->where('id',$rdv['id'])->delete() ;

	}

	public function sup_rdv()
	{
		$mRDV = new RdvModel() ;
		$id = $this->request->getVar('id') ;
		$mRDV->where('id',$id)->delete() ;
	}

	public function maj_rdv()
	{
		$mRDV = new RdvModel() ;
		$id = $this->request->getVar('id') ;
		$champ = $this->request->getVar('champ') ;
		if($champ=="date"){
			$tmp = explode("/",$this->request->getVar('val')) ;
			$val = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
		}
		else $val = $this->request->getVar('val') ;
		$mRDV->where('id',$id) ;
		$mRDV->set($champ,$val) ;
		$mRDV->update() ;
		// echo $mRDV->getLastQuery() ;
	}

	public function trait_new_rdv()
	{
		$mPRE = new PrescriptionModel() ;
		$mPAT = new PatientModel() ;
		$mACT = new ActeModel() ;
		$mRDV = new RdvModel() ;

		$tmp = explode("-",$this->request->getVar('new_date')) ;
		$dd = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
		$patient = $mPAT->find(intval($this->request->getVar('id_patient'))) ;
		$acte = $mACT->find(intval($this->request->getVar('id_acte'))) ;
    $diff_repete = $this->request->getVar('diff_repete') ;
    $fin_actes = $this->request->getVar('fin_actes') ;

		$petit_matin = 0 ;
		$matin = 0 ;
		$midi = 0 ;
		$soir = 0 ;
		if($this->request->getVar('type_new')=="petit_matin") $petit_matin = 1 ;
		else if($this->request->getVar('type_new')=="matin") $matin = 1 ;
		else if($this->request->getVar('type_new')=="midi") $midi = 1 ;
		else if($this->request->getVar('type_new')=="soir") $soir = 1 ;

		// 1 ON CREE UNE PRESCRIPTION A LA VOLEE
		$pre = [
			'id_patient'=>$patient['id'],
			'date'=>date('Y-m-d'),
			'details'=>"",
			'renouvelable'=>intval($this->request->getVar('renouvelable'))
		] ;
		$mPRE->insert($pre) ;
		$id_prescription = $mPRE->getInsertID() ;

		// 2 ON CREE AUTANT DE RDV LIEE A LA PRESCRIPTION QUE DEMANDE
		if($this->request->getVar('type_acte')=="unique"){
      $rdv = [
			  'id_prescription'=>$id_prescription,
			  'id_acte'=>$acte['id'],
			  'num_jour'=>$dd->getDayOfWeek(),
			  'date'=>$this->request->getVar('new_date'),
			  'id_bo_user'=>$this->request->getVar('id_bo_user'),
			  'petit_matin'=>$petit_matin,
			  'matin'=>$matin,
			  'midi'=>$midi,
			  'soir'=>$soir,
				'id_frequence'=>$this->request->getVar("frequence_new"),
				'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
			] ;
			$mRDV->insert($rdv) ;
		}
		else if($this->request->getVar('type_acte')=="multiple"){

      switch ($this->request->getVar('type_repete')) {
      	case 'j':
          if($fin_actes=="d"){ // se finit à une date (tous les diff jour jusqu'a la date)
            $date_fin = $this->request->getVar('date_fin') ;
            $tmpf = explode("/",$date_fin) ;
						$df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;
						$tmp = explode("-",$this->request->getVar('new_date')) ;
						$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
						while($dref->isBefore($df)||$dref->sameAs($df)){
              $rdv = [
							  'id_prescription'=>$id_prescription,
							  'id_acte'=>$acte['id'],
							  'num_jour'=>$dref->getDayOfWeek(),
								'date'=>$dref->toDateString(),
								'id_bo_user'=>$this->request->getVar('id_bo_user'),
								'petit_matin'=>$petit_matin,
							  'matin'=>$matin,
							  'midi'=>$midi,
							  'soir'=>$soir,
								'id_frequence'=>$this->request->getVar("frequence_new"),
								'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
							] ;
							$mRDV->insert($rdv) ;
							$dref = $dref->addDays($diff_repete) ;
						}
					}
					else if($fin_actes=="o"){ // se finit apres un nb occur
						$nb_o = $this->request->getVar('nb_o') ;
						$tmp = explode("-",$this->request->getVar('new_date')) ;
						$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
						for ($i=1; $i <=$nb_o ; $i++) {
							$rdv = [
							  'id_prescription'=>$id_prescription,
							  'id_acte'=>$acte['id'],
							  'num_jour'=>$dref->getDayOfWeek(),
								'date'=>$dref->toDateString(),
								'id_bo_user'=>$this->request->getVar('id_bo_user'),
								'petit_matin'=>$petit_matin,
							  'matin'=>$matin,
							  'midi'=>$midi,
							  'soir'=>$soir,
								'id_frequence'=>$this->request->getVar("frequence_new"),
								'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
							] ;
							$mRDV->insert($rdv) ;
							$dref = $dref->addDays($diff_repete) ;
						}
					}
      	break;
      	case 's':
				  if($fin_actes=="d"){ // se finit à une date (tous les diff jour jusqu'a la date)
            // on obtiens le lundi de la semaine en cours de la dd
						$num_jour_dd = $dd->getDayOfWeek() ;
						$lundi_ref = $dd->subDays($num_jour_dd-1) ;

						$date_fin = $this->request->getVar('date_fin') ;
						$tmpf = explode("/",$date_fin) ;
						$df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;

						$tab_js = explode(",",$this->request->getVar('liste_semaine')) ;
						$stop = false ;

						while(!$stop){
							$tmp = explode("-",$lundi_ref->toDateString()) ;
							$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;// la dref est le lundi
							echo count($tab_js) ;
              foreach ($tab_js as $key => $num) { // pour chaque jour de la semaine selectionné
              	$dins = $dref->addDays($num-1) ;
								if(($dd->isBefore($dins) || $dd->sameAs($dins)) && ($dins->isBefore($df) || $dins->sameAs($df) ) ){ // si date d'ins n'est pas avant la dd
								  $rdv = [
								    'id_prescription'=>$id_prescription,
								    'id_acte'=>$acte['id'],
								    'num_jour'=>$dins->getDayOfWeek(),
									  'date'=>$dins->toDateString(),
									  'id_bo_user'=>$this->request->getVar('id_bo_user'),
										'petit_matin'=>$petit_matin,
									  'matin'=>$matin,
									  'midi'=>$midi,
									  'soir'=>$soir,
										'id_frequence'=>$this->request->getVar("frequence_new"),
										'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
								  ] ;
								  $mRDV->insert($rdv) ;
							  }
								else if(!$dins->isBefore($df) && !$dins->sameAs($df)) $stop = true ;
              }

						  $lundi_ref = $lundi_ref->addDays(7*$diff_repete) ; // semaine suivante

						}

					}
				  else if($fin_actes=="o"){ // se finit apres un nb occur

						// on obtiens le lundi de la semaine en cours de la dd
						$num_jour_dd = $dd->getDayOfWeek() ;
						$lundi_ref = $dd->subDays($num_jour_dd-1) ;

						$tab_js = explode(",",$this->request->getVar('liste_semaine')) ;
						$stop = false ;
						$nb_ins = 0 ;
						$nb_o = $this->request->getVar('nb_o') ;

						while(!$stop){
							$tmp = explode("-",$lundi_ref->toDateString()) ;
							$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;// la dref est le lundi
              foreach ($tab_js as $key => $num) { // pour chaque jour de la semaine selectionné
              	$dins = $dref->addDays($num-1) ;
								if(($dd->isBefore($dins) || $dd->sameAs($dins)) && $nb_ins<$nb_o){ // si date d'ins n'est pas avant la dd
								  $rdv = [
								    'id_prescription'=>$id_prescription,
								    'id_acte'=>$acte['id'],
								    'num_jour'=>$dins->getDayOfWeek(),
									  'date'=>$dins->toDateString(),
									  'id_bo_user'=>$this->request->getVar('id_bo_user'),
										'petit_matin'=>$petit_matin,
									  'matin'=>$matin,
									  'midi'=>$midi,
									  'soir'=>$soir,
										'id_frequence'=>$this->request->getVar("frequence_new"),
										'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
								  ] ;
								  $mRDV->insert($rdv) ;
									$nb_ins++;
							  }
								else if($nb_ins>=$nb_o) $stop = true ;
              }

						  $lundi_ref = $lundi_ref->addDays(7*$diff_repete) ; // semaine suivante

						}

					}
      	break;
      	case 'm':
				  if($fin_actes=="d"){ // se finit à une date (tous les diff mois jusqu'a la date)
            $choix = $this->request->getVar('choix_repete_mois') ;
						if($choix==1){ // ex le 10 de chaque mois
              // ma date ref
							$tmp = explode("-",$this->request->getVar('new_date')) ;
							$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
							// ma date de fin
							$date_fin = $this->request->getVar('date_fin') ;
							$tmpf = explode("/",$date_fin) ;
							$df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;

							while( ($dref->isBefore($df) || $dref->sameAs($df)) ){
								$rdv = [
								  'id_prescription'=>$id_prescription,
								  'id_acte'=>$acte['id'],
								  'num_jour'=>$dref->getDayOfWeek(),
									'date'=>$dref->toDateString(),
									'id_bo_user'=>$this->request->getVar('id_bo_user'),
									'petit_matin'=>$petit_matin,
								  'matin'=>$matin,
								  'midi'=>$midi,
								  'soir'=>$soir,
									'id_frequence'=>$this->request->getVar("frequence_new"),
									'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
								] ;
								$mRDV->insert($rdv) ;
								$dref = $dref->addMonths($diff_repete) ;
							}

						}
						else if($choix==2){ // ex le 2eme vendredi de chaque mois

							// ma date ref
							$tmp = explode("-",$this->request->getVar('new_date')) ;
							$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
							// ma date de fin
							$date_fin = $this->request->getVar('date_fin') ;
							$tmpf = explode("/",$date_fin) ;
							$df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;
							// quelle jour et quelle semaine
							$num_jour_ref = $dref->getDayOfWeek() ;
							$num_sem_ref = $dref->getWeekOfMonth() ;

							while( ($dref->isBefore($df) || $dref->sameAs($df)) ){
								$rdv = [
								  'id_prescription'=>$id_prescription,
								  'id_acte'=>$acte['id'],
								  'num_jour'=>$dref->getDayOfWeek(),
									'date'=>$dref->toDateString(),
									'id_bo_user'=>$this->request->getVar('id_bo_user'),
									'petit_matin'=>$petit_matin,
								  'matin'=>$matin,
								  'midi'=>$midi,
								  'soir'=>$soir,
									'id_frequence'=>$this->request->getVar("frequence_new"),
									'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
								] ;
								$mRDV->insert($rdv) ;

								// 1er jour du mois de la $dref
								$firstDay = Time::createFromDate($dref->getYear(),$dref->getMonth(),1) ;
								// premier jour du moi suivant
								$dref = $firstDay->addMonths(1) ;

								// ON RECHERCHE le Neme num jour demandé
								$stop = false ;
								$nbOk = 0 ;
								while(!$stop){
									if($dref->getDayOfWeek()==$num_jour_ref) $nbOk++;
									if($nbOk==$num_sem_ref) $stop = true ;
									else $dref = $dref->addDays(1) ;
								}

							}


						}
				  }
				  else if($fin_actes=="o"){ // se finit apres un nb occur
            $nb_o = $this->request->getVar('nb_o') ;
						$choix = $this->request->getVar('choix_repete_mois') ;
						if($choix==1){ // ex le 10 de chaque mois
              // ma date ref
							$tmp = explode("-",$this->request->getVar('new_date')) ;
							$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
							// ma date de fin
							$date_fin = $this->request->getVar('date_fin') ;
							$tmpf = explode("/",$date_fin) ;
							$df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;

							for ($i=1; $i <=$nb_o ; $i++) {
								$rdv = [
								  'id_prescription'=>$id_prescription,
								  'id_acte'=>$acte['id'],
								  'num_jour'=>$dref->getDayOfWeek(),
									'date'=>$dref->toDateString(),
									'id_bo_user'=>$this->request->getVar('id_bo_user'),
									'petit_matin'=>$petit_matin,
								  'matin'=>$matin,
								  'midi'=>$midi,
								  'soir'=>$soir,
									'id_frequence'=>$this->request->getVar("frequence_new"),
									'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
								] ;
								$mRDV->insert($rdv) ;
								$dref = $dref->addMonths($diff_repete) ;
							}

						}
						else if($choix==2){ // ex le 2eme vendredi de chaque mois

							// ma date ref
							$tmp = explode("-",$this->request->getVar('new_date')) ;
							$dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
							// ma date de fin
							$date_fin = $this->request->getVar('date_fin') ;
							$tmpf = explode("/",$date_fin) ;
							$df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;
							// quelle jour et quelle semaine
							$num_jour_ref = $dref->getDayOfWeek() ;
							$num_sem_ref = $dref->getWeekOfMonth() ;

							for ($i=1; $i <=$nb_o ; $i++) {
								$rdv = [
								  'id_prescription'=>$id_prescription,
								  'id_acte'=>$acte['id'],
								  'num_jour'=>$dref->getDayOfWeek(),
									'date'=>$dref->toDateString(),
									'id_bo_user'=>$this->request->getVar('id_bo_user'),
									'petit_matin'=>$petit_matin,
								  'matin'=>$matin,
								  'midi'=>$midi,
								  'soir'=>$soir,
									'id_frequence'=>$this->request->getVar("frequence_new"),
									'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
								] ;
								$mRDV->insert($rdv) ;

								// 1er jour du mois de la $dref
								$firstDay = Time::createFromDate($dref->getYear(),$dref->getMonth(),1) ;
								// premier jour du moi suivant
								$dref = $firstDay->addMonths(1) ;

								// ON RECHERCHE le Neme num jour demandé
								$stop = false ;
								$nbOk = 0 ;
								while(!$stop){
									if($dref->getDayOfWeek()==$num_jour_ref) $nbOk++;
									if($nbOk==$num_sem_ref) $stop = true ;
									else $dref = $dref->addDays(1) ;
								}
							}

						}

				  }
      	break;
      	case 'a':
				  if($fin_actes=="d"){ // se finit à une date (tous les diff ans jusqu'a la date)
					  $date_fin = $this->request->getVar('date_fin') ;
					  $tmpf = explode("/",$date_fin) ;
					  $df = Time::createFromDate($tmpf[2],$tmpf[1],$tmpf[0]) ;
					  $tmp = explode("-",$this->request->getVar('new_date')) ;
					  $dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
					  while($dref->isBefore($df)||$dref->sameAs($df)){
						  $rdv = [
							  'id_prescription'=>$id_prescription,
							  'id_acte'=>$acte['id'],
							  'num_jour'=>$dref->getDayOfWeek(),
							  'date'=>$dref->toDateString(),
							  'id_bo_user'=>$this->request->getVar('id_bo_user'),
								'petit_matin'=>$petit_matin,
							  'matin'=>$matin,
							  'midi'=>$midi,
							  'soir'=>$soir,
								'id_frequence'=>$this->request->getVar("frequence_new"),
								'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
						  ] ;
						  $mRDV->insert($rdv) ;
						  $dref = $dref->addYears($diff_repete) ;
					  }
				  }
				  else if($fin_actes=="o"){ // se finit apres un nb occur
					  $nb_o = $this->request->getVar('nb_o') ;
					  $tmp = explode("-",$this->request->getVar('new_date')) ;
					  $dref = Time::createFromDate($tmp[0],$tmp[1],$tmp[2]) ;
					  for ($i=1; $i <=$nb_o ; $i++) {
						  $rdv = [
							  'id_prescription'=>$id_prescription,
							  'id_acte'=>$acte['id'],
							  'num_jour'=>$dref->getDayOfWeek(),
							  'date'=>$dref->toDateString(),
							  'id_bo_user'=>$this->request->getVar('id_bo_user'),
								'petit_matin'=>$petit_matin,
							  'matin'=>$matin,
							  'midi'=>$midi,
							  'soir'=>$soir,
								'id_frequence'=>$this->request->getVar("frequence_new"),
								'horaire_souhaite'=>$this->request->getVar("horaire_souhaite")
						  ] ;
						  $mRDV->insert($rdv) ;
						  $dref = $dref->addYears($diff_repete) ;
					  }
				  }
      	break;
      }

		}

		// var param = "id_bo_user="+$("#new_id_bo_user").val() ;
    // param+="&new_date="+$("#new_date").val() ;
    // param+="&id_patient="+$("#new_id_patient").val() ;
    // param+="&id_acte="+$("#new_id_acte").val() ;
    // param+="&type_acte="+type_acte ;
    // param+="&diff_repete="+$("#new_acte_diff_repete").val() ;
    // param+="&type_repete="+type_repete ;
    // param+="&liste_semaine="+liste_semaine ;
    // param+="&choix_repete_mois="+choix_repete_mois ;
    // param+="&fin_actes="+fin_actes ;
    // param+="&date_fin="+$("#new_acte_date_fin").val() ;
    // param+="&nb_o="+$("#new_acte_nb_o").val() ;
    // param+="&renouvelable="+renouvelable ;
	}


}
