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

class Login extends CI_Controller
{

    public function _construct()
    {
        parent::__construct();
        $this->load->model('user_model');

    }

    public function index()
    {

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

            $this->session->set_flashdata('error', 'log in first');
            $data['alert'] = '';
            $this->load->view('login', $data);

        } else {

            $data['active'] = "dash";
            $this->load->view('header', $data);
            $this->load->view('admin_dash');
            $this->load->view('footer');
        }

    }

    public function register()
    {

        if (!$this->session->userdata('email')) {
            $this->session->set_flashdata('error', 'log in first');
            $data['alert'] = '';
            $this->load->view('register', $data);
        } else {
            $data['active'] = "dash";
            $this->load->view('header_admin', $data);
            $this->load->view('admin_dash');
            $this->load->view('footer');
        }
    }

    public function register_user()
    {

        $user = array(
            'user_name' => $this->input->post('user_name'),
            'user_email' => $this->input->post('user_email'),
            'user_password' => md5($this->input->post('user_password')),
            'user_age' => $this->input->post('user_age'),
            'user_mobile' => $this->input->post('user_mobile')
        );
        print_r($user);

        $email_check = $this->user_model->email_check($user['user_email']);

        if ($email_check) {
            $this->user_model->register_user($user);
            $this->session->set_flashdata('success_msg', 'Registered successfully.Now login to your account.');
            redirect('user/login_view');

        } else {

            $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
            redirect('user');


        }

    }


    public function validate()
    {

        $user_login = array(
            'user_email' => $this->input->post('user_mail'),
            'user_password' => $this->input->post('user_password')
        );

        $data = $this->user_model->login_user($user_login['user_email'], $user_login['user_password']);

        if ($data) {
            $this->session->set_userdata('id', $data['id']);
            $this->session->set_userdata('email', $data['email']);
            $this->session->set_userdata('username', $data['username']);
            $this->session->set_userdata('phone', $data['phone']);
            $this->session->set_userdata('adresse', $data['adresse']);
            $this->session->set_userdata('category', $data['category']);


            $hyper = "hyperviseur";
            $super = "superviseur";
            $admin = "admin";
            if ($data['category'] == $admin) {

                redirect('Admin_dash');
                //$this->load->view('user_profile.php');
            } else if ($data['category'] == $hyper) {
                redirect('Hyperviseur_dash');
            } else if ($data['category'] == $super) {
                redirect('Superviseur_dash');
            }

            $this->load->view('user_profile.php');

        } else {
            $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
            //$this->load->view("login.php");
            $data['alert'] = "ok";
            $data['message'] = "The Email " . $user_login['user_email'] . " or the password is incorrect!";

            //redirect('Login', $data);
            $this->load->view('login', $data);

        }

        /*
                   $this->load->database();

                   if($this->input->post('maill') && $this->input->post('password')){
                       $email=$this->input->post('maill');
                       $password = $this->input->post('password');
                       $m=$email;
                       $query= $this->db->query("SELECT * FROM membres WHERE Email='$email' and password='$password'");
                       $numrows=$query->num_rows();

                       if($numrows!=0){

                          foreach($query->result_array() as $row){
                             $dbemail=$row['Email'];
                       $dbusername=$row['username'];
                             $dbpassword=$row['password'];
                             $dbcat=$row['category'];
                                }

                          if($email==$dbemail && $password==$dbpassword  ){
                             $this->session->set_userdata('Email', $email);
                             $this->session->set_userdata('username', $dbusername);

                             if($dbcat==$simple){
                                 @$_SESSION['category']=$simple;
                                 redirect('Admin_dash');
                         } else if($dbcat==$super){
                               @$_SESSION['category']=$super;
                               redirect('Admin_dash');
                       } else if($dbcat==$admin){
                               @$_SESSION['category']=$admin;
                               redirect('Admin_dash');
                             }
                           } else {
                             // echo "email ou password incorrect";
                             $this->session->set_flashdata('error', 'email ou password incorrect');
                             $data['alert']="ok";
                             $data['message']="The Email or the password is incorrect!";

                             //redirect('Login', $data);
                             $this->load->view('login',$data);
                          }
                       } else{
                          $this->session->set_flashdata('error', 'email ou password incorrect');
                          $data['alert']="ok";
                          $data['message']="The Email or the password is incorrect!";

                          //redirect('Login',$data);
                          $this->load->view('login',$data);
                        }
                 } else {
                      die("erreur de connexion ");
                  $data['alert']="ok";
                  $data['message']="Connexion error!";

                      //redirect('Login',$data);
                      $this->load->view('login',$data);
                   }*/

    }

    public function validate_login_register()
    {


        if ($this->input->post('maill') && $this->input->post('password')) {
            $email = $this->input->post('maill');
            $password = $this->input->post('password');
            $m = $email;
            $query = $this->db->query("SELECT * FROM membres WHERE Email='$email' and password='$password'");
            $numrows = $query->num_rows();

            if ($numrows != 0) {

                foreach ($query->result_array() as $row) {
                    $dbemail = $row['Email'];
                    $dbusername = $row['username'];
                    $dbpassword = $row['password'];
                    $dbcat = $row['category'];
                }

                if ($email == $dbemail && $password == $dbpassword) {
                    $this->session->set_userdata('Email', $email);
                    $this->session->set_userdata('username', $dbusername);
                    $simple = "simple";
                    $super = "super";
                    $admin = "admin";

                    if ($dbcat == $simple) {
                        @$_SESSION['category'] = $simple;
                        redirect('Admin_dash');
                    } else if ($dbcat == $super) {
                        @$_SESSION['category'] = $super;
                        redirect('Admin_dash');
                    } else if ($dbcat == $admin) {
                        @$_SESSION['category'] = $admin;
                        redirect('Admin_dash');
                    }
                } else {
                    // echo "email ou password incorrect";
                    $this->session->set_flashdata('error', 'email ou password incorrect');
                    $data['alert'] = "ok";
                    $data['message'] = "The Email or the password is incorrect!";

                    //redirect('Login', $data);
                    $this->load->view('login', $data);
                }
            } else {
                $this->session->set_flashdata('error', 'email ou password incorrect');
                $data['alert'] = "ok";
                $data['message'] = "The Email or the password is incorrect!";

                //redirect('Login',$data);
                $this->load->view('login', $data);
            }
        } else {
            die("erreur de connexion ");
            $data['alert'] = "ok";
            $data['message'] = "Connexion error!";

            //redirect('Login',$data);
            $this->load->view('login', $data);
        }

    }

    function login_user()
    {
        $user_login = array(

            'user_email' => $this->input->post('user_email'),
            'user_password' => md5($this->input->post('user_password'))

        );

        $data = $this->user_model->login_user($user_login['user_email'], $user_login['user_password']);
        if ($data) {
            $this->session->set_userdata('user_id', $data['user_id']);
            $this->session->set_userdata('user_email', $data['user_email']);
            $this->session->set_userdata('user_name', $data['user_name']);
            $this->session->set_userdata('user_age', $data['user_age']);
            $this->session->set_userdata('user_mobile', $data['user_mobile']);

            $this->load->view('user_profile.php');

        } else {
            $this->session->set_flashdata('error_msg', 'Error occured,Try again.');
            $this->load->view("login.php");

        }


    }

    public function logout()
    {
        $this->session->sess_destroy();
        $data['alert'] = '';
        redirect('Login', $data);
    }
}

?>
