<?php
include("../../../include/startup.php"); 
ob_clean();
if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_package_price'){
	$packages_id = $_REQUEST['packages_id'];
	$row = utility::getTableRowByField("fixed_packages","id",$packages_id);
	$returnArray["price"] = $row["price"];
	echo json_encode($returnArray);
}else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'get_enq_drodown') {
	$dataArray = array();
	$dataArray["enq_id"]=array();
	$dataArray["status"] = 0;
	$customer_id = $_REQUEST['customer_id'];
	$sql = "SELECT id, part_number,enquiry_no FROM enquiry_management  WHERE customer = '$customer_id' order by id DESC";
	$rs = mysqli_query($conn, $sql);
	if (mysqli_num_rows($rs) > 0) {

		while ($row = mysqli_fetch_array($rs)) {
			$row = utility::validate_crypt_data($row);
			
			$dataArray["enq_id"][] = array("enquiry_no" => $row['enquiry_no'].' - '.$row["part_number"], "id" => $row["id"]);
		}
		$dataArray["status"] = 1;
	}
	echo json_encode($dataArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_enquiry_details'){
	$dataArray = array();
	$dataArray["status"] = 0;
	$enq_id = $_REQUEST['enq_id'];
	$sql = "SELECT id,part_number ,part_description, model_number,drawing_issue, volume, type FROM enquiry_management WHERE id = '$enq_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row=utility::validate_crypt_data($row);
		$dataArray["part_number"]  = $row['part_number'];
		$dataArray["part_description"]  = $row['part_description'];
		$dataArray["model_number"]  = $row['model_number'];
		$dataArray["drawing_issue"]  = $row['drawing_issue'];
		$dataArray["volume"]  = $row['volume'];
		$dataArray["type"]  = $row['type'];
		$dataArray["status"]		= 1;
	}
	echo json_encode($dataArray);

}else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'get_enq_drodown') {
	$dataArray = array();
	$dataArray["enq_id"]=array();
	$dataArray["status"] = 0;
	$customer_id = $_REQUEST['customer_id'];
	$sql = "SELECT id, part_number,enquiry_no FROM enquiry_management  WHERE customer = '$customer_id'";
	$rs = mysqli_query($conn, $sql);
	if (mysqli_num_rows($rs) > 0) {

		while ($row = mysqli_fetch_array($rs)) {
			$row = utility::validate_crypt_data($row);
			
			$dataArray["enq_id"][] = array("enquiry_no" => $row['enquiry_no'].' - '.$row["part_number"], "id" => $row["id"]);
		}
		$dataArray["status"] = 1;
	}
	echo json_encode($dataArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_brought_part_mapping'){
	$brought_part_mapping_id = $_REQUEST['brought_part_mapping_id'];
	mysqli_query($conn,"DELETE FROM quot_brought WHERE id = '$brought_part_mapping_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_raw_material_mapping'){
	$raw_material_mapping_id = $_REQUEST['raw_material_mapping_id'];
	mysqli_query($conn,"DELETE FROM quot_raw_materials WHERE id = '$raw_material_mapping_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_operation_mapping'){
	$operation_mapping_id = $_REQUEST['operation_mapping_id'];
	mysqli_query($conn,"DELETE FROM quot_operations WHERE id = '$operation_mapping_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_raw_material_details'){
	 error_log("Fetching raw material details");
	$dataArray = array();
	$dataArray["status"] = 0;
	$type_id = $_REQUEST['type_id'];
	$sql = "SELECT id,description,format,mpe,uom,density FROM raw_materials WHERE id = '$type_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row=utility::validate_crypt_data($row);
		$dataArray["description"]  = $row['description'];
		$dataArray["format"]  = $row['format'];
		$dataArray["mpe"]  = $row['mpe'];
		$dataArray["uom"]  = $row['uom'];	
		$dataArray["density"]  = $row['density'];
		$dataArray["status"]		= 1;
	}
	echo json_encode($dataArray);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_operation_details'){
	 error_log("Fetching raw material details");
	$dataArray = array();
	$dataArray["status"] = 0;
	$work_station_id = $_REQUEST['work_station_id'];
	$sql = "SELECT id,station,facility_rate_oem,labour_rate_oem,hrs_week FROM quotation_machines WHERE id = '$work_station_id' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row=utility::validate_crypt_data($row);
		$dataArray["station"] = $row['station'];
$parts = explode(' - ', $dataArray["station"], 2);
$filteredStation = isset($parts[1]) ? $parts[1] : $dataArray["station"];
 htmlspecialchars($filteredStation, ENT_QUOTES, 'UTF-8');



		$dataArray["facility_rate_oem"]  = $row['facility_rate_oem'];
		$dataArray["labour_rate_oem"]  = $row['labour_rate_oem'];
		$dataArray["hrs_week"]  = $row['hrs_week'];
		$dataArray["status"]		= 1;
	}
	echo json_encode($dataArray);

}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_tooling_mapping'){
	$tooling_mapping_id = $_REQUEST['tooling_mapping_id'];
	mysqli_query($conn,"DELETE FROM quot_tooling WHERE id = '$tooling_mapping_id'");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_feasibility_files'){
	$id = $_REQUEST['imageid'];
	$product_image = $_REQUEST['product_image'];
		$dir = "../feasibility_images/";
		if(file_exists($dir.$product_image)){ 
			unlink($dir.$product_image);				
		}
	mysqli_query($conn,"DELETE FROM quot_feasibility_files WHERE id = '$id' ");
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}
die;
?>