<?php
include_once "../includes/connectConfig.php";
if($_POST)
{
	//$cart_id = $_POST['cart_id'];
	
	$order_number =  gntRdmorderNum();
	$user_id = $set_sess;
	
	$chk_crt = getAll("my_cart"," session_rec = '$set_sess' ",' id ASC');
	
	if((!empty($chk_crt)) && (cart_stk("tot_crt") > 0)){
	
	$fn_ame = test_input($_POST['fname']);
	$lnam_e = test_input($_POST['lname']);
	$coun_try = test_input($_POST['country']);
	$st_reet = test_input($_POST['street']);
	$optstr_eet = (isset($_POST['optstreet']) && !empty($_POST['optstreet'])) ? test_input($_POST['optstreet']) : NULL;
	$p_ostal = (isset($_POST['postal']) && !empty($_POST['postal'])) ? test_input($_POST['postal']) : NULL;
	$regi_on = test_input($_POST['region']);
	$sc_ity = test_input($_POST['scity']);
	$sema_il = test_input($_POST['semail']);
	$sp_hone = test_input($_POST['sphone']);
	
	$dlvad_rs = test_input($_POST['dlvadrs']);
	
	if($dlvad_rs == "another"){
		$df_name = test_input($_POST['dfname']);
		$dln_ame = test_input($_POST['dlname']);
		$dcom_pany = test_input($_POST['dcompany']);
		$dco_untry = test_input($_POST['dcountry']);
		$ds_treet = test_input($_POST['dstreet']);
		$dopts_treet = (isset($_POST['doptstreet']) && !empty($_POST['doptstreet'])) ? test_input($_POST['doptstreet']) : NULL;
		$dpos_tal = (isset($_POST['dpostal']) && !empty($_POST['dpostal'])) ? test_input($_POST['dpostal']) : NULL;
		$dre_gion = test_input($_POST['dregion']);
		$dsci_ty = test_input($_POST['dscity']);
		$dsem_ail = test_input($_POST['dsemail']);
		$dsph_one = test_input($_POST['dsphone']);
	}
	
	$ntpo_rd = (isset($_POST['ntpord']) && !empty($_POST['ntpord'])) ? test_input($_POST['ntpord']) : NULL;
	
	
	$order = $db_con->prepare("INSERT INTO order_mart (om_number, session_num, remarks, delivery, status) VALUES (:order_number,:user_id, :rmks, :dlv,'Pending') "); //MAIN ORDER TABLE
	$order->bindParam(':order_number',$order_number);
	$order->bindParam(':user_id',$user_id);
	$order->bindParam(':rmks',$ntpo_rd);
	$order->bindParam(':dlv',$dlvad_rs);
	$order->execute();
	
	$last_id = $db_con->lastInsertId();
	
	
	
	$mainsubt = 0;
	foreach($chk_crt as $dcv){
		$fdid = $dcv['id'];
		$chkct = $dcv['qty'];
		$fdprp = $dcv['pqrcode'];
		
		$chk_prdz = $db_con->prepare("SELECT * FROM kmartstockprds WHERE pqrcode = '$fdprp' ");
		$chk_prdz->execute(); $pdz = $chk_prdz->fetch();
		$prdid = $pdz['ksp_id'];
		
		$chk_sl = $pdz['psell'];    $chk_ds = $pdz['pdiscount'];
		if($dcv['prd_attr'] == 'promo'){
		//FROM GENERAL FEATURED SPECIAL PROMO
		$chk_ftp = $db_con->prepare("SELECT * FROM kmt_ft_prds WHERE ksp_id = '$pdz[ksp_id]' AND delete_status = 0 ");
		$chk_ftp->execute(); $fpd = $chk_ftp->fetch();
		
		$chk_sl = (!empty($fpd) && !empty($fpd['km_sell'])) ? $fpd['km_sell'] : $pdz['psell'];
		$chk_ds = (!empty($fpd) && !empty($fpd['km_discount'])) ? $fpd['km_discount'] : $pdz['pdiscount'];
		}
		
		$prd_img = (!empty($chk_mgi) && file_exists('prdimgs/'.$chk_mgi)) ? "prdimgs/".$chk_mgi : 'img/kmart-icon.png';
		
		$shw_pec = calcprcs($chk_sl,$chk_ds,'spc');
		$shw_ds = calcprcs($chk_sl,$chk_ds,'ssl');
		$shw_dsc = calcprcs($chk_sl,$chk_ds,'sds');
		
		$imdsubt = ($shw_ds*$chkct);
		$mainsubt += $imdsubt;
		
		$order_items = $db_con->prepare("INSERT INTO omart_item(om_id, ksp_id, qty, sub_cost)	VALUES (:order_number, :prd_code, :quantity, :subtotal)");
		$order_items->bindParam(':order_number',$last_id);
		$order_items->bindParam(':prd_code',$prdid);
		$order_items->bindParam(':quantity',$chkct);
		$order_items->bindParam(':subtotal',$imdsubt);
		$order_items->execute();
		
		$order = $db_con->prepare("DELETE FROM my_cart WHERE id = $fdid ");
		$order->execute();	
	}
	
	$order_adrs = $db_con->prepare("INSERT INTO omart_address(om_id, cf_name, cl_name, ccountry, cstreet, capartment, cregion, cstate, cpostal, cemail, cphone)	
														VALUES (:order_number, :usrfnm, :usrlnm, :usrctry, :usrstr, :usrapt, :usrreg, :usrstt, :usrpst, :usrmail, :usrphn)");
	$order_adrs->bindParam(':order_number',$last_id);
	$order_adrs->bindParam(':usrfnm',$fn_ame);
	$order_adrs->bindParam(':usrlnm',$lnam_e);
	$order_adrs->bindParam(':usrctry',$coun_try);
	$order_adrs->bindParam(':usrstr',$st_reet);
	$order_adrs->bindParam(':usrapt',$optstr_eet);
	$order_adrs->bindParam(':usrreg',$regi_on);
	$order_adrs->bindParam(':usrstt',$sc_ity);
	$order_adrs->bindParam(':usrpst',$p_ostal);
	$order_adrs->bindParam(':usrmail',$sema_il);
	$order_adrs->bindParam(':usrphn',$sp_hone);
	$order_adrs->execute();
	
	if($dlvad_rs == "another"){
		$order_adrs = $db_con->prepare("INSERT INTO omart_deliveryaddress(om_id, cf_name, cl_name, ccompany, ccountry, cstreet, capartment, cregion, cstate, cpostal, cemail, cphone)
														VALUES (:order_number, :usrfnm, :usrlnm, :compny, :usrctry, :usrstr, :usrapt, :usrreg, :usrstt, :usrpst, :usrmail, :usrphn)");
		$order_adrs->bindParam(':order_number',$last_id);
		$order_adrs->bindParam(':usrfnm',$df_name);
		$order_adrs->bindParam(':usrlnm',$dln_ame);
		$order_adrs->bindParam(':compny',$dcom_pany);
		$order_adrs->bindParam(':usrctry',$dco_untry);
		$order_adrs->bindParam(':usrstr',$ds_treet);
		$order_adrs->bindParam(':usrapt',$dopts_treet);
		$order_adrs->bindParam(':usrreg',$dre_gion);
		$order_adrs->bindParam(':usrstt',$dsci_ty);
		$order_adrs->bindParam(':usrpst',$dpos_tal);
		$order_adrs->bindParam(':usrmail',$dsem_ail);
		$order_adrs->bindParam(':usrphn',$dsph_one);
		$order_adrs->execute();
	}
	
	
	$order_stm = $db_con->prepare("INSERT INTO omart_status(om_id, status)	VALUES (:order_number, 'Pending')");
	$order_stm->bindParam(':order_number',$last_id);      		$order_stm->execute();
		
		
	unset($_SESSION['account_session']);
	unset($_SESSION['unknown_cart']);

	
	$length = 50;
	$ur = bin2hex(openssl_random_pseudo_bytes($length));		
	$output["direct"] = 'checkout?preg='.$ur.'&order=complete&ordered='.$order_number;
	
	$output["mkresult"] = "done";
	
	$output["runsttinfo"] = '<div class="alert alert-success">
		<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-alert-triangle"><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"></path><line x1="12" y1="9" x2="12" y2="13"></line><line x1="12" y1="17" x2="12" y2="17"></line></svg>
		<strong>Success!</strong> Order Placed Successful, Will Be Processed.
	</div>';
	echo json_encode($output);
	exit;
	
	}else{
		$output["mkresult"] = "warant";
		$output["runsttinfo"] = '<div class="alert alert-error">
						<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-bell"><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"></path><path d="M13.73 21a2 2 0 0 1-3.46 0"></path></svg>
						<strong>Failed!</strong> Invalid Data Record, Try Again.
					</div>';
		echo json_encode($output);
		exit;
	}
}
?>