<?php

session_start();

if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");

$mainaction 	 = $_POST['mainaction'];  if($mainaction =='') $mainaction = $_GET['mainaction'];

$attribute_code  = $_POST['attribute_code']; if($attribute_code == '') $attribute_code = $_GET['attribute_code']; 
$attribute_value = $_POST['attribute_value']; 
$item_code 		 = $_POST['item_code']; 
$ItemDt_ID 		 = $_POST['ItemDt_ID']; if($attribute_code == '')$ItemDt_ID = $_GET['ItemDt_ID']; 
$sorderdt_id 	 = $_POST['sorderdt_id'];
$vdetaildesc	 = $_POST['vdetaildesc'];
$vSoRemark	 	 = $_POST['vSoRemark'];

$mode = $_GET['mode'];

if($mode == 'deletepic'){
	
	$picnbr = $_GET['picnbr'];
	
	if($picnbr == 1){
		$delpic = "update temp_items set item_picture = null where item_code = '$item_code' and trans_id = '$sorderdt_id'";
	} else if($picnbr == 2){
		$delpic = "update temp_items set item_picture2 = null where item_code = '$item_code' and trans_id = '$sorderdt_id'";
	} 
	
	$res = mysql_query($delpic, $connection);
	
}
if($mode == 'SaveAll' && $mainaction == ''){
	$query = "update temp_sorderdt set sorderdt_desc1 = ".strformat($vdetaildesc).", SorderDt_Remark = ".(strformat($vSoRemark))." where sorderdt_id=$sorderdt_id;";//echo$query;exit;
	$res = mysql_query($query,$connection);
	
	$querychk = "select 1 from temp_items where trans_id=$sorderdt_id and item_code='$item_code';";
	$res = mysql_query($querychk,$connection);
	$numrw = mysql_num_rows($res);
	if(is_uploaded_file($_FILES['image']['tmp_name'])) {
	 
		$fileType1 = $_FILES['image']['type'];$fileName1 = $_FILES['image']['name'];
		$tmpName1  = $_FILES['image']['tmp_name'];$fileSize1 = $_FILES['image']['size'];

		$dest_dir='./uploads/items_photos';
		$foldername = $dest_dir;

		if (!is_dir($foldername)){
			mkdir($foldername, 0700, true);}
						
		$dest1 = $foldername.'\\'.$fileName1;
		//echo "dest1".$dest1;
		$fileName1 = 'items_photos/'.$fileName1;
		

		$fp1 = fopen($tmpName1, 'r');
		$content1 = fread($fp1, $fileSize1);
		//$content1 = addslashes($content1);
		fclose($fp1);
		if(!get_magic_quotes_gpc())
			$fileName1 = addslashes($fileName1);
		$picture=$fileName1;
		$r1 = move_uploaded_file ($tmpName1, $dest1);
		if($picture != '' ){
			if($numrw > 0)
			$querypic =  "update temp_items set item_picture='$picture' where item_code='$item_code' and trans_id=$sorderdt_id;";
			else 
			$querypic =  "insert into temp_items (item_code, item_picture, trans_id) values ('$item_code','$picture',$sorderdt_id);";	
			//echo $updatestaff;exit;
			$queryres=mysql_query($querypic,$connection);
		}
	}
	
	$res = mysql_query($querychk,$connection);
	$numrw = mysql_num_rows($res);
	if(is_uploaded_file($_FILES['image2']['tmp_name'])) {
	 
		$fileType1 = $_FILES['image2']['type'];$fileName1 = $_FILES['image2']['name'];
		$tmpName1  = $_FILES['image2']['tmp_name'];$fileSize1 = $_FILES['image2']['size'];

		$dest_dir='./uploads/items_photos';
		$foldername = $dest_dir;

		if (!is_dir($foldername)){
			mkdir($foldername, 0700, true);}
						
		$dest1 = $foldername.'\\'.$fileName1;
		//echo "dest1".$fileName1;exit;
		$fileName1 = 'items_photos/'.$fileName1;
		

		$fp1 = fopen($tmpName1, 'r');
		$content1 = fread($fp1, $fileSize1);
		//$content1 = addslashes($content1);
		fclose($fp1);
		if(!get_magic_quotes_gpc())
			$fileName1 = addslashes($fileName1);
		$picture2=$fileName1;
		$r1 = move_uploaded_file ($tmpName1, $dest1);
		if($picture2 != '' ){
			if($numrw > 0)
			$querypic =  "update temp_items set item_picture2='$picture2' where item_code='$item_code' and trans_id=$sorderdt_id;";
			else 
			$querypic =  "insert into temp_items (item_code, item_picture2, trans_id) values ('$item_code2','$picture',$sorderdt_id);";	
			//echo $updatestaff;exit;
			$queryres=mysql_query($querypic,$connection);
		}
	}
}
if($mainaction=='insert'){
	$query="insert into temp_itemsdt(Item_Code,ItemParam_Code,itemdt_value, transid)
				values ('".$item_code."','".$attribute_code."','".$attribute_value."', '$sorderdt_id')";
	$result=mysql_query($query,$connection);
}

if($mainaction=='edit'){
	$query="update temp_itemsdt set 
				ItemParam_Code='".$attribute_code."',
				itemdt_value='".$attribute_value."'
			where ItemDt_ID='".$ItemDt_ID."'";//echo$query;
	$result=mysql_query($query,$connection);		
}

if($mainaction=='delete'){
	$query_delete="delete from temp_itemsdt where ItemDt_ID='".$ItemDt_ID."'";
	$result_delete=mysql_query($query_delete,$connection);
}

echo "<form name='attributeaction' method='post'>".
	 "<input type='hidden' name='message' id='message' value='$message'>".
	 "<input type='hidden' name='mainaction' id='mainaction' value=''>".
	 "<input type='hidden' name='ItemDt_ID' id='ItemDt_ID' value=''>".
	 "<input type='hidden' name='item_code' id='item_code' value='$item_code'>".
	 "<input type='hidden' name='sorderdt_id' id='sorderdt_id' value='$sorderdt_id'>".
	 "<script> document.attributeaction.action='Form_add_itemAttr.php?item_code=$item_code&sorderdt_id=$sorderdt_id';".
	 " document.attributeaction.submit(); </script>";
	 exit;
function strformat($str){
	if($str == '') $str = "NULL";
	else $str = "'".ereg_replace("'", "\'", trim($str))."'";
	return $str;
}
	
mysql_close($connection);

?>