<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
if(isset($_POST['Submit']))
{
	$sql_check="select * from prd_safty where site='".$_REQUEST['site']."'and description='".$_REQUEST['description']."' and seq='".$_REQUEST['sequence']."' ";
	$result_check=DB_query($sql_check,$db);
    $rs_check=DB_fetch_array($result_check);
    $num_row=DB_num_rows($result_check);

  if($num_row<1)
  {
    $insert="insert into prd_safty set description='".$_REQUEST['description']."',site='".$_REQUEST['site']."',seq='".$_REQUEST['sequence']."',CompanyId='".$_SESSION['ACGCompanyId']."',Created_by='".$_SESSION['UserID']."',Created_date='".date('y-m-d')."'";
   $query=Db_query($insert,$db);
  }
  else
  {
  	 prnMsg( _('Data Already exists!'),'failure');
     exit;
  }  
}
if(isset($_POST['Update']))
{
	$sql_check="select * from prd_safty where site='".$_REQUEST['site']."'and description='".$_REQUEST['description']."'and seq='".$_REQUEST['sequence']."' and id!='".$_REQUEST['Id']."' ";
	$result_check=DB_query($sql_check,$db);
    $rs_check=DB_fetch_array($result_check);
    $num_row=DB_num_rows($result_check);

    if($num_row<1)
    {
      $update=DB_query("update prd_safty set description='".$_REQUEST['description']."'
,site='".$_REQUEST['site']."',seq='".$_REQUEST['sequence']."',
 Modified_by='".$_SESSION['UserID']."',Modified_date='".date('y-m-d')."' where id='".$_REQUEST['Id']."'",$db);	
    }
    else
    {
    	prnMsg( _('Data Already exists!'),'failure');
        exit;
    }  
      $s->pageLocation("index.php?route=Production/Plant/break_down_maintanance_safty".$rurl);	
}
if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['leadid']);$i++){
	$del=DB_query("delete from prd_safty where id='".$_REQUEST['leadid'][$i]."'",$db);	
	}
$s->pageLocation("index.php?route=Production/Plant/break_down_maintanance_safty".$rurl);	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['action']==''){ ?>
<form action="" method="post">
<h1>Break Down Safty Master</h1>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
 	<td width="13%" align="right">Branch</td>	
	  <td width="20%">	  
	   <select name="site"  id="site">
	  	<option value="000">--Select---</option>
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' ",$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>
		   <td width="13%" align="right">Descriptions</td>	
	       <td width="20%"><input type="text" name="description"  id="description" autocomplete="off" ></td>
           <td width="13%" align="right">Sequence</td>
	       <td><input name="sequence" id="seq"  type="text"></td>
</tr>
<tr class="row1">
	<td colspan="6" align="center"><input type="submit" value="Save" name="Submit" onclick="return valid();" class="search"></td>
</tr>
</table>

</form>
<?php } if($_REQUEST['action']=='edit' and $_REQUEST['Id']!=''){
 	$sql=DB_query("select * from prd_safty where id='".$_REQUEST['Id']."'",$db);
	$edit_row=DB_fetch_array($sql);
	
	?>
<form action="" method="post">
<h1>Edit Break Down Reason</h1>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
        <td width="13%" align="right">Branch</td>	
	    <td width="20%"><select name="site" >
<?php $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
            while($site_row=DB_fetch_array($site_sql)){
            $k='';
			if($site_row['loccode']==$edit_row['site']){
			$k="selected";	} ?>
         <option value="<?php echo $site_row['loccode'];?>" <?php echo $k; ?> ><?php echo $site_row['locationname'];?></option>
                      <?php } ?>		
		</option></select></td>
		<td width="13%" align="right">Descriptions</td>
	    <td width="20%"><input type="text" name="description" value="<?php echo $edit_row['description']; ?>" > </td>
	    <td width="13%" align="right">Sequence</td>
	    <td><input type="text" name="sequence" value="<?php echo $edit_row['seq']; ?>"  /></td>
</tr>
<tr class="row1">
	<td colspan="6" align="center"><input type="submit" value="Update" name="Update" class="search"></td>
</tr>
</table>

</form>
	<?php  } ?>

<form name="myform" method="post">
<p align="left">
<input type="submit" name="Delete" value="Delete" class="search" onclick="return confirm('Are you sure you want to delete?')" />
</p>

<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
<th width="3%"><input name="leadid" type="checkbox" id="leadid" onclick="LeadData()" /></th>
<th>Branch</th>
<th width="40%">Description</th>
<th width="15%">Sequence</th>
<th width="5%" style="text-align:center;">Edit</th>
</tr>
<?php $i=0;
$sql="select * from prd_safty where CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr>
<td><input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>" value="<?php  echo $row['id']?>" /></td>	
<td><?php	echo $location=$s->getField('locations','loccode',$row['site'],'locationname');	 ?></td>
<td><?php echo $row['description'];	 ?></td>
<td><?php echo $row['seq']; ?></td>
<td style="text-align:center;"><a href="index.php?route=Production/Plant/break_down_maintanance_safty&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&Id=<?php echo $row['id'];?>"><img src="images/icon-edit.png" width="15" height="15" title="Edit" /></a></td>
</tr>
<?php $i++;} ?>
</table>

</form>
</div></div>

<?php 
//include('includes/footer.inc');
?>
<script language="JavaScript">
function valid()
{
	if(document.getElementById("site").value=='000')
	{
	alert("Must Select Branch Name");
	document.getElementById("site").focus();
	return false;	
	}
	if(document.getElementById("shift_category").value=='')
	{
	alert("Please Enter Break Down Reason");
	document.getElementById("shift_category").focus();
	return false;	
	}
	if(document.getElementById("shift_category").value=='')
	{
	alert("Please Enter Break Down Reason");
	document.getElementById("shift_category").focus();
	return false;	
	}
	return true;	
}

function LeadData()
{	
	myCheckboxArray = document.myform.elements["leadid[]"];
	var t=myCheckboxArray.length;
	//alert(myCheckboxArray);
	if(document.getElementById('leadid').checked==true)
	{	
	if(t==undefined)
	document.getElementById('leadid_'+1).checked=true;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=true;	
	}	
	}
	else
	{
	if(t==undefined)		
	document.getElementById('leadid_'+1).checked=false;		
	for (var i = 1; i <= t; i++)
	{
	document.getElementById('leadid_'+i).checked=false;	
	}
	}
}
	
</script>