<?php
session_start();
$where 					= " WHERE 1=1 AND id = '".$_REQUEST['id']."'";
$next_page_condition 	= "";
$paging ="";
################################################## 

$qot_id = utility::app_crypt($_REQUEST['qot_id'],'d');
if($_REQUEST['id'] != $qot_id){
	die("Invaild URL");
}


$anuual_value=1;
$query_quotation = "SELECT * FROM quotation WHERE id ='".$qot_id."' ";
$sql_quotation= mysqli_query($conn,$query_quotation);
$result_brought_total= mysqli_num_rows($sql_quotation);
$row_quotation = mysqli_fetch_assoc($sql_quotation);
$row_quotation = utility::validate_crypt_data($row_quotation);
$volume =  $row_quotation['volume'];
																		
$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;
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'];
}

$query_operation = "SELECT * FROM quot_operations WHERE quot_id ='".$qot_id."' ";
$sql_operation=mysqli_query($conn,$query_operation);
$result_operation=mysqli_num_rows($sql_operation);
$total_total_process=0;
while($row_operation =mysqli_fetch_array($sql_operation)){ 
	$row_operation=utility::validate_crypt_data($row_operation);
	 $total_total_process += $row_operation['total'];
}

$query_brought_total = "SELECT * FROM quot_brought_total WHERE quot_id ='".$qot_id."' ";
$sql_brought_total= mysqli_query($conn,$query_brought_total);
$result_brought_total= mysqli_num_rows($sql_brought_total);
$row_brought_total = mysqli_fetch_assoc($sql_brought_total);
$row_brought_total = utility::validate_crypt_data($row_brought_total);
$totals_brought =  $row_brought_total['totals_brought'];


$total_subtotal_other=0;
$query_other = "SELECT * FROM quot_other WHERE quot_id ='".$qot_id."' ";
$sql_other= mysqli_query($conn,$query_other);
$result_other= mysqli_num_rows($sql_other);
$row_other = mysqli_fetch_assoc($sql_other);
$row_other = utility::validate_crypt_data($row_other);
$cost_general_overheads =  $row_other['cost_general_overheads']; 
$cost_profit =  $row_other['cost_profit'];
$cost_other = $row_other['cost_other'];
$total_subtotal_other= ($cost_general_overheads + $cost_other + $cost_profit);


$total_feas_manu_cost= ($total_unit_cost+$total_total_process+$total_subtotal_other+$totals_brought);

$total_logistic_cost=0;
$sql_log= "SELECT * FROM quot_logistics WHERE quot_id ='".$qot_id."' ";
$result_log = mysqli_query($conn,$sql_log);
$row_log = mysqli_fetch_array($result_log);
$row_log = utility::validate_crypt_data($row_log);
$pcost  = $row_log['pcost'];
$pacost  = $row_log['pacost'];
$othcost  = $row_log['othcost'];
$palcost  = $row_log['palcost'];

$total_logistic_cost= ($pcost+$pacost+$palcost+$othcost);

$each_cost  =$total_logistic_cost + $total_feas_manu_cost ; 
$each_cost = (float) $each_cost;
$volume = (float) $volume;

$anuual_value = $each_cost * $volume;
$quotation_annual_update_sql = "UPDATE quotation SET anuual_value = '$anuual_value' WHERE id ='".$qot_id."' ";
mysqli_query($conn, $quotation_annual_update_sql);

$complete ='complete';
if($_REQUEST['red']==0){
	$active_show = 'active';
	$active_show_content = 'active show';
}
if($_REQUEST['red']==1){
     
	 $active_show_1 = 'active';
	 $active_show_content_1 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-profile\"]').tab('show');
		});
	  </script>
<?}
 if($_REQUEST['red']==2){
	  $active_show_2 = 'active';
	 $active_show_content_2 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?} 
if($_REQUEST['red']==3){
	  $active_show_3 = 'active';
	 $active_show_content_3 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?}
if($_REQUEST['red']==4){
	  $active_show_4 = 'active';
	 $active_show_content_4 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?} 
if($_REQUEST['red']==5){
	  $active_show_5 = 'active';
	  $active_show_content_5 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?} 
if($_REQUEST['red']==6){
	  $active_show_6 = 'active';
	 $active_show_content_6 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?}  
if($_REQUEST['red']==7){
	  $active_show_7 = 'active';
	 $active_show_content_7 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?} 
if($_REQUEST['red']==8){
	  $active_show_8 = 'active';
	 $active_show_content_8 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?} 
