
<!---------------------------------- cloning start for Accreditation --------------------------------------------->
	
<div class="col-sm-12 col-lg-12 col-12" id="brought_out_parts_div_to_clone" style="display:none;">
		<div class="row">
			<!--<div class="col-sm-1"> 
				 <label class="control-label hide" for="Part">&nbsp;<em>*</em></label> 
				 <?= $gsi->getPartNumberList("part[]","","100%",false,"form-control part_number part","required"); ?>
                                	
			</div>-->
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Part Description">&nbsp;<em></em></label> 
				 <input type="text" class="form-control part description" id="part" name="part[]" value="" placeholder="Description" autocomplete="off" oninput="this.value=this.value.toUpperCase();">
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Supplier">&nbsp;<em></em></label> 
				 <input type="text" class="form-control supplier" id="supplier" name="supplier[]" value="" placeholder="Supplier" autocomplete="off" oninput="this.value=this.value.toUpperCase();">
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Currency">&nbsp;<em></em></label> 
				 <input type="text" class="form-control currency" id="currency" name="currency[]" value="" placeholder="Currency" autocomplete="off"style="background-color: #FFFFE0;font-size:12px;"readonly >   
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Exch Rate">&nbsp;<em></em></label> 
				 <input type="text" class="form-control exch_rate" id="exch_rate" name="exch_rate[]" value="" placeholder="Exch Rate" autocomplete="off" style="background-color: #FFFFE0;"oninput="this.value=this.value.toUpperCase();">  
			</div><div class="col-sm-1"> 
				 <label class="control-label hide" for="Price">&nbsp;<em></em></label> 
				 <input type="number" class="form-control price" id="price" name="price[]" value="" placeholder="Price" autocomplete="off" step="0.01">  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Duty%">&nbsp;<em></em></label> 
				 <input type="number" class="form-control duty_percent" id="duty_percent" name="duty_percent[]" value="" placeholder="Duty%" autocomplete="off" style="background-color: #FFFFE0;" 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" >  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Scrap%">&nbsp;<em></em></label> 
				 <input type="number" class="form-control scrap_percent" id="scrap_percent" name="scrap_percent[]" value="" placeholder="Scrap%" autocomplete="off" step="0.01">  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Handling%">&nbsp;<em></em></label> 
				 <input type="number" class="form-control handling_percent" id="handling_percent" name="handling_percent[]" value="" placeholder="Handling%" autocomplete="off" step="0.01">  
			</div>

			
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Total">&nbsp;<em></em></label> 
				 <input type="text" class="form-control total_b" id="total" name="total[]" value="" placeholder="Total" autocomplete="off" readonly>  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Handling">&nbsp;<em></em></label> 
				 <input type="text" class="form-control handling" id="handling" name="handling[]" value="" placeholder="Handling" autocomplete="off" readonly>  
			</div>
			<div class="col-sm-1"> 
				 <label class="control-label hide" for="Scrap">&nbsp;<em></em></label> 
				 <input type="text" class="form-control scrap" id="scrap" name="scrap[]" value="" placeholder="Scrap" autocomplete="off" readonly>  
			</div>
			<div class="col-sm-12" style="margin-top:0px; float:right;"> 
				<div class="custom-trash " style=" float:right;">
					<label class="control-label hide"></label> 
					<span class="fa fa-trash delete_new_brought_out_parts" alt="" style="color: red;"></span>
				</div>
			</div>
		</div>
	</div>	
