<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name = "index?page=view";
$form_action = "insert";
$button_value = "Add";
$table_name = "route_details"; 
$page_name = "Add Route";
$query_string = "";
$rr_query = mysqli_query($conn, "SELECT IFNULL(MAX(CAST(SUBSTRING(route_no, 5) AS UNSIGNED)), 0) + 1 AS new_id FROM route_details");
$rr_row = mysqli_fetch_assoc($rr_query);
$route_no = '1' . str_pad($rr_row['new_id'], 2, '0', STR_PAD_LEFT);

// Insert action
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert') {    
    utility::addslashesOnAllInput(array("query_string"));
    
    $route_no = $_REQUEST['route_no'];
    $route_desc = $_REQUEST['route_desc'];
    $part_no = $_REQUEST['part_no'];
    $previous_part_no = $_REQUEST['previous_part_no'];
    $operation = $_REQUEST['operation'];
    $machine_lock = isset($_REQUEST['machine_lock']) ? 1 : 0;
    $machine_high_priority = $_REQUEST['machine_high_priority'];
    $machine_medium_priority = $_REQUEST['machine_medium_priority'];
    $machine_low_priority = $_REQUEST['machine_low_priority'];
    $machine_rate = $_REQUEST['machine_rate'];
    $set_time = $_REQUEST['set_time'];
    $operation_time = $_REQUEST['operation_time'];
    $parts_per_operation = $_REQUEST['parts_per_operation'];
    $workers_needed = $_REQUEST['workers_needed'];
    $labour_rate = $_REQUEST['labour_rate'];
    $notes = $_REQUEST['notes'];
    
	$created_by					= $_SESSION['user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	$updated_by					= $_SESSION['user_id'];
	
  echo  $sql = "INSERT INTO $table_name (route_no,route_desc, part_no, previous_part_no, operation, machine_lock, 
            machine_high_priority, machine_medium_priority, machine_low_priority, machine_rate, set_time, operation_time, 
            parts_per_operation, workers_needed, labour_rate, notes,created_on,created_by,updated_by) 
            VALUES ('$route_no','$route_desc', '$part_no', '$previous_part_no', '$operation', '$machine_lock', 
            '$machine_high_priority', '$machine_medium_priority', '$machine_low_priority', '$machine_rate', '$set_time', 
            '$operation_time', '$parts_per_operation', '$workers_needed', '$labour_rate', '$notes', '$created_on', '$created_by', '$updated_by')";die;
    mysqli_query($conn, $sql);
    
    $_SESSION['info_message'] = "Route added successfully";
    header("Location: $file_name");
    exit();
}

// Edit action
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit') {
    $id = $_REQUEST['id'];
    $form_action = "update";
    $button_value = "Update";
    $page_name = "Edit Route";

    $sql = "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysqli_query($conn, $sql);
    $row_edit = mysqli_fetch_array($result_edit);

    // Assign values from database to form variables
    $route_desc = $row_edit['route_desc'];
    $part_no = $row_edit['part_no'];
    $previous_part_no = $row_edit['previous_part_no'];
    $operation = $row_edit['operation'];
    $machine_lock = $row_edit['machine_lock'];
    $machine_high_priority = $row_edit['machine_high_priority'];
    $machine_medium_priority = $row_edit['machine_medium_priority'];
    $machine_low_priority = $row_edit['machine_low_priority'];
    $machine_rate = $row_edit['machine_rate'];
    $set_time = $row_edit['set_time'];
    $operation_time = $row_edit['operation_time'];
    $parts_per_operation = $row_edit['parts_per_operation'];
    $workers_needed = $row_edit['workers_needed'];
    $labour_rate = $row_edit['labour_rate'];
    $notes = $row_edit['notes'];
}

