<?php namespace App\Controllers;

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


class Patient extends BaseController
{

	public function index($orderBy="pseudo",$sens="ASC")
	{
		$mAC = new PatientModel() ;
    $data = [] ;

    if(null!==$this->request->getVar('action') && $this->request->getVar('action')=="new") $data['action'] = $this->request->getVar('action') ;
		else $data['action'] = "aucune" ;

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

	}

	public function supprime()
	{
		$mP = new PatientModel() ;
		$mPRE = new PrescriptionModel() ;
		$mRDV = new RdvModel() ;
		$pat = $mP->find($this->request->getVar('id')) ;

		$mRDV->join('prescription pre','pre.id = rdv.id_prescription','inner') ;
		$mRDV->where('pre.id_patient',$pat['id']) ;
		$mRDV->where('rdv.date >=',date('Y-m-d')) ;
		$mRDV->where('rdv.effectue',0) ;
    $test = $mRDV->first() ;

		if($test && (null===$this->request->getVar('force'))){
			echo "deja" ;
			// echo "SUPPRESSION IMPOSSIBLE : Ce patient a encore des rendez vous à venir." ;
		}
		else{
			$dir = "./assets/fichiers/patient/".$pat['id'] ;
			if(is_dir($dir) && glob($dir."/*.*")!==false){
				array_map('unlink', glob($dir."/*.*"));
				rmdir($dir);
			}
			$pres = $mPRE->where('id_patient',$pat['id'])->findAll() ;
			foreach ($pres as $key => $pre) {
				$mRDV->where('id_prescription',$pre['id'])->delete() ;
				$mPRE->where('id',$pre['id'])->delete() ;
			}
			$mP->where('id',$pat['id'])->delete() ;
		}


	}

	public function liste_snap()
	{
		$mP = new PatientModel() ;
		$data = [] ;
		$data['patient'] = $mP->find(intval($this->request->getVar('id'))) ;
		$dir = "./assets/fichiers/patient/".$data['patient']['id'] ;
		if(is_dir($dir)) $data['snaps'] = scandir($dir) ;
		else $data['snaps'] = null ;
		echo view("patient/liste_snap",$data) ;
	}

  public function trait_form()
  {
		$mAC = new PatientModel() ;
		if ($this->request->getVar('dn')!="") {
			$tmp = explode("/",$this->request->getVar('dn')) ;
			$dn = $tmp[2]."-".$tmp[1]."-".$tmp[0] ;
		} else $dn = "" ;

		if(intval($this->request->getVar('id'))>0){
			$ac = [
				'id'=>$this->request->getVar('id'),
				'nom'=>$this->request->getVar('nom'),
				'prenom'=>$this->request->getVar('prenom'),
				'dn'=>$dn,
				'adresse'=>$this->request->getVar('adresse'),
				'complement'=>$this->request->getVar('complement'),
				'cp'=>$this->request->getVar('cp'),
				'id_ville'=>$this->request->getVar('id_ville'),
				'tel'=>$this->request->getVar('tel'),
				'memo'=>$this->request->getVar('memo')
				] ;
			$mAC->save($ac) ;
			echo $ac['id'] ;
		}
		else{
			$ac = [
				'nom'=>$this->request->getVar('nom'),
				'prenom'=>$this->request->getVar('prenom'),
				'dn'=>$dn,
				'adresse'=>$this->request->getVar('adresse'),
				'complement'=>$this->request->getVar('complement'),
				'cp'=>$this->request->getVar('cp'),
				'id_ville'=>$this->request->getVar('id_ville'),
				'tel'=>$this->request->getVar('tel'),
				'memo'=>$this->request->getVar('memo')
				] ;
			$mAC->insert($ac) ;
			echo $mAC->getInsertID() ;
		}

  }

	public function trait_form_new()
	{
		$mP = new PatientModel() ;
		$pat = [
		  'nom'=>$this->request->getVar('nom'),
		  'prenom'=>$this->request->getVar('prenom'),
		  'adresse'=>$this->request->getVar('adresse'),
		  'cp'=>$this->request->getVar('cp'),
		  'id_ville'=>$this->request->getVar('id_ville'),
		  'tel'=>$this->request->getVar('tel')
		] ;
		$mP->insert($pat) ;
	}

	public function form_new()
	{
		$mP = new PatientModel() ;
		$data = [] ;
		echo view('patient/form_new',$data) ;
	}

	public function form()
	{
		helper(['form']) ;
		$mAC = new PatientModel() ;
		$data = [] ;
		$data['patient'] = $mAC->find(intval($this->request->getVar('id'))) ;
		if(!$data['patient']){
			$data['patient'] = ['id'=>0,'nom'=>'','prenom'=>'','dn'=>'','aff_dn'=>'','adresse'=>'','complement'=>'','cp'=>'','id_ville'=>'','memo'=>''] ;
		}
		else if($data['patient']['dn']!="" && $data['patient']['dn']!="0000-00-00"){
			$tmp = explode("-",$data['patient']['dn']) ;
			$data['patient']['aff_dn'] = $tmp[2]."/".$tmp[1]."/".$tmp[0] ;
		}
		else{
			$data['patient']['aff_dn'] = "" ;
		}

		// print_r($data['patient']) ;

		echo view('patient/form',$data) ;
	}