if($_REQUEST['red']==9){
	  $active_show_9 = 'active';
	 $active_show_content_9 = 'active show';?>
	 <script>
		$(document).ready(function() {
			$('#fact_details_tab a[href=\"#custom-tabs-four-settings7\"]').tab('show');
		});
	  </script>
<?} 
################################################## START INSERT  Brought Part##################################################
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_brought_out') {
    utility::addslashesOnAllInput(array("query_string"));
	
    $quot_id				      = $_REQUEST['quot_id'];
    $totals_brought				  = $_REQUEST['totals_brought'];
	$handling_brought			  = $_REQUEST['handling_brought'];
	$scrap_brought				  = $_REQUEST['scrap_brought'];
	$average_brought			  = $_REQUEST['average_brought'];
	$created_by					  = $_SESSION['user_id'];
	$created_on					  = date("Y-m-d H:i:s");	
	$updated_by					  = $_SESSION['user_id'];
	
	mysqli_query($conn,"DELETE FROM quot_brought_total WHERE quot_id = '".$quot_id."' ");
	$sql = "quot_id = '".$quot_id."',average_brought = '".$average_brought."',scrap_brought = '".$scrap_brought."', totals_brought = '".$totals_brought."', handling_brought = '".$handling_brought."', updated_on = '".$updated_on."'";
	
	$sql= "INSERT INTO quot_brought_total SET ".$sql;
	mysqli_query($conn,$sql);

    if (count($_REQUEST['part']) > 0) {  
        $section_status = $_REQUEST['section_status'];

        foreach ($_REQUEST['part'] as $key => $part) {
                	$quot_id				= $_REQUEST['quot_id'];
					$supplier				= $_REQUEST['supplier'][$key];
					$currency				= $_REQUEST['currency'][$key];
					$exch_rate				= $_REQUEST['exch_rate'][$key];	
					$price			        = $_REQUEST['price'][$key];	
					$duty_percent			= $_REQUEST['duty_percent'][$key];	
					$number				    = $_REQUEST['number'][$key];
					$scrap_percent			= $_REQUEST['scrap_percent'][$key];
					$handling_percent		= $_REQUEST['handling_percent'][$key];
					$total				    = $_REQUEST['total'][$key];
					$handling				= $_REQUEST['handling'][$key];
					$scrap				    = $_REQUEST['scrap'][$key];
					$created_on             = date("Y-m-d H:i:s");
					$created_by             = $_SESSION["user_id"];		
					$updated_by_by          = $_SESSION["user_id"];		
					$brought_part_mapping_id 			= $_REQUEST['brought_part_mapping_id'][$key];	

            /* if (empty($part) || empty($supplier) || empty($currency) || empty($exch_rate) || empty($price) || 
                empty($duty_percent) || empty($number) || empty($scrap_percent) || empty($handling_percent) || 
                empty($total) || empty($handling) || empty($scrap)) {
                $status = 1; // Incomplete status
            }
 */
            if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM quot_brought WHERE id ='$brought_part_mapping_id' ")) > 0) {
                $sql1 = "UPDATE quot_brought SET part = '$part', supplier = '$supplier', currency = '$currency', exch_rate = '$exch_rate',price = '$price', duty_percent = '$duty_percent', number = '$number', scrap_percent = '$scrap_percent',handling_percent = '$handling_percent', total = '$total', handling = '$handling', updated_by = '$updated_by'WHERE id = '$brought_part_mapping_id'";
            } else {
                $sql1 = "INSERT INTO quot_brought(quot_id, part, supplier, currency, exch_rate, price, duty_percent, number, scrap_percent, handling_percent,total, handling, scrap, created_by, created_on, updated_by)VALUES ('$quot_id','$part', '$supplier', '$currency', '$exch_rate', '$price', '$duty_percent', '$number','$scrap_percent', '$handling_percent', '$total', '$handling', '$scrap', '$created_by', '$created_on', '$updated_by')";
            }
            mysqli_query($conn, $sql1);
        }

        // Update the status of the quotation based on completeness
        $status_sql = "UPDATE quot_brought SET status = '$section_status' WHERE quot_id = '$quot_id'";
        mysqli_query($conn, $status_sql);
    }

    $quot_ide = urlencode(utility::app_crypt($quot_id, 'e'));
    $_SESSION['info_message'] = "Brought Out Part added successfully";
    header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=1");
    exit();
}
    $sql_brou= "SELECT * FROM quot_brought_total WHERE quot_id = '".$_REQUEST['id']."'";
    $result_brou = mysqli_query($conn,$sql_brou);
    $row_brou = mysqli_fetch_array($result_brou);
	$row_brou = utility::validate_crypt_data($row_brou);

