<link rel="stylesheet" href="css/new_mxerp.css">
<?php
if($_REQUEST['action']=='delete')
{
$del=DB_query("delete from tbl_sub_break_type where id='".$_REQUEST['Id']."'",$db);	
$s->pageLocation("index.php?route=ToolPlants/maintenance/sub_break_down&daction=Updated".$rurl);	
}
if(isset($_POST['Save']))
{
	 for($i=0;$i<count($_REQUEST['shift_category']);$i++)
	 {
		  if($_REQUEST['shift_category'][$i]!='')
		  {	
        $sql_check="select * from tbl_sub_break_type where breakdownid='".$_REQUEST['break_type']."' and asset_group='".$_REQUEST['under_group']."' and sub_breaktype='".$_REQUEST['shift_category'][$i]."'";
        $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)
        {
            $data['asset_group']=$_REQUEST['under_group'];
		        $data['breakdownid']=$_REQUEST['break_type'];
		        $data['sub_breaktype']=$_REQUEST['shift_category'][$i];
		        $data['CompanyId']=$_SESSION['ACGCompanyId'];
		        $s->RecordInsert('tbl_sub_break_type',$data); 
         }
       else
        {
            prnMsg( _('Data Already exists!'),'failure');
            exit;
        }   
		 }
	 }
}

?>

<div class="body_contain">
<div class="tab_div">
<h1>Sub Break Down</h1>
<form action="" method="post">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
 <tr class="row1">
    <td width="11%" style="text-align:right;">Asset Group</td>  
    <td width="20%"><select name="under_group"  id="under_group"  >
        <option value="0">Select</option> 
        <?php $sql="select * from machine_group_master";
    $query=DB_query($sql,$db);
    while($row1=DB_fetch_array($query))
    {
      $t1='';
      if($row1['id']==$under_group)
       $t1="selected";
      ?>
      <option value="<?php echo $row1['id']; ?>" <?php echo $t1; ?>><?php echo $row1['machine_gp_location']; ?></option>
    <?php } ?>          
        </select>
    </td>
    <td width="13%" style="text-align:right;">BreakDown Type</td>	
	  <td width="20%"><select name="break_type" id="break_type" >
	  <?php 
            $site_sql1=DB_query("Select id,breakdown_reason from break_down_type where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                        while($site_row1=DB_fetch_array($site_sql1)){
                        	$k='';
							if($site_row1['id']==$edit_row['break_type'])
							{
							$k="selected";	
							}
                    ?>
                      <option value="<?php echo $site_row1['id'];?>" <?php echo $k; ?> ><?php echo $site_row1['breakdown_reason'];?></option>
                      <?php } ?>		
		</option></select></td>
		<td width="15%" style="text-align:right;">Sub Break Down Required <font color="#FF0000">*</font></td>
    <td><input type="text" name="box_required" id="description"  value="" placeholder="No of Break Down Required" onkeyup="showtabledata(this.value)" autocomplete="off" onkeypress="return isNumberKey(event)" /></td>  
</tr>
</table> 
<div id="boxdata"></div>
</form>
<br>


<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
<th rowspan="2">Asset Group</th>  
<th rowspan="2" width="18%">Break Down Type</th>
<th rowspan="2" width="18%">Sub Break Down</th>
<th colspan="2" style="text-align:center;">Action</th>
</tr>
<tr>
  <th width="5%" style="text-align:center;">Edit</th>
  <th width="6%">Delete</th>
</tr>
</table>
<div class="overpan">
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
<tr>
  <td style="padding:0;" height="0"></td>
  <td width="18%"></td>
  <td width="18%"></td>
  <td width="5%"></td>
  <td width="5%"></td>
</tr>
<?php
$undergroup='<select name="under_group" >';
   $sql="select * from machine_group_master";
    $query=DB_query($sql,$db);
    while($row1=DB_fetch_array($query))
    {

$undergroup.='<option value='.$row1['id'].'>'.$row1['machine_gp_location'].'</option>';
     }
    $undergroup.='</select>';

    $breakdowntype='<select name="break_type" >';
  $site_sql1=DB_query("Select id,breakdown_reason from break_down_type where CompanyId='".$_SESSION['ACGCompanyId']."' ",$db);
                        while($site_row1=DB_fetch_array($site_sql1)){

$breakdowntype.='<option value='.$site_row1['id'].'>'.$site_row1['breakdown_reason'].'</option>';
     }
    $breakdowntype.='</select>';

