
<!---------------------------------- cloning start for Accreditation --------------------------------------------->
	
<div class="col-sm-12 col-lg-12 col-12" id="tooling_div_to_clone" style="display:none;">
		<div class="row">
			<div class="col-sm-2"> 
				 <label class="control-label hide" for="Tool Description">&nbsp;<em></em></label> 
				 <input type="text" class="form-control tool_description" id="tool_description" name="tool_description[]" value="" placeholder="Tool Description" autocomplete="off" oninput="this.value=this.value.toUpperCase();">  
			</div>
			<div class="col-sm-2"> 
				 <label class="control-label hide" for="Supplier">&nbsp;<em></em></label> 
				 <?= $gsi->getSuppliersList("supplier[]",$supplier,"100%",false,"form-control add_supplier select2"); ?>
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Lead Time">&nbsp;<em></em></label> 
				 <input type="text" class="form-control" id="lead_time" name="lead_time[]" value="" placeholder="Lead Time" autocomplete="off" >  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Cost">&nbsp;<em></em></label> 
				 <input type="number" class="form-control cost" id="cost" name="cost[]" value="" placeholder="Cost" autocomplete="off" step="0.01">  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Number">&nbsp;<em></em></label> 
				 <input type="number" class="form-control number" id="number" name="number[]" value="" placeholder="Number" autocomplete="off" oninput="this.value=this.value.toUpperCase();">  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Other">&nbsp;<em></em></label> 
				 <input type="number" class="form-control other" id="other" name="other[]" value="" placeholder="Other" autocomplete="off" step="0.01"> 
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" style="font-size:14px;" for="Parts per Tool">&nbsp;<em></em></label> 
				 <input type="text" class="form-control" id="parts_per_tool" name="parts_per_tool[]" value="" placeholder="Parts per Tool" autocomplete="off" >  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Tool Life">&nbsp;<em></em></label> 
				 <input type="text" class="form-control" id="tool_life" name="tool_life[]" value="" placeholder="Tool Life" autocomplete="off"> 
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Total Cost">&nbsp;<em></em></label> 
				 <input type="number" class="form-control total_cost" id="total_cost" name="total_cost[]" value="" placeholder="Total Cost" autocomplete="off" readonly>  
			</div>
			<div class="col-sm-1" style="margin-top:2px;"> 
				<div class="custom-trash ">
					<label class="control-label"></label> 
					<span class="fa fa-trash delete_new_tooling" alt="" style="color: red;"></span>
				</div>
			</div>
		</div><br>
	</div>	