################################################# END INSERT Brought Part #################################################
############################################## FOR RAW MATERIALS ##########################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_raw_materials'){	
utility::addslashesOnAllInput(array("query_string"));
$quot_id              = $_REQUEST['quot_id'];
    if (count($_REQUEST['type_id']) > 0) {
		
        $section_status = $_REQUEST['section_status_raw'];
		foreach ($_REQUEST['type_id'] as $key => $type_id) {
				$quot_id              = $_REQUEST['quot_id'];
	         	$description          = $_REQUEST['description'][$key];
				$spec                 = $_REQUEST['spec'][$key];
				$grade                = $_REQUEST['grade'][$key];
				$format               = $_REQUEST['format'][$key];
				$density              = $_REQUEST['density'][$key];
				$supplier             = $_REQUEST['supplier'][$key];
				$mpe                  = $_REQUEST['mpe'][$key];
				$length               = $_REQUEST['length'][$key];
				$width                = $_REQUEST['width'][$key];
				$thickness            = $_REQUEST['thickness'][$key];
				$mks                  = $_REQUEST['mks'][$key];
				$gross                = $_REQUEST['gross'][$key];
				$nett                 = $_REQUEST['nett'][$key];
				$scrapPercent         = $_REQUEST['scrap_percent'][$key];
				$uom                  = $_REQUEST['uom'][$key];				
				$price_uom            = $_REQUEST['price_uom'][$key];
				$unitCost             = $_REQUEST['unit_cost'][$key];
				$raw_scrap_value             = $_REQUEST['raw_scrap_value'][$key];

				$createdOn            = date("Y-m-d H:i:s");
				$createdBy            = $_SESSION["user_id"];
				$updatedBy            = $_SESSION["user_id"];
				$quotRawMaterialsId = $_REQUEST['raw_material_mapping_id'][$key];

				/*  if (empty($type_id) || empty($description) || empty($spec) || empty($grade) || empty($format) ||empty($density) || empty($supplier) || empty($mpe) || empty($length) || empty($width) || 
					empty($thickness) || empty($mks) || empty($gross)|| empty($nett)|| empty($scrapPercent)|| empty($uom)||empty($unitCost)) {
					$status = 1; // Incomplete status
				} */
				if(mysqli_num_rows(mysqli_query($conn,"SELECT * FROM quot_raw_materials WHERE id = '$quotRawMaterialsId' ")) > 0){
					$sql1 = "UPDATE quot_raw_materials SET type_id = '$type_id', description = '$description', spec = '$spec',grade = '$grade', format = '$format', density = '$density', supplier = '$supplier',mpe = '$mpe', length = '$length', width = '$width', thickness = '$thickness',mks = '$mks', gross = '$gross', nett = '$nett', scrap_percent = '$scrapPercent',uom = '$uom',price_uom = '$price_uom', unit_cost = '$unitCost', raw_scrap_value = '$raw_scrap_value', updated_by = '$updatedBy' WHERE id = '$quotRawMaterialsId'";
					mysqli_query($conn, $sql1);
				} else {
					$sql1 = "INSERT INTO quot_raw_materials (quot_id, type_id, description, spec, grade, format,density, supplier, mpe, length, width, thickness, mks, gross, nett, scrap_percent,uom, price_uom, unit_cost, raw_scrap_value, created_by, created_on, updated_by) VALUES ('$quot_id','$type_id', '$description', '$spec', '$grade', '$format', '$density', '$supplier','$mpe', '$length', '$width', '$thickness', '$mks', '$gross', '$nett', '$scrapPercent','$uom', '$price_uom', '$unitCost', '$raw_scrap_value', '$createdBy', '$createdOn', '$updatedBy')";

					mysqli_query($conn, $sql1);
				}

			   // Update the status of the quotation based on completeness
				$status_sql = "UPDATE quot_raw_materials SET status = '$section_status' WHERE quot_id = '$quot_id'";
				mysqli_query($conn, $status_sql);
			
			}
			
		}
		
		
		$query_operation = "SELECT * FROM quot_operations WHERE quot_id = '$quot_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 = '$quot_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 = '$quot_id'";
		
		$other_sql = mysqli_query($conn, $other_query);

		if (mysqli_num_rows($other_sql) > 0) {
			$other_result=mysqli_num_rows($other_sql);
			while($mapp_row = mysqli_fetch_array($other_sql)){
			
				$rate_to_use_general_overheads	    = $mapp_row['rate_to_use_general_overheads'];						
				$cost_general_overheads 			= $mapp_row['cost_general_overheads'];
				$rate_to_use_profit				    = $mapp_row['rate_to_use_profit'];
				$cost_profit 						= $mapp_row['cost_profit'];
			}
		  
		   $cost_general_overheads = $total_overhead_value * ($rate_to_use_general_overheads / 100);
		   $baseProfitCalculation = $total_unit_cost - $total_raw_scrap_value + $total_total - $total_scrap_value + $cost_general_overheads;
		   $cost_profit = $baseProfitCalculation * ($rate_to_use_profit / 100);

		   $status_sql = "UPDATE quot_other SET cost_profit = '$cost_profit', cost_general_overheads = '$cost_general_overheads'  WHERE quot_id = '$quot_id'";
					mysqli_query($conn, $status_sql);
		   
		} else {
			
			
		}
						
			
	$quot_ide = urlencode(utility::app_crypt($quot_id,'e'));
	$_SESSION['info_message'] = "Raw Materials added successfully";
	 header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=2");
    exit();
}	
###################################################################################################################		
############################################### FOR Operations ##########################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_operations'){	
utility::addslashesOnAllInput(array("query_string"));
  $quot_id                    = $_REQUEST['quot_id'];   
	if(count($_REQUEST['operation']) > 0){	 
     $section_status = $_REQUEST['section_status_operation'];
		foreach($_REQUEST['operation'] as $key => $operation){
			$quot_id                    = $_REQUEST['quot_id'];
			$work_station_id            = $_REQUEST['work_station_id'][$key];
			$work_station_description   = $_REQUEST['work_station_description'][$key];
			$w_hrs                      = $_REQUEST['w_hrs'][$key];	
			$operator                   = $_REQUEST['operator'][$key];	
			$labour_rate                = $_REQUEST['labour_rate'][$key];	
			$fac_rate                   = $_REQUEST['fac_rate'][$key];
			$scrap_percent              = $_REQUEST['scrap_percent'][$key];
			$units_per_hour             = $_REQUEST['units_per_hour'][$key];
			$total                      = $_REQUEST['total'][$key];
			$per_scrap                  = $_REQUEST['per_scrap'][$key];
			$scrap_value                = $_REQUEST['scrap_value'][$key];
			$overhead_value             = $_REQUEST['overhead_value'][$key];
			$created_on                 = date("Y-m-d H:i:s");
			$created_by                 = $_SESSION["user_id"];		
			$updated_by                 = $_SESSION["user_id"];		
			$operation_mapping_id   	 = $_REQUEST['operation_mapping_id'][$key];		
			
			if (empty($work_station_id) || empty($work_station_description) || empty($w_hrs) || empty($operator) || empty($labour_rate) ||empty($fac_rate) || empty($scrap_percent) || empty($units_per_hour) || empty($total) || empty($per_scrap) || 
					empty($scrap_value) || empty($overhead_value)) {
					$status = 1; // Incomplete status
				}

			if(mysqli_num_rows(mysqli_query($conn, "SELECT * FROM quot_operations WHERE id = '$operation_mapping_id' ")) > 0){
				$sql1 = "UPDATE quot_operations SET operation = '$operation', work_station_id = '$work_station_id', work_station_description = '$work_station_description', w_hrs = '$w_hrs', operator = '$operator', labour_rate = '$labour_rate', fac_rate = '$fac_rate', scrap_percent = '$scrap_percent', units_per_hour = '$units_per_hour', total = '$total', per_scrap = '$per_scrap', scrap_value = '$scrap_value', overhead_value = '$overhead_value', updated_by = '$updated_by' WHERE id = '$operation_mapping_id'";
			} else {
				echo $sql1 = "INSERT INTO quot_operations (quot_id, operation, work_station_id, work_station_description, w_hrs, operator, labour_rate, fac_rate, scrap_percent, units_per_hour, total, per_scrap, scrap_value, overhead_value, created_by, created_on, updated_by) VALUES ('$quot_id', '$operation', '$work_station_id', '$work_station_description', '$w_hrs', '$operator', '$labour_rate', '$fac_rate', '$scrap_percent', '$units_per_hour', '$total', '$per_scrap', '$scrap_value', '$overhead_value', '$created_by', '$created_on', '$updated_by')";
				
			}

			mysqli_query($conn, $sql1);
			
		}
		        // Update the status of the quotation based on completeness
        $status_sql = "UPDATE quot_operations SET status = '$section_status' WHERE quot_id = '$quot_id'";
        mysqli_query($conn, $status_sql);
	}
		$query_operation = "SELECT * FROM quot_operations WHERE quot_id = '$quot_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 = '$quot_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 = '$quot_id'";
		
		$other_sql = mysqli_query($conn, $other_query);

		if (mysqli_num_rows($other_sql) > 0) {
			$other_result=mysqli_num_rows($other_sql);
			while($mapp_row = mysqli_fetch_array($other_sql)){
			
				$rate_to_use_general_overheads	    = $mapp_row['rate_to_use_general_overheads'];						
				$cost_general_overheads 			= $mapp_row['cost_general_overheads'];
				$rate_to_use_profit				    = $mapp_row['rate_to_use_profit'];
				$cost_profit 						= $mapp_row['cost_profit'];
			}
		   
		   $cost_general_overheads = $total_overhead_value * ($rate_to_use_general_overheads / 100);
		   $baseProfitCalculation = $total_unit_cost - $total_raw_scrap_value + $total_total - $total_scrap_value + $cost_general_overheads;
		   $cost_profit = $baseProfitCalculation * ($rate_to_use_profit / 100);

		   $status_sql = "UPDATE quot_other SET cost_profit = '$cost_profit', cost_general_overheads = '$cost_general_overheads'  WHERE quot_id = '$quot_id'";
				mysqli_query($conn, $status_sql);
		   
		} else {
			
		}
				
	$quot_ide = urlencode(utility::app_crypt($quot_id,'e'));
	$_SESSION['info_message'] = "Operations added successfully";
	 header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=3");
    exit();
}	
###################################################################################################################	
############################################### FOR Other ##########################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_other'){	
            utility::addslashesOnAllInput(array("query_string"));
			 
		    	$section_status 					= $_REQUEST['section_status_other'];
				$quot_id      				        = $_REQUEST['quot_id'];
				$grade_general_overheads 			= $_REQUEST['grade_general_overheads'];
				$rate_to_use_general_overheads	    = $_REQUEST['rate_to_use_general_overheads'];							
				$cost_general_overheads 			= $_REQUEST['cost_general_overheads'];
				$grade_other 						= $_REQUEST['grade_other'];
				$rate_to_use_other 					= $_REQUEST['rate_to_use_other'];
				$cost_other 						= $_REQUEST['cost_other'];
				$grade_profit					    = $_REQUEST['grade_profit'];
				$rate_to_use_profit				    = $_REQUEST['rate_to_use_profit'];
				$cost_profit 						= $_REQUEST['cost_profit'];
				
				$created_on         = date("Y-m-d H:i:s");
				$created_by         = $_SESSION["user_id"];
				$updated_by         = $_SESSION["user_id"];
				$quot_other_id      = $_REQUEST['quot_other_id'];

				if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM quot_other WHERE id ='$quot_other_id'")) > 0) {
					
					
					$sql1 = "UPDATE quot_other SET grade_general_overheads = '$grade_general_overheads', rate_to_use_general_overheads = '$rate_to_use_general_overheads', cost_general_overheads = '$cost_general_overheads', grade_other = '$grade_other', rate_to_use_other = '$rate_to_use_other', cost_other = '$cost_other', grade_profit = '$grade_profit', rate_to_use_profit = '$rate_to_use_profit', cost_profit = '$cost_profit', updated_by = '$updated_by' WHERE id = '$quot_other_id'";

				} else {
					 $sql1 = "INSERT INTO quot_other(quot_id, grade_general_overheads, rate_to_use_general_overheads, cost_general_overheads, grade_other, rate_to_use_other, cost_other, grade_profit, rate_to_use_profit, cost_profit, created_by, created_on, updated_by) VALUES ('$quot_id', '$grade_general_overheads', '$rate_to_use_general_overheads', '$cost_general_overheads', '$grade_other', '$rate_to_use_other', '$cost_other', '$grade_profit', '$rate_to_use_profit', '$cost_profit', '$created_by', '$created_on', '$updated_by')";
					
				}
				mysqli_query($conn, $sql1);
			

		$status_sql = "UPDATE quot_other SET status = '$section_status' WHERE quot_id = '$quot_id'";
        mysqli_query($conn, $status_sql);
			
		

	$quot_ide = urlencode(utility::app_crypt($quot_id,'e'));
	$_SESSION['info_message'] = "Other added successfully";
	 header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=4");
    exit();
}
###################################################################################################################		
############################################### FOR Tooling ##########################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_tooling'){	
utility::addslashesOnAllInput(array("query_string"));
			 
		if (count($_REQUEST['tool_description']) > 0) {
			$section_status = $_REQUEST['section_status_tool'];

			foreach ($_REQUEST['tool_description'] as $key => $tool_description) {
				$quot_id            = $_REQUEST['quot_id'];
				$supplier           = $_REQUEST['supplier'][$key];
				$lead_time          = $_REQUEST['lead_time'][$key];
				$cost               = $_REQUEST['cost'][$key];
				$number             = $_REQUEST['number'][$key];
				$other              = $_REQUEST['other'][$key];
				$parts_per_tool     = $_REQUEST['parts_per_tool'][$key];
				$tool_life          = $_REQUEST['tool_life'][$key];
				$total_cost         = $_REQUEST['total_cost'][$key];
				$created_on         = date("Y-m-d H:i:s");
				$created_by         = $_SESSION["user_id"];
				$updated_by         = $_SESSION["user_id"];
				$tooling_mapping_id = $_REQUEST['tooling_mapping_id'][$key];

				if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM quot_tooling WHERE id ='$tooling_mapping_id'")) > 0) {
					$sql1 = "UPDATE quot_tooling SET tool_description = '$tool_description', supplier = '$supplier', lead_time = '$lead_time', cost = '$cost', number = '$number', other = '$other', parts_per_tool = '$parts_per_tool', tool_life = '$tool_life', total_cost = '$total_cost', updated_by = '$updated_by' WHERE id = '$tooling_mapping_id'";

				} else {
					$sql1 = "INSERT INTO quot_tooling(quot_id, tool_description, supplier, lead_time, cost, number, other, parts_per_tool, tool_life, total_cost, created_by, created_on, updated_by) VALUES ('$quot_id', '$tool_description', '$supplier', '$lead_time', '$cost', '$number', '$other', '$parts_per_tool', '$tool_life', '$total_cost', '$created_by', '$created_on', '$updated_by')";
				}
				mysqli_query($conn, $sql1);
			}

		
			$status_sql = "UPDATE quot_tooling SET status = '$section_status' WHERE quot_id = '$quot_id'";
			mysqli_query($conn, $status_sql);
		}

	$quot_ide = urlencode(utility::app_crypt($quot_id,'e'));
	$_SESSION['info_message'] = "Tooling added successfully";
	 header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=5");
    exit();
}	
###################################################################################################################		

