<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name = "index?page=view";
$form_action = "insert";
$button_value = "Add";
$table_name = "work_order_planning"; 
$page_name = "Add Works Order";
$query_string = "";
$rr_query = mysqli_query($conn, "SELECT IFNULL(MAX(CAST(SUBSTRING(works_order_no, 5) AS UNSIGNED)), 0) + 1 AS new_id FROM route_details");
$rr_row = mysqli_fetch_assoc($rr_query);
$works_order_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'];
    $works_order_no = $_REQUEST['works_order_no'];
    $operation = $_REQUEST['operation'];
    $primary_resource = $_REQUEST['primary_resource'];
    $plan_rate = $_REQUEST['plan_rate'];
    $set_time = $_REQUEST['set_time'];
    $operation_time = $_REQUEST['operation_time'];
    $planned_qty = $_REQUEST['planned_qty'];
    $status = $_REQUEST['status'];
    $required_by = $_REQUEST['required_by'];
    
	$created_by					= $_SESSION['user_id'];
	$created_on					= date("Y-m-d H:i:s");	
	$updated_by					= $_SESSION['user_id'];
	
    $sql = "INSERT INTO $table_name (route_no, works_order_no, operation,primary_resource, plan_rate,set_time,operation_time,planned_qty, status, required_by,created_on,created_by,updated_by) 
	VALUES ('$route_no', '$works_order_no', '$operation','$primary_resource', '$plan_rate', '$set_time', 
	'$operation_time', '$planned_qty', '$status', '$required_by', '$created_on', '$created_by', '$updated_by')";
    mysqli_query($conn, $sql);
    
    $_SESSION['info_message'] = "Works Order 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);

    $route_no = $row_edit['route_no'];
    $works_order_no = $row_edit['works_order_no'];
    $operation = $row_edit['operation'];
    $machine_lock = $row_edit['machine_lock'];
    $primary_resource = $row_edit['primary_resource'];
    $plan_rate = $row_edit['plan_rate'];
    $set_time = $row_edit['set_time'];
    $operation_time = $row_edit['operation_time'];
    $planned_qty = $row_edit['planned_qty'];
    $status = $row_edit['status'];
    $required_by = $row_edit['required_by'];
}

// Update action
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'update') {
    utility::addslashesOnAllInput(array("query_string"));
    
    $id = $_REQUEST['id'];
    $route_no = $_REQUEST['route_no'];
    $works_order_no = $_REQUEST['works_order_no'];
    $operation = $_REQUEST['operation'];
    $primary_resource = $_REQUEST['primary_resource'];
    $plan_rate = $_REQUEST['plan_rate'];
    $set_time = $_REQUEST['set_time'];
    $operation_time = $_REQUEST['operation_time'];
    $planned_qty = $_REQUEST['planned_qty'];
    $status = $_REQUEST['status'];
    $required_by = $_REQUEST['required_by'];
    $updated_by					= $_SESSION['user_id'];
	
	$sql = "UPDATE $table_name SET works_order_no = '$works_order_no', 
	operation = '$operation', primary_resource = '$primary_resource',plan_rate = '$plan_rate',
	set_time = '$set_time', operation_time = '$operation_time', planned_qty = '$planned_qty',status = '$status', required_by = '$required_by', updated_by = '$updated_by' WHERE id = $id";
    mysqli_query($conn, $sql);
    
    $_SESSION['info_message'] = "Works Order 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-4">
						  <label for="works_order_no" class="control-label">Works Order Number<em>*</em></label>
						  <div>
							<input type="text" id="works_order_no" name="works_order_no" value="<?php echo $works_order_no; ?>" class="form-control" placeholder="Works Order Number" required readonly>
						  </div>
						</div>
                        <div class="col-lg-4">
						  <label for="route_no" class="control-label">Route Number<em>*</em></label>
						  <div>
							 <?= $gsi->getRouteNumberList("route_no",$route_no,"100%",false,"form-control select2 part_number",""); ?>
						  </div>
						</div>

						<div class="col-lg-4">
						  <label for="required_by" class="control-label">Required Date<em>*</em></label>
						  <div>
							<input type="text" class="form-control datepicker" id="required_by" name="required_by" value="<?php (isset($required_by) && $required_by !="") ? utility::getFormatedDate($required_by) : "";?>"placeholder="DD-MM-YYYY"  autocomplete="off" required> 
						  </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-2">
						  <label for="primary_resource" class="control-label">Primary Resource<em></em></label>
						  <div>
							<input type="text" id="primary_resource" name="primary_resource" value="<?php echo $primary_resource; ?>" class="form-control" placeholder="Primary Resource">
						  </div>
						</div>
						<div class="col-lg-2">
						  <label for="plan_rate" class="control-label"> Plan Rate<em></em></label>
						  <div>
							<input type="number" step="0.01" id="plan_rate" name="plan_rate" value="<?php echo $plan_rate; ?>" class="form-control" >
						  </div>
						</div>

						<div class="col-lg-2">
						  <label for="set_time" class="control-label">Set Time<em></em></label>
						  <div>
							<input type="text" id="set_time" name="set_time" class="form-control sitetime" value="<?php echo (isset($set_time) && $set_time !="00:00:00") ? $set_time: "";//$enq_row['set_time'];?>" placeholder="" autocomplete="off">
						  </div>
						</div>

						<div class="col-lg-1">
						  <label for="operation_time" class="control-label"> Op/Time<em></em></label>
						  <div>
							<input type="text" id="operation_time" name="operation_time" class="form-control sitetime" value="<?php echo (isset($operation_time) && $operation_time !="00:00:00") ? $operation_time : "";//$enq_row['operation_time'];?>" placeholder="" autocomplete="off" required>
						  </div>
						</div>
						<div class="col-lg-2">
						  <label for="status" class="control-label">Status<em></em></label>
						  <div>
							<input type="number" step="0.01" id="s" name="status" value="<?php echo $status; ?>" class="form-control" >
						  </div>
						</div>
                         <div class="col-lg-1">
						  <label for="planned_qty" class="control-label">QTY<em>*</em></label>
						  <div>
							<input type="number" step="" id="planned_qty" name="planned_qty" value="<?php echo $planned_qty; ?>" class="form-control" >
						  </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(){
	
var mask = "99:99";
	$(".sitetime").inputmask(mask);	
	$("#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>