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

class Lhome {

	//Home Page Load Here
	public function home_page() {
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Brands');
		$CI->load->model('web/Products_model');
		$CI->load->model('dashboard/Themes');

		//added category
		$CI->load->model('web/Categories');

		//end added category

		$theme = $CI->Themes->get_theme();

		$parent_category_list = $CI->Homes->parent_category_list();
		$pro_category_list = $CI->Homes->category_list();
		$best_sales = $CI->Homes->best_sales();
		$footer_block = $CI->Homes->footer_block();
		$slider_list = $CI->web_settings->home_slider_list();
		$block_list = $CI->Blocks->block_list_for_home_page();
		$currency_details = $CI->Soft_settings->retrieve_currency_info();
		$Soft_settings = $CI->Soft_settings->retrieve_setting_editdata();
		$languages = $CI->Homes->languages();
		$currency_info = $CI->Homes->currency_info();
		$selected_currency_info = $CI->Homes->selected_currency_info();
		$select_home_adds = $CI->Homes->select_home_adds();
		$promotion_product = $CI->Products_model->promotion_product();
		$most_popular_product = $CI->Homes->most_popular_product();
		$brands = $CI->Homes->get_brand_list();
		//added category list
		$category_list = $CI->Categories->all_main_category();

		//end added category list

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

		$data = array(
			'title' => display('home'),
			'category_id' => 'home',
			'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,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'select_home_adds' => $select_home_adds,
			'selected_cur_id' => (($selected_currency_info->currency_id) ? $selected_currency_info->currency_id : ""),
			'Soft_settings' => $Soft_settings,
			'currency' => $currency_details[0]['currency_icon'],
			'position' => $currency_details[0]['currency_position'],
			'promotion_product' => $promotion_product,
			'most_popular_product' => $most_popular_product,
			'brands' => $brands,
			'displaycarousel' => 1,
			//added category list
			'category_list' => $category_list, //duplicated may delete ->to change to all main category
			'all_main_category' => $all_main_category,
			'all_sub_category' => $all_sub_category,
			//end added category list
		);

		$HomeForm = $CI->parser->parse('web/themes/' . $theme . '/home', $data, true);
		return $HomeForm;
	}

	//Checkout
	public function checkout() {
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Themes');
		$theme = $CI->Themes->get_theme();

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

		$payment_gateways = $CI->Homes->get_payment_gateway_list();

		$country_id = $CI->session->userdata('country');
		if (!empty($country_id)) {
			$state_list = $CI->Homes->select_state_country();
		} else {
			$default_contry = (!empty($Soft_settings[0]['country_id']) ? $Soft_settings[0]['country_id'] : 18); //18=bangladesh
			$state_list = $CI->Homes->get_country_state($default_contry);
		}

		$ship_country = $CI->session->userdata('ship_country');
		if ($ship_country) {
			$ship_state_list = $CI->Homes->select_ship_state_country();
		} else {
			$ship_state_list = $state_list;
		}

		$CI->_cart_contents['cart_ship_cost'] = $CI->session->userdata('cart_ship_cost');
		$CI->_cart_contents['cart_ship_name'] = $CI->session->userdata('cart_ship_name');

		$data = array(
			'title' => display('checkout'),
			'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,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'Soft_settings' => $Soft_settings,
			'state_list' => (!empty($state_list) ? $state_list : null),
			'ship_state_list' => (!empty($ship_state_list) ? $ship_state_list : null),
			'select_shipping_method' => $select_shipping_method,
			'selected_country_info' => $selected_country_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'],
			'payment_gateways' => $payment_gateways,
			'theme' => $theme,

		);
		$checkout = $CI->parser->parse('web/themes/' . $theme . '/checkout', $data, true);
		return $checkout;
	}