############################################### FOR insert_feasibility ##########################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_feasibility'){	
    utility::addslashesOnAllInput(array("query_string"));
			 
		$quot_id               = $_REQUEST['quot_id'];
		$fsb_type              = $_REQUEST['fsb_type'];
		$issue_concern         = $_REQUEST['issue_concern'];
		$risk_assessment_comments      = $_REQUEST['risk_assessment_comments'];
		$feasibility_assessment_comment      = $_REQUEST['feasibility_assessment_comment'];
		$section_status        = $_REQUEST['section_status_feasibility'];
         
		$created_on         = date("Y-m-d H:i:s");
		$created_by         = $_SESSION["user_id"];
		$updated_by         = $_SESSION["user_id"];

		if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM quot_feasibility WHERE quot_id ='$quot_id'")) > 0) {
			$sql1 = "UPDATE quot_feasibility SET fsb_type = '$fsb_type', issue_concern = '$issue_concern',risk_assessment_comments = '$risk_assessment_comments',feasibility_assessment_comment = '$feasibility_assessment_comment',updated_by = '$updated_by' WHERE quot_id = '$quot_id'";
			mysqli_query($conn, $sql1);
			
			$target_dir = "../feasibility_images/";
			$risk_assessment_total_files = count($_FILES['risk_assessment_images']['name']);

			for ($i = 0; $i < $risk_assessment_total_files; $i++) {
				$image_name = $_FILES['risk_assessment_images']['name'][$i];
				$tmp_name = $_FILES['risk_assessment_images']['tmp_name'][$i];
				$imageFileType = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));

				if ($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png" && $imageFileType != "gif" && $imageFileType != "pdf") {
					$_SESSION['info_message'] = "Sorry, only JPG, JPEG, PNG, GIF & PDF files are allowed.";
					continue; 
				}

				$fname = pathinfo($image_name, PATHINFO_FILENAME);
				$target_file = $target_dir . $fname . "." . $imageFileType;
				$counter = 1;

				while (file_exists($target_file)) {
					$target_file = $target_dir . $fname . "_" . $counter . "." . $imageFileType;
					$counter++;
				}

				if (move_uploaded_file($tmp_name, $target_file)) {
					$sql_img = "INSERT INTO quot_feasibility_files (quot_id, file_for, file_name, created_by, created_on, updated_by) 
								VALUES ('$quot_id','1','$target_file','$created_by', '$created_on', '$updated_by')";
					mysqli_query($conn, $sql_img);
				}
			}
			
		
			$target_dir = "../feasibility_images/";
			$feasibility_assessment_total_files = count($_FILES['feasibility_assessment_images']['name']);

			for ($i = 0; $i < $feasibility_assessment_total_files; $i++) {
				$image_name = $_FILES['feasibility_assessment_images']['name'][$i];
				$tmp_name = $_FILES['feasibility_assessment_images']['tmp_name'][$i];
				$imageFileType = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));

				if ($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "png" && $imageFileType != "gif" && $imageFileType != "pdf") {
					$_SESSION['info_message'] = "Sorry, only JPG, JPEG, PNG, GIF & PDF files are allowed.";
					continue; 
				}

				$fname = pathinfo($image_name, PATHINFO_FILENAME);
				$target_file = $target_dir . $fname . "." . $imageFileType;
				$counter = 1;

				while (file_exists($target_file)) {
					$target_file = $target_dir . $fname . "_" . $counter . "." . $imageFileType;
					$counter++;
				}

				if (move_uploaded_file($tmp_name, $target_file)) {
					$sql_img = "INSERT INTO quot_feasibility_files (quot_id, file_for, file_name, created_by, created_on, updated_by) 
								VALUES ('$quot_id','2','$target_file','$created_by', '$created_on', '$updated_by')";
					mysqli_query($conn, $sql_img);
				}
			}

	        $_SESSION['info_message'] = "Feasibility Updated successfully";
		} else {
			$sql1 = "INSERT INTO quot_feasibility(quot_id, fsb_type, issue_concern, risk_assessment_comments, feasibility_assessment_comment,created_by, created_on, updated_by) VALUES ('$quot_id', '$fsb_type', '$issue_concern','$risk_assessment_comments','$feasibility_assessment_comment','$created_by', '$created_on', '$updated_by')";
			
			
			mysqli_query($conn, $sql1);
			$feasibility_id = mysqli_insert_id($conn);
			$target_dir = "../feasibility_images/";
			$risk_assessment_total_files = count($_FILES['risk_assessment_images']['name']);

			for ($i = 0; $i < $risk_assessment_total_files; $i++) {
				$image_name = $_FILES['risk_assessment_images']['name'][$i];
				$tmp_name = $_FILES['risk_assessment_images']['tmp_name'][$i];
				$imageFileType = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
				if (!in_array($imageFileType, ["jpg", "jpeg", "png", "gif", "pdf"])) {
					$_SESSION['info_message'] = "Sorry, only JPG, JPEG, PNG, GIF & PDF files are allowed.";
					continue; // Skip this file
				}

				$fname = pathinfo($image_name, PATHINFO_FILENAME);
				$target_file = $target_dir . $fname . "." . $imageFileType;
				$counter = 1;

				while (file_exists($target_file)) {
					$target_file = $target_dir . $fname . "_" . $counter . "." . $imageFileType;
					$counter++;
				}

			 
				if (move_uploaded_file($tmp_name, $target_file)) {
					$sql_img = "INSERT INTO quot_feasibility_files (quot_id, file_for, file_name, created_by, created_on, updated_by) 
								VALUES ('$quot_id','1','$target_file','$created_by', '$created_on', '$updated_by')";
					if (!mysqli_query($conn, $sql_img)) {
						$_SESSION['info_message'] = "Error saving file information to the database.";
					}
				} else {
					$_SESSION['info_message'] = "Failed to upload file: $image_name.";
				}
			}
			
			
			$feasibility_assessment_total_files = count($_FILES['feasibility_assessment_images']['name']);
			for ($i = 0; $i < $feasibility_assessment_total_files; $i++) {
				$image_name = $_FILES['feasibility_assessment_images']['name'][$i];
				$tmp_name = $_FILES['feasibility_assessment_images']['tmp_name'][$i];
				$imageFileType = strtolower(pathinfo($image_name, PATHINFO_EXTENSION));
				if (!in_array($imageFileType, ["jpg", "jpeg", "png", "gif", "pdf"])) {
					$_SESSION['info_message'] = "Sorry, only JPG, JPEG, PNG, GIF & PDF files are allowed.";
					continue; // Skip this file
				}

				$fname = pathinfo($image_name, PATHINFO_FILENAME);
				$target_file = $target_dir . $fname . "." . $imageFileType;
				$counter = 1;

				while (file_exists($target_file)) {
					$target_file = $target_dir . $fname . "_" . $counter . "." . $imageFileType;
					$counter++;
				}

			 
				if (move_uploaded_file($tmp_name, $target_file)) {
					$sql_img = "INSERT INTO quot_feasibility_files (quot_id, file_for, file_name, created_by, created_on, updated_by) 
								VALUES ('$quot_id','2','$target_file','$created_by', '$created_on', '$updated_by')";
					if (!mysqli_query($conn, $sql_img)) {
						$_SESSION['info_message'] = "Error saving file information to the database.";
					}
				} else {
					$_SESSION['info_message'] = "Failed to upload file: $image_name.";
				}
			}

		
		
		
		
	       $_SESSION['info_message'] = "Feasibility added successfully";
		}
		


		$status_sql = "UPDATE quot_feasibility SET status = '$section_status' WHERE quot_id = '$quot_id'";
		mysqli_query($conn, $status_sql);
		

	$quot_ide = urlencode(utility::app_crypt($quot_id,'e'));
	 header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=6");
     exit();
}	
    $sql_fasb= "SELECT * FROM quot_feasibility WHERE quot_id = '".$_REQUEST['id']."'";
    $result_fabs = mysqli_query($conn,$sql_fasb);
    $row_fasb = mysqli_fetch_array($result_fabs);
	$row_fasb = utility::validate_crypt_data($row_fasb);
	$fsb_type  = $row_fasb['fsb_type'];
	
