<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "quotation_machines";
$page_name      = "Add Operation";
$id	 = "";
$vat = "";
$query_string = "";


################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $station 	        = $_REQUEST['station']; 
    $facility_rate_oem 	= $_REQUEST['facility_rate_oem']; 
	$labour_rate_oem 	= $_REQUEST['labour_rate_oem']; 
	$has_operation_time       	= $_REQUEST['has_operation_time']; 
	$has_set_time       	= $_REQUEST['has_set_time']; 
	$hrs_week       	= $_REQUEST['hrs_week']; 
	$machine_type       = $_REQUEST['machine_type']; 
	
    $created_by			= $_SESSION['user_id'];
	$created_on			= date("Y-m-d H:i:s");	
	$updated_by			= $_SESSION['user_id'];

	$sql = "station = '".$station."',facility_rate_oem = '".$facility_rate_oem."',labour_rate_oem = '".$labour_rate_oem."',has_operation_time = '".$has_operation_time."',has_set_time = '".$has_set_time."',hrs_week = '".$hrs_week."', machine_type = '".$machine_type."',created_on = '".$created_on."', created_by = '".$created_by."', updated_by = '".$updated_by."'";

	$sql= "INSERT INTO $table_name SET ".$sql;

	mysqli_query($conn,$sql);
	$inserted_id = mysqli_insert_id($conn);

	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "Operation added successfully";
    header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];

	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Operation";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn,$sql);
    $row_edit = mysqli_fetch_array($result_edit);

	$station 	        = $row_edit['station']; 
    $facility_rate_oem 	= $row_edit['facility_rate_oem']; 
	$labour_rate_oem 	= $row_edit['labour_rate_oem'];
	$has_operation_time 	= $row_edit['has_operation_time'];
	$has_set_time 	= $row_edit['has_set_time']; 
	$machine_type 	= $row_edit['machine_type'];   
	$hrs_week			= date("H:i",strtotime($row_edit['hrs_week']));
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id					= $_REQUEST['id'];
	$station 	        = $_REQUEST['station']; 
    $facility_rate_oem 	= $_REQUEST['facility_rate_oem']; 
	$labour_rate_oem 	= $_REQUEST['labour_rate_oem']; 
	$has_operation_time       	= $_REQUEST['has_operation_time'];
	$has_set_time       	= $_REQUEST['has_set_time'];
	$hrs_week       	= $_REQUEST['hrs_week']; 
	$machine_type       	= $_REQUEST['machine_type']; 
	//die;
	$updated_by			= $_SESSION['user_id'];
	
    $sql = "station = '".$station."',facility_rate_oem = '".$facility_rate_oem."',labour_rate_oem = '".$labour_rate_oem."',has_operation_time = '".$has_operation_time."',has_set_time = '".$has_set_time."',hrs_week = '".$hrs_week."', machine_type = '".$machine_type."', updated_by = '".$updated_by."'";
	 $sql= "UPDATE $table_name SET ".$sql." WHERE id = '".$id."'";
	//die;
    mysqli_query($conn,$sql);

	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add", "page=view", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "Operation updated successfully";
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<style>
em{color:red;font-size: 20px;position: absolute;}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row mb-2">
			<div class="col-sm-6">
				<h1>
					<?=$page_name;?>
				</h1>
			</div>
			<div class="col-sm-6">
			<?
			$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
			if(isset($from) && $from == "view_station_details"){
				$query_string = str_ireplace("page=add", "page=view_station_details", $query_string);
			}else {
				$query_string = str_ireplace("page=add", "page=view", $query_string);
			}
				
			?>
			<button type="button" id="back" name="back" value="back"  style="float:right;margin-right:5px;margin-bottom:8px;" class="btn btn-primary" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
			</div>
		</div>
	</div>
