<?php namespace App\Controllers;

use App\Controllers\BaseController ;
use App\Models\ActeModel ;


class Acte extends BaseController
{
	public function index($orderBy="pseudo",$sens="ASC")
	{
		$mAC = new ActeModel() ;
    $data = [] ;

    $data['actes'] = $mAC->orderBy('libel')->findAll() ;

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

	}

	public function ajouter()
	{
		$mAC = new ActeModel() ;
		$act = [
		  "libel"=>""
		] ;
		$mAC->insert($act) ;
	}

  public function trait_form()
  {
		$mAC = new ActeModel() ;
		if(intval($this->request->getVar('id'))>0){
			$ac = [
				'id'=>$this->request->getVar('id'),
				'libel'=>$this->request->getVar('libel'),
				'abrev'=>$this->request->getVar('abrev'),
				'duree'=>$this->request->getVar('duree')
				] ;
			$mAC->save($ac) ;
			echo $ac['id'] ;
		}
		else{
			$ac = [
				'libel'=>$this->request->getVar('libel'),
				'abrev'=>$this->request->getVar('abrev'),
				'duree'=>$this->request->getVar('duree')
				] ;
			$mAC->insert($ac) ;
			echo $mAC->getInsertID() ;
		}

  }

	public function supprime()
	{
		$mAC = new ActeModel() ;
		$mAC->where('id',$this->request->getVar('id'))->delete() ;
	}

	public function maj()
	{
		$mAC = new ActeModel() ;
		$mAC->where('id',$this->request->getVar('id')) ;
		$mAC->set($this->request->getVar('champ'),$this->request->getVar('val')) ;
		$mAC->update() ;
	}

	public function form()
	{
		helper(['form']) ;
		$mAC = new ActeModel() ;
		$data = [] ;
		$data['acte'] = $mAC->find(intval($this->request->getVar('id'))) ;
		if(!$data['acte']){
			$data['acte'] = ['id'=>0,'libel'=>'','abrev'=>'','duree'=>''] ;
		}
		echo view('acte/form',$data) ;
	}

	public function liste()
	{
		$mAC = new ActeModel() ;
		$data = [] ;
		$data['actes'] = $mAC->orderBy('libel')->findAll() ;
		echo view('acte/liste',$data);
	}

}