###################################################################################################################	
############################################### FOR insert_logistics ##########################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert_logistics'){	
    utility::addslashesOnAllInput(array("query_string"));
			 
		$quot_id                = $_REQUEST['quot_id'];
		$logistics              = $_REQUEST['logistics'];
		$transport              = $_REQUEST['transport'];
		$pcost                  = $_REQUEST['pcost'];
		$packaging              = $_REQUEST['packaging'];
		$pacost                 = $_REQUEST['pacost'];
		$pallet                 = $_REQUEST['pallet'];
		$palcost                = $_REQUEST['palcost'];
		$other                  = $_REQUEST['other'];
		$othcost                  = $_REQUEST['othcost'];
		$section_status         = $_REQUEST['section_status_logistics'];
         
		$created_on         = date("Y-m-d H:i:s");
		$created_by         = $_SESSION["user_id"];
		$updated_by         = $_SESSION["user_id"];

		if (mysqli_num_rows(mysqli_query($conn, "SELECT * FROM quot_logistics WHERE quot_id ='$quot_id'")) > 0) {
			$sql1 = "UPDATE quot_logistics SET logistics = '$logistics', transport = '$transport',pcost = '$pcost',packaging = '$packaging',pacost = '$pacost',pallet = '$pallet',palcost = '$palcost',other = '$other',othcost = '$othcost',updated_by = '$updated_by' WHERE quot_id = '$quot_id'";
	        $_SESSION['info_message'] = "Logistics Updated successfully";
		} else {
			$sql1 = "INSERT INTO quot_logistics(quot_id, logistics, transport,pcost,packaging,pacost,pallet, palcost,other,othcost,created_by,created_on, updated_by) VALUES ('$quot_id', '$logistics', '$transport','$pcost','$packaging','$pacost','$pallet','$palcost','$other','$othcost','$created_by', '$created_on', '$updated_by')";
	       $_SESSION['info_message'] = "Logistics added successfully";
		}
		mysqli_query($conn, $sql1);


		$status_sql = "UPDATE quot_logistics SET status = '$section_status' WHERE quot_id = '$quot_id'";
		mysqli_query($conn, $status_sql);
		

	 $quot_ide = urlencode(utility::app_crypt($quot_id,'e'));
	 header("Location:index?page=view_quotation_progress&from=view_quotation&qot_id='$quot_ide'&id=$quot_id&red=7");
     exit();
}	
    $sql_log= "SELECT * FROM quot_logistics WHERE quot_id = '".$_REQUEST['id']."'";
    $result_log = mysqli_query($conn,$sql_log);
    $row_log = mysqli_fetch_array($result_log);
	$row_log = utility::validate_crypt_data($row_log);
	$logistics  = $row_log['logistics'];
	
