<?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_bprice 		= $conn->real_escape_string($_POST['pm_bprice']);

$pm_desc	 	= $conn->real_escape_string($_POST['pm_desc']);
$pm_disc	 	= $conn->real_escape_string($_POST['pm_disc']);
$pm_ums		 	= $conn->real_escape_string($_POST['pm_ums']);
$pm_umb		 	= $conn->real_escape_string($_POST['pm_umb']);
$pm_ums_convert		 	= $conn->real_escape_string($_POST['pm_ums_convert']);
$pm_brand 		= $conn->real_escape_string($_POST['pm_brand']);


$pm_disc 		= $_POST['pm_disc'];
$pm_disc_type 	= $_POST['pm_disc_type'];
$pm_disc_status = $_POST['pm_disc_status'];
$pm_min_stok 	= $_POST['pm_min_stok'];


$autoid 		= $_POST['autoid'];
$act 			= $_POST['act'];
$pc_autoid 		= $_POST['pc_autoid'];



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) 
			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')";
			$conn->query($sql) or die($sql);
	
	
include('save_upload.php');

$chk = 1;

header('Location:index');

} // 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'
				$size_query
				$image_filter
				WHERE pm_autoid = '$autoid'
				";
			$conn->query($sql) or die($sql);


	$conn->query("delete from dl_product_category where product_id = '$autoid'");
	
	//$sqld = "insert into dl_product_category (product_id, pc_autoid,sc_autoid) values ('$autoid','$pc_id','$sc_autoid')";
	//$conn->query($sqld) or die($sqld);		
	
	include('save_upload.php');
	header('Location:edit.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');
	}

?>