<!----------------------------------- Cloning end for Accreditation  --------------------------------------------->

        <section class="content">
			<!-- general form elements -->
                <form class="" method="post" name="broughtForm" id="broughtForm" action="index?page=view_quotation_progress&action=insert_brought_out" 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">
							<? 
								$brought_part_query = "SELECT * FROM quot_brought WHERE quot_id='".$_REQUEST['id']."' ";
								$brought_part_sql=mysqli_query($conn,$brought_part_query);
								$brought_part_result=mysqli_num_rows($brought_part_sql);
								if($brought_part_result>0){
									$j = 0;
									while($mapp_row = mysqli_fetch_array($brought_part_sql)){
							?>
							<div class="col-sm-12 col-lg-12 col-12 initial_row_for_brought_out_parts" id="new_delivery_addres<?=$mapp_row['id'];?>" data-personal_count="<?=$mapp_row['id'];?>">
							<div class="row">
							<!--<div class="col-sm-1"> 
								 <label class="control-label" for="Part">Part<em>*</em></label> 
								 <?= $gsi->getPartNumberList("part[$j]",$mapp_row['part'],"100%",false,"form-control select2 part_number","required"); ?>
								 
							</div>-->
							<div class="col-sm-1"> 
								 <label class="control-label" for="Part Description">Description<em>*</em></label> 
								 <input type="text" id="part" name="part[<?=$j;?>]" class="form-control part" value="<?php echo $mapp_row['part'];?>" placeholder="Description" style="font-size:12px;" oninput="this.value=this.value.toUpperCase();">
								 
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Supplier">Supplier<em></em></label> 
								 <input type="text" id="supplier" name="supplier[<?=$j;?>]" class="form-control supplier" value="<?php echo $mapp_row['supplier'];?>" placeholder="Supplier" style="font-size:12px;" oninput="this.value=this.value.toUpperCase();">

							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Currency">Currency<em></em></label> 
								 <input type="text" id="currency" name="currency[<?=$j;?>]" class="form-control currency" value="<?php echo $mapp_row['currency'];?>" placeholder="Currency" style="background-color: #FFFFE0; font-size:12px;" oninput="this.value=this.value.toUpperCase();" readonly>
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Exch Rate">Exch Rate<em></em></label> 
								 <input type="text" id="exch_rate" name="exch_rate[<?=$j;?>]" class="form-control exch_rate" value="<?php echo $mapp_row['exch_rate'];?>" placeholder="Exch Rate" style="background-color: #FFFFE0;" oninput="this.value=this.value.toUpperCase();" readonly> 
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Price">Price<em></em></label> 
								 <input type="number" id="price" name="price[<?=$j;?>]" class="form-control price" value="<?php echo $mapp_row['price'];?>" placeholder="Price" step="0.01">
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Duty%">Duty%<em></em></label> 
								 <input type="number" id="duty_percent" name="duty_percent[<?=$j;?>]" class="form-control duty_percent" value="<?php echo $mapp_row['duty_percent'];?>" placeholder="Duty%" style="background-color: #FFFFE0;" 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">
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Scrap%">Scrap%<em></em></label> 
								 <input type="number" id="scrap_percent" name="scrap_percent[<?=$j;?>]" class="form-control scrap_percent" value="<?php echo $mapp_row['scrap_percent'];?>" placeholder="Scrap%" step="0.01">
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Scrap%">Handling%<em></em></label> 
								 <input type="number" id="handling_percent" name="handling_percent[<?=$j;?>]" class="form-control handling_percent" value="<?php echo $mapp_row['handling_percent'];?>" placeholder="Scrap%" step="0.01" >
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Scrap%">Total<em></em></label> 
								 <input type="text" id="total_b" name="total[<?=$j;?>]" class="form-control total_b" value="<?php echo $mapp_row['total'];?>" placeholder="Scrap%" readonly>
							</div>
							
							<div class="col-sm-1"> 
								 <label class="control-label" for="Scrap">Scrap<em></em></label> 
								 <input type="text" id="scrap" name="scrap[<?=$j;?>]" class="form-control scrap" value="<?php echo $mapp_row['scrap'];?>" placeholder="Scrap" readonly>
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Handling">Handling<em></em></label> 
								 <input type="text" id="handling" name="handling[<?=$j;?>]" class="form-control handling" value="<?php echo $mapp_row['handling'];?>" placeholder="Handling" readonly>
							</div>
							<div class="col-sm-12" style="margin-top:0px;"> 
								<div class="custom-trash" style="float:right;">
									<label class="control-label"></label>
									<input type="hidden" name="brought_part_mapping_id[<?=$j;?>]" value="<?php echo $mapp_row['id'];?>">
									<?php if ($j != 0) { ?>
									<span class="fa fa-trash delete_new_brought_out_parts" style="cursor:pointer; color: red;"  alt="<?php echo $mapp_row['id'];?>"></span>
									<?php } ?>
						
									<i class="fas fa-sync-alt fa-spin" id="load_new_brought_out_parts<?=$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>
					</div>
					<?
					$j++;
					}}else{?>
					<div class="col-sm-12 col-lg-12 col-12 initial_row_for_brought_out_parts mb-3" id="new_delivery_addres0" data-personal_count="0" >
						<div class="row">
							<!--<div class="col-sm-1"> 
								 <label class="control-label" for="Part">Part<em>*</em></label> 
								 <? $gsi->getPartNumberList("part[]","part","100%",false,"form-control select2 part_number","required"); ?>
   							</div>-->
							<div class="col-sm-1"> 
								 <label class="control-label" for="Part Description">Description<em></em></label> 
								 <input type="text" class="form-control part description" id="part" name="part[]" value="" placeholder="Description" autocomplete="off" style="font-size:12px;" oninput="this.value=this.value.toUpperCase();"> 
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Supplier">Supplier<em></em></label> 
								 <input type="text" class="form-control supplier" id="supplier" name="supplier[]" value="" placeholder="Supplier" autocomplete="off" style="font-size:12px;" oninput="this.value=this.value.toUpperCase();"> 
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Currency">Currency<em></em></label> 
								 <input type="text" class="form-control currency" id="currency" name="currency[]" value="" placeholder="Currency" autocomplete="off" style="background-color: #FFFFE0;font-size:12px;" > 
							</div>
							<div class="col-sm-1"> 
								<label class="control-label" for="Exch Rate">Exch Rate<em></em></label> 
								<input type="text" class="form-control exch_rate" id="exch_rate" name="exch_rate[]" value="" placeholder="Exch Rate" autocomplete="off" style="background-color: #FFFFE0;">  
							</div>

							<div class="col-sm-1"> 
								 <label class="control-label" for="Price">Price<em></em></label> 
								 <input type="number" class="form-control price" id="price" name="price[]" value="" placeholder="Price" autocomplete="off" step="0.01">  
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Duty%">Duty%<em></em></label> 
								 <input type="number" class="form-control duty_percent" id="duty_percent" name="duty_percent[]" value="" placeholder="Duty%" autocomplete="off" style="background-color: #FFFFE0;"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" >  
							</div>
							<div class="col-sm-1"> 
								<label class="control-label" for="Scrap%">Scrap%<em></em></label> 
								<input type="number" class="form-control scrap_percent" id="scrap_percent" name="scrap_percent[]" 
								placeholder="Scrap%" autocomplete="off" 
								onblur="this.value = parseFloat(this.value).toFixed(2) || '';" step="0.01">

							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Handling%">Handling%<em></em></label> 
								 <input type="number" class="form-control handling_percent" id="handling_percent" name="handling_percent[]" value="" placeholder="Handling%" autocomplete="off" onblur="this.value = parseFloat(this.value).toFixed(2) || '';" step="0.01"> 
							</div>
							
							<div class="col-sm-1"> 
								 <label class="control-label" for="Total">Total<em></em></label> 
								 <input type="text" class="form-control total_b " id="total" name="total[]" value="" placeholder="Total" autocomplete="off" readonly >  
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Handling">Handling<em></em></label> 
								 <input type="text" class="form-control handling" id="handling" name="handling[]" value="" placeholder="Handling" autocomplete="off" readonly>  
							</div>
							<div class="col-sm-1"> 
								 <label class="control-label" for="Scrap">Scrap<em></em></label> 
								 <input type="text" class="form-control scrap" id="scrap" name="scrap[]" value="" placeholder="Scrap" autocomplete="off" readonly>  
							</div>
						</div>
					</div>
					<?}?>
					<div class="form-group col-sm-12 brought_out_parts_div_to_append">	
						<label class="add_one_more_brought_out_parts" style="margin-left:0%;cursor:pointer;color: #a423a4; "><span class="fa fa-plus"></span>Add More</label>
					</div>
					</div>
						
					</div>
					 
				</div><!-- /.row -->
				 <div class="row text-dark text-bold"> 
					 <div class="col-lg-8">
						<label class="control-label" for="Scrap"><em></em></label> 
					 </div>
					 <div class="col-lg-1 text-center">
						<label class="control-label" for="Scrap"><span class="text-dark text-bold">Totals :</span></label> 
					 </div>
					 <div class="col-lg-1 text-center">
						<input type="text" class="form-control" id="totals_brought" name="totals_brought" value="<?=$row_brou['totals_brought'];?>" placeholder="Totals" readonly> 
					 </div>
					 
					 <div class="col-lg-1 text-center">
						<input type="text" class="form-control handling_brought" id="handling_brought" name="handling_brought" value="<?=$row_brou['handling_brought'];?>" placeholder="Handling" readonly> 
					 </div>
					 <div class="col-lg-1 text-center">
						<input type="text" class="form-control scrap_brought" id="scrap_brought" name="scrap_brought" value="<?=$row_brou['scrap_brought'];?>" placeholder="Scrap" readonly> 
					 </div>
				  </div><!-- /.row -->
				  <div class="row mt-2 text-dark text-bold"> 
					 <div class="col-lg-10">
						<label class="control-label" for="Scrap"><em></em></label> 
					 </div>
					 <div class="col-lg-1 text-center">
						<label class="control-label" for="Scrap"><span id="average">Average :</span></label> 
					 </div>
					 <div class="col-lg-1 text-center">
						<input type="text" class="form-control average_brought" id="average_brought" name="average_brought" value="<?=$row_brou['average_brought'];?>" placeholder="Average" readonly> 
					 </div>
				  </div><!-- /.row -->
                 <div class="card-footer mt-1">
					<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" name="section_status" value="">
					<button type="button" id="submit_incomplete" class="btn btn-danger" onclick="submitFormWithStatus(1, 'spinner_incomplete')">
						Save Incomplete
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="spinner_incomplete"></i>
					</button>
					<button type="button" id="submit_completed" class="btn btn-success" onclick="submitFormWithStatus(2, 'spinner_completed')">
						Section Completed
						<i class="fas fa-sync-alt fa-spin" style="display:none;" id="spinner_completed"></i>
					</button>

					<div style="float:right;">
						<button type="button" id="nextButton" class="btn btn-secondary" onclick="ToNextTab()">Next</button>

                   </div>
				</div>
                </form>
