<?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_bprice,pm_ums,pm_umb,pc_autoid,active) 
			VALUES 
			(NULL,'$pm_name','$pm_bprice','Kg','Kg','$pc_autoid',1)";
			$conn->query($sql) or die($sql);
	
	


$chk = 1;

header('Location:index');

} // END ADD DATA //




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

	$sql = "UPDATE mst_product SET
				pm_name = '$pm_name',
				pm_bprice = '$pm_bprice',
				pc_autoid = '$pc_autoid'
				WHERE pm_autoid = '$autoid'
				";
			$conn->query($sql) or die($sql);



	

	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');
	}

?>