<?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");

/// Color Grids    Base1 Base2     Decimal for Amounts

$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$sorderdt_id = $_GET['sorderdt_id'];
$item_code = $_GET['item_code'];

if($mainaction=='edit'){

	$query_edit=" select itemsdt.ItemParam_Code,itemdt_value,unit_code,ItemParam_Name ".
				" from temp_itemsdt as itemsdt ".
				" left join itemsparam on itemsparam.ItemParam_Code=itemsdt.ItemParam_Code ".
				" where ItemDt_ID = $ItemDt_ID;";
	$resultedit=mysql_query($query_edit,$connection) or die(mysql_error());
	$row_edit=mysql_fetch_row($resultedit);
	$attribute_code =$row_edit[0];
	$attribute_desc =$row_edit[3];
	$attribute_value =$row_edit[1];
	$attribute_unit =$row_edit[2];
}

$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

if($mainaction=='')
	$mainaction='insert';
	
print <<<HERE
<html>
<head>
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">
<SCRIPT type="text/javascript" src="shortcut.js"></script>
<script type="text/javascript" language="javascript">
function SaveAll(){
	x=document.attributes;
	x.mainaction.value='';
	x.action="Form_Add_itemAttr_Actions.php?mode=SaveAll";
	x.submit();
}
function ClosePage(){
	window.close();
}
function SaveChanges(){
	x=document.attributes;
		
	if(x.attribute_code.value==''){
		alert('Code is Mandatory');
		x.attribute_code.focus();
	}
	else{
		x.action="Form_Add_itemAttr_Actions.php";
		x.submit();
	}
}

function UpdateDetail(ItemDt_ID){
	x=document.attributes;
	x.ItemDt_ID.value=ItemDt_ID;
	x.mainaction.value='edit';
	//x.action="Form_add_itemAttr.php";
	x.submit();
}