</section>
<a href="" data-toggle="modal" data-target="#myModal"
									class="btn btn-primary add_new_station hide" alt=""
									data-station_name=""
									style="float:right;margin-right:35px;margin-bottom:5px;">Add New Part</a>
<script type="text/javascript">
	function calculateTotal(row) {
    var price = parseFloat(row.find('.price').val()) || 0;
    var number = parseFloat(row.find('.number').val()) || 1; 
    var duty = parseFloat(row.find('.duty_percent').val()) || 0;
    var scrap = parseFloat(row.find('.scrap_percent').val()) || 0;
    var handling = parseFloat(row.find('.handling_percent').val()) || 0;
    
    var finalTotal = (price * number) * (1 + (scrap / 100)) * (1 + (handling / 100));
    
    var handlingAmount = (price / number) * (scrap / 100);
    var scrapAmount = (finalTotal - (number * price)) - handlingAmount;

    row.find('.total_b').val(finalTotal.toFixed(2));
    row.find('.handling').val(handlingAmount.toFixed(2));
    row.find('.scrap').val(scrapAmount.toFixed(2));
}

	function calculateGrandTotal() {
		let total = 0;
		$('.total_b').each(function() {
			let value = parseFloat($(this).val()) ||0;
			//alert(value);
			if (!isNaN(value)) {
				total += value;
			}
		});
		console.log(`Calculated total: ${total}`); 
		$('#totals_brought').val(total.toFixed(2)); 
	}

        function calculateScrapTotal() {
			let scrap = 0;
			$('.scrap').each(function() {
				let value = parseFloat($(this).val());
				if (!isNaN(value)) {
					scrap += value;
				}
			});
			$('#scrap_brought').val(scrap.toFixed(2));
		}
		function calculateHandlingTotal() {
			let handling = 0;
			$('.handling').each(function() {
				let value = parseFloat($(this).val());
				if (!isNaN(value)) {
					handling += value;
				}
			});
			$('#handling_brought').val(handling.toFixed(2));
		}
        function calculateAverageTotal() {
			let handlingTotal = parseFloat($('#handling_brought').val()) || 0;
			let scrapTotal = parseFloat($('#scrap_brought').val()) || 0;
			
			let average = (handlingTotal + scrapTotal) / 2;
			
			$('#average_brought').val(average.toFixed(2));
		 }

		$('#broughtForm').on('change', '.price, .number, .scrap_percent, .handling_percent', function () {
			var row = $(this).closest('.row');  
			calculateTotal(row);
			calculateGrandTotal();
			calculateScrapTotal();
			calculateHandlingTotal();
			calculateAverageTotal();
		});


