<?php
class Utilisateur extends BaseModel {
    public static $tableName='utilisateur';
    public function __construct(){
        
    }

    public static function Login()
    {
    	global $BD;
        $pwd = $_REQUEST['password'];
        $result = array();
        // $res = $BD->prepare("SELECT * FROM administrateur WHERE pseudo=? AND password=?");
        // $res->execute(array($_REQUEST["pseudo"],$pwd));
        if ($_REQUEST['pseudo'] == 'admin@savanah.com' && $_REQUEST['password'] == 'savanah@2023') {            
            $_SESSION["iduser"] = 1;
            $_SESSION["nom"] = 'Administrateur';
            $_SESSION["pseudo"] = 'admin';
            $result['state'] = "success";
            header('Location: ../service');
        }
        else{
            $result['state'] = "error";
            $_SESSION['error'] = 'Mot de passe ou pseudo érroné';
            header('Location: ../login');
        }
    }

    public static function logout()
    {
        session_destroy();

        header('Location: home');
    }

    public static function supprimer()
    {
        global $BD;
        $result = array();

        $BD->query("DELETE FROM administrateur WHERE id IN (".$_REQUEST["id"].")");
        $result['state'] = "success";

        echo json_encode($result);
        
    }

    public static function addclient()
    {

		if (isset($_FILES["fichier"]) AND strlen($_FILES["fichier"]["name"])>0) {
			$folder = '../media/';
			$tmp_name = $_FILES["fichier"]["tmp_name"];

			$name = $_FILES["fichier"]["name"];

			$img = pathinfo($_FILES["fichier"]['name']);
			$cv = "client".time().'.'.$img["extension"];
            move_uploaded_file($tmp_name, $folder.$cv);
            header('Location: ../client');
        }
    }

    public static function deleteclient()
    {
        unlink('../media/'.$_REQUEST['fichier']);
        header('Location: ../client');
    }

}