<?php
if(isset($_POST['Search']))
  {
     $data['site']          =$_REQUEST['site'];
     $data['modified_by']    =$_SESSION['UserID'];   
     $data['modified_date'] =date('Y-d-y');
     $cond="id='".$_REQUEST['id']."'";
     $s->RecordEdit('tbl_prementive', $data,$cond);  
     $last_id=$_REQUEST['id'];
    foreach($_REQUEST['breakdown'] as $key=>$value)
    {       
       $last_id1=$_REQUEST['prementive_dtls'][$key];
           foreach($_REQUEST['assetid'][$value] as $key1=>$val)
           {
                   if(!empty($_REQUEST['subbreak'][$value][$val]))
                   {
                    foreach($_REQUEST['subbreak'][$value][$val] as $key2=>$val2)
                    {
                      if($_REQUEST['service_day'][$value][$val][$key2]!='')
                      {
                           if($_REQUEST['prementive_dtls_id'][$value][$val][$key2]=='')
                           { 
                            $data3['tbl_prementive_details_id']=$last_id1;
                            $data3['asset_id']=$val;
                            $data3['tbl_prementive_id']=$last_id;
                            $data3['sub_break_down_id']=$val2;
                            $data3['service_days']=$_REQUEST['service_day'][$value][$val][$key2];
                            $data3['last_service_date']=FormatDateForSQL($_REQUEST['last_service_date'][$value][$val][$key2]);
                            $s->RecordInsert('tbl_prementive_sub_details', $data3);
                           }
                           else
                           {
                            $data4['tbl_prementive_details_id']=$last_id1;
                            $data4['asset_id']=$val;
                            $data4['sub_break_down_id']=$val2;
                            $data4['service_days']=$_REQUEST['service_day'][$value][$val][$key2];
                            $data4['last_service_date']=FormatDateForSQL($_REQUEST['last_service_date'][$value][$val][$key2]);
                            $cond1="id='".$_REQUEST['prementive_dtls_id'][$value][$val][$key2]."'";
                            $s->RecordEdit('tbl_prementive_sub_details', $data4,$cond1);
                           }
                      }
                    }
                  }
           }
    }    
  $s->pageLocation("index.php?route=ToolPlants/fixed_assets/sub_asset_master_index".$rurl);
}

 $sql_search=DB_query("select * from tbl_prementive where id='".$_REQUEST['Id']."'",$db);
 $row=DB_fetch_array($sql_search);
 extract($row);
 $warrenty=array("Y"=>"Yes","N"=>"No");
 $row1=DB_fetch_array(DB_query("select * from machine_center where id='".$id."'",$db));

 $prementive_service='';
 $prementive_last_service_date='';
 $prementive_details_id='';
 $prementive_details='';
 $sql_prementive=DB_query("select id,break_down_id from tbl_prementive_details where tbl_prementive_id='".$id."'",$db);
 while($r_prementive=DB_fetch_array($sql_prementive))
 {
  $prementive_details[$r_prementive['break_down_id']]=$r_prementive['id'];
  $sql_prementive1=DB_query("select * from tbl_prementive_sub_details where tbl_prementive_details_id='".$r_prementive['id']."'",$db);
  while($r1_prementive=DB_fetch_array($sql_prementive1))
    {
    $prementive_service[$r_prementive['break_down_id']][$r1_prementive['asset_id']][$r1_prementive['sub_break_down_id']]=$r1_prementive['service_days'];
    $prementive_last_service_date[$r_prementive['break_down_id']][$r1_prementive['asset_id']][$r1_prementive['sub_break_down_id']]=$r1_prementive['last_service_date'];
    $prementive_details_id[$r_prementive['break_down_id']][$r1_prementive['asset_id']][$r1_prementive['sub_break_down_id']]=$r1_prementive['id'];
    }
 }
?>
<div style="display:none" id="div_id"></div>
<link rel="stylesheet" href="css/new_mxerp.css">