/* by default currency */
        $(document).ready(function(){
         $("#myModal .close_button2").click(function(){
				location.reload();
		  });	
      $('#broughtForm').on('change', '.description', function () {
			var row = $(this).closest('.row');  
			//alert(row);
			pulDefultCurrency(row);
			  var part_id = $(this).val();
			  if(part_id =='-1'){
			    $('.add_new_station').click(); 
				$("#myModalLabel").html("Add New Part");
				var hide_top_left = "&hide_top=1&hide_left=1";
				var from = "&from=add_new_item_progress";
				var client_id = 'test';
				$('.modal-dialog').css({"max-width":"60%"});
				$(".modal_iframe").attr("src","<?=$config->frontEndURL?>/admin/item_management/index?page=add&client_id="+client_id+hide_top_left+from);
				 
			  }
			
		});
		});
		
		function pulDefultCurrency(row) {
			var currency = 'Pounds Sterling';
			var exchRate = '1';
			var duty ='0.0000';

			row.find('.currency').val(currency);
			row.find('.exch_rate').val(exchRate);
			row.find('.duty_percent').val(duty);
		}
		function submitFormWithStatus(status, spinnerId) {
			document.getElementById('section_status').value = status;
			$("#submit_incomplete").attr("disabled", true);
			$("#submit_completed").attr("disabled", true);
			document.querySelectorAll('.fa-spin').forEach(function(spinner) {
				spinner.style.display = 'none';
			});
			document.getElementById(spinnerId).style.display = 'inline-block';

			document.getElementById('broughtForm').submit();
		}
   $(function(){

	$('.add_one_more_brought_out_parts').click(function() {		
		$('#brought_out_parts_div_to_clone').show();		
		var div_count = $('.initial_row_for_brought_out_parts').last().attr("data-personal_count");
		div_count++;
		var cloned_div = $('#brought_out_parts_div_to_clone').clone().addClass("initial_row_for_brought_out_parts").attr("id","new_delivery_addres"+div_count).attr("data-personal_count",div_count);		
		$('.brought_out_parts_div_to_append').before(cloned_div);		
		$('#brought_out_parts_div_to_clone').hide();
        $('#new_delivery_addres'+div_count).children().find(".part_number").attr("id","part"+div_count).attr("alt",div_count).select2();		
	});
	
	
		$("#broughtForm").on("click", ".delete_new_brought_out_parts",function() {
	    if(confirm('Are you sure you want to delete?')){		
			if($(this).attr("alt") != ""){
				var brought_part_mapping_id = $(this).attr("alt");
				$('#load_new_brought_out_parts'+brought_part_mapping_id).show();
				var form_data   = {'brought_part_mapping_id':brought_part_mapping_id};      
                //alert(brought_part_mapping_id);				
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/quotation_management/index.php?page=ajax_actions&action=delete_brought_part_mapping",
					//url: site_url+"/client_management/index?page=ajax_actions&action=delete_brought_part_mapping",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#status_update_personal_done_'+brought_part_mapping_id).show();
							$('#load_new_brought_out_parts'+brought_part_mapping_id).hide();
							
							setTimeout(function() {
								$('#status_update_personal_done_'+brought_part_mapping_id).hide();
								$('#new_delivery_addres'+brought_part_mapping_id).remove();
								//location.reload();
							},1000);
						}
					}
				});
			}else{
				$(this).parent().parent().parent().parent().remove();
			}
		}
	});

	
});
</script>