﻿<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Home Class
 * 
 * @author dzungnv02
 *
 */

class Home extends MX_Controller 
{
	
	const TOKENPW = 'violet';
	
	public function __construct() 
	{
		parent::__construct();
		$this->load->helper('cookie');
	}

	public function index() 
	{
		$user_info = $this->session->userdata('userInfo');
		if ($user_info)
		{
			redirect('/frontend/lecture');
		}else{
			$this->load->view ( 'home');
			if (isset($_POST['submit'])){
				$this->signin();
			}
		}
	}
	
	public function test()
	{
		if (isset($_POST['submit'])){
			$this->signin();
		}
	}
	
	public function signin() 
	{
		$username = $this->input->post('username', TRUE);
		$password = $this->input->post('password', TRUE);
		$token = md5($username.self::TOKENPW);
		$this->load->model(array('user_model'));
		$data = $this->user_model->checkLogin('violet', $token, $username, $password);
		echo $data;
		$arr_users = explode("&", $data);
		$str_status = "";
		$str_fullname = "";
		$fullname = "";
		for ($i=0; $i<count($arr_users); $i++)
		{
			if (strpos($arr_users[$i], 'status=') !== false)
			{
				$str_status = $arr_users[$i];
			}
			if (strpos($arr_users[$i], 'fullname=') !== false)
			{
				$str_fullname = $arr_users[$i];
			}
		}
		$arr_status = explode("=", $str_status);
		if ($str_fullname != ""){
			$arr_fullname = explode("=", $str_fullname);
			$fullname = $arr_fullname[1];
		}
		$status = (int)$arr_status[1];
		switch($status)
		{
			case 0:
				$userdata = array('username' => $username, "logined_in" => TRUE);
				$this->session->set_userdata('userInfo', $userdata);
				redirect('/frontend/lecture');
				break;
			case 1:
				echo "Thiếu thông tin đăng nhập !";
				break;
			case 2:
			case 3:
				echo "Sai username hoặc password !";
				break;
			case 4:
				echo "Tài khoản chưa được Activated";
				break;
			case 10:
				echo "Sai Token";
				break;
			default:
				break;
		}
	}
	
	public function signout() 
	{
		
	}

}

/* End of file home.php */
/* Location: ./application/modules/frontend/controllers/home.php */