###################################################################################################################	
###########################################  PAGING WITH PER PAGE  #####################################
 $file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "quotation";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ";
$rs = mysqli_query($conn,$query); 
$client_info = mysqli_fetch_array($rs);
?>

<style>
em{
	color:red;
}
.card.card-purple .card-header {
    background-color: #9c4893;
}

  .status-box {
    display: inline-block;
    padding: 5px 10px;
    border-radius: 5px;
    color: white;
	font-size:13px;
    font-weight: bold;
    margin-right: 5px;
  }

  .Complete {
    background-color: green; /* Light green */
  }

  .inprogress{
    background-color: #9c4893; /* Yellow */
    color: white; /* Dark text for better readability on yellow */
  }

  .Incomplete {
    background-color: #e31414; /* Orange */
  }
  
    .progress-icon {
		display: inline-block;
		width: 10px;
		height: 10px;
		border-radius: 50%;
		margin-left: 5px;
	}
	.progress-icon.red {
		background-color: red;
	}
	.progress-icon.yellow {
		background-color: #9c4893;
	}
	.progress-icon.green {
		background-color: green;
	}
  
</style>
<section class="content-header">
   <?php if(isset($_SESSION['info_message'])) { ?>
		<script>
			$(document).ready(function(){
				Toastify({
					text: "<?php echo $_SESSION['info_message']; ?>",duration: 2000,close: true,gravity: "top",position: 'right',backgroundColor: "#28a745",style: {borderBottom: "3px solid white",},
				}).showToast();
				<?php unset($_SESSION['info_message']); ?>
			});
		</script>
	<?}?>
    <div class="container-fluid">
        <div class="row">
            <div class="col-sm-8">
                <h1 style="float:left;clear:right;margin-top:0px;padding-bottom: 0;">
                     <span class="text-danger"><?//=$gsi->getCustomerById($client_info['customer_id']); ?></span>
                    Quotation Number : <span class="text-danger"><?=($client_info['quotation_number']); ?></span>
                </h1>
            </div>
			<div class="col-sm-4 hide">
               <div class="progress-status-info" style="text-align: right; margin-bottom: 10px; margin-right: 8px;">
				  <span class="status-box inprogress">No-progress</span>
				  <span class="status-box Incomplete">Incomplete</span>
				  <span class="status-box Complete">Completed </span>
			  </div>
            </div>
			<div class="col-sm-4">
			  <? if(isset($_REQUEST["from"]) && $_REQUEST["from"] == "view_quotation"){  ?>
				<button type="button" id="back" name="back" value="back"  style="float:right;" class="btn btn-primary" onclick="window.location.href = '<?=$config->siteURL;?>/quotation_management/index?page=view'">Back</button>
			  <? }else{ ?>
				<button type="button" id="back" name="back" value="back"  style="float:right;" class="btn btn-primary" onclick="window.location.href = 'index?<?=$query_string;?>'">Back</button>
			  <? } ?>
			 </div>
        </div>
    </div>
