<style>
em {
    color: red;
    font-size: 20px;
    position: absolute;
}

em {
    color: red;
    font-size: 20px;
    position: absolute;
}

.ModelfixTableHead {
    overflow-y: auto;
    height: 250px;
}

.ModelfixTableHead thead th {
    position: sticky !important;
    top: 0;
    z-index: 1;
    background-color: white;
</style>
<!---------- Post Out ---------------->
<div class="text-center">
 <a href="" data-toggle="modal" data-backdrop="static" data-target="#myModalAjaxForPartsUsed" class=" text-center btn btn-warning add_new_part_used " style=" padding: 8px 10px; float:right"> Add Route</a>
                          
</div>
<?php

$set_id = $_REQUEST['id'];
$rr_query = mysqli_query($conn, "SELECT IFNULL(MAX(CAST(SUBSTRING(route_no, 2) AS UNSIGNED)), 0) + 1 AS new_id FROM set_route_details");
$rr_row = mysqli_fetch_assoc($rr_query);
$route_no = '2' . str_pad($rr_row['new_id'], 2, '0', STR_PAD_LEFT);

   $sql = "SELECT * FROM set_route_details WHERE set_id = '".$_REQUEST['id']."'";
    $result_edit = mysqli_query($conn, $sql);
	$resultNo=mysqli_num_rows($result_edit);
    $row_edit = mysqli_fetch_array($result_edit);
   if($resultNo >0){
    // Assign values from database to form variables
    $route_no = $row_edit['route_no'];
    $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'];
	$hide_res = $machine_lock == 1 ? 'hidden' : '';

    $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'];
    $operation_unit = $row_edit['operation_unit'];
    $parts_per_operation = $row_edit['parts_per_operation'];
    $workers_needed = $row_edit['workers_needed'];
    $labour_rate = $row_edit['labour_rate'];
    $document_upload = $row_edit['document_upload'];
    $notes = $row_edit['notes'];?>
	
	<script type="text/javascript">
	 $(document).ready(function(){
		$("#page_action_part_used").val("update");
		$("#submit_form_parts_used").html("Update");
		$("#myModalLabelAjaxForPartsUsed").html("Update Route");

	 });
	</script>
	<?
	 $hideRoute='';
   }else{
	   $hideRoute='hide';
	   $messgae='Please Add Route First';
   }
?>
<style>
    .modal-centered-80 {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 70%;
        max-width: 80%;
    }
    .modal-dialog {
        margin: 0 auto;
    }
	.hidden {
    display: none;
}
</style><div class="modal fade" id="myModalAjaxForPartsUsed" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-centered-80">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="myModalLabelAjaxForPartsUsed">Add Route</h4>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            </div>
            <div class="modal-body">
                <form action="" method="post" id="add_form_parts_used">
                    <div class="form-row">
                        <div class="form-group col-md-4">
                            <label for="route_no">Route Number</label>
                            <input type="text" id="route_no" name="route_no" value="<?php echo $route_no; ?>" class="form-control" placeholder="Route Number" required readonly>
                        </div>
                        <div class="form-group col-md-4">
                            <label for="route_desc">Route Description</label>
                            <input type="text" id="route_desc" name="route_desc" value="<?php echo $route_desc; ?>" class="form-control" placeholder="Route Description" required>
                        </div>
                        <div class="form-group col-md-4">
                            <label for="part_no">Set Number</label>
                            <?= $gsi->getSetNumberList("part_no",$set_id,"100%",false,"form-control select2 part_number",""); ?>
							<span style="inline:block;color:red;display:none;" id="part_no_check"></span>
                        </div>
                    </div>

                    <div class="form-row">
                        <div class="form-group col-md-1">
                            <label for="operation">Operation</label>
                            <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 class="form-group col-md-1">
                            <label>M-Lock</label>
                            <div class="form-check">
                                <input type="checkbox" class="form-check-input" id="machine_lock" name="machine_lock"  value="1"  <?php echo ($machine_lock ==1) ? 'checked':''; ?>> 
                            </div>
                        </div>
                        <div class="form-group col-md-2">
                            <label for="primary_resource">Primary Resource</label>
							<?= $gsi->getStationList("machine_high_priority",$machine_high_priority,"100%",false,"form-control select2 ",""); ?>
                        </div>
                        <div class="form-group col-md-1">
                            <label for="primary_rate">Rate</label>
                            <input type="text" id="machine_rate" name="machine_rate" value="<?php echo $machine_rate; ?>" class="form-control" placeholder="Rate">
							
                        </div>
                         <div class="form-group col-md-1">
                            <label for="set_time">Set Time</label>
                            <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 class="form-group col-md-1">
							<label for="operation_time">Op-Time</label>
							<div class="input-group">
								  <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="form-group col-md-2">
                            <label for="parts_per_operation">Parts/Oper</label>
                            <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 class="form-group col-md-1">
                            <label for="workers_needed">WorkNeed</label>
                            <input type="number" step="0.01" id="workers_needed" name="workers_needed" value="<?php echo $workers_needed; ?>" class="form-control" >
                        </div>
                        <div class="form-group col-md-2">
                            <label for="labour_rate">L-Rate</label>
							<input type="number" step="0.01" id="labour_rate" name="labour_rate" value="<?php echo $labour_rate; ?>" class="form-control" >
                        </div>

                   </div>
				   <div class="form-row">
                        <div class="form-group col-md-2">
                        </div>
                        <div class="form-group col-md-2  secondary-tertiary-group <?=$hide_res?>">
                           <label for="secondary_resource">Secondary Resource</label>
                           <?= $gsi->getStationList("machine_low_priority",$machine_low_priority,"100%",false,"form-control select2 ",""); ?>
                        </div>
                        <div class="form-group col-md-1  secondary-tertiary-group <?=$hide_res?>">
                            <label for="secondary_rate">Rate</label>
                            <input type="number" step="0.01" id="machine_low_rate" name="machine_low_rate" value="<?php echo $machine_low_rate; ?>" class="form-control" placeholder="Rate">
                        </div>
                         <div class="form-group col-md-7">
                        </div>
                   </div>
                    <div class="form-row">
                        <div class="form-group col-md-2">
                        </div>
                        <div class="form-group col-md-2  secondary-tertiary-group <?=$hide_res?>">
                            <label for="tertiary_resource">Tertiary Resource</label>
                            <?= $gsi->getStationList("tertiary_resource",$tertiary_resource,"100%",false,"form-control select2 ",""); ?>
                        </div>
                        <div class="form-group col-md-1  secondary-tertiary-group <?=$hide_res?>">
                             <label for="tertiary_rate">Rate</label>
                            <input type="number" step="0.01" id="tertiary_rate" name="tertiary_rate" value="<?php echo $tertiary_rate; ?>" class="form-control" placeholder="Rate">
                        </div>
                         <div class="form-group col-md-7">
                        </div>
                   </div>
                   <div class="form-group">
						 <label for="File" class="control-label">File<em></em></label><br/>
							<span style="font-weight:bold;color:#ff0000;">(Please upload only pdf/jpg/jpeg/png)</span><br/>
							<input type="file" name="document_upload" id="document_upload" accept=".pdf,.jpg,.jpeg,.png"/>
							<div style="">
							<?php
							$dir = "../route_documents/";    
									$ext = strtolower(pathinfo($document_upload, PATHINFO_EXTENSION));

									if ($ext == 'pdf') {
										$image_path = '<a href="'.$dir.$document_upload.'" target="_blank">
														  <span class="primary text-primary" 
																title="'.$document_upload.'">'
														  .utility::ellipsis($document_upload, 20).'</span></a>';
									} else {
										$image_path = '<img style="width:100px;border:1px solid #777" src="'.$dir.'no-image.gif">';
									}
							   

								echo $image_path;

							?>
							<input type="hidden" name="hidden_document_upload" value="<?php echo $document_upload; ?>">

							<input type="hidden" name="hidden_document_upload" value="<? echo $document_upload;?>">
							</div>
					</div>
                    <div class="form-group">
                        <label for="notes">Notes</label>
						<textarea id="notes" name="notes" class="form-control"><?php echo $notes; ?></textarea>
                    </div>

                   <div style="clear:both;margin-top:13px;"></div>
						<div class="col-lg-12" style="margin-top:13px;">
							<input type="hidden" id="part_used_id" name="id" value="">
							<input type="hidden" id="set_id" name="set_id" value="<?=$_REQUEST['id']?>">
							<input type="hidden" id="page_action_part_used" name="page_action_part_used" value="add">
							<button type="submit" name="submit_form_parts_used" id="submit_form_parts_used" class="btn btn-primary">Submit</button>
							<i class="fas fa-sync-alt fa-spin" style="display:none;" id="page_load_div_for_parts_used"></i>
							<i class="fa fa-check" id="notes_done" style="color:#bad23a;font-size: 20px;display:none;"></i>
						</div>
					<div class="form-group text-right">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>

$(document).ready(function() {
var mask = "99:99";
	$(".sitetime").inputmask(mask);	
});
  document.getElementById('machine_lock').addEventListener('change', function () {
    const fields = document.querySelectorAll('.secondary-tertiary-group');
    fields.forEach(field => {
        field.style.display = this.checked ? 'none' : 'block';
    });
});

</script>


<!-- /.modal -->
<script type="text/javascript">
var flag_for_table = 0;
$(function() {
    $("#parts_used_div").on("click", ".add_new_part_used", function() {
        flag_for_table = 0;
        $("#add_form_parts_used")[0].reset();
        $("#myModalLabelAjaxForPartsUsed").html("Notes");
        $("#page_action_part_used").val("add");
        $("#submit_form_parts_used").html("Submit");
    });
	
	$(document).on('submit', 'form#add_form_parts_used', function(e) {
    var notes_done = $(this).find('#notes_done');
    notes_done.hide();
    var page_load_div_for_parts_used = $(this).find('#page_load_div_for_parts_used');
    page_load_div_for_parts_used.show();
    var submit_form_parts_used = $(this).find("#submit_form_parts_used");
    submit_form_parts_used.attr("disabled", true);
    var add_form_parts_used = $(this);

    e.preventDefault(); //prevent Default functionality
    var page_action_part_used = $("#page_action_part_used").val();
    var flag = 0;
    if (page_action_part_used == "add") {
        flag = 1;
    }

    //var form_data = $('#add_form_parts_used').serialize();
	var form_data = new FormData($('#add_form_parts_used')[0]);

	$.ajax({
		url: site_url+"/modules/set_management/index.php?page=ajax_actions&action=add_route_details",
		type: "POST",
		data: form_data,
		contentType: false, 
		processData: false, 
		cache: false,
		success: function(result) {
			flag_for_table = 1;
			if (flag == 1) {
				add_form_parts_used[0].reset();
				var parts_used_add_update_message = "Route added successfully";
				setTimeout(function() {
					$(".close_button_parts_used").trigger("click");
				}, 1000);
			} else {
				var parts_used_add_update_message = "Route updated successfully";
				setTimeout(function() {
					$(".close_button_parts_used").trigger("click");
				}, 1000);
			}
			location.reload();
			notes_done.show();
			page_load_div_for_parts_used.hide();
			submit_form_parts_used.attr("disabled", false);
			$(".parts_used_add_update_message").show().html(parts_used_add_update_message);
			setTimeout(function() {
				notes_done.hide();
				$(".parts_used_add_update_message").hide();
			}, 3000);
		},
		error: function(xhr, status, error) {
			console.log("Error: " + error);
			page_load_div_for_parts_used.hide();
			submit_form_parts_used.attr("disabled", false);
		}
	});
   /* $.ajax({
        async: true,
        type: "POST",
        url: site_url+"/modules/set_management/index.php?page=ajax_actions&action=add_route_details",
        data: form_data,
        success: function(result) {
            flag_for_table = 1;
            //alert(flag_for_table);
            if (flag == 1) {
                add_form_parts_used[0].reset(); // Reset all form data
                var parts_used_add_update_message = "Route added successfully";
                setTimeout(function() {
                    $(".close_button_parts_used").trigger("click");
                }, 1000);
            } else if (flag == 0) {
                var parts_used_add_update_message = "Route Used updated successfully";
                setTimeout(function() {
                    $(".close_button_parts_used").trigger("click");
                }, 1000);
            } location.reload();
            notes_done.show();
            page_load_div_for_parts_used.hide();
            submit_form_parts_used.attr("disabled", false);
            $(".parts_used_add_update_message").show();
            $(".parts_used_add_update_message").html(parts_used_add_update_message);
            setTimeout(function() {
                notes_done.hide();
                $(".parts_used_add_update_message").hide();
            }, 3000);
        }
    });*/

});
    /*********************************** Post Out By Ajax and JSON *************************/
	
    $("#part_used_table").on("click", ".part_used_edit", function() {
        $("#myModalLabelAjaxForPartsUsed").html("Edit Route Details <span style='color:Tomato'></span>");
        var id = $(this).attr("alt");
        var form_data = {'id': id};
        $.ajax({
            async: true,
            type: "POST",
            url: "<?php echo $siteURL?>/modules/set_management/index.php?page=ajax_actions&action=get_edit_route",
            data: form_data,
            success: function(result) {
                var resultObj = jQuery.parseJSON(result);
                if (resultObj.status == "success") {
                    $.each(resultObj.data_array, function(data_array_key, data_array_val) {
                        if(data_array_key == "arolite_job_id"){
							$("#myModalAjaxForPartsUsed").children().find("#arolite_job_id").val(data_array_val);
						}else{
                            $("#myModalAjaxForPartsUsed").children().find("#"+data_array_key).val(data_array_val);
                        }
                    });
                    $("#part_used_id").val(id);
                    $("#page_action_part_used").val("update");
                    $("#submit_form_parts_used").html("Update");
                }
            }
        });
    });
    var post_out_t = $('#part_used_table').DataTable();
	
    $('#part_used_table tbody').on('click', '.part_used_remove', function() {
        if (confirm("Do you want to delete.")) {
            var part_used_id = $(this).attr("alt");
            $("#delete_loading_part_used_"+part_used_id).show();
            var form_data = {'part_used_id': part_used_id};
            $.ajax({
                async: true,
                type: "POST",
                url: "<?php echo $siteURL?>/modules/jobs/index.php?page=ajax_actions&action=delete_job_part_used",
                data: form_data,
                success: function(result) {
                    var resultObj = jQuery.parseJSON(result);
                    if (resultObj.status == "success") {
                        $("#delete_loading_part_used_"+part_used_id).hide();
                        $("#delete_part_used_done"+part_used_id).show();
                        flag_for_table = 1;
                        post_out_t.draw(false);
                    }
                }
            });
        }
    });

    $("#myModalAjaxForPartsUsed").on("click", ".close_button_parts_used", function() {
        //alert(flag_for_table);
        if (flag_for_table == 1) {
            flag_for_table = 0;
            post_out_t.draw(true);
        }
    });
    /******************************* End Post Out By Ajax and JSON *************************/
});

</script>