$sql="select * from tbl_sub_break_type where CompanyId='".$_SESSION['ACGCompanyId']."'";
$query=DB_query($sql,$db);
while($row=DB_fetch_array($query))
{
	
?>
<tr>
  <td><?php echo $s->getField('machine_group_master','id',$row['asset_group'],'machine_gp_location')?></td> 
  <td><?php echo $break=$s->getField('break_down_type','id',$row['breakdownid'],'breakdown_reason'); ?></td>
  <td><?php echo $row['sub_breaktype']; ?></td>
  <td style="text-align:center;"><img src="images/icon-edit.png" width="15" height="15" title="Edit" onclick="createForEdit('<?php echo $row['emp_id']; ?>',this,'<?php echo $row['asset_group']; ?>','<?php echo $row['breakdownid'];?>');" style="cursor:pointer;"><span onclick="saveForEdit('<?php echo $row['id']; ?>',this);" style="color:green;display:none;cursor:pointer;">Save</span></td>
  <td style="text-align:center;"><a href="index.php?route=ToolPlants/maintenance/sub_break_down&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&action=delete&Id=<?php echo $row['id'];?>" onclick="return confirm('Are you sure you want to delete?')"><img src="images/Icon_Delete.png" width="15" height="15" title="Delete" /></a></td>
</tr>
<?php } ?>
</table>
</div>
</div></div>

<script>
 function showtabledata(i)
 {
if(document.getElementById("break_type").value==0)
 {
 alert("Must Select Break Down first");
 }
 else
  {
 if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{		
			document.getElementById("boxdata").innerHTML=xmlhttp.responseText;		
	}
	}
	xmlhttp.open("GET","ajax_sub_break_down_box.php?str="+i,true);
	xmlhttp.send();
  }
}
</script> 

<script type="text/javascript">
  function createForEdit(empid,obj,undergp12,breaddwon12)
  {//ajax_emp_loan_edit
  	//alert(breaddwon12)
  	 $(obj).hide();
  	 $(obj).next().show();
    	var subbreak=$(obj).closest('tr').find('td:eq(2)').html(); 
      $(obj).closest('tr').find('td:eq(0)').html('<?php echo $undergroup; ?>');
      $(obj).closest('tr').find('td:eq(0)').find('select').val(undergp12); 	 
  	  $(obj).closest('tr').find('td:eq(1)').html('<?php echo $breakdowntype; ?>');
  	  $(obj).closest('tr').find('td:eq(1)').find('select').val(breaddwon12);
  	  $(obj).closest('tr').find('td:eq(2)').html('<input type="text" value="'+subbreak+'" />');
  }

  function saveForEdit(empId,obj)
  {
     $(obj).hide();
  	 $(obj).prev().show();
       var undergp=$(obj).closest('tr').find('td:eq(0)').find('select').val(); 
  	   var breakgp=$(obj).closest('tr').find('td:eq(1)').find('select').val();   	
       var subbreak=$(obj).closest('tr').find('td:eq(2)').find('input[type="text"]').val();
      $(obj).closest('tr').find('td:eq(2)').html(subbreak);
   
   //alert(undergp);

     $.ajax({
            url : "ajax_break_down_edit.php",
            type : "POST",
            data : {             
            emp_id:empId,            
            breakgp:breakgp,
             undergp:undergp,
            subbreak:subbreak         
            },
            success : function(data)
             {
             	var data1=data.split("<>");
        //  alert(data1);
      $(obj).closest('tr').find('td:eq(0)').html(data1[0]);
      $(obj).closest('tr').find('td:eq(1)').html(data1[1]);     
            	//alert(data);              
            alert('You have update record successfully !');  
                          }
          });
  }

   function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : evt.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }

</script>