<div class="body_contain">
<div class="tab_div">
<h1><?php echo $s->getField('modules_form','id',$_REQUEST['frmid'],'frm_name'); ?> </h1>
<form action="" method="Post">
<table width="100%" border="0" cellspacing="0" cellpadding="3">
  <tr class="row1">
    <td width="13%" align="right">Branch</td>
    <td width="20%"><select name="site" id="site"  >
                    <option value="0">Select</option>
                    <?php
                     $sql=DB_query("select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
                     while($r=DB_fetch_array($sql))
                     {
                        $t2='';
                      if($r['loccode']==$site)
                        $t2="selected";
                     ?>
                     <option value="<?php echo $r['loccode']; ?>" <?php echo $t2; ?>><?php echo $r['locationname']; ?></option>
                     <?php } ?>
                     </select>    
    </td>
    <td width="13%" align="right">Asset Group</td>
    <td width="20%"><select name="group"  onchange="show_asset(this.value)" >
                    <option value="0">Select</option> 
                    <?php
                     $sql_group=DB_query("select * from machine_group_master where id='".$asset_group."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
                     while($r_group=DB_fetch_array($sql_group))
                     {
                        $t='';
                      if($r_group['id']==$asset_group)
                        $t="selected";

                     ?>
                     <option value="<?php echo $r_group['id']; ?>" <?php echo $t; ?> ><?php echo $r_group['machine_gp_location']; ?></option>
                    <?php } ?>
                  </select>
    </td>
    <td width="13%" align="right">Asset Name</td>
    <td><select name="asset" id="asset"  onchange="show_asset_info(this.value)" >
       <option value="0">Select</option> 
       <?php
       $sql_asset=DB_query("select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."' and id='".$asset_id."' and asset_type in('A','NA')",$db);
       while($r_asset=DB_fetch_array($sql_asset))
       {
                       $t1='';
                      if($r_asset['id']==$asset_id)
                        $t1="selected";
       ?>
       <option value="<?php echo $r_asset['id']; ?>" <?php echo $t1; ?>><?php echo $r_asset['name']; ?></option>
       <?php } ?></select></td>
  </tr>
  <tr class="row1">
    <td align="right">Warranty</td>
    <td><select name="warranty" id="warranty" >
       <option value="0">Select</option>
        <option <?php if($warranty=="Y"){ ?> selected="selceted" <?php }?>>Yes</option>
        <option <?php if($warranty=="N"){ ?> selected="selceted" <?php }?>>No</option>
     <?php 
    // echo $data="<option value='".$row['warranty_type']."'>".$warrenty[$row1['warranty_type']]."</option>";
     ?>
    </select>
     </td>
    <td align="right">Status</td>
    <td><select name="Status" id="Status" >
          <?php //echo $data1="<option value='".$row['working']."'>".$warrenty[$row1['working']]."</option>"; ?>
          <option value="0">Select</option>
            <option <?php if($wo_status=="Y"){ ?> selected="selceted" <?php }?>>Yes</option>
            <option <?php if($wo_status=="N"){ ?> selected="selceted" <?php }?>>No</option>
    </select>
  </td>
    <td align="right">Code</td>
    <?php 
       $sql_asset_code=DB_query("select * from machine_center where CompanyId='".$_SESSION['ACGCompanyId']."' and id='".$asset_id."' and asset_type='NA'",$db);
       $r_asset_code=DB_fetch_array($sql_asset_code);
    ?> 
    <td><input type="text" name="code" id="code"  value="<?php echo $r_asset_code['code']; ?>" readonly /></td>
  </tr>
</table>
<br>
    <div id="datashow">    
    <?php 
    $breakdown='';$breakdown_details='';
$sql_breakdown=DB_query("select id from break_down_type where  CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
while($row_break=DB_fetch_array($sql_breakdown))
{
  $breakdown[]=$row_break['id'];

$sql_breakdown1=DB_query("select id from tbl_sub_break_type where asset_group='".$asset_group."' and breakdownid='".$row_break['id']."'",$db);
while($row_break1=DB_fetch_array($sql_breakdown1))
{
  $breakdown_details[$row_break['id']][]=$row_break1['id'];
}
}

$subasset='';
$subasset[]=$asset_id;
//echo "select machine_center_id from machine_center_details where asset_id='".$row1['id']."'";
$sql_asset=DB_query("select machine_center_id from machine_center_details where asset_id='".$asset_id."'",$db);
while($row_asset=DB_fetch_array($sql_asset))
{
  $subasset[]=$row_asset['machine_center_id'];
}
//print_r($subasset);

$data3.='';
$data3.='<table width="100%" border="0" cellspacing="0" cellpadding="3" class="thborder">
  <tr>
    <th>Break Down</th>
    <th width="400">Sub Break Down</th>
    <th width="150">Service Day</th>
    <th width="150">Last Service Date</th>
  </tr>';
      $i=1;
  foreach($breakdown as $key=>$val)
  {
  $prementive_id=$prementive_details[$val];
  $data3.='<tr>
    <td colspan="4">'.$s->GetFieldValue("break_down_type","id='".$val."'","breakdown_reason").'<input type="hidden" name="breakdown[]" value="'.$val.'">
    <input type="hidden" name="prementive_dtls[]"  value="'.$prementive_id.'" ></td>
   
  </tr>';
   
   foreach($subasset as $key1=>$val1)
   {   
    $data3.='<tr>
     <td>'.$s->GetFieldValue("machine_center","id='".$val1."'","name").'<input type="hidden" name="assetid['.$val.'][]" value="'.$val1.'"></td>
    <td colspan="3" style="padding:0px;"><div id="amc-tab"><table width="100%" border="0" cellspacing="0" cellpadding="3">';
  if(!empty($breakdown_details[$val]))
  {
  foreach($breakdown_details[$val] as $key2=>$val2)
   {
    $service=$prementive_service[$val][$val1][$val2];
    if($prementive_last_service_date[$val][$val1][$val2]!='')
    $lastdate=ConvertSQLDate($prementive_last_service_date[$val][$val1][$val2]);
    else
    $lastdate='';  
    $prementive_dtl_id=$prementive_details_id[$val][$val1][$val2];

   $data3.='<tr><td width="400">'.$s->GetFieldValue("tbl_sub_break_type","id='".$val2."'","sub_breaktype").'<input type="hidden" name="subbreak['.$val.']['.$val1.'][]" value="'.$val2.'"></td>
          <td width="150"><input type="text" name="service_day['.$val.']['.$val1.'][]"  value="'.$service.'"   ></td>
          <td width="150"><input type="text" id="lastdate_'.$i.'" class="datepicker" name="last_service_date['.$val.']['.$val1.'][]"  value="'.$lastdate.'" >
          <input type="hidden" name="prementive_dtls_id['.$val.']['.$val1.'][]"  value="'.$prementive_dtl_id.'" >
           </td>
   </tr>';
    $i++;
  }
}
    $data3.='</table></div></td>   
    </tr>';
    }
   } 
$data3.='</table>';
  
    $data3.='<p style="text-align:center;"><input type="submit" class="search" id="view" value="Update" name="Search"></p>';
echo $data3;
    ?>
</div>
</form>

</div></div>

<script>
 
 
 function show_asset(str)
 {


  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)
  {   
  // alert(xmlhttp.responseText);
  document.getElementById("asset").innerHTML=xmlhttp.responseText;
  }
  }
  xmlhttp.open("GET","ajax_dynamic_subasset_creation.php?str="+str,true);
  xmlhttp.send();
 }

 function show_asset_info(str)
 {


  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)
  {    
   // alert(xmlhttp.responseText);
  var data=xmlhttp.responseText.split("__");
  document.getElementById("warranty").innerHTML=data[0];
  document.getElementById("Status").innerHTML=data[1];
  document.getElementById("code").value=data[2];
  document.getElementById("datashow").innerHTML=data[3];
  $('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
  }
  }
  xmlhttp.open("GET","ajax_dynamic_subasset_creation.php?asset="+str,true);
  xmlhttp.send();
 }
  

</script> 

