<?php
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$rurl="&modid=".$modid."&frmid=".$frmid;
if(isset($_POST['Submit']))
{
 $insert="insert into break_down_master set brack_down_type='".$_REQUEST['shift_category']."',branch_code='".$_REQUEST['site']."',brack_down_id='".$_REQUEST['break_down_type']."',CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=Db_query($insert,$db);
}
if(isset($_POST['Update']))
{
$update=DB_query("update break_down_master set brack_down_type='".$_REQUEST['shift_category']."'
,branch_code='".$_REQUEST['site']."',brack_down_id='".$_REQUEST['break_down_type']."' where id='".$_REQUEST['Id']."'",$db);	
$s->pageLocation("index.php?route=Break_down_reason&daction=Updated".$rurl);	
}
if($_REQUEST['Delete'])
{
	for($i=0;$i<count($_REQUEST['leadid']);$i++){
	$del=DB_query("delete from break_down_master where id='".$_REQUEST['leadid'][$i]."'",$db);	
	}
$s->pageLocation("index.php?route=Break_down_reason&daction=Updated".$rurl);	
}
?>
<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<?php if($_REQUEST['action']==''){ ?>
<h1>Break Down Reason</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
 <td class="tabledata1">Branch</td>	
	  <td>
	  
	   <select name="site" style="width:150px;" id="site">
	  	
	  <?php 
            $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
                        while($site_row=DB_fetch_array($site_sql)){
                    ?>
                      <option value="<?php echo $site_row['loccode'];?>"><?php echo $site_row['locationname'];?></option>
                      <?php } ?>		
		</select></td>
		   <td class="tabledata1">Break Down Type</td>	
	  <td> <select name="break_down_type" style="width:150px;" id="site" >
	  	<option value="000">--Select---</option>
	  <?php 
            $site_sql1=DB_query("Select id,breakdown_reason from break_down_type",$db);
                        while($site_row1=DB_fetch_array($site_sql1)){
                    ?>
                      <option value="<?php echo $site_row1['id'];?>"><?php echo $site_row1['breakdown_reason'];?></option>
                      <?php } ?>		
		</option></select></td>

</tr>
<tr class="row1">
	<td class="tabledata1">Break Down Reason</td>
	<td colspan="3"><input name="shift_category" id="shift_category" style="width:150px;" type="text"></td>
</tr>  
<tr class="row1">
	<td colspan="4">
		<p align="center"><input type="submit" value="Save" name="Submit" onclick="return valid();" class="search"></p>
	</td>
</tr>

</table>
</form>
<?php } if($_REQUEST['action']=='edit' and $_REQUEST['Id']!=''){
	$sql=DB_query("select * from break_down_master where id='".$_REQUEST['Id']."'",$db);
	$edit_row=DB_fetch_array($sql);
	
	?>
<h1>Edit Break Down Reason</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
  <td class="tabledata1">Branch</td>	
	  <td><select name="site" style="width:150px;">
<?php $site_sql=DB_query("Select loccode, locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db);
            while($site_row=DB_fetch_array($site_sql)){
            $k='';
			if($site_row['loccode']==$edit_row['branch_code']){
			$k="selected";	} ?>
         <option value="<?php echo $site_row['loccode'];?>" <?php echo $k; ?> ><?php echo $site_row['locationname'];?></option>
                      <?php } ?>		
		</select></td>
		<td class="tabledata1">Break Down Type</td>
	  <td> <select name="break_down_type" style="width:150px;" id="site" >
	  	<option value="000">--Select---</option>
	  <?php $site_sql=DB_query("Select id,breakdown_reason from break_down_type ",$db);
            while($site_row=DB_fetch_array($site_sql)){
			$j='';
            if($site_row['id']==$edit_row['brack_down_id'])
			$j="selected";
			 ?>
       <option value="<?php echo $site_row['id'];?>"<?php echo $j; ?>><?php echo $site_row['breakdown_reason'];?></option>
       <?php } ?>		
		
	</select></td>
</tr>
<tr class="row1">
	<td class="tabledata1">Break Down Reason</td>
	<td colspan="3"><input name="shift_category" id="shift_category" style="width:150px;" type="text" value="<?php echo $edit_row['brack_down_type']; ?>">
	</td>
</tr>  
<tr class="row1">
	<td colspan="4">
		<p align="center"><input type="submit" value="Update" name="Update" class="search"></p>
	</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="1" cellpadding="3" cellspacing="0">
<tr>
<td class="tabledata"><input name="leadid" type="checkbox"   id="leadid" onclick="LeadData()"    /></td>
<td class="tabledata">Branch</td>
<td class="tabledata">Break Down Type  </td>
<td class="tabledata">Break Down Reason  </td>
</tr>
<?php $i=0;
$sql="select * from break_down_master where CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr class="row1">
<td style="white-space:nowrap;"><input name="leadid[]" type="checkbox" id="leadid_<?php echo $i+1; ?>"   value="<?php  echo $row['id']?>" />
	
	<a href="index.php?route=Break_down_reason&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=edit&Id=<?php echo $row['id'];?>"><img src="images/icon_edit.gif" width="12px"  height="12" /></a>
	
	<!--<a href="index.php?route=view_machinecente&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=view&Id=<?php echo $row1['id'];?>"><img src="images/search.gif" width="12px"  height="12" /></a>-->
	</td>	
<td><?php	echo $location=$s->getField('locations','loccode',$row['branch_code'],'locationname');	 ?></td>
<td><?php echo $btype=$s->getField('break_down_type','id',$row['brack_down_id'],'breakdown_reason');	 ?></td>
<td><?php echo $row['brack_down_type']; ?></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>