</section>
<section class="content">
	<div class="row">
		<div class="col-lg-12">
			<!-- general form elements -->
              <div class="card box-primary">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="card-body">
				    <div class="row">
                    <div class="form-group col-lg-6">
                      <label for="Resource Station" class="control-label">Resource Station<em>*</em></label>
                      <div class="">
                        <input type="text" id="station" name="station" value="<?php echo $station;?>" class="form-control" placeholder="Resource Station" required>
						<span style="inline:block;color:red;display:none;" id="station_check"></span>
                      </div>
                    </div>
					<div class="form-group col-lg-6">
						<label for="facility_rate_oem" class="control-label">Facility Rate OEM<em>*</em></label>
						<div class="">
							<input type="number" id="facility_rate_oem" name="facility_rate_oem" value="<?php echo $facility_rate_oem;?>" class="form-control" placeholder="Facility Rate OEM" required step="0.01">
						</div>
					</div>
					</div>
					<div class="row">
					<div class="form-group col-lg-6">
                      <label for="Labour Rate OEM" class="control-label">Labour Rate OEM<em>*</em></label>
                      <div class="">
                        <input type="number" id="labour_rate_oem" name="labour_rate_oem" value="<?php echo $labour_rate_oem;?>" class="form-control" placeholder="Labour Rate OEM" required step="0.01">
                      </div>
                    </div>
					<div class="form-group col-lg-6">
					  <label for="Has Set Time" class="control-label">Has Set Time<em></em></label>
					  <div class="">
						<? $gsi->getYesNo("has_set_time", $has_set_time, "100%", false, "form-control"); ?>
					  </div>
					</div>
					</div>
					<div class="row">
					<div class="form-group col-lg-6">
					  <label for="Has Operation Time" class="control-label">Has Operation Time<em></em></label>
					  <div class="">
						<? $gsi->getYesNo("has_operation_time", $has_operation_time, "100%", false, "form-control"); ?>
					  </div>
					</div>
					<div class="form-group col-lg-6 hide">
					  <label for="Hrs Week" class="control-label">Hrs Week<em></em></label>
					  <div class="">
						<input type="text" class="form-control" id="hrs_week" name="hrs_week" value="<?php echo (isset($hrs_week) && $hrs_week != "00:00:00") ? $hrs_week : '78:00'; ?>" autocomplete="off">
					  </div>
					</div>
					<!--<div class="form-group col-lg-6">
					  <label for="Machine Type" class="control-label">Machine Type<em>*</em></label>
					  <div class="">
					  <?php echo  $machinetype = $gsi->getmachineTypeList("machine_type", $machine_type, "100%", false, "form-control select2", "required"); ?>
					  </div>
					</div>-->
					<!--<div class="form-group col-lg-6">
					  <label for="Machine Tab Code" class="control-label">Machine Tab Code<em></em></label>
					  <div class="">
						<input type="text" id="machine_tab_code" name="machine_tab_code" value="<?php echo $machine_tab_code;?>" class="form-control" placeholder="Machine Tab Code">
					  </div>
					</div>-->
					
					</div>
                  </div><!-- /.box-body -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-primary">Submit
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div"></i>
					</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>
<script type="text/javascript">
$(document).ready(function(){
	$("#form1").submit(function() {
		$("#submit").attr("disabled", true);
		$('#page_load_div').show();
		//return true;
	});
	$("#form1").on("blur","#station",function(){	
		var station = $(this).val();
		var station = $("#station").val();
		var id = $("#id").val();
		if(station != ""){
		var form_data   = {'station':station,'id':id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/station/index?page=ajax_actions&action=check_station_exist",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "failure"){
						//alert("This station already in system");
						$("#station_check").show();
						$("#station_check").html("This station already in system.");
						//$("#email").val("");
						$("#station").focus();
					}else{
						$("#station_check").html("");
						$("#station_check").hide();
					}
				}
			});
		}
	});
	$('#machine_type').change(function(){
		var machine_type = $(this).val();
		if(machine_type == 4){ 
			$('#otherCustomerFields').show();
		}else{
			$('#otherCustomerFields').hide();
		}
	});	
});
</script>