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


class Login extends CI_Controller 
{       
    function __construct() 
    {
        parent::__construct();
        $this->load->model('main_model');
        $this->load->database();
        $this->load->helper('form');
        $this->load->library('session');
        $this->load->helper('url');
    }

    /** Default Function **/
    public function index() 
    {
        $page_data['page_title'] = 'SMART EDUCATION MANAGEMENT SYSTEM';
        $this->load->view('login', $page_data);

    }

    /** Validate Login **/
    public function validate_login()
    { 
        $query = $this->db->get_where('user', array (
                'email' => $this->input->post('email'),
                'password' => $this->input->post('password'),
                'status' => 'Active'
        ));

        if ($query->num_rows() > 0) 
        {
            $row = $query->row();

            $data = array('status'=>'1', 'user_id'=>$row->id, 'user_type'=>$row->type, 'user_email'=>$row->email);
            $this->session->set_userdata($data);
            
            redirect('everbright');
            
        } else 
        {
            $page_data['page_title'] = 'Login or Register';
            $this->load->view('login', $page_data);
        }

    }

    
    function logout() 
    {
            $data = array('status'=>'', 'user_id'=>'', 'user_type'=>'', 'user_email'=>'');
            $this->session->unset_userdata($data);
            $this->session->sess_destroy();
            $this->session->set_flashdata('logout_notification', 'logged_out');
            redirect(base_url(), 'refresh');
    }


}

?>
