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

if($_POST)
{
	
	//TO FETCH MENU DATA RECORDS
	if(isset($_POST['fetch_clprdss']))
	{
		$subj = test_input($_POST['namesujid']);
		
		/*$getsubjj = $db_con->prepare("SELECT * FROM assign_class_subject INNER JOIN classes ON assign_class_subject.cl_id =
		 classes.cl_id WHERE sj_id = '$subj' AND assign_class_subject.delete_status = 0 AND classes.delete_status = 0"); 
		$getsubjj->execute();
		$getallsubj = $getsubjj->fetchAll();
		*/
		if(isset($_POST['sgnmk']) && $_POST['sgnmk'] == 'hsrec'){
			$getallsubj = gtALL("ktmmenu_subcategory"," mc_id = '$subj' ","sname ASC");
			
			$subj_data['subjdata'] = '<div class="table-responsive">
								<table class="table table-bordered table-hover table-striped table-highlight-head mb-4">
									<thead>
										<tr>
											<th class="">Name</th>
											<th class="text-center">&nbsp;</th>
										</tr>
									</thead>
									<tbody>';
			if(!empty($getallsubj))
			{
				
							foreach($getallsubj as $sjs):
				$subj_data['subjdata'] .= '<tr>
								<td>
									<p class="mb-0">'.$sjs['sname'].'</p>
								</td>
								<td class="vertical-align text-center">
									<div class="table-data-feature">
									
										<button class="bg-danger text-white bs-tooltip border-0 btn rounded-circle" id="del_'.$sjs['ms_id'].'" onClick="takeoff('.$sjs['ms_id'].',\'sdclai\')"  title="Delete">
											<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-trash-2">
											<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>
										</button>
										
									</div>
								</td>
							</tr>';
								endforeach;
										
			}
			else
			{
				$subj_data['subjdata'] .= '
					<tr>
						<td colspan="2">No Data to this Category.</td>
					</tr>
				';
			}
				$subj_data['subjdata'] .= '</tbody>
								</table>
							</div>';
			echo json_encode($subj_data);
			exit;	
		}
		else{
			$getallsubj = gtALL("ktmmenu_category"," mn_id = '$subj' ","cname ASC");
			
			$subj_data['subjdata'] = '<div class="table-responsive">
								<table class="table table-bordered table-hover table-striped table-highlight-head mb-4">
									<thead>
										<tr>
											<th colspan="2">Name</th>
										</tr>
									</thead>
									<tbody>';
			if(!empty($getallsubj)){
							foreach($getallsubj as $sjs){
							$fndur = $sjs['mc_id'];
							$sbmc = gtAll("ktmmenu_subcategory ","mc_id = '$fndur' ", 'sname ASC');
								
					$subj_data['subjdata'] .= '<tr>
								<td colspan="2">
									<h5 class="mb-0 font-weight-bold">'.$sjs['cname'].'</h5>
								</td>
							</tr>';
					if(!empty($sbmc)){
						$l = 0;
						foreach($sbmc as $sv){
							$l++;
						$subj_data['subjdata'] .= '
								<tr>
								<td>
									<p class="mb-0 text-center">'.$l.'</p>
								</td>
								<td width="70%">
									<p class="mb-0">'.$sv['sname'].'</p>
								</td>
								</tr>
						';
						}
					}
				}	
			}
			else
			{
				$subj_data['subjdata'] .= '
					<tr>
						<td colspan="2">No Data to this Menu.</td>
					</tr>
				';
			}
				$subj_data['subjdata'] .= '</tbody>
								</table>
							</div>';
			echo json_encode($subj_data);
			exit;
		}
	}
	
	//SHOW RELATED MENU CATEGORY RECORDS
	if(isset($_POST["selclas_id"]) && !empty($_POST["selclas_id"])){ 
		$clsid =  $_POST['selclas_id'];
		$chk_rec = gtOne("ktmmenu_name"," mn_id = '$clsid' ","mn_id");
		if(!empty($chk_rec)){
			$getallsubj = gtALL("ktmmenu_category"," mn_id = '$chk_rec' ","cname ASC");
			$output["sub_menu"] ="";
			if(!empty($getallsubj)){
				$output["sub_menu"] .= '<option  disabled selected value="">Select Category</option>';
				foreach($getallsubj as $cub){ $output["sub_menu"] .= '<option value="'.$cub['mc_id'].'">'.ucwords($cub['cname']).'</option>'; }
			}
			else{ $output["sub_menu"] .= '<option name="" value="">No Category</option>'; }
		}
		else{ $output["sub_menu"] .= '<option name="" value="">N/A</option>'; }
	echo json_encode($output);
	exit;		
	}
	
	//SHOW RELATED SUB CATEGORY RECORDS FROM  CATEGORY
	if(isset($_POST["selcsubm_id"]) && !empty($_POST["selcsubm_id"])){ 
		$clsid =  $_POST['selcsubm_id'];
		$chk_rec = gtOne("ktmmenu_category"," mc_id = '$clsid' ","mc_id");
		if(!empty($chk_rec)){
			$getallsubj = gtALL("ktmmenu_subcategory"," mc_id = '$chk_rec' ","sname ASC");
			$output["sub_menu"] ="";
			if(!empty($getallsubj)){
				$output["sub_menu"] .= '<option  disabled selected value="">Select Category</option>';
				foreach($getallsubj as $cub){ $output["sub_menu"] .= '<option value="'.$cub['ms_id'].'">'.ucwords($cub['sname']).'</option>'; }
			}
			else{ $output["sub_menu"] .= '<option name="" value="">No Category</option>'; }
		}
		else{ $output["sub_menu"] .= '<option name="" value="">N/A</option>'; }
	echo json_encode($output);
	exit;		
	}
	
	
	
	
	
	//TO FEATURE CATEGORY AS TABS
	if(isset($_POST['featuringmenucategory']))
	{
		$featurephasest = test_input($_POST['pstnt']); // feature type name
		$featureproductst = test_input($_POST['pstrec']);  // feature item id number
		
		if($featurephasest == "homtb"){	
			$st_st = 'hometab'; //FOR HOME TABS TO SET CATEGORY
			$chk_pd = gtOne("ktmmenu_name"," mn_id = '$featureproductst' ","mn_id");
			if(empty($chk_pd)){ $output["runstt"] = "warant"; $output["runsttinfo"] = "Invalid Request Data."; echo json_encode($output); exit; }
			$st_rc = " rec_nm = 'hometab' ";
			$st_ct = 6;
		} 
		elseif($featurephasest == "prdtab"){	
			$st_st = 'prodtab'; // FOR HOME TABS CATEGORY PRODUCTS
			$chk_pd = gtOne("kmartstockprds"," ksp_id = '$featureproductst' ","ksp_id");
			if(empty($chk_pd)){ $output["runstt"] = "warant"; $output["runsttinfo"] = "Invalid Request Data."; echo json_encode($output); exit; }
			$st_rc = " rec_nm = 'prodtab' ";
			$st_ct = 50;
		}
		elseif($featurephasest == "catshp"){	
			$st_st = 'categoryshop'; // TO SET PRODUCTS FOR CATEGORY VIEW
			$chk_pd = gtOne("kmartstockprds"," ksp_id = '$featureproductst' ","ksp_id");
			if(empty($chk_pd)){ $output["runstt"] = "warant"; $output["runsttinfo"] = "Invalid Request Data."; echo json_encode($output); exit; }
			$st_rc = " rec_nm = 'categoryshop' ";
			$st_ct = 10;
		}
		elseif($featurephasest == "shpftu"){	
			$st_st = 'featuredshop'; // TO SET GENERAL FETURTED PRODUCTS ON SHOP PAGE
			$chk_pd = gtOne("kmartstockprds"," ksp_id = '$featureproductst' ","ksp_id");
			if(empty($chk_pd)){ $output["runstt"] = "warant"; $output["runsttinfo"] = "Invalid Request Data."; echo json_encode($output); exit; }
			$st_rc = " rec_nm = 'featuredshop' ";
			$st_ct = 10;
		}
		
		elseif($featurephasest == "hmbgprm"){
			$st_st = 'homepromo'; // TO SET HOME PAGE PROMO PAGE
			$chk_pd = gtOne("kmartstockprds"," ksp_id = '$featureproductst' ","ksp_id");
			if(empty($chk_pd)){ $output["runstt"] = "warant"; $output["runsttinfo"] = "Invalid Request Data."; echo json_encode($output); exit; }
			$st_rc = " rec_nm = 'homepromo' ";
			$st_ct = 15;
		}
		
		
		$gtPost = gtCount("kmt_featuresd"," rec_nm = '".$st_st."' AND delete_status = 0");
		if(($st_st == "hometab" && $gtPost == $st_ct) || ($st_st == "prodtab" && $gtPost == $st_ct) || ($st_st == "categoryshop" && $gtPost == $st_ct) || ($st_st == "featuredshop" && $gtPost == $st_ct)
			|| ($st_st == "homepromo" && $gtPost == $st_ct) )
		{ $output["runstt"] = "warant"; $output["runsttinfo"] = "Limit Reached"; echo json_encode($output); exit; }
			
		//CHECK IF ALREADY FEATURED
		$chkPstdel = getOne('kmt_featuresd'," rec_id = '$featureproductst' AND rec_nm = '$st_st' AND delete_status = 0",'id');
		if(!empty($chkPstdel))
		{
			$output["runstt"] = "grant";
			$output["runsttinfo"] = "Already Featured";
			echo json_encode($output);
			exit;
		}
		// IF ALREADY THERE FEATURED, THEN GET ID AND RE-FEATURE
		$chkPstNdel = getOne('kmt_featuresd'," rec_id = '$featureproductst' AND rec_nm = '$st_st' AND delete_status = 1",'id');
		if(!empty($chkPstNdel))
		{
			$prd = $db_con->prepare("UPDATE kmt_featuresd SET delete_status = 0 WHERE id =:jrec ");
			$prd->bindParam(':jrec',$chkPstNdel);
			$prd->execute();
		
			$output["runstt"] = "grant";
			$output["runsttinfo"] = "Featured Successfully";
			echo json_encode($output);
			exit;
		}
		
		$prd = $db_con->prepare("INSERT INTO kmt_featuresd(rec_id, rec_nm) VALUES(:jrec, :jrole)");
		$prd->bindParam(':jrec',$featureproductst);
		$prd->bindParam(':jrole',$st_st);
		$prd->execute();
		
		$output["runstt"] = "grant";
		$output["runsttinfo"] = "Featured Successfully";
		echo json_encode($output);
		exit;
	}
}

?>