function DeleteDetail(ItemDt_ID) {

	x=document.attributes;
	if (confirm ( "Are you sure you want to Delete this Record ?")) {
		x.mainaction.value='delete';
		x.action="Form_Add_itemAttr_Actions.php?ItemDt_ID="+ItemDt_ID;
		x.submit();
	}
}
function showlistitemparam(str){
	window.open('List_ItemParam.php?str=2','paramwindow','width=850,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}		

function deletePic(nbr){
	x=document.attributes;
	if (confirm ( "Are you sure you want to Delete this Picture ?")) {
		x.action="Form_Add_itemAttr_Actions.php?mainaction=&mode=deletepic&picnbr="+nbr;
		x.submit();
	}
}
</script>
<link rel="stylesheet" href="default.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}


</style>

<title>Items Attributes</title>
</head>
<body>
<form name='attributes' method='post' enctype='multipart/form-data'>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='ItemDt_ID' value='$ItemDt_ID'>
<input type='hidden' name='item_code' value='$item_code'>
<input type='hidden' name='sorderdt_id' value='$sorderdt_id'>
HERE;


echo "<table  border=0 ><col width='350'><col width='350'><col width='200px'><col width='200px'>";
echo "<tr><td colspan='4'><button type='button' id='btnClose' name='btnClose' onclick='ClosePage();return false;' title='Close Page'><span><span>Close</span></span></button>
	<button type='button' id='btnPost' name='btnPost' onclick='SaveAll();return false;' title='Save Changes'>
	<span><span>Save</span></span></button></td></tr>";	
echo '<tr><th height="20"  bgcolor="#E6E6E6" colspan="4" align="left" valign="top"><span class="style3">Parameters Attributes</span></th></tr>';
echo "<tr bgcolor='#F7F7F7'><td height='20' colspan='4' align='center'><font size='1' color='#FF0000' face='Verdana'><b>$message</b></font></td></tr>";

echo"<tr><td colspan=2 valign='top'>";
echo "<table  border=0>
		<col width=80><col width=80>
		<col width=300>
		<col width=120>
		<col width=120>";

echo"<tr ><td colspan='2'></td>
		<td align='center'><span class='style1'>Attribute Code/Description<span class='style26'>*</span></span></td>
		<td align='center'><span class='style1'>Value<span class='style26'>*</span></span></span></td>
		<td align='center'><span class='style1'>Unit<span class='style26'>*</span></span></td>
	</tr>";		

echo"<tr><td align=center colspan=2>
	<button type='button' id='submitrecord' name='submitrecord' onclick='SaveChanges(); return false;' title='Submit Record' ><span><span>Submit</span></span></button>";

echo"<button type='button' id='clearbutton' name='clearbutton' onclick='cleardetail(); return false;' title='Clear '><span><span>Clear </span></span></button>";
echo"</td>
	<td><input type='text' name='attribute_code' id='attribute_code' value='$attribute_code'  style='width:100px;height:20px;' maxlength=10>";

echo"&nbsp;<label><span class='style1'><img src='img/btnopen.jpg' title=' Select Parameter ' width='15' height='20' border='0' onMouseOver=\"style.cursor='hand'\" onClick='showlistitemparam(); '  /></span></label>";
			
echo"&nbsp;<input type='text' name='attribute_desc' id='attribute_desc' value='$attribute_desc'  readonly style='width:150px;height:20px;'>";						
echo"</td>
	<td><input type='text' name='attribute_value' id='attribute_value' value='$attribute_value' style='width:120px;height:18px;text-align:right;' maxlength='50'></td>
	<td><input type='text' name='attribute_unit' id='attribute_unit' value='$attribute_unit' maxlength='15' style='width:120px;height:18px;text-align:left;' readonly></td>
</tr>";	

$querydt = " select ItemDt_ID,itemsdt.ItemParam_Code,itemdt_value,unit_code,ItemParam_Name ".
			" from temp_itemsdt as itemsdt ".
			" left join itemsparam on itemsparam.ItemParam_Code=itemsdt.ItemParam_Code ".
			" where Item_Code='".$item_code."' and transid='$sorderdt_id'";
//echo $querydt;

$resultdt = mysql_query($querydt, $connection) or die(mysql_error());
while($rowsdt=mysql_fetch_row($resultdt)){
	
	$ct+=1;

	if($ct%2==0)
		$Color="style='background-color:".$grid_color1."'";
	else			
		$Color="style='background-color:".$grid_color2."'";	
						
				
	echo "<tr ".$Color." >";
	
	echo "	<td align='center' valign='center'><span class='style15'><img src='img/button_edit.png'  onMouseOver=\"style.cursor='hand'\" onclick=UpdateDetail('".$rowsdt[0]."') title='Edit Record'  ></span></td>";
	
	echo "	<td align='center' valign='center'><span class='style15'><img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteDetail('".$rowsdt[0]."') title='Delete Record'  ></span></td>";
	echo "	<td align='left'><span class='style15'>".$rowsdt[1].'   '.$rowsdt[4]."</span></td>";
	echo "	<td align='right'><span class='style15'>".$rowsdt[2]."</span></td>";
	echo "	<td align='left'><span class='style15'>".$rowsdt[3]."</span></td>";
	echo "</tr>";
						
}	

echo"</table>";
echo"</td>";


$querypic = "select item_picture, item_picture2 from temp_items where item_code = '$item_code' and trans_id = $sorderdt_id;";//echo $querypic;
$respic = mysql_query($querypic, $connection);
$rowpic = mysql_fetch_row($respic);
$item_picture = $rowpic[0];
$item_picture2 = $rowpic[1];
if($item_picture == ''){
	$querypic = "select item_picture from items where item_code = '$item_code';";
	$respic = mysql_query($querypic, $connection);
	$rowpic = mysql_fetch_row($respic);
	$item_picturetemp = $rowpic[0];
	
	$querychk = "select temp_id from temp_items where item_code = '$item_code' and trans_id = $sorderdt_id;";
	$reschk = mysql_query($querychk);
	$rwchk = mysql_fetch_row($reschk);
	$temp_id = $rwchk[0];
	if($temp_id == '' && $item_picturetemp != ''){
		$querypic =  "insert into temp_items (item_code, item_picture, trans_id) values ('$item_code','$item_picturetemp',$sorderdt_id);";	
		$queryres=mysql_query($querypic,$connection);
		$item_picture = $item_picturetemp;
	} /* else if($temp_id != ''){
		$querypic =  "update temp_items set item_picture='$picture' where item_code='$item_code' and trans_id=$sorderdt_id;";
	} */
	
}


echo "<td><table border='0' width='200px'>";
echo "<tr>
		<td  rowspan='4' valign='top' align='right'><div><div style='float:right;'>
		<img src='img/close-button.png' height='20px' width='20px' onclick=\"deletePic('1');\" onmouseover=\"style.cursor='hand'\"></div>
		<div style='float:right;'><img src='uploads/$item_picture' width='120px' height='100px'></div></div>";
echo" </td></tr></table><br><table border='0' width='200px'>";
echo "<tr><td  align='right' align=''><input type='file' name='image' style='width:200px;'></td></tr>";
echo"</table></td>";

echo "<td><table border='0' width='200px'>";
echo "<tr>
		<td  rowspan='4' align='right'><div><div style='float:right;'>
		<img src='img/close-button.png' height='20px' width='20px' onclick=\"deletePic('2');\" onmouseover=\"style.cursor='hand'\"></div>
		<div style='float:right;'><img src='uploads/$item_picture2' width='120px' height='100px'></div></div>";
echo" </td></tr></table><br><table border='0' width='200px'>";
echo "<tr><td  align='right' align=''><input type='file' name='image2' style='width:200px;'></td></tr>";
echo"</table></td>";
echo "</tr>";
$query = "select sorderdt_desc1, SorderDt_Remark from temp_sorderdt where sorderdt_id=$sorderdt_id;";
$res = mysql_query($query,$connection);
$row = mysql_fetch_row($res);
$vdetaildesc = $row[0];
$vSoRemark = $row[1];
if($vSoRemark == '')
	$vSoRemark = "PATTERN: \n".
				 "BEADING: \n".
				 "DRAWING: \n".
				 "OTHERS: \n";
echo"<tr height='30px'><td colspan=2 valign='top'></td></tr>";

echo"</table>";
echo "<table  width='850' border='0' align='center'>";
echo "<tr><td><span class='style1'>Description </span></td></tr><tr><td><span class='style1'> </span></td></tr>
      <tr><td bgcolor='#DDDDDD' > <textarea name='vdetaildesc' id='vdetaildesc' rows='5' cols='95'>".htmlspecialchars($vdetaildesc,ENT_QUOTES)."</textarea></td>
	  </tr>";
echo "<tr height='30px'><td></td></tr>";
echo "<tr><td><span class='style1'>Remark </span></td></tr><tr><td><span class='style1'> </span></td></tr>
      <tr><td bgcolor='#DDDDDD' > <textarea name='vSoRemark' id='vSoRemark' rows='5' cols='95'>";
	  echo $vSoRemark;
echo	  "</textarea></td>
	  </tr>";	  
echo "</table>";
echo"</form>";
echo"</body></html>";
?>
