<?php
include_once "../../includes/connectConfig.php";

$query = '';
$querygt = '';
$querysrch = '';
$column = array("order_mart.om_number","order_mart.status","order_mart.dated");
$output = array();

$query .= "SELECT * FROM order_mart WHERE session_num <> '' ";


if(isset($_POST["order_status"]))
{
	$querygt .= " AND order_mart.status = '".$_POST["order_status"]."' ";
}

if(isset($_POST["search"]["value"]))
{	
	$querysrch .= ' AND  (order_mart.om_number LIKE "%'.$_POST["search"]["value"].'%")';
	$query .= $querysrch;
}

if(isset($_POST["order"]))
{
	$query .= ' ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= ' ORDER BY order_mart.dated DESC ';
}

if($_POST["length"] != -1) {
	$query .= ' LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
}

$statement = $db_con->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$data = array();
$filtered_rows = $statement->rowCount();
$i = 0;
foreach($result as $row)
{
	$i++;
	$prd_id = $row['om_id'];
	
	$tt_num = 0; $sbt = 0;
	$odcnt = $db_con->query("SELECT * FROM omart_item WHERE om_id = '$prd_id'");
	$odcnt->execute(); $ftcnt = $odcnt->fetchAll();
	if(!empty($ftcnt)){
		foreach($ftcnt as $dc){
			$tt_num++;
			
			$sbt += $dc['sub_cost'];
		}
	}
	
	
	$stst = $row['status']; 
	$stt = '';
	if($stst == "Completed")
	{
		$stt = '<b class="fonts com">Completed</b>';
	}
	elseif($stst == "Processing")
	{
		$stt = '<b class="fonts pros">Processing</b>';
	}
	elseif($stst == "Pending")
	{
		$stt = '<b class="fonts text-danger">Pending</b>';
	}
	
	$sub_array = array();
	$sub_array[] = '<span class="d-block text-right">'.$i.'</span>';
	
	$sub_array[] = '
	<div class="smlinf">
		<div class="prdinfo">
			<div class="image d-flex align-item-center">
				<a href="orderlink" >
					<img src="../img/kmart-icon.png" alt="Logo" class="img-fluid" />
				</a>
			</div>
			
			<div class="content py-0">
				<h5 class="name">
					<a href="orderlink" class="text-primary font-weight-bold" target="t_blank">'.$row['om_number'].'</a>
				</h5>
				<span class="email text-dark font-weight-bold"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-shopping-cart text-info"><circle cx="9" cy="21" r="1"></circle>
				<circle cx="20" cy="21" r="1"></circle><path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path></svg> Qty:'.$tt_num.'&nbsp 
				<svg xmlns="http://www.w3.org/2000/svg" width="20" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-archive text-warning"><polyline points="21 8 21 21 3 21 3 8"></polyline>
				<rect x="1" y="3" width="22" height="5"></rect><line x1="10" y1="12" x2="14" y2="12"></line></svg>
				Cost: GH₵'.$sbt.'</span>
			</div>
		</div>
	</div>';
	
	
	$sub_array[] = '<div class="text-center">'.$stt.'</div>';
	
	$sub_array[] = date('jS-M-Y  -- h:i a', strtotime($row['dated']));

	$sub_array[] = '
		<div class="table-data-feature vertical-align text-center">
			<a href="?setup=view&prd='.$row['om_number'].'" class="bg-secondary text-white bs-tooltip border-0 btn rounded-circle editsubjs"  title="Edit">
				<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-package"><line x1="16.5" y1="9.4" x2="7.5" y2="4.21"></line><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z">
				</path><polyline points="3.27 6.96 12 12.01 20.73 6.96"></polyline><line x1="12" y1="22.08" x2="12" y2="12"></line></svg>
			</a>
		</div>
	';
	
	$data[] = $sub_array;
}

$output = array(
	"draw"				=>	intval($_POST["draw"]),
	"recordsTotal"  	=>  $filtered_rows,
	"recordsFiltered" 	=> 	get_total_all_records($db_con),
	"data"    			=> 	$data
);
echo json_encode($output);

function get_total_all_records($db_con)
{
	global $querygt,$querysrch;
	$statement = $db_con->prepare("SELECT * FROM order_mart WHERE session_num <> '' ".$querygt.$querysrch);
	$statement->execute();
	return $statement->rowCount();
}



?>