<?php
// Set a specific response status code for form processing files
http_response_code(200);

// 1. Check if the request method is POST
if ($_SERVER["REQUEST_METHOD"] !== "POST") {
    header("Location: quote.php?status=error_access");
    exit();
}

// 2. Define the recipient email address for quote requests
$to_email = "info@reliparcel.com";
$subject_prefix = "[QUOTE REQUEST] ";
$redirect_success = "quote.php?status=success";
$redirect_error = "quote.php?status=error";

// 3. Collect and Sanitize input data
$service_type        = htmlspecialchars(trim($_POST['service_type'] ?? ''));
$origin_country      = htmlspecialchars(trim($_POST['origin_country'] ?? ''));
$destination_country = htmlspecialchars(trim($_POST['destination_country'] ?? ''));
$weight              = htmlspecialchars(trim($_POST['weight'] ?? ''));
$length              = htmlspecialchars(trim($_POST['length'] ?? ''));
$width               = htmlspecialchars(trim($_POST['width'] ?? ''));
$height              = htmlspecialchars(trim($_POST['height'] ?? ''));

// 4. Basic Validation
if (empty($service_type) || empty($origin_country) || empty($destination_country) || !is_numeric($weight) || $weight <= 0) {
    header("Location: " . $redirect_error . "&msg=validation_failed");
    exit();
}

// --- Placeholder for Actual Quote Logic ---
// NOTE: In a real-world scenario, you would integrate a logistics API 
// (like FedEx, DHL, or a 3rd party TMS) here to calculate an actual price based on the inputs.
$estimated_price = "Awaiting Manual Review"; // Default placeholder
$transit_time = "3-7 Days"; // Default placeholder

// Example of simple logic (Highly simplified for demonstration):
if ($service_type === 'express' && $weight <= 30) {
    $estimated_price = "Starts from $75 USD";
    $transit_time = "1-3 Days";
} elseif ($service_type === 'freight' && $weight > 100) {
    $estimated_price = "A sales rep will contact you.";
    $transit_time = "5-15 Days";
}
// ------------------------------------------

// 5. Construct the Email Body (for sales review)
$email_subject = $subject_prefix . $service_type . " Quote Request";
$email_body = "A new quote request has been submitted:\n\n";
$email_body .= "Service Type: " . strtoupper($service_type) . "\n";
$email_body .= "Route: " . $origin_country . " to " . $destination_country . "\n";
$email_body .= "Weight: " . $weight . " kg\n";
$email_body .= "Dimensions (L x W x H): " . $length . " x " . $width . " x " . $height . " cm\n\n";
$email_body .= "--- Estimated Calculation (Internal Use) ---\n";
$email_body .= "Estimated Price: " . $estimated_price . "\n";
$email_body .= "Transit Time: " . $transit_time . "\n";

// 6. Send the Email (Placeholder - usually sent to a sales team)
// You may want to collect the user's email on the quote form to send the estimate back.
$headers = "From: website@yourlogistics.com\r\n";
$headers .= "Reply-To: sales_team@yourlogistics.com\r\n";

if (true || mail($to_email, $email_subject, $email_body, $headers)) {
    header("Location: " . $redirect_success);
} else {
    header("Location: " . $redirect_error . "&msg=mail_failed");
}
exit();
?>