<?php

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

class Home extends MX_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('cookie');
		$this->load->helper('language');
		$this->lang->load('messages', 'message');
    }

    public function index() {
        $admin_info = $this->session->userdata('adminInfo');
        if ($admin_info) {
            $data['statistics'] = $this->getStatistics();
            $data['content'] = "";
            $this->load->view('home/index', $data);
        } else {
            $this->load->view('login');
        }
    }

    public function getStatistics() {
        $this->load->model('collabolator_model');
        $data = $this->collabolator_model->getStatistics();
        return $this->load->view('home/statistics', $data, true);
    }
    public function login() {

        $login_name = $this->input->post('login_name');
        $passwd = $this->input->post('passwd');

        $this->load->model(array('admin_model'));
        $data = $this->admin_model->check_login($login_name, $passwd);
        if ($data == null) {
            $this->session->set_flashdata('login_error', TRUE);
            redirect("/admin/home");
        } else {
            $admindata = array('login_name' => $login_name, 'admin_id' => $data->admin_id, 'role_id' => $data->role_id, 'logined_in' => TRUE);
            $this->session->set_userdata('adminInfo', $admindata);
            redirect("/admin/home");
        }
    }

    public function logout() {
        $this->session->sess_destroy();
        redirect("/admin/home");
    }
    
    public function is_timeout()
    {
        $result['login']=1;
        $admin_info = $this->session->userdata('adminInfo');
        if (!$admin_info) {
            $result['login']=0;
        }
        echo json_encode($result);
    }

}
