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

/**
 * function list index
 * need to list the details and function called
 *


do_login
 * for Register Member to login
 * first change valide of google captcha
 * change to not check tempUser 1 account, only tempUser 0 account can login
 * function used in Route Login
 *
checkout_login
 *
 *







 * end function list index
 **/

class Login extends MX_Controller {

	function __construct() {
		parent::__construct();
		$this->load->library('web/customer/Llogin');
		$this->load->library('web/customer/User_auth');
		$this->load->model('web/customer/Logins');
		$this->load->model('web/Homes');
		$this->load->model('dashboard/Subscribers');
		$this->load->model('dashboard/web_settings');
		$this->load->model('dashboard/Soft_settings');
		$this->load->model('dashboard/Themes');
		$this->load->model('dashboard/Blocks');
		$this->load->model('dashboard/color_frontends');
		$this->load->model('dashboard/Companies');
		$this->load->model('dashboard/pay_withs');
		$this->load->model('template/Template_model');

		//added model for setting
		$this->load->model('web/Settings');

		//end added model for setting

		//category
		$this->load->model('web/Categories');
		//end category
	}

	//Default loading for Home Index.
	public function index() {
		if ($this->user_auth->is_logged()) {
			$this->output->set_header("Location: " . base_url('customer/customer_dashboard'), TRUE, 302);

			//added to move to new style
			// $this->output->set_header("Location: " . base_url('customer/customer_dashboard/newstyle'), TRUE, 302);
		}

		$parent_category_list = $this->Logins->parent_category_list();
		$pro_category_list = $this->Logins->category_list();
		$best_sales = $this->Logins->best_sales();
		$footer_block = $this->Logins->footer_block();
		$slider_list = $this->web_settings->slider_list();
		$block_list = $this->Blocks->block_list();
		$currency_details = $this->Soft_settings->retrieve_currency_info();
		$Soft_settings = $this->Soft_settings->retrieve_setting_editdata();
		$languages = $this->Homes->languages();
		$currency_info = $this->Homes->currency_info();
		$selected_currency_info = $this->Homes->selected_currency_info();

		//all_main_category
		$all_main_category = $this->Categories->all_main_category();
		$all_sub_category = $this->Categories->all_sub_category();
		//end all_main_category

		$data = array(
			'title' => display('login'),
			'category_id' => 'login',
			'category_list' => $parent_category_list,
			'pro_category_list' => $pro_category_list,
			'slider_list' => $slider_list,
			'block_list' => $block_list,
			'best_sales' => $best_sales,
			'footer_block' => $footer_block,
			'Soft_settings' => $Soft_settings,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'selected_cur_id' => (($selected_currency_info->currency_id) ? $selected_currency_info->currency_id : ""),
			'currency' => $currency_details[0]['currency_icon'],
			'position' => $currency_details[0]['currency_position'],

			'all_main_category' => $all_main_category,
			'all_sub_category' => $all_sub_category,

		);

		$theme = $this->Themes->get_theme();
		$content = $this->parser->parse('web/themes/' . $theme . '/login', $data, true);
		$this->template_lib->full_website_html_view($content);

	}

	#==============Do Login=======#
	public function do_login() {

		if (!empty($this->input->post('recaptcha_response'))) {
			if ($this->Settings->googlecaptchaverify($this->input->post('recaptcha_response')) == true) {

				$error = '';
				$email = $this->input->post('email', TRUE);
				$password = $this->input->post('password', TRUE);

				//use library user_auth
				if ($email == '' || $password == '' || $this->user_auth->login($email, $password) === FALSE) {
					$error = display('wrong_username_or_password');
				}

				if ($error != '') {
					$this->session->set_userdata(array('error_message' => $error));

					$this->output->set_header("Location: " . base_url('login'), TRUE, 302);

				} else {
					$this->session->set_userdata(array('message' => display('login_successfully')));

					redirect(base_url());
				}

			} //if ($this->Settings->googlecaptchaverify($this->input->post('recaptcha_response')) == true) {
		} //if (!empty($this->input->post('recaptcha_response'))) {
	}

	//Customer checkout login
	public function checkout_login() {
		$error = '';
		$logresult = FALSE;
		$email = $this->input->post('login_email', TRUE);
		$password = $this->input->post('login_password', TRUE);
		$remember_me = $this->input->post('remember_me', TRUE);
		if ($remember_me == 1) {
			$email_cookie = array(
				'name' => 'email',
				'value' => $email,
				'expire' => '86500',
			);
			$this->input->set_cookie($email_cookie);

			$pass_cookie = array(
				'name' => 'password',
				'value' => $password,
				'expire' => '86500',
			);
			$this->input->set_cookie($pass_cookie);
		}
		if (($email == '') || ($password == '')) {
			echo FALSE;
			exit();
		}

		if ($this->user_auth->login($email, $password) == TRUE) {
			echo TRUE;
		} else {
			echo FALSE;
		}
	}

