<?php namespace App\Controllers;

use App\Controllers\BaseController ;
use App\Models\BoUserModel ;
use App\Models\PatientModel ;

class Scan extends BaseController
{
	public function index()
	{
    $mBU = new BoUserModel() ;
		$data = [] ;
		$data['bu'] = $mBU->find(intval(session('id'))) ;
		$data['footer_js'] = "scan" ;
		if(null!==$this->request->getVar('facingMode') && $this->request->getVar('facingMode')=="user"){
			$data['facingMode'] = $this->request->getVar('facingMode') ;
      $data['facingChange'] = "environment" ;
		}
		else{
			$data['facingMode'] = "environment" ;
			$data['facingChange'] = "user" ;
		}

		// echo $data['facingMode']."<br>" ;
		// echo $data['facingChange']."<br>" ;

		echo view('header',$data) ;
		echo view("scan/index",$data) ;
		echo view('footer',$data) ;
	}

	public function simple()
	{
		$data =[] ;
		if(null!==$this->request->getVar('facingMode') && $this->request->getVar('facingMode')=="user"){
			$data['facingMode'] = $this->request->getVar('facingMode') ;
      $data['facingChange'] = "environment" ;
		}
		else{
			$data['facingMode'] = "environment" ;
			$data['facingChange'] = "user" ;
		}
		echo view("scan/simple",$data) ;
	}

	public function dirToArray($dir) {

   $result = array();

   $cdir = scandir($dir);
   foreach ($cdir as $key => $value)
   {
      if (!in_array($value,array(".","..")))
      {
         if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
         {
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
         }
         else
         {
            $result[] = $value;
         }
      }
   }

   return $result;
}

public function liste_snap()
{
	$mBU = new BoUserModel() ;
	$data = [] ;
	$data['bu'] = $mBU->find(intval(session('id'))) ;
	$dir = './assets/images/snap/'.$data['bu']['id'].'/' ;
	if(!is_dir($dir)) mkdir($dir) ;
	$data['tab'] = array_reverse($this->dirToArray($dir)) ;
	$data['dir'] = $dir ;

	echo view("scan/liste_snap",$data) ;

}

// public function liste_ordo()
// {
// 	$mBU = new BoUserModel() ;
// 	$data = [] ;
// 	$data['bu'] = $mBU->find(intval(session('id'))) ;
// 	$dir = './assets/images/snap/'.$data['bu']['id'].'/' ;
// 	if(!is_dir($dir)) mkdir($dir) ;
// 	$data['tab'] = array_reverse($this->dirToArray($dir)) ;
// 	$data['dir'] = $dir ;
//
// 	echo view("scan/liste_ordonnance",$data) ;
//
// }

	public function scan_document()
	{
		// $mPR = new PrescriptionModel() ;
		// $pre = $mPR->find(intval($this->request->getVar('id_prescription'))) ;
		$mP = new PatientModel() ;
		$patient = $mP->find($this->request->getVar('id_patient_scan')) ;
		if(!is_dir("./assets/fichiers")) mkdir("./assets/fichiers") ;
		if(!is_dir("./assets/fichiers/patient")) mkdir("./assets/fichiers/patient") ;
		if(!is_dir("./assets/fichiers/patient/".$patient['id'])) mkdir("./assets/fichiers/patient/".$patient['id']) ;
		$chemin = "./assets/fichiers/patient/".$patient['id']."/" ; //.$this->request->getVar('id_prescription').".png" ;
		// if(is_file($chemin)) unlink($chemin) ;
		$data = [] ;
		$data['base64'] = $this->request->getVar('base64') ;
		// if(!is_dir('./assets/images/ordonnance/')) mkdir('./assets/images/ordonnance/') ;
		// define('UPLOAD_DIR', './assets/images/ordonnance/');
		define('UPLOAD_DIR', "./assets/fichiers/patient/".$patient['id']."/");
		$image_parts = explode(";base64,", $data['base64']);
		$image_type_aux = explode("image/", $image_parts[0]);
		$image_type = $image_type_aux[1];
		$image_base64 = base64_decode($image_parts[1]);
		$file = UPLOAD_DIR . uniqid() . '.png';
		file_put_contents($file, $image_base64);
		echo "SCAN OK" ;
	}
	public function scan_ordonnance()
	{
		// $mPR = new PrescriptionModel() ;
		// $pre = $mPR->find(intval($this->request->getVar('id_prescription'))) ;
		$chemin = "./assets/images/ordonnance/".$this->request->getVar('id_prescription').".png" ;
		if(is_file($chemin)) unlink($chemin) ;
		$data = [] ;
		$data['base64'] = $this->request->getVar('base64') ;
		if(!is_dir('./assets/images/ordonnance/')) mkdir('./assets/images/ordonnance/') ;
		define('UPLOAD_DIR', './assets/images/ordonnance/');
		$image_parts = explode(";base64,", $data['base64']);
		$image_type_aux = explode("image/", $image_parts[0]);
		$image_type = $image_type_aux[1];
		$image_base64 = base64_decode($image_parts[1]);
		$file = UPLOAD_DIR . $this->request->getVar('id_prescription') . '.png';
		file_put_contents($file, $image_base64);
		echo "SCAN OK" ;
	}

	public function sav_snap()
	{
		$data = [] ;
		$data['base64'] = $this->request->getVar('base64') ;
		if(!is_dir('./assets/images/snap/'.$this->request->getVar('id_bu').'/')) mkdir('./assets/images/snap/'.$this->request->getVar('id_bu').'/') ;
		define('UPLOAD_DIR', './assets/images/snap/'.$this->request->getVar('id_bu').'/');
		$image_parts = explode(";base64,", $data['base64']);
		$image_type_aux = explode("image/", $image_parts[0]);
		$image_type = $image_type_aux[1];
		$image_base64 = base64_decode($image_parts[1]);
		$file = UPLOAD_DIR . uniqid() . '.png';
		file_put_contents($file, $image_base64);
		echo "SAV OK" ;
	}

}