<!----------------------------------- Cloning end for Accreditation  --------------------------------------------->

        <section class="content">
			<!-- general form elements -->
                <form class="" method="post" name="toolingForm" id="toolingForm" action="index?page=view_quotation_progress&action=insert_tooling" onsubmit="return validateForm(this)" enctype="multipart/form-data">
					  <div class="row">
						  <div class="col-lg-12 col-sm-12">
						  <div class="form-section mt-2">
							<? 
								$tooling_query = "SELECT * FROM quot_tooling WHERE quot_id='".$_REQUEST['id']."' ";
								$tooling_sql=mysqli_query($conn,$tooling_query);
								$tooling_result=mysqli_num_rows($tooling_sql);
								if($tooling_result>0){
									$j = 0;
									while($mapp_row = mysqli_fetch_array($tooling_sql)){
							?>
							<div class="col-sm-12 col-lg-12 col-12 initial_row_for_tooling" id="new_tooling<?=$mapp_row['id'];?>" data-personal_count="<?=$mapp_row['id'];?>" alt="<?=$mapp_row['id'];?>">
							<div class="row">
							<div class="col-sm-2">
								<label class="control-label" for="Tool Description">Tool Description<em></em></label>
								<input type="text" id="tool_description" name="tool_description[<?=$j;?>]" class="form-control tool_description" value="<?php echo $mapp_row['tool_description'];?>" placeholder="Tool Description" autocomplete="off"oninput="this.value=this.value.toUpperCase();">
							</div>
							<div class="col-sm-2">
								<label class="control-label" for="Supplier">Supplier<em></em></label>
								
				                <?= $gsi->getSuppliersList("supplier[]",$supplier,"100%",false,"form-control add_supplier select2"); ?>
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Lead Time">Lead Time<em></em></label>
								<input type="text" id="lead_time" name="lead_time[<?=$j;?>]" class="form-control" value="<?php echo $mapp_row['lead_time'];?>" placeholder="Lead Time" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Cost">Cost<em></em></label>
								<input type="number" id="cost" name="cost[<?=$j;?>]" class="form-control cost" value="<?php echo $mapp_row['cost'];?>" placeholder="Cost" autocomplete="off" step="0.01">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Number">Number<em></em></label>
								<input type="number" id="number" name="number[<?=$j;?>]" class="form-control number" value="<?php echo $mapp_row['number'];?>" placeholder="Number" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Other">Other<em></em></label>
								<input type="number" id="other" name="other[<?=$j;?>]" class="form-control other" value="<?php echo $mapp_row['other'];?>" placeholder="Other" autocomplete="off" step="0.01">
							</div>
							<div class="col-sm-1">
								<label class="control-label" style="font-size:14px;" for="Parts per Tool">Parts per Tool<em></em></label>
								<input type="text" id="parts_per_tool" name="parts_per_tool[<?=$j;?>]" class="form-control" value="<?php echo $mapp_row['parts_per_tool'];?>" placeholder="Parts per Tool" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Tool Life">Tool Life<em></em></label>
								<input type="text" id="tool_life" name="tool_life[<?=$j;?>]" class="form-control" value="<?php echo $mapp_row['tool_life'];?>" placeholder="Tool Life" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Total Cost">Total Cost<em></em></label>
								<input type="number" id="total_cost" name="total_cost[<?=$j;?>]" class="form-control total_cost" value="<?php echo $mapp_row['total_cost'];?>" placeholder="Total Cost" autocomplete="off" readonly oninput="this.value=this.value.toUpperCase();">
							</div>
							<div class="col-sm-1" style="margin-top:35px;"> 
								<div class="custom-trash">
									<label class="control-label"></label>
									<input type="hidden" name="tooling_mapping_id[<?=$j;?>]" value="<?php echo $mapp_row['id'];?>">
									<?php if ($j != 0) { ?>
									    <span class="fa fa-trash delete_new_tooling" style="cursor:pointer; color: red;"  alt="<?php echo $mapp_row['id'];?>"></span>
									    <?php } ?>
									<i class="fas fa-sync-alt fa-spin" id="load_new_tooling<?=$mapp_row['id'];?>" style="font-size: 20px;display:none;"></i>
									<i class="fa fa-check" id="status_update_personal_done_<?=$mapp_row['id'];?>" style="color:#bad23a;font-size: 20px;display:none;"></i>
								</div>
							</div>
						</div>
					<br>
					</div>

					<?
					$j++;
					}}else{?>
					<div class="col-sm-12 col-lg-12 col-12 initial_row_for_tooling" id="new_tooling0" data-personal_count="0" alt="0">
						<div class="row">
							<div class="col-sm-2">
								<label class="control-label" for="Tool Description">Tool Description<em></em></label>
								<input type="text" class="form-control tool_description" id="tool_description" name="tool_description[]" value="" placeholder="Tool Description" autocomplete="off"oninput="this.value=this.value.toUpperCase();">
							</div>
							<div class="col-sm-2">
								<label class="control-label" for="Supplier">Supplier<em></em></label>
								
				               <?= $gsi->getSuppliersList("supplier[]",$supplier,"100%",false,"form-control add_supplier select2"); ?>
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Lead Time">Lead Time<em></em></label>
								<input type="text" class="form-control" id="lead_time" name="lead_time[]" value="" placeholder="Lead Time" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Cost">Cost<em></em></label>
								<input type="number" class="form-control cost" id="cost" name="cost[]" value="" placeholder="Cost" autocomplete="off" step="0.01">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Number">Number<em></em></label>
								<input type="number" class="form-control number" id="number" name="number[]" value="" placeholder="Number" autocomplete="off"oninput="this.value=this.value.toUpperCase();">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Other">Other<em></em></label>
								<input type="number" class="form-control other" id="other" name="other[]" value="" placeholder="Other" autocomplete="off" step="0.01">
							</div>
							<div class="col-sm-1">
								<label class="control-label" style="font-size:14px;" for="Parts per Tool">Parts per Tool<em></em></label>
								<input type="text" class="form-control" id="parts_per_tool" name="parts_per_tool[]" value="" placeholder="Parts per Tool" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Tool Life">Tool Life<em></em></label>
								<input type="text" class="form-control" id="tool_life" name="tool_life[]" value="" placeholder="Tool Life" autocomplete="off">
							</div>
							<div class="col-sm-1">
								<label class="control-label" for="Total Cost">Total Cost<em></em></label>
								<input type="number" class="form-control total_cost" id="total_cost" name="total_cost[]" value="" placeholder="Total Cost" autocomplete="off" readonly oninput="this.value=this.value.toUpperCase();">
							</div>
						</div><br>
					</div>
					<?}?>
					<div class="form-group col-sm-12 tooling_div_to_append">	
						<label class="add_one_more_tooling" style="margin-left:0%;cursor:pointer;color: #a423a4; "><span class="fa fa-plus"></span>Add More</label>
					</div>
							</div>
						 </div>
					  </div><!-- /.row -->
                  <div class="card-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="quot_id" name="quot_id" value="<?php echo $_REQUEST['id'];?>">
					<input type="hidden" id="from" name="from" value="<?php echo $from;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
					<input type="hidden" id="section_status_tool" name="section_status_tool" value="">
					<button type="button" id="submit_incomplete" class="btn btn-danger" onclick="submitFormtoolingStatus(1,'spinner_tool_incomplete')">
						Save Incomplete
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="spinner_tool_incomplete"></i>
					</button>
					<button type="button" id="submit_completed" class="btn btn-success" onclick="submitFormtoolingStatus(2,'spinner_tool_completed')">
						Section Completed
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="spinner_tool_completed"></i>
					</button>
					<div style="float:right;">
						<button type="button" id="prevButton" class="btn btn-secondary" onclick="ToPrevTab()">Previous</button>
						<button type="button" id="nextButton" class="btn btn-secondary" onclick="ToNextTab()">Next</button>

                   </div>
                  </div>
                </form>