	//This function is used to Generate Key
	public function generator($lenth) {
		$number = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "N", "M", "O", "P", "Q", "R", "S", "U", "V", "T", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0");

		for ($i = 0; $i < $lenth; $i++) {
			$rand_value = rand(0, 34);
			$rand_number = $number["$rand_value"];

			if (empty($con)) {
				$con = $rand_number;
			} else {
				$con = "$con" . "$rand_number";
			}
		}
		return $con;
	}

	public function send_mail($email, $ptoken) {
		$CI = &get_instance();
		$CI->load->model('dashboard/Soft_settings');
		$setting_detail = $CI->Soft_settings->retrieve_email_editdata();

		$subject = 'Reset Password';
		$message = 'To reset your password, Click the link bellow...' . '<br>' . '<a href="' . base_url('password_reset/' . $ptoken) . '"> Reset Password</a>';

		$config = array(
			'protocol' => $setting_detail[0]['protocol'],
			'smtp_host' => $setting_detail[0]['smtp_host'],
			'smtp_port' => $setting_detail[0]['smtp_port'],
			'smtp_user' => $setting_detail[0]['sender_email'],
			'smtp_pass' => $setting_detail[0]['password'],
			'mailtype' => $setting_detail[0]['mailtype'],
			'charset' => 'utf-8',
		);

		$CI->load->library('email');
		$CI->email->initialize($config);

		$CI->email->set_newline("\r\n");
		$CI->email->from($setting_detail[0]['sender_email']);
		$CI->email->to($email);
		$CI->email->subject($subject);
		$CI->email->message($message);
		if ($CI->email->send()) {
			$CI->session->set_userdata(array('message' => display('email_send_to_customer')));
			return true;
		} else {
			return false;
		}
	}

	public function forget_password() {

		if (!empty($this->input->post('recaptcha_response'))) {
			if ($this->Settings->googlecaptchaverify($this->input->post('recaptcha_response')) == true) {

				$this->form_validation->set_rules('email', display('email'), 'required|valid_email|max_length[100]|trim');

				$email = $this->input->post('email', TRUE);

				if ($this->form_validation->run()) {
					$user = $this->get_user_info($email);
					$ptoken = date('ymdhis');
					if ($user) {
						$email = $user[0]['customer_email'];

						$precdat = array(
							'email' => $email,
							// 'email' => "jack@at-creative.com",
							'token' => $ptoken,

						);
						$send_email = '';
						if (!empty($email)) {
							//			add email contact
							// $send_email = $this->send_mail($email, $ptoken);
							$this->load->model('web/Sendgrid_model');

							// $send_email = $this->_emailAction($precdat);

							$this->Sendgrid_model->sendForgetPasswordEmail($email, $ptoken);
							$send_email = true;
							$this->update_recovery_pass($precdat);

						}
						if ($send_email) {
							echo 1;
						} else {
							echo 2;
						}

					} else {
						echo 3;
					}
				} else {
					echo 4;
				}

				//start check send email
				// $data = array();
				// $emailresult = $this->_emailAction($data);
				// var_dump($emailresult);
				//end start check send email
			} //if ($this->Settings->googlecaptchaverify($this->input->post('recaptcha_response')) == true) {
		} //if (!empty($this->input->post('recaptcha_response'))) {

	}

//check user exists on the database or not
	public function get_user_info($email) {
		$CI = &get_instance();
		$CI->load->model('dashboard/Customers');
		$result = $CI->Customers->get_user_info($email);
		return $result;
	}

	//set token to specific email
	public function update_recovery_pass($precdat) {
		$CI = &get_instance();
		$CI->load->model('dashboard/Customers');
		$result = $CI->Customers->set_token($precdat);
		return $result;
	}

	//show password reset form from email that we sent a link
	public function password_reset_form($token) {
		$data['token'] = $token;
		$CI = &get_instance();
		$test = $CI->load->view('web/password_reset', $data);

	}

	//show password reset form
	public function forget_password_form() {
		$content = $this->llogin->forget_password_form();
		$this->template_lib->full_website_html_view($content);

	}
	// Password update on pass reset
	public function password_update() {
		$CI = &get_instance();
		$CI->load->model('dashboard/Customers');
		$token = $this->input->post('token', TRUE);
		$data = [
			'token' => $this->input->post('token', TRUE),
			'password' => $this->input->post('password', TRUE),
		];

		$result = $CI->Customers->password_update($data);
		if ($result) {
			$this->session->set_flashdata(array('message' => display('successfully_updated')));

		} else {
			$this->session->set_flashdata(array('error_message' => display('failed_try_again')));
		}
		redirect('password_reset/' . $token);
	}

