<section class="content">
	<form class="" method="post" name="otherForm" id="otherForm" action="index?page=view_quotation_progress&action=insert_other" 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">
					<?php 
					    $grade_other="LOW VOLUME SET UPS";
						$grade_general_overheads="17.5% Of Facility Overhead Rates";
						$grade_profit="10.0 % of Mat, proc & gen overhead, not inc scrap";
						$rate_to_use_general_overheads=17.5;
					    $rate_to_use_profit=10.0;
					    $query_operation = "SELECT * FROM quot_operations WHERE quot_id='".$_REQUEST['id']."' ";
						$sql_operation=mysqli_query($conn,$query_operation);
						$result_operation=mysqli_num_rows($sql_operation);
						$total_overhead_value=0;		
						$total_scrap_value=0;
						$total_total=0;
						while($row_operation = mysqli_fetch_array($sql_operation)){ 
							$row_operation=utility::validate_crypt_data($row_operation);
							$total_overhead_value += $row_operation['overhead_value'];
							$total_scrap_value += $row_operation['scrap_value'];
							$total_total += $row_operation['total'];
						}
						$query_raw_materials = "SELECT * FROM quot_raw_materials WHERE quot_id ='".$qot_id."' ";
						$sql_raw_materials=mysqli_query($conn,$query_raw_materials);
						$result_raw_materials=mysqli_num_rows($sql_raw_materials);
												$total_unit_cost=0;
								 $total_raw_scrap_value=0;
								while($row_raw_materials =mysqli_fetch_array($sql_raw_materials)){ 
									$row_raw_materials=utility::validate_crypt_data($row_raw_materials);
									 $total_unit_cost += $row_raw_materials['unit_cost'];
									 $total_raw_scrap_value += $row_raw_materials['raw_scrap_value'];
								}
						$other_query = "SELECT * FROM quot_other WHERE quot_id='".$_REQUEST['id']."' ";
						$other_sql=mysqli_query($conn,$other_query);
						$other_result=mysqli_num_rows($other_sql);
						while($mapp_row = mysqli_fetch_array($other_sql)){
							$grade_general_overheads 			= $mapp_row['grade_general_overheads'];
							$rate_to_use_general_overheads	    = $mapp_row['rate_to_use_general_overheads'];							
							$cost_general_overheads 			= $mapp_row['cost_general_overheads'];
							$grade_other 						= $mapp_row['grade_other'];
							$rate_to_use_other 					= $mapp_row['rate_to_use_other'];
							$cost_other 						= $mapp_row['cost_other'];
							$grade_profit					    = $mapp_row['grade_profit'];
							$rate_to_use_profit				    = $mapp_row['rate_to_use_profit'];
							$cost_profit 						= $mapp_row['cost_profit'];
							$quot_other_id 						= $mapp_row['id'];
						}
					?>
					<div class="row">
						<div class="col-sm-2"> 
							<br>
							<label class="control-label" for="grade">General Overheads<em></em></label> 
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="grade">Grade<em></em></label> 
							<input type="text" class="form-control" id="grade_general_overheads" name="grade_general_overheads" value="<?php echo $grade_general_overheads;?>" placeholder="Please enter" readonly>
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="Enter Rate To Use">Enter Rate To Use<em></em></label> 
							<input type="number" class="form-control" id="rate_to_use_general_overheads" name="rate_to_use_general_overheads" value="<?php echo $rate_to_use_general_overheads;?>" placeholder="Please enter" step="0.01" >
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="cost">Cost<em></em></label> 
							<input type="text" class="form-control" id="cost_general_overheads" name="cost_general_overheads" value="<?php echo $cost_general_overheads;?>" placeholder="Please enter" readonly oninput="this.value=this.value.toUpperCase();">
						</div>
					</div>	
					<div class="row">
						<div class="col-sm-2"> 
							<br>
							<label class="control-label" for="grade">Other<em></em></label> 
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="grade"><em></em></label> 
							<input type="text" class="form-control" id="grade_other" name="grade_other" value="<?php echo $grade_other;?>" placeholder="Please enter" oninput="this.value=this.value.toUpperCase();">
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="Enter Rate To Use"><em></em></label> 
							<input type="text" class="form-control" id="rate_to_use_other" name="rate_to_use_other" value="<?php echo $rate_to_use_other;?>" placeholder="Please enter" oninput="this.value=this.value.toUpperCase();">
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="cost"><em></em></label> 
							<input type="text" class="form-control" id="cost_other" name="cost_other" value="<?php echo $cost_other;?>" placeholder="Please enter" oninput="this.value=this.value.toUpperCase();">
						</div>
					</div>
					<div class="row">
						<div class="col-sm-2"> 
							<br>
							<label class="control-label" for="grade">Profit<em></em></label> 
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="grade"><em></em></label> 
							<input type="text" class="form-control" id="grade_profit" name="grade_profit" value="<?php echo $grade_profit;?>" placeholder="Please enter" readonly oninput="this.value=this.value.toUpperCase();">
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="Enter Rate To Use"><em></em></label> 
							<input type="number" class="form-control" id="rate_to_use_profit" name="rate_to_use_profit" value="<?php echo $rate_to_use_profit;?>" placeholder="Please enter" step="0.01"oninput="this.value=this.value.toUpperCase();">
						</div>
						<div class="col-sm-3"> 
							<label class="control-label" for="cost"><em></em></label> 
							<input type="text" class="form-control" id="cost_profit" name="cost_profit" value="<?php echo $cost_profit;?>" placeholder="Please enter" readonly oninput="this.value=this.value.toUpperCase();">
						</div>
					</div>
					<div class="my-4"></div>
				</div>
			</div>
		</div>
		<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="quot_other_id" name="quot_other_id" value="<?php echo $quot_other_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_other" name="section_status_other" value="">
			<button type="button" id="submit_incomplete" class="btn btn-danger" onclick="submitFormOtherStatus(1,'spinner_other_incomplete')">
				Save Incomplete
				<i class="fas fa-sync-alt fa-spin" style="display:none;" id="spinner_other_incomplete"></i>
			</button>
			<button type="button" id="submit_completed" class="btn btn-success" onclick="submitFormOtherStatus(2, 'spinner_other_completed')">
				Section Completed
				<i class="fas fa-sync-alt fa-spin" style="display:none;" id="spinner_other_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 submitFormOtherStatus(status, spinnerId) {
    document.getElementById('section_status_other').value = status;

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

    const totalOverheadValue = <?php echo $total_overhead_value; ?>;
    const totalUnitCost = <?php echo $total_unit_cost; ?>;
    const totalRawScrapValue = <?php echo $total_raw_scrap_value; ?>;
    const totalTotal = <?php echo $total_total; ?>;
    const totalScrapValue = <?php echo $total_scrap_value; ?>;

    function recalculateCostProfit() {
        let rateToUseProfit = parseFloat($('#rate_to_use_profit').val()) || 0;
        let costGeneralOverheads = parseFloat($('#cost_general_overheads').val()) || 0;

        let baseProfitCalculation = totalUnitCost - totalRawScrapValue + totalTotal - totalScrapValue + costGeneralOverheads;
        let calculatedProfit = baseProfitCalculation * (rateToUseProfit / 100);

        $('#cost_profit').val(calculatedProfit.toFixed(5));
    }

    function recalculateGeneralOverheads(){
        let rateToUse = parseFloat($('#rate_to_use_general_overheads').val()) || 0;
        let calculatedCost = totalOverheadValue * (rateToUse / 100);

        $('#cost_general_overheads').val(calculatedCost.toFixed(5));
        $('#grade_general_overheads').val(rateToUse + '% Of Facility Overhead Rates');

        recalculateCostProfit();
    }

    $('#rate_to_use_general_overheads').on('input', recalculateGeneralOverheads);

    $('#rate_to_use_profit').on('input', function(){
        let costValue = parseFloat($(this).val()) || 0;
        $('#grade_profit').val(costValue + '% of Mat, proc & gen overhead, not inc scrap');
        recalculateCostProfit();
    });

    $('#cost_general_overheads').on('input', recalculateCostProfit);

    // ✅ Run once when page loads
    recalculateGeneralOverheads();
    recalculateCostProfit();
});
</script>