	// added new checkoutnewstyle
	public function checkoutnewstyle() {
		//data in viewcart or checkout near same
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Themes');
		$theme = $CI->Themes->get_theme();

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

		$payment_gateways = $CI->Homes->get_payment_gateway_list();

		$country_id = $CI->session->userdata('country');
		if (!empty($country_id)) {
			$state_list = $CI->Homes->select_state_country();
		} else {
			$default_contry = (!empty($Soft_settings[0]['country_id']) ? $Soft_settings[0]['country_id'] : 18); //18=bangladesh
			$state_list = $CI->Homes->get_country_state($default_contry);
		}

		$ship_country = $CI->session->userdata('ship_country');
		if ($ship_country) {
			$ship_state_list = $CI->Homes->select_ship_state_country();
		} else {
			$ship_state_list = $state_list;
		}

		$CI->_cart_contents['cart_ship_cost'] = $CI->session->userdata('cart_ship_cost');
		$CI->_cart_contents['cart_ship_name'] = $CI->session->userdata('cart_ship_name');

		$data = array(
			'title' => display('checkout'),
			'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,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'Soft_settings' => $Soft_settings,
			'state_list' => (!empty($state_list) ? $state_list : null),
			'ship_state_list' => (!empty($ship_state_list) ? $ship_state_list : null),
			'select_shipping_method' => $select_shipping_method,
			'selected_country_info' => $selected_country_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'],
			'payment_gateways' => $payment_gateways,
			'theme' => $theme,

		);
		$checkout = $CI->parser->parse('web/themes/' . $theme . '/checkoutnewstyle', $data, true);
		return $checkout;
	} //public function checkoutnewstyle() {
	//end  added new checkoutnewstyle

	// added new checkoutnewstyle2
	public function checkoutnewstyle2() {
		//data in viewcart or checkout near same
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Themes');
		$theme = $CI->Themes->get_theme();

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

		//category
		$CI->load->model('web/Custpoint_model');
		//end category

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

		$payment_gateways = $CI->Homes->get_payment_gateway_list();

		$country_id = $CI->session->userdata('country');
		if (!empty($country_id)) {
			$state_list = $CI->Homes->select_state_country();
		} else {
			$default_contry = (!empty($Soft_settings[0]['country_id']) ? $Soft_settings[0]['country_id'] : 18); //18=bangladesh
			$state_list = $CI->Homes->get_country_state($default_contry);
		}

		$ship_country = $CI->session->userdata('ship_country');
		if ($ship_country) {
			$ship_state_list = $CI->Homes->select_ship_state_country();
		} else {
			$ship_state_list = $state_list;
		}

		$CI->_cart_contents['cart_ship_cost'] = $CI->session->userdata('cart_ship_cost');
		$CI->_cart_contents['cart_ship_name'] = $CI->session->userdata('cart_ship_name');

		//cart content list
		$cartlistArray = array();
		foreach ($CI->cart->contents() as $items) {
			$itemList = array();
			$itemList['rowid'] = $items['rowid'];
			$itemList['name'] = $items['name'];
			$itemList['variant'] = $items['variant'];
			$itemList['variant_color'] = $items['variant_color'];
			$itemList['price'] = $items['price'];
			$itemList['qty'] = $items['qty'];

			//added reward content
			$itemList['is_reward_product'] = $items['is_reward_product'];
			$itemList['reward_point'] = $items['reward_point'];
			//end added reward content

			//check have stock of not
			$cartinfo = $CI->cart->get_item($items['rowid']);
			// $stock = $CI->Products_model->check_variant_wise_stock($cartinfo['variant'], $cartinfo['product_id']); //compare issue invoice stock - sell greater then purchase stock

			$stock = $CI->Products_model->check_order_variant_wise_stock($cartinfo['variant'], $cartinfo['product_id']); //compare against order to purchase stock - sell same as purchase

			//0 stand for have stock
			//1 means out of stock
			$itemList['withoutstock'] = 0;
			if ($items['qty'] > $stock) {
				$itemList['withoutstock'] = 1;
			}
			//end check have stock of not

			array_push($cartlistArray, $itemList);
		} //foreach ($CI->cart->contents() as $items) {

		//end cart content list

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

		//Customer point
		$customer_point = $CI->Custpoint_model->getUserPoint($CI->session->userdata('customer_id'));

		$customer_payment_dollar = $CI->Custpoint_model->pointToPaymentDollar($customer_point);
		//end Customer point

		$data = array(
			'title' => display('checkout'),
			'category_id' => 'check_out',
			'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,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'Soft_settings' => $Soft_settings,
			'state_list' => (!empty($state_list) ? $state_list : null),
			'ship_state_list' => (!empty($ship_state_list) ? $ship_state_list : null),
			'select_shipping_method' => $select_shipping_method,
			'selected_country_info' => $selected_country_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'],
			'payment_gateways' => $payment_gateways,
			'theme' => $theme,

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

			//added cart list
			'cartlistArray' => $cartlistArray,
			//end added cart list

			// customer point
			'customer_point' => $customer_point,
			'customer_payment_dollar' => $customer_payment_dollar,
			// end customer point

		);
		$checkout = $CI->parser->parse('web/themes/' . $theme . '/checkoutnewstyle2', $data, true);
		return $checkout;
	} //public function checkoutnewstyle() {
	//end  added new checkoutnewstyle2

