<?php
include_once "../../includes/connectConfig.php";
if($_POST)
{
	$crtss = (!isset($_SESSION['prdss'])) ? $_SESSION['prdss'] = gntRdmStgs() : $_SESSION['prdss'] = $_SESSION['prdss'];
	
	$pstrec = (isset($_POST['pstrec']) && !empty($_POST['pstrec'])) ? test_input($_POST["pstrec"]) : '';
	$post_title = test_input($_POST["jewtitle"]);
	$j_brf = $_POST["jewbrief"];
	
	$j_emcn = test_input($_POST["jewkmn"]);
	$j_cnt = (isset($_POST['jewcnm']) && !empty($_POST['jewcnm'])) ? test_input($_POST["jewcnm"]) : NULL;
	$j_sct = (isset($_POST['jewsbt']) && !empty($_POST['jewsbt'])) ? test_input($_POST["jewsbt"]) : NULL;
	
	$je_wnote = (isset($_POST['jewnote']) && !empty($_POST['jewnote'])) ? test_input($_POST["jewnote"]) : NULL;
	
	$j_qty = change_currency(test_input($_POST["jewqty"]));
	$j_cst = change_currency(test_input($_POST["jewcost"]));
	$j_sl = change_currency(test_input($_POST["jewsell"]));
	$j_ds = change_currency(test_input($_POST["jecdiscount"]));
		
	$p_url = php_url_slug($post_title.'-'.date('jmY'));
	
	$dfv = gntRdmSdk();
	
	if($_POST['post_info'] == "add-product"){
		
		$prd = $db_con->prepare("INSERT INTO kmartstockprds(kmsku, pname, pbreif, pdesc, mn_id, mc_id, ms_id, pqty, pcost, psell, pdiscount, pqrcode, purl)
													VALUES('$dfv', :pname, :pbrf,  :p_des, :p_mt, :p_ct, :p_st, :pqty, :p_cost, :p_prc, :p_dis, :prec, :p_url)");
		$prd->bindParam(':pname',$post_title);
		$prd->bindParam(':pbrf',$je_wnote);
		$prd->bindParam(':p_des',$j_brf);
		
		$prd->bindParam(':p_mt',$j_emcn);
		$prd->bindParam(':p_ct',$j_cnt);
		$prd->bindParam(':p_st',$j_sct);
		
		$prd->bindParam(':pqty',$j_qty);
		$prd->bindParam(':p_cost',$j_cst);
		$prd->bindParam(':p_prc',$j_sl);
		$prd->bindParam(':p_dis',$j_ds);
		
		$prd->bindParam(':prec',$crtss);
		$prd->bindParam(':p_url',$p_url);
		$prd->execute();
		
		$last_id = $db_con->lastInsertId();
		
		if(isset($_POST['dmdname']))
		{
			$scls = $_POST['dmdname'];
			$sc = count($scls);
			$c = 0;
			do{
				$clsnm = test_input($scls[$c]);
				$clsinf = test_input($_POST['atdname'][$c]);
				if(!empty($clsnm)){
					$ins_subcls = $db_con->prepare("INSERT INTO kprdadditional(ksp_id, ka_head, ka_info) VALUES(:subnm, :clsnm, :clsinf)"); 
					$ins_subcls->bindParam(':subnm',$last_id);
					$ins_subcls->bindParam(':clsnm',$clsnm);
					$ins_subcls->bindParam(':clsinf',$clsinf);
					$ins_subcls->execute();
				} $c++;
			}
			while($c < $sc);
		}
		
		$gt_imgs = gtAll("kprdw_imgs"," prd_rec = '$crtss' ","kmg_id ASC");
		if(!empty($gt_imgs))
		{
			foreach($gt_imgs as $ac)
			{
				if(!empty($ac['pjm_pic']) && file_exists("../sample-imgs/".$ac['pjm_pic']))
				{
					rename("../sample-imgs/".$ac['pjm_pic'], "../../prdimgs/".$ac['pjm_pic']);
				}
			}
		}
		
		unset($_SESSION['prdss']);
		$output["runstt"] = "grant";
		$output["errorreason"] = "Product Saved Successfully";
		echo json_encode($output);
		exit;
		
	}
	elseif($_POST['post_info'] == "edit-product" && !empty($pstrec)){
		$chkjw = getOne("kmartstockprds"," ksp_id  = '$pstrec' AND delete_status = 0 ","pqrcode");
		if(!empty($chkjw)){
			$prd = $db_con->prepare("UPDATE kmartstockprds SET pname=:pname, pbreif=:pbrf, pdesc=:p_des, mn_id=:p_mt, mc_id=:p_ct, ms_id=:p_st, 
											pqty=:pqty, pcost=:p_cost, psell=:p_prc, pdiscount=:p_dis, purl=:p_url WHERE ksp_id  = '$pstrec' ");
			$prd->bindParam(':pname',$post_title);
			$prd->bindParam(':pbrf',$je_wnote);
			$prd->bindParam(':p_des',$j_brf);
			
			$prd->bindParam(':p_mt',$j_emcn);
			$prd->bindParam(':p_ct',$j_cnt);
			$prd->bindParam(':p_st',$j_sct);
			
			$prd->bindParam(':pqty',$j_qty);
			$prd->bindParam(':p_cost',$j_cst);
			$prd->bindParam(':p_prc',$j_sl);
			$prd->bindParam(':p_dis',$j_ds);
			$prd->bindParam(':p_url',$p_url);
			$prd->execute();
			
			// UPDATE OLD ADDITIONAL RECORDS
			if(isset($_POST['dmedame']))
			{
			$scls = $_POST['dmedame'];
			$sc = count($scls);
			$c = 0;
			do{
				$clsnm = test_input($scls[$c]);
				$hdnm = test_input($_POST['dmhdn'][$c]);
				$infnm = test_input($_POST['dminfn'][$c]);
				
				$chadj = gtOne("kprdadditional"," ksp_id  = '$pstrec' AND ka_id = '$clsnm' ","ka_id");
				if(!empty($chadj))
				{
					$ins_subcls = $db_con->prepare("UPDATE kprdadditional SET ka_head = '$hdnm', ka_info = '$infnm' WHERE ka_id='$chadj'"); $ins_subcls->execute();
				}
				$c++;
			}
			while($c < $sc);
			}
			//IF THERE'S NEW , ADD
			if(isset($_POST['dmdname']))
			{
				$scls = $_POST['dmdname'];
				$sc = count($scls);
				$c = 0;
				do{
					$clsnm = test_input($scls[$c]);
					$clsinf = test_input($_POST['atdname'][$c]);
					if(!empty($clsnm)){
						$ins_subcls = $db_con->prepare("INSERT INTO kprdadditional(ksp_id, ka_head, ka_info) VALUES(:subnm, :clsnm, :clsinf)"); 
						$ins_subcls->bindParam(':subnm',$pstrec);
						$ins_subcls->bindParam(':clsnm',$clsnm);
						$ins_subcls->bindParam(':clsinf',$clsinf);
						$ins_subcls->execute();
					} $c++;
				}
				while($c < $sc);
			}
			
			$gt_imgs = gtAll("kprdw_imgs"," prd_rec = '$crtss' ","kmg_id ASC");
			if(!empty($gt_imgs))
			{
				foreach($gt_imgs as $ac)
				{
					if(!empty($ac['pjm_pic']) && file_exists("../sample-imgs/".$ac['pjm_pic']))
					{
						$img_id = $ac['kmg_id'];
						rename("../sample-imgs/".$ac['pjm_pic'], "../../prdimgs/".$ac['pjm_pic']);
						$ins_img = $db_con->prepare("UPDATE kprdw_imgs SET prd_rec = '$chkjw' WHERE kmg_id = '$img_id' "); $ins_img->execute();
					}
				}
			}
			
			
			unset($_SESSION['prdss']);
			$output["runstt"] = "grant";
			$output["errorreason"] = "Product Record Updated";
			echo json_encode($output);
			exit;
		}
		else
		{
			$output["runstt"] = "warant";
			$output["errorreason"] = "Invalid Record";
			echo json_encode($output);
			exit;
		}
	}
	else
	{
		$output["runstt"] = "warant";
		$output["errorreason"] = "";
		echo json_encode($output);
		exit;
	}
	
}

?>