<?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' );
		$this->load->helper('language');
		$this->lang->load('messages', 'message');
	}
	
	public function index() {
		$user_info = $this->session->userdata ( 'userInfo' );
		if ($user_info) {
			redirect ( '/frontend/lecture' );
		} else {
			$cookie_name = "siteCookie";
			if (isset ( $_COOKIE [$cookie_name] )) {
				$arr_user = parse_str ( $_COOKIE [$cookie_name] );
				$user_info = array (
						'username' => $arr_user ['username'],
						'user' => $arr_user ['user'],
						'us_id' => $arr_user ['us_id'],
                                                'is_viettel' => $arr_user ['is_viettel'],
						'logined_in' => TRUE 
				);
				$this->session->set_userdata ( 'userInfo', $user_info );
				redirect ( '/frontend/lecture' );
			} else {
				$this->load->view ( 'home' );
			}
		}
	}
	
	public function signin() {
		$username = $this->input->post ( 'username', TRUE );
		$password = $this->input->post ( 'password', TRUE );
		$autologin = ($this->input->post ( 'remember_me' ) == 'on') ? 1 : 0;
		$src = 'SBG';
		$token = md5 ( $username . self::TOKENPW );
		$this->load->model ( array (
				'user_model' 
		) );
		$result = $this->user_model->checkLogin ( $src, $token, $username, $password );
		if ($result['success'] == -1)
		{
			$this->session->set_flashdata ( 'login_api_error', TRUE );
			redirect ( '/frontend/home' );
			
		}else if ($result['success'] == 0)
		{
			$this->session->set_flashdata ( 'login_error', TRUE );
			redirect ( '/frontend/home' );
		}else{
			parse_str ( $result['data'] );
			$is_viettel = $this->is_viettel ( $phone );
			if ($autologin == 1) {
				$cookie_name = 'siteCookie';
				// $cookie_time = 7200;
				$cookie_time = 3600 * 24 * 30;
				setcookie ( $cookie_name, 'username=' . $username . '&user=' . $result['data'] . '&us_id=' . $id, time () + $cookie_time );
			}
			$userdata = array (
					'username' => $username,
					'user' => $result['data'],
					'us_id' => $id,
					'logined_in' => TRUE,
					'is_viettel' => $is_viettel 
			);
			$this->session->set_userdata ( 'userInfo', $userdata );
			redirect ( '/frontend/lecture' );
		}
	}
	
	public function signout() {
		$this->session->sess_destroy ();
		// unset($this->session->userdata);
		$cookie_name = "siteCookie";
		setcookie ( $cookie_name, 'username=' . "", time () - 3600 );
		redirect ( '/frontend/home' );
	}
	
	public function register() {
		$this->load->model ( 'user_model' );
		$collaborator = '0979947802';
		$serviceID = 1;
		$username = '0978618113';
		$commandcode = 'commandcode1';
		$info = 'ABCD';
		// $receiveTime = time();
		$receiveTime = date ( "Y-m-d H:i:s" );
		$data = $this->user_model->register ( $username, $collaborator );
		echo $data;
	}
	
	public function change_profile() {
		$this->load->model ( 'user_model' );
		$sentNumber = '0979947802';
		$serviceID = 1;
		$password = '2933369';
		$commandcode = "SBG";
		$receiveID = '0977274183';
		$info = "SBG TD";
		$receiveTime = date ( "Y-m-d H:i:s" );
		$status = 0;
		$data = $this->user_model->changeProfile ( $sentNumber, $receiveID, $password, $status );
		echo $data;
	}
	
	public function change_password() {
		$this->load->model ( 'user_model' );
		$sentNumber = '0979947802';
		$password = '935322';
		$data = $this->user_model->changePassword ( $sentNumber, $password );
		echo $data;
	}
	
	public function register_vip() {
		$this->load->model ( 'user_model' );
		$sentNumber = '0979947802';
		$p_code = 1;
		$serviceID = 1;
		$commandcode = "SBG";
		$vipNum = 1;
		$mainAccBalanced = 15000;
		$info = "SBG DK VIP1";
		$receiveTime = date ( "Y-m-d H:i:s" );
		$status = 0;
		$data = $this->user_model->registerVip ( $sentNumber, $p_code );
		echo $data;
	}
	
	public function recharge_account() {
		$this->load->model ( 'user_model' );
		$sentNumber = '0979947802';
		$receiveID = '01662860000';
		$serviceID = 1;
		$commandcode = "SBG";
		$vipNum = 1;
		$mainAccBalanced = 15000;
		$amount = 1000;
		$info = "SBG NAP 10000";
		$status = 0;
		$receiveTime = date ( "Y-m-d H:i:s" );
		$data = $this->user_model->rechargeAccount ( $sentNumber, $amount );
		//$data = $this->user_model->insertPaidlog($sentNumber, $sentNumber, 2, $amount, $receiveTime);
		echo $data;
	}
	
	public function is_viettel($number) {
		$viettel = array (
				'096',
				'097',
				'098',
				'0163',
				'0164',
				'0165',
				'0166',
				'0167',
				'0168',
				'0169' 
		);
		foreach ( $viettel as $num ) {
			if (strpos ( $number, $num ) === 0) {
				return 1;
			}
		}
		return 0;
	}
}

/* End of file home.php */
/* Location: ./application/modules/frontend/controllers/home.php */