<?php
include_once "connectConfig.php";
	
	$gt_themn = '';
	if(!empty($category_url)){
		$drepds = gtOne("ktmmenu_category"," curl = '$category_url' ","mc_id");
		$drecv = (!empty($drepds)) ? " AND mc_id = '$drepds'  " : '';
		$drenm = gtOne("ktmmenu_category"," mc_id = '$drepds' ","cname");
		$nv = (!empty($drecv)) ? "?kcm=".$category_url."&" : "?";
		
		$gt_themn = gtOne("ktmmenu_category"," mc_id = '$drepds' ","mn_id");
	}
	elseif(!empty($sub_url)){
		$drepds = gtOne("ktmmenu_subcategory"," surl = '$menu_url' ","ms_id");
		$drecv = (!empty($drepds)) ? " AND mc_id = '$drepds'  " : '';
		$drenm = gtOne("ktmmenu_subcategory"," ms_id = '$drepds' ","sname");
		$nv = (!empty($drecv)) ? "?ksm=".$sub_url."&" : "?";
		
		$gt_thesb = gtOne("ktmmenu_subcategory"," ms_id = '$drepds' ","mc_id");
		$gt_themn = gtOne("ktmmenu_category"," mc_id = '$gt_thesb' ","mn_id");
	}
	elseif(!empty($menu_url)){
		$drepds = gtOne("ktmmenu_name"," murl = '$menu_url' ","mn_id");
		$drecv = (!empty($drepds)) ? " AND mn_id = '$drepds'  " : '';
		$drenm = gtOne("ktmmenu_name"," mn_id = '$drepds' ","mname");
		$nv = (!empty($drecv)) ? "?kmm=".$menu_url."&" : "?";
		
		$gt_themn = $drepds;
		
	}else{ $drecv = '' ; $nv = "?"; $drenm = ''; }
	
	
	
	$gt_shopcategory = (!empty($drecv)) ?  gtALL('kmt_featuresd'," rec_nm = 'categoryshop' ",'id ASC') : ''; //FETCH FEATURED CATEGORY SHOP PRODUCTS
	
	$kpchk = ''; $sql_arprd = '';
	if(($paginator_pg == "" || $paginator_pg == 1) && empty($pdserch)){
		if(!empty($drecv) && !empty($gt_shopcategory)){
		$arprd = array(); //ARRAY TO HOLD PRODUCT ALREADY FEATURED
		foreach($gt_shopcategory as $pds){
			$gt_prds = gtOne("kmartstockprds","  ksp_id = '$pds[rec_id]' ".$drecv,'ksp_id');
			if((!empty($gt_prds))){       $kpchk = 'record';        $arprd[] .= $gt_prds;       }
		} 
			$nwimp_arprd = implode("','",$arprd);  $sql_arprd = " AND ksp_id NOT IN('".$nwimp_arprd."')";
		}
		
		$gt_shopfeature = gtALL('kmt_featuresd'," rec_nm = 'featuredshop' ",'id ASC'); //FETCH FEATURED GENERAL SHOP VIEW PRODUCTS
		if(!empty($gt_shopfeature) && empty($kpchk)){
		$arprd = array(); //ARRAY TO HOLD PRODUCT ALREADY FEATURED
		foreach($gt_shopfeature as $pds){
			$gt_prds = gtOne("kmartstockprds","  ksp_id = '$pds[rec_id]' ",'ksp_id');
			if((!empty($gt_prds))){  $arprd[] .= $gt_prds;   }
		} 
			$nwimp_arprd = implode("','",$arprd);  $sql_arprd = " AND ksp_id NOT IN('".$nwimp_arprd."')";
		}
	}
	
	if(!empty($pdserch)){
		$sql_arprd = " AND pname LIKE '%".$pdserch."%' ";
	}
	
	
	$sng_prdx = $db_con->prepare("SELECT COUNT(ksp_id) FROM kmartstockprds WHERE delete_status = 0 ".$sql_arprd.$drecv);
	$sng_prdx->execute(); $sum_rows = $sng_prdx->fetchColumn();

	$rows = $sum_rows;
	
	$page_rows = 16;

	$last = ceil($rows/$page_rows);

	if($last < 1){ $last = 1; }
	
	$pagenum = (isset($_GET['pd'])) ? preg_replace('#[^0-9]#', '', $_GET['pd']) : 1 ;
	
	
	if ($pagenum < 1) {
		$pagenum = 1; 
	} 
	else if ($pagenum > $last){ 
		$pagenum = $last; 
	}
	
	$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
	
	$nquery = gtAll("kmartstockprds"," ksp_id <> 0 ".$sql_arprd.$drecv," dated DESC $limit");
	
	$current = ($pagenum * $page_rows);
	$start_limit = (($pagenum - 1) * $page_rows) + 1;
	
	if($pagenum == $last){ $current_limit = $sum_rows; } else { $current_limit = $current;	}
	
	
	$paginationCtrls = $paginationCtrlsLeft = $paginationCtrlsRight = '';
	//if($last != 1){
		if ($pagenum > 1) 
		{
			
			$previous = $pagenum - 1;
			$paginationCtrlsLeft .= '<li><a href="'.$_SERVER['PHP_SELF'].$nv.'pd='.$previous.'" class="link-page prev"><i class="fa fa-angle-left" aria-hidden="true"></i> Prev</a></li>';
		}
		else
		{
			$paginationCtrlsLeft .= '<li><a href="javascript:void(0);"  style="cursor: not-allowed;" class="link-page prev"><i class="fa fa-angle-left" aria-hidden="true"></i> Prev</a></li>';
		}
		
		if ($pagenum != $last) 
		{
			
			$next = $pagenum + 1;
			$paginationCtrlsRight .= '<li><a href="'.$_SERVER['PHP_SELF'].$nv.'pd='.$next.'" class="link-page next">Next <i class="fa fa-angle-right" aria-hidden="true"></i></a></li>';
			
		}
		else
		{
			$paginationCtrlsRight .= '<li><a href="javascript:void(0);"  style="cursor: not-allowed;" class="link-page next">Next <i class="fa fa-angle-right" aria-hidden="true"></i></a></li>';
		}
		
		$paginationCtrls .= $paginationCtrlsLeft.'<li><span class="current-page">Showing '.$start_limit .'-'. $current_limit.' of '.$sum_rows.'</span></li>'.$paginationCtrlsRight;
	//}
	
	
		
	
	
	
?>