</section>
<section>
<div class="row">
	   <div class="container-fluid">
			
        <div class="col-md-12">
            <div class="card card-purple card-tabs">
                <div class="card-header p-0 pt-1">
                    <ul class="nav nav-tabs" id="custom-tabs-five-tab" role="tablist">
                        <li class="nav-item">
                            <a class="nav-link <?=$active_show;?> " id="boughtParts-tab" data-bs-toggle="pill" href="#boughtParts" role="tab" aria-controls="boughtParts" aria-selected="true">
                                Bought Parts
                                <span class="progress-icon" id="boughtParts-status"></span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link <?=$active_show_1;?>" id="rawMaterials-tab" data-bs-toggle="pill" href="#rawMaterials" role="tab" aria-controls="rawMaterials" aria-selected="false">
                                Raw Materials
                                <span class="progress-icon" id="rawMaterials-status"></span>
                            </a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link <?=$active_show_2;?>" id="operations-tab" data-bs-toggle="pill" href="#operations" role="tab" aria-controls="operations" aria-selected="false">
                                Operations
                                <span class="progress-icon" id="operations-status"></span>
                            </a>
                        </li> 
						<li class="nav-item">
                            <a class="nav-link <?=$active_show_3;?>" id="other-tab" data-bs-toggle="pill" href="#other" role="tab" aria-controls="other" aria-selected="false">
                                Other
                                <span class="progress-icon" id="other-status"></span>
                            </a>
                        </li>
						<li class="nav-item">
                            <a class="nav-link <?=$active_show_4;?>" id="tooling-tab" data-bs-toggle="pill" href="#tooling" role="tab" aria-controls="tooling" aria-selected="false">
                                Tooling
                                <span class="progress-icon" id="tooling-status"></span>
                            </a>
                        </li>
						<li class="nav-item">
                            <a class="nav-link <?=$active_show_5;?>" id="feasibility-tab" data-bs-toggle="pill" href="#feasibility" role="tab" aria-controls="feasibility" aria-selected="false">
                                Feasibility
                                <span class="progress-icon" id="feasibility-status"></span>
                            </a>
                        </li>
						<li class="nav-item">
                            <a class="nav-link <?=$active_show_6;?>" id="logistics-tab" data-bs-toggle="pill" href="#logistics" role="tab" aria-controls="logistics" aria-selected="false">
                                Logistics
                                <span class="progress-icon" id="logistics-status"></span>
                            </a>
                        </li>						
						
						<li class="nav-item">         
                            <a class="nav-link <?=$active_show_8;?>" id="front_sheet-tab" data-bs-toggle="pill" href="#front_sheet" role="tab" aria-controls="front_sheet" aria-selected="false">
                                Front Sheet
                                <span class="progress-icon" id="front_sheet-status"></span>
                            </a>
                        </li>
						
						<li class="nav-item">         
                            <a class="nav-link <?=$active_show_9;?>" id="procured_sheet-tab" data-bs-toggle="pill" href="#procured_sheet" role="tab" aria-controls="procured_sheet" aria-selected="false">
                                Procured Sheet
                                <span class="progress-icon" id="procured_sheet-status"></span>
                            </a>
                        </li>
						<li class="nav-item">         
                            <a class="nav-link <?=$active_show_7;?>" id="feasibility_sheet-tab" data-bs-toggle="pill" href="#feasibility_sheet" role="tab" aria-controls="feasibility_sheet" aria-selected="false">
                                Feasibility Sheet
                                <span class="progress-icon" id="feasibility_sheet-status"></span>
                            </a>
                        </li>
						<li class="nav-item">
							<?php if ($_SERVER['HTTP_HOST'] == "localhost") { ?>
								<button type="button" id="back" name="back" value="back" style="float:right; color:white; margin-top:3px; margin-bottom:; padding:5px 10px; font-size:12px;" class="btn btn-success <?=$hide;?>">
									<a href="<?=$config->frontEndURL?>/admin/quotation_management/index?page=quotation_all_pdf&id=<?=$_REQUEST['id'];?>&hide_top=1&hide_left=1" target="_blank" style="color:white;">Generate PDF</a>
								</button>
							<?php } else { ?>
								<button type="button" id="back" name="back" value="back" style="float:right; color:white; margin-top:3px; margin-bottom:; padding:5px 10px; font-size:12px;" class="btn btn-success <?=$hide;?>">
									<a href="<?=$config->frontEndURL;?>quotation_front_sheet_pdf.php?id=<?=$_REQUEST['id'];?>&pdf_name=front_sheet_pdf" target="_blank" style="color:white;">Download PDF</a>
								</button>
							<?php } ?>
						</li>



                        <!-- Repeat for other hidden tabs if needed -->
                    </ul>
                </div>
                <div class="card-body">
                    <div class="tab-content" id="custom-tabs-five-tabContent">
                        <div class="tab-pane fade <?=$active_show_content;?>" id="boughtParts" role="tabpanel" aria-labelledby="boughtParts-tab">
                            <? include('tab_brought_out_parts.php'); ?>
                        </div>
                        <div class="tab-pane fade <?=$active_show_content_1;?>" id="rawMaterials" role="tabpanel" aria-labelledby="rawMaterials-tab">
                            <? include('tab_raw_materials.php'); ?>
                        </div>
                        <div class="tab-pane fade <?=$active_show_content_2;?>" id="operations" role="tabpanel" aria-labelledby="operations-tab">
                            <? include('tab_operations.php'); ?>
                        </div>
						<div class="tab-pane fade <?=$active_show_content_3;?> " id="other" role="tabpanel" aria-labelledby="other-tab">
                            <? include('tab_other.php'); ?>
                        </div>
						<div class="tab-pane fade <?=$active_show_content_4;?>" id="tooling" role="tabpanel" aria-labelledby="tooling-tab">
                            <? include('tab_tooling.php'); ?>
                        </div>
						 <div class="tab-pane fade <?=$active_show_content_5;?>" id="feasibility" role="tabpanel" aria-labelledby="feasibility-tab">
                            <? include('tab_feasibility.php'); ?>
                        </div>
						<div class="tab-pane fade <?=$active_show_content_6;?>" id="logistics" role="tabpanel" aria-labelledby="logistics-tab">
                            <? include('tab_logistics.php'); ?>
                        </div>						
						<div class="tab-pane fade <?=$active_show_content_7;?>" id="feasibility_sheet" role="tabpanel" aria-labelledby="feasibility_sheet-tab">
                            <? include('feasibility_sheet_pdf.php'); ?>
                        </div>
						<div class="tab-pane fade <?=$active_show_content_8;?>" id="front_sheet" role="tabpanel" aria-labelledby="front_sheet-tab">
                            <? include('front_sheet_pdf.php'); ?>
                        </div>
						<div class="tab-pane fade <?=$active_show_content_9;?>" id="procured_sheet" role="tabpanel" aria-labelledby="procured_sheet-tab">
                            <? include('procured_sheet_pdf.php'); ?>
                        </div>
                        <!-- Add content for other tabs as needed -->
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<?
/* check progress */
$broughtPart = !empty(utility::getTableFieldById("quot_brought", "status", "quot_id", $_REQUEST['id'])) ? utility::getTableFieldById("quot_brought", "status", "quot_id", $_REQUEST['id']) : 0;

