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

class Login extends CI_Controller {

	public function __construct() 
	{
		parent::__construct();
		
		$this->lang->load('account_general', get_language());
		$this->load->model('member_model');
	}
	
	public function index()
	{
		if( ! $this->session->userdata('user_type')) {
			$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_login');
			
			$this->load->view('login', $data);
		}
		else {
			redirect('home', 'refresh');
		}		
	}
	
	public function submit()
	{
		if( ! $this->session->userdata('user_type')) {
			$username = trim($this->input->post('username', TRUE));
			$password = trim($this->input->post('password', TRUE));
			// $remember_password = (isset($_POST['remember_password']) ? 1 : 0);
			// $tnc = trim($this->input->post('tnc', TRUE));

			if ($username == '') {
				echo $this->lang->line('login_enter_username_message');
				exit();
			} else if ($password == '') {
				echo $this->lang->line('login_enter_password_message');
				exit();
			}
			
			// session_start();

			// if (isset($_SESSION['captcha'])) {
				// $captcha_code = $this->input->post('captcha', TRUE);

				// if ($_SESSION['captcha'] != $captcha_code) {
					// echo $this->lang->line('login_invalid_captcha_message');
					// exit();
				// }
				// else {
					// unset($_SESSION['captcha']);
				// }

			// }
			
			// if ($tnc == '') {
				// echo $this->lang->line('login_check_tnc_message');
				// exit();
			// }
			
			#Check for username availbility
			$query = $this->db->query("SELECT member_id, full_name, country, username, password_origin, password, package_type, member_type, allowed_withdraw, unlimited_withdraw, allowed_transfer, allowed_convert, allowed_topup, active, platform_type, is_locked, is_claim_only, last_login_date FROM sw_members WHERE username = ? LIMIT 1", array($username));
			if($query->num_rows() > 0) {
				$row = $query->row();
				
				$verify_password = sha1(md5(sha1(md5($password)) . sha1(md5($this->config->item('encryption_key')))));
				
				if($row->password == $verify_password) {
					if($row->is_locked == 0) {
						$this->session->set_userdata(array('member_id' => $row->member_id, 'member_name' => $row->full_name, 'member_country' => $row->country, 'member_username' => $row->username, 'member_package_type' => $row->package_type, 'member_member_type' => $row->member_type, 'member_allowed_withdraw' => $row->allowed_withdraw, 'member_unlimited_withdraw' => $row->unlimited_withdraw, 'member_allowed_transfer' => $row->allowed_transfer, 'member_allowed_convert' => $row->allowed_convert, 'member_allowed_topup' => $row->allowed_topup, 'member_active' => $row->active, 'member_platform_type' => $row->platform_type, 'member_is_claim_only' => $row->is_claim_only, 'member_login_datetime' => date('Y-m-d H:i'), 'user_type' => MEMBER));
						
						// if($remember_password == 1) {
							// $this->session->set_userdata('remember_username', $row->username);
							// $this->session->set_userdata('remember_password', $row->password_origin);
						// }
						// else {
							// $this->session->set_userdata('remember_username', '');
							// $this->session->set_userdata('remember_password', '');
						// }
						
						$this->db->trans_start();
						
						$this->db->query("UPDATE sw_members SET last_login_date = ? WHERE member_id = ? AND username = ? LIMIT 1", array(time(), $row->member_id, $row->username));
						
						$this->member_model->insert_member_log($row->member_id, LOG_LOGIN);
						
						$this->db->trans_complete();
						
						if ($this->db->trans_status() === TRUE) {
							echo STATUS_200;
						}
						else {
							echo $this->lang->line('login_failed');
						}
					}
					else {
						echo $this->lang->line('login_failed');
					}	
				}
				else {
					echo $this->lang->line('login_invalid_username_or_password');
				}
			}
			else {
				echo $this->lang->line('login_invalid_username_or_password');
			}
		}
		else {
			redirect('home', 'refresh');
		}		
	}
	
	public function token()
	{
		echo $this->security->get_csrf_hash();
	}
	
	public function notice()
	{
		if( ! $this->session->userdata('lang') OR $this->session->userdata('lang') == 'zh-CN' OR $this->session->userdata('lang') == 'zh-HK'){
			echo '<img src="' . base_url() . 'assets/img/notice_cn.jpg" border="0" style="width: 100%;" />';
		}
		else {
			echo '<img src="' . base_url() . 'assets/img/notice_en_.jpg" border="0" style="width: 100%;" />';
		}
	}
	
	public function tnc()
	{
		$data['page_title'] = $this->lang->line('website_name') . ' | ' . $this->lang->line('hf_tnc');
			
		$this->load->view('tnc', $data);
	}
}

/* End of file login.php */
/* Location: ./application/controllers/login.php */