<?php
include '../backend/db/db.php';
session_start();
	
if (isset($_SESSION['normal_user_cey'])) {
	$user_id = htmlspecialchars(strip_tags(trim($_SESSION['normal_user_cey'])), ENT_QUOTES, 'UTF-8');
	$whr = 'User_ID';
	$guest = 0;
	$st = 1;

} else if (isset($_SESSION['temp_user_cey']) && isset($_SESSION['guest_user_cey'])) {
	$user_id = htmlspecialchars(strip_tags(trim($_SESSION['temp_user_cey'])), ENT_QUOTES, 'UTF-8');
	$whr = 'tmp_id';
	$guest = 1;
	$st = 1;

} else {
	$response['status'] = '3';
	echo json_encode($response);
	$st = 0;
}

date_default_timezone_set('Asia/Colombo');
$date = date('Y-m-d');
$now = date('Y-m-d H:i:s');

if($st == 1) {
	$sql = mysqli_query($conn, "SELECT order_id, inv_id, SUM(price*Qty) AS sub_tot, SUM(price*Qty) AS item_tot FROM live_cart WHERE $whr=$user_id");
	if(mysqli_num_rows($sql) == 1) {
		$row = mysqli_fetch_assoc($sql);
		$inv_id = $row['inv_id'];
		$item_tot = $row['item_tot'];
		$sub_tot = $row['sub_tot'];
		$tot_discount = $item_tot-$sub_tot;
		$order_id = $row['order_id'];
		
		if(empty($inv_id)) {
			$response['status'] = '3';
			echo json_encode($response);
			
		} else {
			if (isset($_SESSION['normal_user_cey']) || isset($_SESSION['guest_user_cey'])) {
			
				/*********************** Get Checkout Details **********************/
				
				if (array_key_exists('get_chkout_details', $_POST)) {
					$sql_tmp = "SELECT f_name, l_name, tel_no, email, address_1, address_2, city, district, country, postal_code, delivery_type, special_msg, vat_reg, VAT_No FROM order_details_temp WHERE inv_id='$inv_id'";
					$result_tmp = mysqli_query($conn, $sql_tmp);
				
					if(mysqli_num_rows($result_tmp) == 0) {
						if (isset($_SESSION['normal_user_cey'])) {
							$sql = mysqli_query($conn, "SELECT f_name, l_name, email, tel_no, address_1, address_2, city FROM customer WHERE cusID=$user_id AND val=1");
							$row = mysqli_fetch_assoc($sql);
							echo json_encode($row);
						
						} else {
							//echo json_encode('');
						}
			
					} else {
						$sqltmp = mysqli_fetch_assoc($result_tmp);
						echo json_encode($sqltmp);
					}
				}

				/****************************** Checkout **********************************/
				if (isset($_POST['btn_checkout'])) {
					$fname = htmlspecialchars(strip_tags(trim($_POST['fname'])), ENT_QUOTES, 'UTF-8');
					$lname = htmlspecialchars(strip_tags(trim($_POST['lname'])), ENT_QUOTES, 'UTF-8');
					$tel_no = htmlspecialchars(strip_tags(trim($_POST['tel_no'])), ENT_QUOTES, 'UTF-8');
					$address_1 = htmlspecialchars(strip_tags(trim($_POST['address_1'])), ENT_QUOTES, 'UTF-8');
					$address_2 = htmlspecialchars(strip_tags(trim($_POST['address_2'])), ENT_QUOTES, 'UTF-8');
					$city = htmlspecialchars(strip_tags(trim($_POST['city'])), ENT_QUOTES, 'UTF-8');
					$email = htmlspecialchars(strip_tags(trim($_POST['email'])), ENT_QUOTES, 'UTF-8');
					$del_method = 2; //htmlspecialchars(strip_tags(trim($_POST['del_method'])), ENT_QUOTES, 'UTF-8');
					$postal_code = htmlspecialchars(strip_tags(trim($_POST['postal_code'])), ENT_QUOTES, 'UTF-8');
					$s_msg = htmlspecialchars(strip_tags(trim($_POST['s_msg'])), ENT_QUOTES, 'UTF-8');
					//$country = htmlspecialchars(strip_tags(trim($_POST['country'])), ENT_QUOTES, 'UTF-8');
					
					// if(isset($_POST['vatRegChk'])) {
					// 	$vatRegChk = htmlspecialchars(strip_tags(trim($_POST['vatRegChk'])), ENT_QUOTES, 'UTF-8');
					
					// } else {
					// 	$vatRegChk = 0;
					// }
					
					// if(isset($_POST['vat_reg_no'])) {
					// 	$vat_reg_no = htmlspecialchars(strip_tags(trim($_POST['vat_reg_no'])), ENT_QUOTES, 'UTF-8');
					
					// } else {
					// 	$vat_reg_no = '';
					// }

					// Delivery Fee Calculation
					$sql_cou = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM courier_charges"));
					$del_fee = $sql_cou['courier_fee'];
					
					if($del_fee == 0) {
						$response['status'] = '4';
						$response['message'] = 'Invalid Delivery Fee.';
						
					} else {
						$total_val = $sub_tot + $del_fee;
						$sqlu = "SELECT * FROM order_details_temp WHERE user_id = $user_id AND guest=$guest";
						$resultu = mysqli_query($conn, $sqlu);
						$countu = mysqli_num_rows($resultu);

						if($countu == 0) {
							$sql_add_order = "INSERT INTO order_details_temp SET order_id='$order_id', inv_id='$inv_id', user_id=$user_id, guest=$guest, f_name='$fname', l_name='$lname', tel_no='$tel_no', email='$email', address_1='$address_1', address_2='$address_2', city='$city', country='', district='', postal_code='$postal_code', special_msg='$s_msg', delivery_type='$del_method', pay_method=0, payment_status=0, order_status=0, price_total='$item_tot', tot_discount='$tot_discount', sub_total='$sub_tot', delivery_chg='$del_fee', grand_total='$total_val', date='$now'";
							$result_add_order = mysqli_query($conn,$sql_add_order);

							if ($result_add_order) {
								$response['status'] = '1';
							
							} else {
								$response['status'] = '2';
								$response['message'] = 'MySQL Error! Please Try Again';
							}
						
						} else {
							$sql_add_order = "UPDATE order_details_temp SET order_id='$order_id', inv_no='$inv_id', f_name='$fname', l_name='$lname', tel_no='$tel_no', email='$email', address_1='$address_1', address_2='$address_2', city='$city', district='', country='', postal_code='$postal_code', special_msg='$s_msg', delivery_type='$del_method', pay_method=0, payment_status=0, order_status=0, price_total='$item_tot', tot_discount='$tot_discount', sub_total='$sub_tot', delivery_chg='$del_fee', grand_total='$total_val', date='$now' WHERE user_id = $user_id AND guest=$guest";
							$result_add_order = mysqli_query($conn,$sql_add_order);

							if ($result_add_order) {
								$response['status'] = '1';
							
							} else {
								$response['status'] = '2';
								$response['message'] = 'MySQL Error! Please Try Again';
							}
						}
					}
					
					echo json_encode($response);
				}	
				
				/****************************** // **********************************/
			
			} else {
				$response['status'] = '3'; // Session Expired
				echo json_encode($response);
			}
		}
		
	} else {
		$response['status'] = '3';
		echo json_encode($response);
	}
}
?>