<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
if(isset($_POST['Submit']))
{
$check=DB_query("select * from machine_group_master where machine_gp_location='".$_REQUEST['machine_location']."' ",$db);
$num=DB_num_rows($check);
if($num<=0)
{		
$insert ="insert into machine_group_master set branch_code='".$_REQUEST['site']."',machine_gp_location='".$_REQUEST['machine_location']."'";
$query=DB_query($insert,$db);
}
}
if(isset($_POST['Update']))
{
//$check=DB_query("select * from machine_group_master where machine_gp_location='".$_REQUEST['machine_location']."'",$db);
//$num=DB_num_rows($check);
//if($num<=0)
//{			
$update="update machine_group_master set branch_code='".$_REQUEST['site']."',machine_gp_location='".$_REQUEST['machine_location']."' where id='".$_REQUEST['Id']."'";
$query=DB_query($update,$db);	
//}	
$s->pageLocation("index.php?route=Machine_group_center&daction=Updated".$rurl);	
}
if($_REQUEST['action']=='delete')
{
$del="delete from machine_group_master where id='".$_REQUEST['Id']."'";
$query=DB_query($del,$db);
$s->pageLocation("index.php?route=Machine_group_center&daction=Updated".$rurl);	
}
?>
<?php if($_REQUEST['action']==''){?>
<h2 align="center"><strong>Machine Group Center</strong></h2>
<form action="" method="post">
<table width="40%" border="0" cellspacing="1" cellpadding="2" align="center">
<tr>
  <th><strong>BRANCH</strong></th>	
	  <td> <select name="site" style="width:135px"  id="site">
	  	<option value="0">-----Select-----</option>
	  <?php 
            $site_sql=DB_query("Select loccode,locationname from locations ",$db);
            while($site_row=DB_fetch_array($site_sql)){
            ?>
            <option value="<?php echo $site_row['loccode'];?>"><?php echo $site_row['locationname'];?></option>
            <?php } ?>		
		</option></select></td></tr>
      </tr>
      <tr>
     <th><strong>Machine Group Center Location</strong></th>	
	  <td> <input type="text" name="machine_location" style="width:135px" id="machine_location" >
	  	</td></tr>
      </tr>
      
      
</table>
<p align="center">
<input type="submit" value="Save" name="Submit" onclick="return valid();"></p>
</form>
<?php }
if($_REQUEST['action']=='edit' and $_REQUEST['Id']!='')
{
$sql="select * from machine_group_master where id='".$_REQUEST['Id']."'";
$query=DB_query($sql,$db);
$row1=Db_fetch_array($query,$db);
 ?>
 <h2 align="center"><strong>Edit Machine Group Center</strong></h2>
<form action="" method="post">
<table width="40%" border="0" cellspacing="1" cellpadding="2" align="center">
<tr>
  <th><strong>BRANCH</strong></th>	
	  <td> <select name="site" style="width:135px" >
	  	<option value="0">-----Select-----</option>
	  <?php 
            $site_sql=DB_query("Select loccode,locationname from locations ",$db);
            while($site_row=DB_fetch_array($site_sql)){
            	$k='';
				if($site_row['loccode']==$row1['branch_code'])
				{
				$k="selected";	
				}
				
            ?>
            <option value="<?php echo $site_row['loccode'];?>"<?php echo $k; ?>><?php echo $site_row['locationname'];?></option>
            <?php } ?>		
		</option></select></td></tr>
      </tr>
      <tr>
     <th><strong>Machine Group Center Location</strong></th>	
	  <td> <input type="text" name="machine_location" style="width:135px" value="<?php echo $row1['machine_gp_location']; ?>">
	  	</td></tr>
      </tr>
      
      
</table>
<p align="center">
<input type="submit" value="Update" name="Update" onclick="return valid();"></p>
</form><?php } ?>

<br>	
<br>
<table width="60%" border="0" cellspacing="2" cellpadding="2" align="center">
<tr>
<th><strong>BRANCH</strong></th>
<th><strong>Machine Group Center Location </strong></th>
<th colspan="2"><strong>ACTION</strong></th>
</tr>
<?php
$sql="select * from machine_group_master";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr bgcolor="#FFFFFF">
	
<td><?php $db1="select * from locations where loccode='".$row['branch_code']."'";
$query1=Db_query($db1,$db);
$row11=Db_fetch_array($query1); 
echo $row11['locationname']; ?></td>
<td><?php echo $row['machine_gp_location']; ?></td>

<td><a href="index.php?route=Machine_group_center&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&Id=<?php echo $row['id'];?>">Edit</a></td>
<td><a href="index.php?route=Machine_group_center&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=delete&Id=<?php echo $row['id'];?>">Delete</a></td>

</tr>
<?php } ?>
</table>
<?php 
include('includes/footer.inc');
?>

<script language="JavaScript">
function valid()
{
if(document.getElementById("site").value==0)
{
alert("Must Select Site Name");
document.getElementById("site").focus();
return false;	
}
if(document.getElementById("machine_location").value=='')
{
alert("Must Enter Machine Location");
document.getElementById("machine_location").focus();
return false;	
}
return true;	
}		
</script>
