<link rel="stylesheet" href="css/new_mxerp.css">
<?php include('includes/Class_Branch.php');
include('includes/AccountGroupMaster_Class.php');
$modid=$_REQUEST['modid'];
$frmid=$_REQUEST['frmid'];
$action=$_REQUEST['action'];
$daction=$_REQUEST['daction'];
$rurl="&modid=".$modid."&frmid=".$frmid;
$targetpage="index.php?route=ToolPlants/maintenance/technician_master".$rurl;
$msgErr=array();?>

<div class="body_contain">
<div class="tab_div">
  <h1>Technician</h1>

<?php

if($_REQUEST['Save'])
{
  $sql_technician="select id,technician from technicians where technician='".$_REQUEST['technician']."' ";
  $result_technician=DB_query($sql_technician,$db);
  $rs_technician=DB_fetch_array($result_technician);
  $num_row=DB_num_rows($result_technician);

    if($num_row<1)
     {
      //echo $num_row;
	    $Insert = DB_query("INSERT INTO technicians SET 
						location = '".$_REQUEST['location']."',
						CompanyId = '".$_SESSION['ACGCompanyId']."',
						technician = '".$_REQUEST['technician']."'",$db);
     }
    else
     {
        prnMsg( _('Technician Already exists!'),'failure');
         exit;
     }  
		$s->pageLocation("index.php?route=ToolPlants/maintenance/technician_master".$rurl);		
}

if($_REQUEST['action']=='Edit'){
$con ="id='".$_REQUEST['id']."'";
$Get_id = $s->GetRecordById('technicians',$con);
$var = DB_fetch_array($Get_id);

	if($_REQUEST['Update'])
  {

    $sql_technician="select id,technician from technicians where technician='".$_REQUEST['technician']."' and id!='".$_REQUEST['id']."'";
    $result_technician=DB_query($sql_technician,$db);
    $rs_technician=DB_fetch_array($result_technician);
    $num_row=DB_num_rows($result_technician);

      if($num_row<1)
      {

        	$UPDATE = DB_query("UPDATE technicians SET 
        						location = '".$_REQUEST['location']."',
        						technician = '".$_REQUEST['technician']."'
        						where id = '".$_REQUEST['id']."'",$db);
       }
       else
       {
           prnMsg( _('Technician Already exists!'),'failure');
           exit;
       }  

						$s->pageLocation("index.php?route=ToolPlants/maintenance/technician_master".$rurl);
	
	}
	
}
if($_REQUEST['action']=='Delete'){
	$delete = DB_query("DELETE FROM technicians
						WHERE id = '".$_REQUEST['id']."'",$db);
}
?>
<form method="post" name="tech_master">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
    <tr class="row1">
      <td width="13%" align="right">Branch Name <font color="#FF0000">*</font></td>
      <td width="35%"><?php $location_qry = "Select * from locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$get_loc=DB_query($location_qry,$db);?>
        <select name="location" >
          <?php while($fetch = DB_fetch_array($get_loc)){
		  		$p='';
				if($fetch['loccode']==$var['location'])
				$p="selected";
			?>
          <option value="<?php echo $fetch['loccode']?>"<?php echo $p;?>><?php echo $fetch['locationname']?></option>
          <?php } ?>
        </select>
      </td>
      <td width="20%" align="right">Technician <font color="#FF0000">*</font></td>
      <td><input name="technician" type="text" value="<?php echo $var['technician']?>"  /></td>
    </tr>
    <tr class="row1">
    	<td colspan="4" style="text-align:center;"><input name="<?php if($_REQUEST['action']=='Edit') { echo 'Update';}else { echo 'Save'; }?>" type="submit" onclick="return validate_teshMaster()" value="<?php if($_REQUEST['action']=='Edit') { echo 'Update';}else { echo 'Save'; }?>" class="search" /></td>
    </tr>
  </table>  
</form>
<br>
<div class="overpan">
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
  <tr>
    <th width="5%">S. No.</th>
    <th>Branch Name</th>
    <th width="28%">Technician</th>    
    <th width="5%" style="text-align:center;">Edit</strong></th>
    <th width="5%" style="text-align:center;">Delete</th>
  </tr>
  <?php $j=0; 
    $ResultData=DB_query("select *  from technicians where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
    while($RowData = DB_fetch_array($ResultData))
   {  if($j++%2==0)
     $bgcolor="#CCCCCC";
	 else{
	 $bgcolor="#FFFFFF";
	 }
	 ?>
<tr>
    <td><?php echo $j."." ?></td>
    <td><?php echo $s->getField('locations','loccode',$RowData['location'],'locationname');;?></td>
    <td><?php echo $RowData['technician'];?></td>  
    <td style="text-align:center;"><a href="<?php  echo $targetpage;?>&action=Edit&id=<?php echo $RowData['id']?>"><img src="images/icon-edit.png" title="Edit" width="15" /></a></td>
    <td style="text-align:center;"><a href="<?php echo $targetpage;?>&action=Delete&id=<?php echo $RowData['id']?>"><img src="images/Icon_Delete.png" title="Delete" width="15" onClick="return confirm('Are you sure you wish to delete this Record?')" /></a></td>
  </tr>
  <?php } ?>
</table>
</div>
</div></div>
<script type="text/javascript">
function validate_teshMaster() {
	if(document.tech_master.location.value=='')
	{
		alert("Please Select Branch First");
		return false;
	}
	else if(document.tech_master.technician.value=='')
	{
		alert("Please Enter Technician Name");
		return false;
	}
}
</script>