<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
if(isset($_POST['Submit']))
{
$check=DB_query("select * from production_shift where shift_name='".$_REQUEST['shift_category']."' and loccode='".$_REQUEST['site']."'",$db);
$num=DB_num_rows($check);
if($num<=0)
{	
$insert="insert into production_shift set shift_name='".$_REQUEST['shift_category']."',loccode='".$_REQUEST['site']."' ";
$query=Db_query($insert,$db);
}	
}
if(isset($_POST['Update']))
{
$check=DB_query("select * from production_shift where shift_name='".$_REQUEST['shift_category']."' and loccode='".$_REQUEST['site']."'",$db);
$num=DB_num_rows($check);
if($num<=0)
{		
$update="update production_shift set shift_name='".$_REQUEST['shift_category']."',loccode='".$_REQUEST['site']."' where id='".$_REQUEST['Id']."'";
$query=DB_query($update,$db);	
}
$s->pageLocation("index.php?route=Production_Shift&daction=Updated".$rurl);	
}

if($_REQUEST['action']=='delete')
{
$del="delete from production_shift where id='".$_REQUEST['Id']."'";
$query=DB_query($del,$db);	
}
?>
<?php if($_REQUEST['action']=='')
{?>
<h2 align="center"><strong>Production Shift Master</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>SHIFT NAME </strong></th>
<td><input name="shift_category" id="shift_category" style="width:135px" type="text" id="shift_category">
</td>
</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 production_shift where id='".$_REQUEST['Id']."'";
$query=DB_query($sql,$db);
$row1=DB_fetch_array($query);	
?>
<h2 align="center"><strong>Edit Production Shift Master</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)){
                        $k='';
						if($site_row['loccode']==$row1['loccode'])
						{
						$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>SHIFT NAME </strong></th>
<td><input name="shift_category" id="shift_category" style="width:135px" type="text" id="shift_category" value="<?php echo $row1['shift_name']; ?>">
</td>
</tr>  
   
</table>
<p align="center">
<input type="submit" value="Update" name="Update" onclick="return valid();"></p>
<?php }?>
<br>	
<br>
<table width="60%" border="0" cellspacing="2" cellpadding="2" align="center">
<tr>
<th><strong>BRANCH</strong></th>
<th><strong>SHIFT Name </strong></th>
<th colspan="2"><strong>ACTION</strong></th>
</tr>
<?php
$sql="select * from production_shift";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr bgcolor="#FFFFFF">
	
<td><?php $db1="select * from locations where loccode='".$row['loccode']."'";
$query1=Db_query($db1,$db);
$row11=Db_fetch_array($query1); 
echo $row11['locationname']; ?></td>
<td><?php echo $row['shift_name']; ?></td>

<td><a href="index.php?route=Production_Shift&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=Production_Shift&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("shift_category").value=='')
{
alert("Must Enter Shift Name");
document.getElementById("shift_category").focus();
return false;	
}
return true;	
}	
	
	
	
</script>