	public function modal_modif_colonne()
	{
		$mP = new PatientModel() ;
		$mRDV = new RdvModel() ;
		$mACT = new ActeModel() ;
		$mBU = new BoUserModel() ;
		$data = [] ;
		$tab_liste = explode(",",$this->request->getVar('liste')) ;
		$data['champ'] = $this->request->getVar('champ') ;
		$data['actes'] = $mACT->orderBy('libel')->findAll() ;
    $data['bo_users'] = $mBU->orderBy('pseudo')->findAll() ;
		switch ($data['champ']) {
			case 'petit_matin':
				$data['sous_titre'] = "petit matin" ;
			break;
			case 'mms':
				$data['sous_titre'] = "matin/midi/soir" ;
			break;
			case 'id_acte':
				$data['sous_titre'] = "type d'acte" ;
			break;
			case 'id_bo_user':
				$data['sous_titre'] = "effectué par" ;
			break;
		}
		echo view('patient/modal_modif_colonne',$data) ;
	}
	public function trait_modal_modif_colonne()
	{
		$mP = new PatientModel() ;
		$mRDV = new RdvModel() ;
		$mACT = new ActeModel() ;
		$mBU = new BoUserModel() ;
		$data = [] ;
		$tab_liste = explode(",",$this->request->getVar('liste')) ;
		$champ = $this->request->getVar('champ') ;
		foreach ($tab_liste as $key_id => $id) {
			$mRDV->where('id',$id) ;
			$mRDV->set($champ,$this->request->getVar('val_colonne')) ;
			$mRDV->update() ;
			// echo $mRDV->getLastQuery()."\n" ;
		}
	}

  public function supprime_snap()
  {
  	$mP = new PatientModel() ;
		$pat = $mP->find($this->request->getVar('id_patient')) ;
		$snap = $this->request->getVar('snap') ;
		$chemin = "./assets/fichiers/patient/".$pat['id']."/".$snap ;
		if(is_file($chemin)) unlink($chemin) ;
  }

	function if_doc($id){
		$mP = new PatientModel() ;
		$data = [] ;
		$data['patient'] = $mP->find(intval($id)) ;
		$data["upload"] = false ;
		$data['upload_erreur'] = false ;
		$data['texte_erreur'] = "" ;

		if($this->request->getMethod()=='post'){

		$rules['photo'] = [
      'rules' => 'uploaded[photo.0]',
      'label' => 'le fichier',
      'errors' => [
          'uploaded' => 'Vous devez selectionner un fichier',
          // 'max_size' => 'Le fichier image dépasse la taille autorisée de 2048kb',
          // 'is_image' => 'Le fichier image doit être un fichier image',
          // 'max_dims' => 'Les dimensions du fichier image doivent être max 2000x2000',
      ]
    ] ;

		if($this->validate($rules)){

			$fichiers = $this->request->getFiles() ;
      foreach ($fichiers['photo'] as $fichier) {
        if($fichier->isValid() && !$fichier->hasMoved()){
          $fichier->move('./assets/fichiers/patient/'.$data['patient']['id']) ;
          $nom_fic = $fichier->getName() ;
					$data["upload"] = true ;
       }
		}
	}
	else{
		$data['upload_erreur'] = true ;
		$data['texte_erreur'] = $this->validator->listErrors() ;
	}
}


		echo view("patient/if_doc",$data) ;
	}

	public function supprime_selection()
	{
		$mP = new PatientModel() ;
		$mRDV = new RdvModel() ;
		$mACT = new ActeModel() ;
		$mBU = new BoUserModel() ;
		$data = [] ;
		$tab_liste = explode(",",$this->request->getVar('liste')) ;
		foreach ($tab_liste as $key_id => $id) {
			$mRDV->where('id',$id)->delete() ;
		}
	}

	public function liste()
	{
		$mP = new PatientModel() ;
		$data = [] ;
		$data['rech_mc'] = ((null!==$this->request->getVar('rech_mc') && $this->request->getVar('rech_mc')!="undefined" && $this->request->getVar('rech_mc')!="")?$this->request->getVar('rech_mc'):"") ;
		if($data['rech_mc']!=""){
			$mP->groupStart() ;
			$mP->like('nom',$data['rech_mc']) ;
			$mP->orLike('prenom',$data['rech_mc']) ;
			$mP->orLike('adresse',$data['rech_mc']) ;
			$mP->orLike('cp',$data['rech_mc']) ;
			$mP->orLike('tel',$data['rech_mc']) ;
			$mP->groupEnd() ;
		}
    $mP->orderBy('nom, prenom') ;
    $data['patients'] = $mP->findAll() ;

		echo view('patient/liste',$data);
	}

}