	//added send email
	private function _emailAction($data) {
		require dirname(__DIR__, 2) . "/libraries/sendgrid/sendgrid-php.php";
		// require "/home/artechsy/public_html/bigmacmall3.artech-system.com/application/libraries/sendgrid/sendgrid-php.php";
		require_once dirname(__DIR__, 2) . '/libraries/Document.php';
		// require_once "/home/artechsy/public_html/bigmacmall3.artech-system.com/application/libraries/Document.php";

		global $p, $doc, $a, $c;
		$email = new \SendGrid\Mail\Mail();

		// $chosen_prod = isset($data['chosen_prod']) ? $data['chosen_prod'] : false;
		// $product_code = isset($data['product_code']) ? $data['product_code'] : false;

		$doc = new Document();
		$doc->loadHTMLFile('application/libraries/email.html');
		// var_dump($data);
		if ($this->session->userdata('language') == "english") {
			$subject = 'Reset Password';
		}
		if ($this->session->userdata('language') == "chinese") {
			$subject = '重置密碼';
		}
		if ($this->session->userdata('language') == "indonesian") {
			$subject = 'Reset Password';
		}
		// $message = 'To reset your password, Click the link bellow...' . '<br>' . '<a href="' . base_url('password_reset/' . $data->ptoken) . '"> Reset Password</a>';

		if ($this->session->userdata('language') == "english") {
			$message = 'To reset your password, Click the link bellow...' . '<br>' . '<a href="' . base_url('password_reset/' . $data["token"]) . '"> Reset Password</a>';
		}

		if ($this->session->userdata('language') == "chinese") {
			$message = '重置密碼，請單擊下面的鏈接' . '<br>' . '<a href="' . base_url('password_reset/' . $data["token"]) . '"> 重置密碼</a>';
		}
		if ($this->session->userdata('language') == "indonesian") {
			$message = 'To reset your password, Click the link bellow...' . '<br>' . '<a href="' . base_url('password_reset/' . $data["token"]) . '"> Reset Password</a>';
		}
		// $doc->getElementsByTagName('title')->item(0)->nodeValue = 'Contact';
		$doc->getElementsByTagName('title')->item(0)->nodeValue = $subject;
		$doc->getElementById('greeting')->nodeValue = '';
		$c = $doc->getElementById('content');
		$c->nodeValue = '';

		$c->appendChild($doc->createElement('p', $message));
		$aTag = $doc->createElement('a');
		$aTag->setAttribute('href', "yourlink.htm");
		$aTag->textContent = "link text";
		$c->appendChild($aTag);
		// $c->appendChild($doc->createElement('p', $message));
		// $c->appendChild($doc->createElement('p', "電郵:" . $data['inputemail']));
		// $c->appendChild($doc->createElement('p', "電話:" . $data['staticmobile']));
		// $c->appendChild($doc->createElement('p', "性別:" . $data['inputgender']));

		// $displayage = "";
		// if ($data['inputage'] == 1) {
		// 	$displayage = "18-25";
		// }
		// if ($data['inputage'] == 2) {
		// 	$displayage = "25-35";
		// }
		// if ($data['inputage'] == 3) {
		// 	$displayage = "35-45";
		// }
		// if ($data['inputage'] == 4) {
		// 	$displayage = "45-60";
		// }
		// if ($data['inputage'] == 5) {
		// 	$displayage = "60+";
		// }
		// $c->appendChild($doc->createElement('p', "年齡:" . $displayage));
		// $c->appendChild($doc->createElement('p', "內容:" . $data['inputmessage']));

		$html = $doc->saveHTML();
//        echo($html);die;

		// $subj = $data['subj'];
		// $subj = "客戶聯絡";

		$email->setFrom("contact_form@at-webs.hk", "BIG MAC MALL");
		$email->setSubject($subject);
		// $email->addTo("bear@at-creative.com", "Bear");

		$email->addTo('jack@at-creative.com');
		// $email->addTo('samshuipo@gmail.com');
		// $email->addTo('info@amigohk.hk');
		// $email->addBcc('jack@at-creative.com');
		// $email->addBcc('arvid@at-creative.com');
		// $email->addBcc('daniel@at-creative.com');

		$email->addContent(
			"text/html", $html
		);
		// $email->addContent(
		//         "text/html", "abc"
		// );

		//end sample email message
		$sendgrid = new \SendGrid('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');
		try {
			$response = $sendgrid->send($email);
			//var_dump($response);
			//echo 'Done!';
			return true;
			// print $response->statusCode() . "\n";
			// print_r($response->headers());
			// print $response->body() . "\n";
		} catch (Exception $e) {
			//echo 'Caught exception: ' . $e->getMessage() . "\n";
			return false;
		}
	}

	//end added send email

}