// Update action
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'update') {
    utility::addslashesOnAllInput(array("query_string"));
    
    $id = $_REQUEST['id'];
    $route_desc = $_REQUEST['route_desc'];
    $part_no = $_REQUEST['part_no'];
    $previous_part_no = $_REQUEST['previous_part_no'];
    $operation = $_REQUEST['operation'];
    $machine_lock = isset($_REQUEST['machine_lock']) ? 1 : 0;
    $machine_high_priority = $_REQUEST['machine_high_priority'];
    $machine_medium_priority = $_REQUEST['machine_medium_priority'];
    $machine_low_priority = $_REQUEST['machine_low_priority'];
    $machine_rate = $_REQUEST['machine_rate'];
    $set_time = $_REQUEST['set_time'];
    $operation_time = $_REQUEST['operation_time'];
    $parts_per_operation = $_REQUEST['parts_per_operation'];
    $workers_needed = $_REQUEST['workers_needed'];
    $labour_rate = $_REQUEST['labour_rate'];
    $notes = $_REQUEST['notes'];
    $updated_by					= $_SESSION['user_id'];
    $sql = "UPDATE $table_name SET route_desc = '$route_desc', part_no = '$part_no', previous_part_no = '$previous_part_no',
            operation = '$operation', machine_lock = '$machine_lock', machine_high_priority = '$machine_high_priority',
            machine_medium_priority = '$machine_medium_priority', machine_low_priority = '$machine_low_priority', machine_rate = '$machine_rate',
            set_time = '$set_time', operation_time = '$operation_time', parts_per_operation = '$parts_per_operation',
            workers_needed = '$workers_needed', labour_rate = '$labour_rate', notes = '$notes', updated_by = '$updated_by' WHERE id = $id";
    mysqli_query($conn, $sql);
    
    $_SESSION['info_message'] = "Route updated successfully";
    header("Location: $file_name");
    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'];
			$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">
              <div class="card box-primary">
               <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add&action=<?php echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
					  <div class="card-body">
					  <div class="row">
						<div class="col-lg-3">
						  <label for="route_no" class="control-label">Route Number<em>*</em></label>
						  <div>
							<input type="text" id="route_no" name="route_no" value="<?php echo $route_no; ?>" class="form-control" placeholder="Route Number" required readonly>
							<span style="inline:block;color:red;display:none;" id="route_no_check"></span>
						  </div>
						</div>

						<div class="col-lg-3">
						  <label for="route_desc" class="control-label">Route Description<em>*</em></label>
						  <div>
							<input type="text" id="route_desc" name="route_desc" value="<?php echo $route_desc; ?>" class="form-control" placeholder="Route Description" required>
							<span style="inline:block;color:red;display:none;" id="route_desc_check"></span>
						  </div>
						</div>

						<div class="col-lg-3">
						  <label for="part_no" class="control-label">Part Number<em></em></label>
						  <div>
							<?= $gsi->getPartNumberListNew("part_no",$part_no,"100%",false,"form-control select2 part_number",""); ?>
							<span style="inline:block;color:red;display:none;" id="part_no_check"></span>
						  </div>
						</div>
						<div class="col-lg-3">
						  <label for="part_no" class="control-label">Previous Part Number<em></em></label>
						  <div>
							<?= $gsi->getPartNumberListNew("previous_part_no",$previous_part_no,"100%",false,"form-control select2 part_no_prev",""); ?>
							<span style="inline:block;color:red;display:none;" id="previous_no_check"></span>
						  </div>
						</div>
						</div>

						<div class="row mt-2">
						<div class="col-lg-2">
						  <label for="operation" class="control-label">Operation<em>*</em></label>
						  <div>
							<input type="text" id="operation" name="operation" value="<?php echo $operation; ?>" class="form-control" placeholder="Operation" required>
							<span style="inline:block;color:red;display:none;" id="operation_check"></span>
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="machine_lock" class="control-label"> Lock<em>*</em></label>
						  <div>
							<input type="checkbox" name="machine_lock" value="1">
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="machine_high_priority" class="control-label">Primary<em></em></label>
						  <div>
							<input type="text" id="machine_high_priority" name="machine_high_priority" value="<?php echo $machine_high_priority; ?>" class="form-control" placeholder="Primary Resource">
						  </div>
						</div>
                         <div class="col-lg-1">
						  <label for="machine_medium_priority" class="control-label">Secondary<em></em></label>
						  <div>
							<input type="text" id="machine_medium_priority" name="machine_medium_priority" value="<?php echo $machine_medium_priority; ?>" class="form-control" placeholder="Secondary Resource">
						  </div>
						</div>
						<div class="col-lg-1">
						  <label for="machine_low_priority" class="control-label">Tertiary <em></em></label>
						  <div>
							<input type="text" id="machine_low_priority" name="machine_low_priority" value="<?php echo $machine_low_priority; ?>" class="form-control" placeholder="Tertiary Resource">
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="machine_rate" class="control-label"> Rate<em>*</em></label>
						  <div>
							<input type="number" step="0.01" id="machine_rate" name="machine_rate" value="<?php echo $machine_rate; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="set_time" class="control-label">Set Time<em></em></label>
						  <div>
							<input type="time" id="set_time" name="set_time" value="<?php echo $set_time; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="operation_time" class="control-label"> Op/Time<em>*</em></label>
						  <div>
							<input type="time" id="operation_time" name="operation_time" value="<?php echo $operation_time; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="parts_per_operation" class="control-label">Parts/Op<em></em></label>
						  <div>
							<input type="number" step="0.01" id="parts_per_operation" name="parts_per_operation" value="<?php echo $parts_per_operation; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="workers_needed" class="control-label">Workers <em></em></label>
						  <div>
							<input type="number" step="0.01" id="workers_needed" name="workers_needed" value="<?php echo $workers_needed; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="labour_rate" class="control-label">Lab Rate<em>*</em></label>
						  <div>
							<input type="number" step="0.01" id="labour_rate" name="labour_rate" value="<?php echo $labour_rate; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-12">
						  <label for="notes" class="control-label">Notes:</label>
						  <div>
							<textarea id="notes" name="notes" class="form-control"><?php echo $notes; ?></textarea>
						  </div>
						</div>
					  </div>
					  </div>

					  <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","#country",function(){	
		var country = $(this).val();
		var country = $("#country").val();
		var id = $("#id").val();
		if(country != ""){
		var form_data   = {'country':country,'id':id};       
			$.ajax({
				async: false,
				type: "POST",
				url: site_url+"/modules/country_management/index.php?page=ajax_actions&action=check_country_exist",
				data: form_data,
				success: function(result){          
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == "failure"){
						//alert("This country already in system");
						$("#country_check").show();
						$("#country_check").html("This country already in system.");
						//$("#email").val("");
						$("#country").focus();
					}else{
						$("#country_check").html("");
						$("#country_check").hide();
					}
				}
			});
		}
	});
});
</script>