	//View Cart
	public function view_cart() {
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Themes');
		$theme = $CI->Themes->get_theme();
		$parent_category_list = $CI->Homes->parent_category_list();
		$pro_category_list = $CI->Homes->category_list();
		$best_sales = $CI->Homes->best_sales();
		$footer_block = $CI->Homes->footer_block();
		$slider_list = $CI->web_settings->slider_list();
		$block_list = $CI->Blocks->block_list();
		$currency_details = $CI->Soft_settings->retrieve_currency_info();
		$soft_settings = $CI->Soft_settings->retrieve_setting_editdata();
		$languages = $CI->Homes->languages();
		$currency_info = $CI->Homes->currency_info();
		$selected_currency_info = $CI->Homes->selected_currency_info();
		$selected_country_info = $CI->Homes->selected_country_info();
		$select_shipping_method = $CI->Homes->select_shipping_method();

		$country_id = $CI->session->userdata('country');
		if ($country_id) {
			$state_list = $CI->Homes->select_state_country();
		}

		$ship_country = $CI->session->userdata('ship_country');
		if ($ship_country) {
			$ship_state_list = $CI->Homes->select_ship_state_country();
		}

		$CI->_cart_contents['cart_ship_cost'] = $CI->session->userdata('cart_ship_cost');
		$CI->_cart_contents['cart_ship_name'] = $CI->session->userdata('cart_ship_name');

		$data = array(
			'title' => display('view_cart'),
			'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,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'Soft_settings' => $soft_settings,
			'state_list' => (!empty($state_list) ? $state_list : null),
			'ship_state_list' => (!empty($ship_state_list) ? $ship_state_list : null),
			'select_shipping_method' => $select_shipping_method,
			'selected_country_info' => $selected_country_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'],
		);
		$viewCart = $CI->parser->parse('web/themes/' . $theme . '/cart', $data, true);
		return $viewCart;
	}

	//Order html Data
	public function order_html_data($order_id) {
		$CI = &get_instance();

		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Orders');
		$CI->load->library('dashboard/occational');
		$CI->load->model('web/Sendgrid_model');

		$order_detail = $CI->Homes->retrieve_order_html_data($order_id);

		$subTotal_quantity = 0;
		$subTotal_cartoon = 0;
		$subTotal_discount = 0;

		if (!empty($order_detail)) {
			foreach ($order_detail as $k => $v) {
				$order_detail[$k]['final_date'] = $CI->occational->dateConvert($order_detail[$k]['date']);
				$subTotal_quantity = $subTotal_quantity + $order_detail[$k]['quantity'];
			}
			$i = 0;
			foreach ($order_detail as $k => $v) {
				$i++;
				$order_detail[$k]['sl'] = $i;
			}
		}
		// var_dump($order_detail);
		$currency_details = $CI->Soft_settings->retrieve_currency_info();
		$company_info = $CI->Orders->retrieve_company();
		$data = array(
			'title' => display('order_details'),
			'order_id' => $order_detail[0]['order_id'],
			'order_no' => $order_detail[0]['order'],
			'customer_address' => $order_detail[0]['customer_short_address'],
			'customer_name' => $order_detail[0]['customer_name'],
			'customer_mobile' => $order_detail[0]['customer_mobile'],
			'customer_email' => $order_detail[0]['customer_email'],
			'final_date' => $order_detail[0]['final_date'],
			'total_amount' => $order_detail[0]['total_amount'],
			'order_discount' => $order_detail[0]['order_discount'],
			'paid_amount' => $order_detail[0]['paid_amount'],
			'due_amount' => $order_detail[0]['due_amount'],
			'details' => $order_detail[0]['details'],
			'service_charge' => $order_detail[0]['service_charge'],
			'subTotal_quantity' => $subTotal_quantity,
			'order_all_data' => $order_detail,
			'company_info' => $company_info,
			'currency' => $currency_details[0]['currency_icon'],
			'position' => $currency_details[0]['currency_position'],
		);

		$chapterList = $CI->parser->parse('dashboard/order/order_pdf', $data, true);

		$CI->load->library('pdfgenerator');
		$file_path = $CI->pdfgenerator->generate_order($order_id, $chapterList);

		//File path save to database
		$CI->db->set('file_path', base_url($file_path));
		$CI->db->where('order_id', $order_id);
		$CI->db->update('order');

		$send_email = '';

		if (!empty($data['customer_email'])) {
			// $send_email = $this->setmail($data['customer_email'], $file_path);
			// $send_email = $this->_sendgridemailAction($data['customer_email'], $file_path);
			$send_email = $CI->Sendgrid_model->sendOrderReceivedEmail($data);
			// echo $data['customer_email'];
		}

		if ($send_email != null) {
			return true;
		} else {
			return true;
		}
	}

