<?
include("opendb.php");
if($act=='del')
{
 mysqli_query($db,"delete from labitem where itemcode='$code'");
}
if($act=='delgrp')
{
 $re=mysqli_query($db,"select * from labitem where grpcode='$code'");
 if(mysqli_num_rows($re)>0)
  {
    print "<script>alert('There are items under this group'); </script>";
  }
  else
   mysqli_query($db,"delete from labgrp where gcode='$code'");
}


?>
<script>
function valid()
{
var grp=document.getElementById("ipgrp").value;
var amt=document.getElementById("amt").value;
var item=document.getElementById("item").value;

sok="true";

if(grp.length<=0)
 {
 alert("Group cannot be empty");
 sok="false";
 }
if(amt.length<=0)
 {
 alert("Amount cannot be empty");
 sok="false";
 }
if(item.length<=0)
 {
 alert("Item cannot be empty");
 document.f2.item.focus();
 sok="false";
 }


if(sok=="false")
 return false;
else 
 return true;
}
</script>

<link rel="stylesheet" href="style.css" type="text/css" />

<center>
<form name=f1 action='svlabgroup.php' method='post'>

<table cellspacing=2 cellpadding=2 bgcolor='gray'>
<tr><td align=center bgcolor=D1D0AF><center><font face='verdana' size=3><b>LAB MASTER</b></font>
</td></tr>
<tr><td align=center bgcolor=#EBF0DB>
<table border=1>
<tr><td>New Group </td><td><input type=text name='grp' size=30></td>
<td><input type=submit value='Add'></td></tr>
</table>
</form>

<form name=f2 action='svlabitem.php' method='post' onSubmit="return valid()">
</td></tr>
<tr><td bgcolor=#EBF0DB>
<table border=1>
<tr><td>New Item </td><td><input type=text name='item' size=20></td>
<td>Under Group</td><td><select name="labgrp"><option></option>
<?
$result=mysqli_query($db,"select * from labgrp order by gname");
while($row=mysqli_fetch_array($result))
{
 print "<option value='$row[gcode]'>$row[gname]</option>";
}
?>
</select> </td>
<tr>
<td>Charge (Rs.)</td><td><input type=text name='amt' size=5></td>
<td colspan=2 align=center><input type=submit value='   Add   '></td></tr>
</table>
</form>
</td></tr>
</table>
<hr>
<TABLE WIDTH=100% BORDER=1>
<tr><td ALIGN=CENTER BGCOLOR='D1D0AF' width=40%>EXISTING GROUPS</td>
<td ALIGN=CENTER BGCOLOR='D1D0AF'>EXISTING ITEMS</TD></tr>

<tr><td valign=top>
<table cellpadding=1 width=100%>
<?
$result=mysqli_query($db,"select * from labgrp order by gname")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<tr bgcolor=silver><td>$row[gname]</td><td align=center><font size=1>";
print "<a href='editlabgrp.php?code=$row[0]' style={color:brown}>Edit</a></td><td align=center><font size=1><a href=javascript:dele2('".$row[0]."','".str_replace(' ','&nbsp;',$row[1])."')>Del</a></td></tr>";
}
?>

</table>

<td valign=top>

<table cellpadding=1 width=100%>
<th bgcolor='silver'>Item<th bgcolor='silver'>Group
<th bgcolor='silver'>Amount
<th bgcolor='silver' colspan=2>Action
<?
$result=mysqli_query($db,"select a.grpcode,a.itemcode,a.itemname,a.amt,b.gname from labitem a,labgrp b where upper(a.grpcode)=upper(b.gcode)  ")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<tr bgcolor=silver><td>$row[itemname]</td>";
 print "<td>$row[gname]</td><td align=right>$row[amt]</td>";
 print "<td align=center><font size=1>";
print "<a href='editlabitem.php?code=$row[1]' style={color:brown}>Edit</a></td><td align=center><font size=1><a href=javascript:dele('".$row[1]."','".str_replace(' ','&nbsp;',$row[itemname])."')>Del</a></td></tr>";
}
?>
</table>

</td></tr>
</table>
<script>
function dele(x,y)
{
 ans=confirm('Delete item '+y+' ?');
 if(ans)
  document.location='<?print $PHP_SELF?>?code='+x+'&&act=del';
}
function dele2(x,y)
{
 ans=confirm('Delete Group '+y+' ?');
 if(ans)
  document.location='<?print $PHP_SELF?>?code='+x+'&&act=delgrp';
}

</script>

