<?php
defined('BASEPATH') or exit('No direct script access allowed');
error_reporting(E_ALL);
ini_set('display_errors', 1);

class Login extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('login_m');
        $this->load->helper(array('form', 'rbac', 'url'));
        $this->load->library('session');
    }

    public function index()
    {
        $this->data['form_validation'] = 'No';
        if (isset($_POST['submit'])) {
            $q = $this->login_m->signin();

            if ($q == 1) {
                $dest = rbac_first_allowed_report();
                if ($dest) {
                    redirect(base_url('index.php/' . $dest));
                }
                $this->session->sess_destroy();
                $this->data['form_validation'] = 'No report access assigned. Contact Super Admin.';
                $this->load->view('login', $this->data);
            } elseif ($q === 'disabled') {
                $this->session->set_flashdata('errors', 'Account disabled');
                $this->data['form_validation'] = 'Your account is disabled. Contact Super Admin.';
                $this->load->view('login', $this->data);
            } elseif ($q === 'expiry') {
                $this->session->set_flashdata('errors', 'That user does not signin');
                $this->data['form_validation'] = 'App has been Expired, Please Contact Admin.';
                $this->load->view('login', $this->data);
            } else {
                $this->session->set_flashdata('errors', 'That user does not signin');
                $this->data['form_validation'] = 'Incorrect Signin';
                $this->load->view('login', $this->data);
            }
        } else {
            $this->load->view('login', $this->data);
            $this->session->sess_destroy();
        }
    }

    public function logout()
    {
        $this->session->sess_destroy();
        redirect(base_url('index.php/login/index'));
    }
}
