<?php
include('config.php');

$pm_name 		= $conn->real_escape_string($_POST['pm_name']);
$pm_barcode 	= $conn->real_escape_string($_POST['pm_barcode']);
$pm_sprice 		= $conn->real_escape_string($_POST['pm_sprice']);
$pm_sprice_app 	= $conn->real_escape_string($_POST['pm_sprice_app']);


$pm_desc	 	= $conn->real_escape_string($_POST['pm_desc']);

$item_1 		= $_POST['item_promo_1'];
$item_2 		= $_POST['item_promo_2'];


$item_param 	= $item_1.",".$item_2;
$autoid 		= $_POST['autoid'];
$act 			= $_POST['act'];
$pc_autoid 		= $_POST['pc_autoid'];
$status = $_POST['pm_status'];


if(!$pm_barcode){
	$pm_barcode = "00000";
	}

	$sqlsc = "select * from mst_partclass WHERE pc_autoid = '$pc_autoid' LIMIT 0,1";
	$sc_con = $conn->query($sqlsc) or die($sqlsc);
	$rst = $sc_con->fetch_assoc();
	$pt_autoid = $rst['pt_autoid'];
	



if($act=="add"){
$chk = 0;

if($chk==1){echo "DOUBLE";return false;}


//SIZE ARRAY//
$q_size = "";
if (isset($_POST['pm_size'])) 
{
	$pm_size = $_POST['pm_size'];
	if(!$pm_size){$size_val = 0;}
	foreach ($pm_size as $pm_size=>$size_val) {
             $q_size = $q_size.",".$size_val;
        }
}
$q_size = substr($q_size,1);



// MAIN QUERY //
	$sql = "INSERT INTO mst_product 
			(pm_autoid,pm_name,pm_code,pm_sprice,pm_bprice,pm_desc,active,pm_ums,
			pc_autoid,pt_autoid,pm_min_stok,pm_brand,pm_umb,pm_ums_convert,pm_barcode,pm_promo_param) 
			VALUES 
			(NULL,'$pm_name','$pm_code','$pm_sprice','$pm_bprice','$pm_desc',1,'$pm_ums',
			'$pc_autoid','$pt_autoid','$pm_min_stok','$pm_brand','$pm_umb','$pm_ums_convert','$pm_barcode','$item_param')";
			$conn->query($sql) or die($sql);
	
	
include('save_upload.php');

$chk = 1;

header('Location:index_promo');

} // END ADD DATA //




// EDIT DATA//
if($act=="edit"){

		
$q_size = "";
if (isset($_POST['pm_size'])) 
{
	$pm_size = $_POST['pm_size'];
	if(!$pm_size){$size_val = 0;}
	foreach ($pm_size as $pm_size=>$size_val) {
             $q_size = $q_size.",".$size_val;
        }
}
$q_size = substr($q_size,1);



if($q_size!=""){
	$size_query = ", pm_size = '$q_size'";
	}

	$sql = "UPDATE mst_product SET
				pm_name = '$pm_name',
				pm_bprice = '$pm_bprice',
				pm_sprice = '$pm_sprice',
				pm_desc = '$pm_desc',
				pm_ums = '$pm_ums',
				pt_autoid = '$pt_autoid',
				pc_autoid = '$pc_autoid',
				pm_min_stok = '$pm_min_stok',
				pm_brand = '$pm_brand',
				pm_umb = '$pm_umb',
				pm_ums_convert = '$pm_ums_convert',
				pm_barcode = '$pm_barcode',
				pm_sprice_app = '$pm_sprice_app',
				pm_promo_param = '$item_param',
				active = '$status'
				$size_query
				$image_filter
				WHERE pm_autoid = '$autoid'
				";
			$conn->query($sql) or die($sql);



	
	include('save_upload.php');
	header('Location:edit_promo.php?info=1&id='.$autoid);
}

if($act=="delete"){
	$sql = "DELETE FROM mst_product WHERE pm_autoid = '$autoid'";
	$conn->query($sql) or die($sql);
		header('Location:index_promo');
	}

?>