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

$judul 		= $conn->real_escape_string($_POST['judul']);
$kode 		= $conn->real_escape_string($_POST['kode']);
$autoid 	= $_POST['autoid'];
$row 		= $_POST['row'];
$price = $_POST['price'];

$valid_formats 		= array("jpg", "png", "gif", "bmp","jpeg","pdf","xls","xlsx");
$imgfile 			= "img_file";
$name_img 			= $_FILES[$imgfile]['name'];
$size_img 			= $_FILES[$imgfile]['size'];
$act = $_POST['act'];

 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 
 if(strlen($name_img))

{
$file_ext = getExtension($name_img);
if(in_array(strtolower($file_ext),$valid_formats))
{
$path 				= "../../../image/product/";
$location 			= "../../image/product";
$actual_image_name 	= "";
$kodefile = "img_".$autoid;

		$actual_image_name = $kodefile.".".$file_ext;
		
		$img_url = "image/product/".$actual_image_name;
		
		$tmp = $_FILES[$imgfile]['tmp_name'];
		if(move_uploaded_file($tmp, $path.$actual_image_name)){
			
				$image_filter = ",image_url = '$img_url'";
			}
			else{
				echo "Gagal Upload";				
				}
}

else
echo "Invalid file format..";
}
if($act==1){
$sql = "INSERT INTO dl_product
		(product_id,product_code,product_name,status_paket,image_url,hrg_3) 
		VALUES 
		('$autoid','$kode','$judul',1,'$img_url','$price')";
$conn->query($sql) or die($sql);

	for($i=1;$i<$row;$i++){
		
		$pm_autoid = $_POST["pm_autoid".$i];
		$qty = $_POST["pm_qty".$i];
		
		$sqld = "INSERT INTO detail_paket (product_id,dp_qty,parent_id) VALUES ('$pm_autoid','$qty','$autoid')";
			if($qty>0){
				$conn->query($sqld) or die($sqld);
			}		
		}
		
header('Location:new?info=1');

}
if($act==2){
	$sql = "UPDATE dl_product SET
					product_code = '$kode',
					product_name = '$judul', 
					hrg_3 = '$price' 
				$image_filter
				 WHERE product_id = '$autoid'
				";
			$conn->query($sql) or die($sql);


	$conn->query("delete from detail_paket WHERE parent_id = '$autoid'");
	
	for($i=1;$i<$row;$i++){
		
		$pm_autoid = $_POST["pm_autoid".$i];
		$qty = $_POST["pm_qty".$i];
		
		$sqld = "INSERT INTO detail_paket (product_id,dp_qty,parent_id) VALUES ('$pm_autoid','$qty','$autoid')";
			if($qty>0){
				$conn->query($sqld) or die($sqld);
			}		
		}
	
	header('Location:edit.php?info=1&id='.$autoid);
}
if($act==3){
	$sql = "UPDATE dl_product SET active = 0 WHERE product_autoid = '$autoid'";
	$conn->query($sql) or die($sql);
		header('Location:index');
	}

?>