<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();

    }

    public function index($params = NULL) {

      if (!empty($this->session->userdata('SOCAR_ID_USER'))) {

        $message='<div class="alert alert-success text-center" id="message">Connexion bien etablie!<br> Les menus sont à gauche</div>';

        $this->session->set_flashdata(array('message'=>$message));    
        redirect(base_url('Acceuil'));


    } else {
       if (!empty($this->session->flashdata('message'))) {
          $message= $this->session->flashdata('message');
      }
      else{
          $message= NULL;

          $datas['message'] = $message;
          $datas['title'] = 'Login';
          $this->load->view('Login_View', $datas);

      }
  }
}

public function do_login() {

    $login = $this->input->post('USERNAME');
    $password = $this->input->post('PASSWORD');

    $user= $this->Model->getOne('config_user',array('USERNAME'=>$login,'STATUS'=>1));


    if (!empty($user)) {
          // echo 'good';
        // print_r($user);exit();

        if ($user['PASSWORD'] == md5($password))

        {

          $droits = $this->Model->getRequete("Select ID_DROIT FROM config_profil_droit WHERE PROFIL_ID = ".$user['PROFIL_ID']."");
          $listdroi[] =NULL;
          foreach ($droits as $key) {
           $listdroi[] .= $key['ID_DROIT'];
       }


       $session = array(
          'SOCAR_ID_USER' => $user['ID_USER'],
          'SOCAR_NOM' => $user['NOM'],
          'SOCAR_PRENOM' => $user['PRENOM'],
          'SOCAR_USERNAME' => $user['USERNAME'],
          'SOCAR_PROFIL_ID' => $user['PROFIL_ID'],
          'ID_EMPLOYE' => $user['ID_EMPLOYE'],
          'SOCAR_DROIT'=>$listdroi,
      );



       $message = "<div class='alert alert-success' id='messages'> Bonne Connexion</div>";
       $this->session->set_userdata($session);

       redirect(base_url('Acceuil'));

   }else{
    $message = "<div class='alert alert-danger' id='messages'> Le  mot de passe incorect(s) !</div>";
    $this->session->set_flashdata(array('message'=>$message));
    redirect(base_url('Login'));

}
}else{
    $message = "<div class='alert alert-danger' id='messages'> Le nom d'utilisateur ou/et mot de passe incorect(s) !</div>";
    $this->session->set_flashdata(array('message'=>$message));
    redirect(base_url());
}
}

public function do_logout(){

    $session = array(
      'SOCAR_ID_USER' => NULL,
      'SOCAR_NOM' => NULL,
      'SOCAR_PRENOM' => NULL,
      'SOCAR_USERNAME' => NULL,
  );                   
    $this->session->set_userdata($session);
    redirect(base_url('Login'));
}




public function valider() 
{

   $USER_ID=$this->input->post('USER_ID');
   $criteres['USER_ID']=$USER_ID;
   $user= $this->Model->getOne('config_user',$criteres);
   $p1 = $this->input->post('PASSWORD');
   $this->form_validation->set_rules('PASSWORD', '', 'trim|required',array('required'=>'Le mot de passe est requis'));
   $this->form_validation->set_rules('PASSWORD_CONFIRM', '', 'trim|required|matches[PASSWORD]',array('matches'=>'Les deux mots de passe doivent etre identiques','required'=>'La confirmation du mot passe est requis'));
   if ($this->form_validation->run()==FALSE)
   {
      $datas['USER_ID'] = $USER_ID;
      $datas['message'] = '';
      $datas['title'] = 'Login';
      $this->load->view('Recover_Password_View', $datas);
  }

  else
  {
      $this->Model->update('config_user',array('USER_ID'=>$USER_ID),array('PASSWORD'=>md5($p1)));
      $message = "<div class='alert alert-info' id='messages'> Récupperation du mot de passe fait avec succès !</div>"; 
      $this->index($message);
  }

}


public function password_recover($params = NULL) {

 if (!empty($this->session->userdata('SOCAR_ID_USER'))) {

    $datas['message']='<div class="alert alert-success text-center" id="message">Connexion bien etablie!<br> Les menus sont à gauche</div>';
    $this->session->set_flashdata($datas);         
    redirect(base_url('Acceuil'));
} else {

    $datas['message'] = $params;
    $datas['title'] = 'Forget Password';
    $this->load->view('Forgot_Password_View', $datas);

}
}



public function do_reset_password() {
    $login = $this->input->post('USERNAME');

    $user= $this->Model->getOne('rh_employe',array('EMAIL'=>$login,'STATUS'=>1));

    if (!empty($user)) {

      $PASSWORD = $this->notifications->generate_UIID(6);

      $this->Model->update('rh_employe',array('ID_EMPLOYE'=>$user['ID_EMPLOYE']), array('PASSWORD'=>md5($PASSWORD)));
      $this->Model->update('config_user',array('ID_EMPLOYE'=>$user['ID_EMPLOYE']), array('PASSWORD'=>md5($PASSWORD)));

      $obj = 'Reinitialisation du mot de passe';
      if ($user['ID_SEXE'] == 1) {
        $message = 'Monsieur '.$user['NOM'].' '.$user['PRENOM'].',<br>';
    }
    else{
     $message = 'Madame '.$user['NOM'].' '.$user['PRENOM'].',<br>';
 }


 $message.= 'Vous avez demandé une réinitialisation de votre mot de passe sur le système de gestion de documents du Mininter.<br><br>Vos nouvelles identifiants de connexion sur le système de gestion de documents du Mininter sont :<br>Nom d\'utilisateur : <b>'.$user['EMAIL'].'</b><br>Mot de passe : <b>'.$PASSWORD.'</b><br><br>Lien de connexion sur la plateforme : <a href="'.base_url().'" target="_blank">Système Gestion de Document</a> ';
 $this->notifications->newsend_mail($user['EMAIL'],$obj,NULL,$message,NULL);

 $message = "<div class='alert alert-success' id='messages'> Mot de passe changé. Vérifier vos mails.</div>";
 $this->password_recover($message);
}
else{
  $message = "<div class='alert alert-danger' id='messages'> Le nom d'utilisateur incorect !</div>";
  $this->password_recover($message);
}






}



}