<?php
session_start();
class model {

    private function access(){

        include('db.php');

        return $db;

    }

       



        //Connexion

        public function login($username){

            $db=$this->access();

            try{

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

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

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

                return $requestLogin;

            }

            catch(PDOException $e){

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

            }

        }

        // modification de profil
        public function profil_modifier($username,$id){
            $db=$this->access();
            try{
            
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            //unicité
            $id_unique=$db->prepare('SELECT * FROM admin WHERE username=? AND id <> ?');
            $id_unique->execute(array($username,$id));
          
            $result_id_unique = $id_unique->rowCount();
            if($result_id_unique == 0){
                $profil_modifier = $db->prepare('UPDATE admin SET username=? WHERE id=?');
                $profil_modifier->execute(array($username,$id));
                return $profil_modifier;
            }else {
                # code...
                //echo "<script>alert(\"Cet utilisateur existe déjà  .\")</script>";  
                $_SESSION['mod_profil']="existe";
            }
            
            }
            catch(PDOException $e){
          
            echo "<script>alert(\"Erreur:".$e->getMessage()."   .\")</script>";  
            }
          }
          public function password_modifier($new_password,$id){
            $db=$this->access();
            try{
            
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            
            
                $password_modifier = $db->prepare('UPDATE admin SET password=? WHERE id=?');
                $password_modifier->execute(array($new_password,$id));
                return $password_modifier;
           
            
            }
            catch(PDOException $e){
          
            echo "<script>alert(\"Erreur:".$e->getMessage()."   .\")</script>";  
            }
          }
          
          public function un_profil($id){
            $db=$this->access();
            try{
            
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          
            $un_profil=$db->prepare('SELECT * FROM admin WHERE admin.id = ?');
            $un_profil->execute(array($id));
            return $un_profil;
            }
            catch(PDOException $e){
          
            echo "<script>alert(\"Erreur:".$e->getMessage()."   .\")</script>";  
            }
          }
          
          
          public function verif_mdp($new_password,$id){
            $db=$this->access();
            try{
            
            $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          
            $verif_mdp=$db->prepare('SELECT admin.* FROM admin WHERE password=? AND id = ?');
            $verif_mdp->execute(array($new_password,$id));
            return $verif_mdp;
            }
            catch(PDOException $e){
          
            echo "<script>alert(\"Erreur:".$e->getMessage()."   .\")</script>";  
            }
          }
          // Fin modification de profil

        public function getDepotPerfect(){

            $db=$this->access();

            try{

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

                $getDepotPerfect=$db->prepare('SELECT depot_perfect.*, user.nom, user.pre FROM depot_perfect, user WHERE depot_perfect.user=user.id AND depot_perfect.valider=?');

                $getDepotPerfect->execute(array(0));

                return $getDepotPerfect;

            }

            catch(PDOException $e){

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

            }

        }

        public function getDepotInter(){

            $db=$this->access();

            try{

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

                $getDepotInter=$db->prepare('SELECT depot_internationaux.*, user.nom, user.pre FROM depot_internationaux, user WHERE depot_internationaux.user=user.id AND depot_internationaux.valider=?');

                $getDepotInter->execute(array(0));

                return $getDepotInter;

            }

            catch(PDOException $e){

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

            }

        }

        // recuperer l'id du user effectuant le depot
        public function infoDepot(string $table, $id){

            $db=$this->access();

            try{

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

                $infoDepot=$db->prepare('SELECT user FROM ' . $table . ' WHERE id=?');

                $infoDepot->execute(array($id));
                while ($infoDepots=$infoDepot->fetch()) {
                    # code...
                    $client=$infoDepots['user'];
                }
                return $client;

            }

            catch(PDOException $e){

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

            }

        }

        public function validDepot(string $table, $id){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('UPDATE ' . $table . ' SET valider=? WHERE id=? ');

                $validDepot->execute(array(1,$id));

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function annulerDepot(string $table, $id){

            $db=$this->access();

            try{

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

                $annulerDepot=$db->prepare('UPDATE ' . $table . ' SET valider=? WHERE id=? ');

                $annulerDepot->execute(array(-1,$id));

                return $annulerDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function infoUser($id){

            $db=$this->access();

            try{

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

                $infoUser=$db->prepare('SELECT email FROM user WHERE id=?');

                $infoUser->execute(array($id));
                while ($infoUsers=$infoUser->fetch()) {
                    # code...
                    $clit=$infoUsers['email'];
                }

                return $clit;

            }

            catch(PDOException $e){

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

            }

        }

        public function pmUser($id){

            $db=$this->access();

            try{

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

                $pmUser=$db->prepare('SELECT adresse_pm FROM user WHERE id=?');

                $pmUser->execute(array($id));
                while ($pmUsers=$pmUser->fetch()) {
                    # code...
                    $pm=$pmUsers['adresse_pm'];
                }

                return $pm;

            }

            catch(PDOException $e){

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

            }

        }

        public function misActive($id){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('UPDATE user SET strategieAcheter=? WHERE id=? ');

                $validDepot->execute(array(1,$id));

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function misSolde($id){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('UPDATE user SET solde=? WHERE id=? ');

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

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function utilisateur(){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('SELECT * FROM user');

                $validDepot->execute(array());

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function inserReversement($user,$mnt,$pm){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('INSERT INTO reversement(user,mnt,pm) VALUES(?,?,?)');

                $validDepot->execute(array($user,$mnt,$pm));

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function reversement(){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('SELECT reversement.*, user.nom, user.pre FROM reversement,user WHERE user.id=reversement.user');

                $validDepot->execute(array());

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function infoFormation(){

            $db=$this->access();

            try{

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

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

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

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function infoConfiguration(){

            $db=$this->access();

            try{

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

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

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

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function modFormation($nom,$prix,$prix_usd){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('UPDATE formation SET nom=?, prix=?, prix_usd=? WHERE id=?');

                $validDepot->execute(array($nom,$prix,$prix_usd,1));

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function modConfiguration($adresse_pm,$pourcentage){

            $db=$this->access();

            try{

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

                $validDepot=$db->prepare('UPDATE param SET adresse_pm=?, pourcentage=? WHERE id=?');

                $validDepot->execute(array($adresse_pm,$pourcentage,1));

                return $validDepot;

            }

            catch(PDOException $e){

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

            }

        }

        public function infoCode($code){

            $db=$this->access();

            try{

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

                $infoCode=$db->prepare('SELECT id FROM user WHERE code_perso=?');

                $infoCode->execute(array($code));
                while ($infoCodes=$infoCode->fetch()) {
                    # code...
                    $client=$infoCodes['id'];
                }
                return $client;

            }

            catch(PDOException $e){

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

            }

        }

        





}
?>