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

if($_POST)
{
	if(isset($_POST['setmenu']) && !empty($_POST['setmenu']) && !empty($_POST['menuname'])){
		$stp = $_POST['setmenu'];
		$fndur = gtOne("ktmmenu_category"," curl = '$stp' ", 'mc_id');
		if(!empty($fndur)){
			if(!empty($_POST['subjid']))
			{
				$sub_j = test_input($_POST['subjid']);
				$sub_nm = test_input($_POST['sinsubjec']);
				$nmnm = php_url_slug($sub_nm);
				$ins_subs = $db_con->prepare("UPDATE ktmmenu_subcategory SET sname=:subname, surl=:suburl WHERE ms_id=:sbj"); 
				$ins_subs->bindParam(':subname',$sub_nm); $ins_subs->bindParam(':suburl',$nmnm); $ins_subs->bindParam(':sbj',$sub_j); $ins_subs->execute();
				
				$output['runstt'] = "grant";
				$output['dtct'] = "Sub Category Record Updated";
				echo json_encode($output);
				exit;
			}
			else
			{
				$S = count($_POST['subjec']); $i = 0;
				do{
					$sub_nm = test_input($_POST['subjec'][$i]);
					if(!empty($sub_nm)){
						$nmnm = php_url_slug($sub_nm);
						$ins_subs = $db_con->prepare("INSERT INTO  ktmmenu_subcategory  (sname, mc_id, surl) VALUES(:subname, :crec, :suburl)"); 
						$ins_subs->bindParam(':subname',$sub_nm); $ins_subs->bindParam(':crec',$fndur); $ins_subs->bindParam(':suburl',$nmnm);  $ins_subs->execute();
					} $i++;
				}
				while($i < $S);
				$output['runstt'] = "grant";
				$output['dtct'] = "Sub Category Menu Added";
				echo json_encode($output);
				exit;
			}
		}else{
			$output['runstt'] = "warant";
			$output['dtct'] = "Invalid Data Entry, Refresh and Try Again.";
			echo json_encode($output);
			exit;
		}
	}
	//FOR CATEGORY
	elseif(isset($_POST['menuname']) && !empty($_POST['menuname']) && empty($_POST['setmenu'])){
		$stp = $_POST['menuname'];
		$fndur = gtOne("ktmmenu_name"," murl = '$stp' ", 'mn_id');
		if(!empty($fndur)){
			if(!empty($_POST['subjid']))
			{
				$sub_j = test_input($_POST['subjid']);
				$sub_nm = test_input($_POST['sinsubjec']);
				$nmnm = php_url_slug($sub_nm);
				$ins_subs = $db_con->prepare("UPDATE ktmmenu_category SET cname=:subname, curl=:suburl WHERE mc_id=:sbj"); 
				$ins_subs->bindParam(':subname',$sub_nm); $ins_subs->bindParam(':suburl',$nmnm); $ins_subs->bindParam(':sbj',$sub_j); $ins_subs->execute();
				
				$output['runstt'] = "grant";
				$output['dtct'] = "Category Record Updated";
				echo json_encode($output);
				exit;
			}
			else
			{
				$S = count($_POST['subjec']); $i = 0;
				do{
					$sub_nm = test_input($_POST['subjec'][$i]);
					if(!empty($sub_nm)){
						$nmnm = php_url_slug($sub_nm);
						$ins_subs = $db_con->prepare("INSERT INTO ktmmenu_category (cname, mn_id, curl) VALUES(:subname, :crec, :suburl)"); 
						$ins_subs->bindParam(':subname',$sub_nm); $ins_subs->bindParam(':crec',$fndur); $ins_subs->bindParam(':suburl',$nmnm);  $ins_subs->execute();
					} $i++;
				}
				while($i < $S);
				$output['runstt'] = "grant";
				$output['dtct'] = "Category Added";
				echo json_encode($output);
				exit;
			}
		}else{
			$output['runstt'] = "warant";
			$output['dtct'] = "Invalid Data Entry, Refresh and Try Again.";
			echo json_encode($output);
			exit;
		}
	}    //FOR MENU NAME
	else{
		if(isset($_POST['setmenu']) && !empty($_POST['setmenu'])){
			$output['runstt'] = "warant";
			$output['dtct'] = "Invalid Data Entry, Refresh and Try Again.";
			echo json_encode($output);
			exit;
		}
		else{
			$valid_extensions = array('png','jpg','jpeg');

			$namea = $_FILES['pic_a']['name'];
			$sizea = $_FILES['pic_a']['size'];
			$extsa = strtolower(pathinfo($namea, PATHINFO_EXTENSION));
			
			if(!empty($namea) && !(in_array($extsa, $valid_extensions)) || $sizea > 5000000)
			{
				$output["runstt"] = "warant";
				$output["errorform"] = "a";
				$output["errorreason"] = "Check file type or size";
				echo json_encode($output);
				exit;
			}
			else
			{
				$folder = "../../prdimgs/"; 
				
				if(!empty($_POST['subjid']))
				{
					
					$rfgb = getOne("ktmmenu_name"," mn_id = '$sub_j' ","mbanner");
					if(!empty($namea)){
						$tmpa = $_FILES['pic_a']['tmp_name'];
						list($txta, $exta) = explode(".", $namea);
						$imga = strtolower('ctbanne_'.rand(1000,100000).'.'.$exta);
						$filePatha = $folder.'/'.$imga;
						move_uploaded_file($tmpa, $filePatha);
					}
					
					$pc_bn = (!empty($namea)) ? $imga : $rfgb ;
			
					$sub_j = test_input($_POST['subjid']);
					$sub_nm = test_input($_POST['sinsubjec']);
					$nmnm = php_url_slug($sub_nm);
					$ins_subs = $db_con->prepare("UPDATE ktmmenu_name SET mname=:subname, murl=:suburl, mbanner=:mbn WHERE mn_id=:sbj"); 
					$ins_subs->bindParam(':subname',$sub_nm); $ins_subs->bindParam(':suburl',$nmnm); $ins_subs->bindParam(':mbn',$pc_bn); $ins_subs->bindParam(':sbj',$sub_j); $ins_subs->execute();
					
					$output['runstt'] = "grant";
					$output['dtct'] = "Menu Record Updated";
					echo json_encode($output);
					exit;
				}
				else
				{
					$S = count($_POST['subjec']); $i = 0;
					do{
						$sub_nm = test_input($_POST['subjec'][$i]);
						if(!empty($sub_nm)){
							$nmnm = php_url_slug($sub_nm);
							$ins_subs = $db_con->prepare("INSERT INTO ktmmenu_name (mname,murl) VALUES(:subname,:suburl)"); 
							$ins_subs->bindParam(':subname',$sub_nm); $ins_subs->bindParam(':suburl',$nmnm);  $ins_subs->execute();
						} $i++;
					}
					while($i < $S);
					$output['runstt'] = "grant";
					$output['dtct'] = "Menu Name Added";
					echo json_encode($output);
					exit;
				}
			}
		}
	}
}
?>