</section>
<script type="text/javascript">

function calculateTool(row) {
    const other = parseFloat(row.find('.other').val()) || 0;
    const cost = parseFloat(row.find('.cost').val()) || 0;
    const number = parseFloat(row.find('.number').val()) || 0;

    let total = "";
    if (cost !== 0) {
        total = (number * cost) + other;
    }
    row.find('.total_cost').val(total !== "" ? total.toFixed(2) : "0");
}
   $(document).ready(function(){
          $("#myModal .close_button2").click(function(){
				location.reload();
		  });	
		  $('#toolingForm').on('change', '.tool_description, .cost, .other, .number, .add_supplier', function () {
			  var row = $(this).closest('.row');  
			  calculateTool(row);
			  var part_id = $(this).val();
			  if(part_id =='-1'){
				$('.add_new_station').click(); 
				$("#myModalLabel").html("Add New Supplier");
				var hide_top_left = "&hide_top=1&hide_left=1";
				var from = "&from=add_new_supplier_progress";
				$('.modal-dialog').css({"max-width":"60%"});
				$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/supplier_management/index?page=add&"+hide_top_left+from);
				 
			  }

		});
	
 });

function submitFormtoolingStatus(status, spinnerId) {
    document.getElementById('section_status_tool').value = status;

    document.querySelectorAll('.fa-spin').forEach(function(spinner) {
        spinner.style.display = 'none';
    });
    document.getElementById(spinnerId).style.display = 'inline-block';
    document.getElementById('toolingForm').submit();
}
$(function(){

	
	$('.add_one_more_tooling').click(function() {		
		$('#tooling_div_to_clone').show();		
		var div_count = $('.initial_row_for_tooling').last().attr("data-personal_count");
		div_count++;
		var cloned_div = $('#tooling_div_to_clone').clone().addClass("initial_row_for_tooling").attr("id","new_tooling"+div_count).attr("data-personal_count",div_count);		
		$('.tooling_div_to_append').before(cloned_div);		
		$('#tooling_div_to_clone').hide();
		$('#new_tooling'+div_count).attr("alt",div_count)
	    $('#new_tooling'+div_count).children().find(".type_clone").attr("id","type_id"+div_count).attr("alt",div_count).select2();		
	});
	
	
	
	$("#toolingForm").on("click", ".delete_new_tooling",function() {
	    if(confirm('Are you sure you want to delete?')){		
			if($(this).attr("alt") != ""){
				var tooling_mapping_id = $(this).attr("alt");
				$('#load_new_tooling'+tooling_mapping_id).show();
				var form_data   = {'tooling_mapping_id':tooling_mapping_id};      
                //alert(tooling_mapping_id);				
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/quotation_management/index.php?page=ajax_actions&action=delete_tooling_mapping",
					//url: site_url+"/client_management/index?page=ajax_actions&action=delete_tooling_mapping",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#status_update_personal_done_'+tooling_mapping_id).show();
							$('#load_new_tooling'+tooling_mapping_id).hide();
							
							setTimeout(function() {
								$('#status_update_personal_done_'+tooling_mapping_id).hide();
								$('#new_tooling'+tooling_mapping_id).remove();
								//location.reload();
							},1000);
						}
					}
				});
			}else{
				$(this).parent().parent().parent().parent().remove();
			}
		}
	});
	
	/*$("#form2").on("change",".type_id",function(){
		var div_no = $(this).parent().parent().parent().attr("alt");
		var type_id = $(this).val();
		if(type_id != ""){
			$('#new_tooling'+div_no).find("#tooling_load").show();
			var form_data   = {'type_id':type_id};
			$.ajax({
				async: true,
				type: "POST",
				url: site_url+"/modules/quotation_management/index.php?page=ajax_actions&action=get_tooling_details",
				data: form_data,
				success: function(result){  
					var resultObj = jQuery.parseJSON(result); 
					if(resultObj.status == 1){
						$('#new_tooling'+div_no).find("#description").val(resultObj.description);
						$('#new_tooling'+div_no).find("#format").val(resultObj.format);
						//$('#new_tooling'+div_no).find("#mpe").val(resultObj.mpe);
						$('#new_tooling'+div_no).find("#mpe").val(resultObj.mpe).trigger("change");
						$('#new_tooling'+div_no).find("#uom").val(resultObj.uom);
						$('#new_tooling'+div_no).find("#density").val(resultObj.density);
						$('#new_tooling'+div_no).find("#tooling_load").hide();
					}
				}
			});
		}
	});*/
	
});
</script>