	//Send Customer Email with invoice
	public function setmail($email, $file_path) {
		$CI = &get_instance();
		$CI->load->model('dashboard/Soft_settings');
		$setting_detail = $CI->Soft_settings->retrieve_email_editdata();

		$server_status = serverAliveOrNot($setting_detail[0]['smtp_host'], $setting_detail[0]['smtp_port']);

		if ($server_status) {

			$subject = display("order_information");
			$message = display("order_info_details") . '<br>' . base_url();

			$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',
				'newline' => "\r\n",
			);

			$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);
			$CI->email->attach($file_path);

			if ($CI->email->send()) {
				return true;
			} else {
				return false;
			}

		} else {
			return false;
		}

	}

	//added sendgrid to send email
	private function _sendgridemailAction($emailaddress, $file_path) {
		require dirname(__DIR__, 3) . "/libraries/sendgrid/sendgrid-php.php";
		// require "/home/artechsy/public_html/bigmacmall3.artech-system.com/application/libraries/sendgrid/sendgrid-php.php";
		require_once dirname(__DIR__, 3) . '/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);
		// $subject = 'Reset Password';
		$subject = display("order_information");
		$message = display("order_info_details") . '<br>' . base_url() . '<br>' . $emailaddress;
		// $message = 'To reset your password, Click the link bellow...' . '<br>' . '<a href="' . base_url('password_reset/' . $data->ptoken) . '"> Reset Password</a>';
		// $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));
		// $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", "Amigo Rejuvence");
		$email->setSubject($subject);
		// $email->setAttachment($file_path);
		$file_encoded = base64_encode(file_get_contents($file_path));
		$email->addAttachment(
			$file_encoded,
			"application/pdf",
			"order.pdf",
			"attachment"
		);
		// $email->addTo("bear@at-creative.com", "Bear");
		// $email->addTo('jack@at-creative.com');
		$email->addTo($emailaddress);
		// $email->addTo('info@amigohk.hk');
		if ($emailaddress != 'jack@at-creative.com') {
			$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 sendgrid to send email

	// Track my order
	public function track_my_order($order_detail) {
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Brands');
		$CI->load->model('web/Products_model');
		$CI->load->model('dashboard/Themes');
		$theme = $CI->Themes->get_theme();

		$parent_category_list = $CI->Homes->parent_category_list();
		$pro_category_list = $CI->Homes->category_list();
		$footer_block = $CI->Homes->footer_block();
		$currency_details = $CI->Soft_settings->retrieve_currency_info();
		$Soft_settings = $CI->Soft_settings->retrieve_setting_editdata();
		$languages = $CI->Homes->languages();
		$currency_info = $CI->Homes->currency_info();
		$selected_currency_info = $CI->Homes->selected_currency_info();
		$data = array(
			'title' => display('track_my_order'),
			'category_list' => $parent_category_list,
			'pro_category_list' => $pro_category_list,
			'footer_block' => $footer_block,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'selected_cur_id' => (($selected_currency_info->currency_id) ? $selected_currency_info->currency_id : ""),
			'Soft_settings' => $Soft_settings,
			'currency' => $currency_details[0]['currency_icon'],
			'position' => $currency_details[0]['currency_position'],
			'is_valid_request' => (!empty($order_detail) ? TRUE : FALSE),
		);

		// If order details found
		if (!empty($order_detail)) {

			$CI->load->model('dashboard/Orders');
			$CI->load->library('dashboard/occational');
			$payinfo = $CI->Homes->get_order_payinfo(@$order_detail[0]['order_id']);
			$order_status = $CI->Homes->get_order_stutus(@$order_detail[0]['order_id']);

			$subTotal_quantity = 0;
			$subTotal_cartoon = 0;
			$subTotal_discount = 0;

			if (!empty($order_detail)) {
				foreach ($order_detail as $k => $v) {
					$order_detail[$k]['final_date'] = $CI->occational->dateConvert($order_detail[$k]['date']);
					$subTotal_quantity = $subTotal_quantity + $order_detail[$k]['quantity'];
				}
				$i = 0;
				foreach ($order_detail as $k => $v) {
					$i++;
					$order_detail[$k]['sl'] = $i;
				}
			}

			$currency_details = $CI->Soft_settings->retrieve_currency_info();

			$trackingdata = array(
				'order_id' => $order_detail[0]['order_id'],
				'order_no' => $order_detail[0]['order'],
				'customer_address' => $order_detail[0]['customer_short_address'],
				'ship_customer_short_address' => @$order_detail[0]['ship_customer_short_address'],
				'ship_customer_name' => @$order_detail[0]['ship_customer_name'],
				'customer_name' => @$order_detail[0]['customer_name'],
				'customer_mobile' => @$order_detail[0]['customer_mobile'],
				'ship_customer_mobile' => @$order_detail[0]['ship_customer_mobile'],
				'customer_email' => @$order_detail[0]['customer_email'],
				'ship_customer_email' => @$order_detail[0]['ship_customer_email'],
				'final_date' => $order_detail[0]['final_date'],
				'total_amount' => $order_detail[0]['total_amount'],
				'order_discount' => $order_detail[0]['order_discount'],
				'service_charge' => $order_detail[0]['service_charge'],
				'paid_amount' => $order_detail[0]['paid_amount'],
				'due_amount' => $order_detail[0]['due_amount'],
				'details' => $order_detail[0]['details'],
				'subTotal_quantity' => $subTotal_quantity,
				'order_all_data' => $order_detail,
				'currency' => $currency_details[0]['currency_icon'],
				'position' => $currency_details[0]['currency_position'],
				'payinfo' => $payinfo,
				'order_status' => $order_status,
			);

			$data = array_merge($data, $trackingdata);

		}

		$HomeForm = $CI->parser->parse('web/themes/' . $theme . '/track_my_order', $data, true);
		return $HomeForm;
	}

	// Track my order
	public function compare_product($compare_product) {
		$CI = &get_instance();
		$CI->load->model('web/Homes');
		$CI->load->model('dashboard/web_settings');
		$CI->load->model('dashboard/Soft_settings');
		$CI->load->model('dashboard/Blocks');
		$CI->load->model('dashboard/Brands');
		$CI->load->model('web/Products_model');
		$CI->load->model('dashboard/Themes');
		$theme = $CI->Themes->get_theme();

		$parent_category_list = $CI->Homes->parent_category_list();
		$pro_category_list = $CI->Homes->category_list();
		$footer_block = $CI->Homes->footer_block();
		$currency_details = $CI->Soft_settings->retrieve_currency_info();
		$Soft_settings = $CI->Soft_settings->retrieve_setting_editdata();
		$languages = $CI->Homes->languages();
		$currency_info = $CI->Homes->currency_info();
		$selected_currency_info = $CI->Homes->selected_currency_info();
		$data = array(
			'title' => display('compare_product'),
			'category_list' => $parent_category_list,
			'pro_category_list' => $pro_category_list,
			'footer_block' => $footer_block,
			'languages' => $languages,
			'currency_info' => $currency_info,
			'selected_cur_id' => (($selected_currency_info->currency_id) ? $selected_currency_info->currency_id : ""),
			'Soft_settings' => $Soft_settings,
			'currency' => $currency_details[0]['currency_icon'],
			'position' => $currency_details[0]['currency_position'],
			'is_valid_request' => (!empty($compare_product) ? TRUE : FALSE),
			'comparison_products' => $compare_product,
		);
		$HomeForm = $CI->parser->parse('web/themes/' . $theme . '/compare_prodct', $data, true);
		return $HomeForm;
	}

}
?>