$quot_raw_materials = !empty(utility::getTableFieldById("quot_raw_materials", "status", "quot_id", $_REQUEST['id'])) ? 
$quot_raw_materials= utility::getTableFieldById("quot_raw_materials","status","quot_id",$_REQUEST['id']): 0;

$quot_operations = !empty(utility::getTableFieldById("quot_operations", "status", "quot_id", $_REQUEST['id'])) ? 
$quot_operations= utility::getTableFieldById("quot_operations","status","quot_id",$_REQUEST['id']): 0;

$quot_other = !empty(utility::getTableFieldById("quot_other", "status", "quot_id", $_REQUEST['id'])) ? 
$quot_other= utility::getTableFieldById("quot_other","status","quot_id",$_REQUEST['id']): 0;


$quot_tooling = !empty(utility::getTableFieldById("quot_tooling", "status", "quot_id", $_REQUEST['id'])) ? 
$quot_tooling= utility::getTableFieldById("quot_tooling","status","quot_id",$_REQUEST['id']): 0;

$quot_feasibility = !empty(utility::getTableFieldById("quot_feasibility", "status", "quot_id", $_REQUEST['id'])) ? 
$quot_feasibility= utility::getTableFieldById("quot_feasibility","status","quot_id",$_REQUEST['id']): 0;

$quot_logistics = !empty(utility::getTableFieldById("quot_logistics", "status", "quot_id", $_REQUEST['id'])) ? 
$quot_logistics= utility::getTableFieldById("quot_logistics","status","quot_id",$_REQUEST['id']): 0;


?>
</section>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript">
    
 function updateProgressStatus(tabId, status) {
        const statusElement = document.getElementById(`${tabId}-status`);
        statusElement.classList.remove('red', 'yellow', 'green');
        
        if (status === 0) {
            statusElement.classList.add('#9c4893'); // Yellow for No-progress
        } else if (status === 1) {
			statusElement.classList.add('red'); // Red for incomplete
        } else if (status === 2) {
            statusElement.classList.add('green'); // Green for complete
        }
    }
	updateProgressStatus('boughtParts', <?=$broughtPart;?>);
    updateProgressStatus('rawMaterials', <?=$quot_raw_materials;?>);
    updateProgressStatus('operations',  <?=$quot_operations;?>);
	updateProgressStatus('tooling',  <?=$quot_tooling;?>);	
	updateProgressStatus('other',  <?=$quot_other;?>);
	updateProgressStatus('feasibility',  <?=$quot_feasibility;?>);
	updateProgressStatus('logistics',  <?=$quot_logistics;?>);
	
   function ToNextTab() {
		const activeTab = document.querySelector('.nav-tabs .nav-link.active');
		const nextTab = activeTab.closest('li').nextElementSibling?.querySelector('.nav-link');
		
		if (nextTab) {
			nextTab.click();
			updateButtonColor(nextTab.id);
		}
	}

	function ToPrevTab() {
		const activeTab = document.querySelector('.nav-tabs .nav-link.active');
		const prevTab = activeTab.closest('li').previousElementSibling?.querySelector('.nav-link');
		
		if (prevTab) {
			prevTab.click();
			updateButtonColor(prevTab.id);
		}
	}


    let completedTabs = 0;
    const totalTabs = 8;

    function openTab(event, tabName) {
        let tabContents = document.querySelectorAll(".tab-content");
        let tabLinks = document.querySelectorAll(".tab-link");

        tabContents.forEach(content => content.classList.remove("active"));
        tabLinks.forEach(link => link.classList.remove("active"));

        document.getElementById(tabName).classList.add("active");
        event.currentTarget.classList.add("active");
    }

    function submitForm(event, tabId) {
        event.preventDefault();

        const formData = new FormData(event.target);
        const tabData = Object.fromEntries(formData.entries());
        const xhr = new XMLHttpRequest();

        xhr.open("POST", "your_php_script.php", true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                const response = JSON.parse(xhr.responseText);

                if (response.success) {
                    document.getElementById(tabId).classList.add('completed');
                    completedTabs++;
                    event.target.reset();
                }
            }
        };

        xhr.send(`tab_data=${encodeURIComponent(tabData)}&tab_id=${tabId}`);
    }
	
	document.addEventListener("DOMContentLoaded", function() {
	  if (window.location.href.includes("page=view_quotation_progress")) {
		document.body.classList.add("sidebar-mini", "layout-fixed", "sidebar-collapse");
	  }
   });

</script>
