<?php
session_start();
class model {

    private function access(){

        include('../db.php');

        return $db;

    }

        public function login($email){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('SELECT * FROM users WHERE username=?');

                $requestLogin->execute(array($email));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        public function demande(){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('SELECT * FROM demandes WHERE valid=?');

                $requestLogin->execute(array(1));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        public function offre(){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('SELECT * FROM offres WHERE valid=?');

                $requestLogin->execute(array(1));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        public function detail_demande($id){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('SELECT * FROM demandes WHERE id=?');

                $requestLogin->execute(array($id));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        public function detail_offre($id){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('SELECT * FROM offres WHERE id=?');

                $requestLogin->execute(array($id));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        public function supprimer_demande($id){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('UPDATE demandes SET valid=? WHERE id=?');

                $requestLogin->execute(array(0,$id));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        public function supprimer_offre($id){

            $db=$this->access();

            try{

                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $requestLogin=$db->prepare('UPDATE offres SET valid=? WHERE id=?');

                $requestLogin->execute(array(0,$id));

                return $requestLogin;

            }

            catch(PDOException $e){

                echo "<script>alert(\"Error:".$e->getMessage()."   .\")</script>";  

            }

        }

        






}
?>