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

if(isset($_POST))
{		

	if(isset($_POST['addgenDiscount'])){
		
		$inp_am = test_input($_POST['inpam']);
		
		if(ctype_digit($inp_am)){
		
		
		$snd = $db_con->prepare("UPDATE pgs_settings SET st_value = '$inp_am' WHERE st_rec = 'gndiscount' "); $snd->execute();
	
		$output["runstt"] = "grant";
		$output["errorreason"] = "Data Saved";
			
		}else{
			$output["runstt"] = "warant";
			$output["errorreason"] = "Check Characters";
		}
	
		echo json_encode($output);
		exit;
		
		
	}else{

	$valid_extensions = array('png','jpg','jpeg');

	$namea = $_FILES['pics_a']['name'];
	$sizea = $_FILES['pics_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/";  
		
		$cf_idf = test_input($_POST["post_info"]);
		
		$pcg_rw = test_input($_POST["pcgrw"]);
		
		$chek_pd = getOne("kmt_featuresd"," id = '$cf_idf' ","rec_id");
		
		
		$j_sl = change_currency(test_input($_POST["jewsell"]));
		$j_ds = change_currency(test_input($_POST["jecdiscount"]));
		
		$imga = NULL;
		if(!empty($chek_pd)){
			if(!empty($namea)){
				$tmpa = $_FILES['pics_a']['tmp_name'];
				list($txta, $exta) = explode(".", $namea);
				$imga = strtolower(substr(md5(uniqid(rand())), 0, 7 ).'_'.rand(1000,100000).'.'.$exta);
				$filePatha = $folder.'/'.$imga;
				move_uploaded_file($tmpa, $filePatha);
				
				$lkj = $imga;
			}
			else{
				if($pcg_rw == 'maintain'){
					$chkj = getOne("kmt_ft_prds"," id = '$dvc' ",'prd_img');
					$lkj = (!empty($chkj)) ? $chkj : NULL;
				}elseif($pcg_rw == 'clear'){
					$lkj = NULL;
				}
			}
			
			$dvc = getOne("kmt_ft_prds"," kmtf_id = '$cf_idf' AND ksp_id = '$chek_pd' ",'id');
	
	
			if(!empty($dvc)){
				$snd = $db_con->prepare("UPDATE kmt_ft_prds SET prd_img=:imga, km_sell=:sellcst, km_discount=:dstcst, delete_status = '0' WHERE id = '$dvc' ");
				$snd->bindParam(':imga',$lkj); $snd->bindParam(':sellcst',$j_sl);$snd->bindParam(':dstcst',$j_ds); $snd->execute();
			}
			else{
				$snd = $db_con->prepare("INSERT INTO kmt_ft_prds(prd_img, kmtf_id, ksp_id, km_sell, km_discount) VALUES(:imga, :ftid, :kspid, :sellcst, :dstcst )");
				$snd->bindParam(':imga',$lkj); $snd->bindParam(':ftid',$cf_idf); $snd->bindParam(':kspid',$chek_pd);$snd->bindParam(':sellcst',$j_sl);$snd->bindParam(':dstcst',$j_ds); $snd->execute();
			}
			
			
			$output["runstt"] = "grant";
			$output["errorreason"] = "Data Saved";
		}
		else{
			$output["runstt"] = "warant";
			$output["errorreason"] = "Can't Find Record, Try Again.";
		}
		echo json_encode($output);
		exit;	
	}
	
	}
}	
?>
