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

$query = '';
$querygt = '';
$querysrch = '';
$column = array("kmartstockprds.pname","kmartstockprds.dated");
$output = array();

$query .= "SELECT * FROM kmartstockprds WHERE delete_status = '0'";

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



if(isset($_POST['setpst']) && $_POST['setpst'] != "all")
{
	$pstd = $_POST['setpst'];
	$query .= $querygt .= " AND kmartstockprds.mc_id = '$pstd' ";	
}
else
{
	$query .= $querygt .= " AND kmartstockprds.ksp_id <> '' ";
}

if(isset($_POST["order"]))
{
	$query .= ' ORDER BY '.$column[$_POST['order']['0']['column']].' '.$_POST['order']['0']['dir'].' ';
}
else
{
	$query .= ' ORDER BY kmartstockprds.pname ASC ';
}

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();

foreach($result as $row)
{
	$prd_id = $row['ksp_id'];	
	$gt_imgs = gtOne("kprdw_imgs"," prd_rec = '$row[pqrcode]' ORDER BY kmg_id ASC","pjm_pic");
	$prd_img = (!empty($gt_imgs) && file_exists('../../prdimgs/'.$gt_imgs)) ? "../prdimgs/".$gt_imgs : '../img/kmart-icon.png';
	
	$jw_ct = gtOne("ktmmenu_name"," mn_id = '$row[mn_id]' ","mname");
	$sub_array = array();

	$sub_array[] = '
	
	<div class="smlinf">
		<div class="prdinfo">
			<div class="image d-flex align-item-center">
				<a href="../product?prd='.$row['purl'].'" target="t_blank">
					<img src="'.$prd_img.'" alt="'.utf8_encode($row['pname']).'" class="img-fluid" />
				</a>
			</div>
			
			<div class="content py-0">
				<h5 class="name">
					<a href="../product?prd='.$row['purl'].'" class="text-primary font-weight-bold" target="t_blank">'.utf8_encode($row['pname']).'</a>
				</h5>
				<span class="email text-dark font-weight-bold"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-codesandbox"><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="7.5 4.21 12 6.81 16.5 4.21"></polyline><polyline points="7.5 19.79 7.5 14.6 3 12"></polyline><polyline points="21 12 16.5 14.6 16.5 19.79"></polyline><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> '.$jw_ct.'</span>
			</div>
		</div>
	</div>';
	
	
	$sub_array[] = '<div class="text-right">'.$row['pqty'].'</div>';
	$sub_array[] = '<div class="text-right">'.$row['psell'].'</div>';
	$sub_array[] = '<div class="text-right">'.$row['pdiscount'].'</div>';
	
	$sub_array[] = date('jS-M-Y', strtotime($row['dated']));

	$sub_array[] = '
		<div class="dropdown custom-dropdown vertical-align text-center">
			<a class="dropdown-toggle bg-danger text-white border-0 btn rounded-circle" href="#" role="button" id="dropdownMenuLink7" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
				<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-more-horizontal"><circle cx="12" cy="12" r="1"></circle><circle cx="19" cy="12" r="1"></circle><circle cx="5" cy="12" r="1"></circle></svg>
			</a>
			
			<div class="dropdown-menu" aria-labelledby="dropdownMenuLink7">
				
				<a class="dropdown-item" href="../product?prd='.$row['purl'].'">
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-external-link"><path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
			<polyline points="15 3 21 3 21 9"></polyline><line x1="10" y1="14" x2="21" y2="3"></line></svg> View </a>
				
				<a class="dropdown-item" href="?setup=edit-product&prd='.$prd_id.'">
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-edit-3">
			<path d="M12 20h9"></path><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L16.5 3.5z"></path></svg> Edit </a>
				
				<a class="dropdown-item" href="javascript:void(0);" id="del_'.$prd_id.'" onClick="takeoff('.$prd_id.',\'tdaplx\')" >
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" class="feather feather-trash-2 table-cancel">
			<polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line>
			</svg> Delete</a>
			
			
			<div class="dropdown-divider"></div>
			
			<a class="dropdown-item nwsfts" href="javascript:void(0);" id="prdtab_'.$prd_id.'" >
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-grid"><rect x="3" y="3" width="7" height="7"></rect><rect x="14" y="3" width="7" height="7"></rect><rect x="14" y="14" width="7" height="7"></rect><rect x="3" y="14" width="7" height="7"></rect></svg> Tab Feature</a>
			
			<a class="dropdown-item nwsfts" href="javascript:void(0);" id="catshp_'.$prd_id.'">
				<svg xmlns="http://www.w3.org/2000/svg" width="18" height="24" viewBox="0 0 24 25" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-layers"><polygon points="12 2 2 7 12 12 22 7 12 2"></polygon><polyline points="2 17 12 22 22 17"></polyline><polyline points="2 12 12 17 22 12"></polyline></svg> Menu Shop Feature</a>
			
			<a class="dropdown-item nwsfts" href="javascript:void(0);" id="shpftu_'.$prd_id.'">
				<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-git-commit"><circle cx="12" cy="12" r="4"></circle><line x1="1.05" y1="12" x2="7" y2="12"></line><line x1="17.01" y1="12" x2="22.96" y2="12"></line></svg> Shop Feature</a>
			
			
			<a class="dropdown-item nwsfts" href="javascript:void(0);" id="hmbgprm_'.$prd_id.'">
				<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-link-2"><path d="M15 7h3a5 5 0 0 1 5 5 5 5 0 0 1-5 5h-3m-6 0H6a5 5 0 0 1-5-5 5 5 0 0 1 5-5h3"></path><line x1="8" y1="12" x2="16" y2="12"></line></svg> Home Promo Feature</a>
			
			</div>
		</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 kmartstockprds WHERE delete_status = 0 ".$querygt.$querysrch);
	$statement->execute();
	return $statement->rowCount();
}

?>