<?php
/**
 * Veluno B2B Lead Processing Script
 */

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 1. Sanitize inputs
    $form_type = isset($_POST['form_type']) ? htmlspecialchars(strip_tags(trim($_POST['form_type']))) : 'general';
    $name = isset($_POST['name']) ? htmlspecialchars(strip_tags(trim($_POST['name']))) : '';
    $phone = isset($_POST['phone']) ? htmlspecialchars(strip_tags(trim($_POST['phone']))) : '';
    $email = isset($_POST['email']) ? filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL) : '';
    $city = isset($_POST['city']) ? htmlspecialchars(strip_tags(trim($_POST['city']))) : '';
    $state = isset($_POST['state']) ? htmlspecialchars(strip_tags(trim($_POST['state']))) : '';
    $company_name = isset($_POST['company_name']) ? htmlspecialchars(strip_tags(trim($_POST['company_name']))) : '';
    $business_type = isset($_POST['business_type']) ? htmlspecialchars(strip_tags(trim($_POST['business_type']))) : '';
    $monthly_requirement = isset($_POST['monthly_requirement']) ? htmlspecialchars(strip_tags(trim($_POST['monthly_requirement']))) : '';
    $requirement = isset($_POST['requirement']) ? htmlspecialchars(strip_tags(trim($_POST['requirement']))) : '';
    $product_name = isset($_POST['product_name']) ? htmlspecialchars(strip_tags(trim($_POST['product_name']))) : '';
    $subject = isset($_POST['subject']) ? htmlspecialchars(strip_tags(trim($_POST['subject']))) : 'New Lead from Website';
    $message = isset($_POST['message']) ? htmlspecialchars(strip_tags(trim($_POST['message']))) : '';

    // 2. Build email body and subject based on form type
    $to = "info@veluno.in";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: Veluno Leads <no-reply@veluno.in>" . "\r\n";
    
    $email_subject = "Veluno B2B Lead: " . ucwords(str_replace('_', ' ', $form_type));
    $email_body = "<html><head><title>New B2B Lead</title></head><body>";
    $email_body .= "<h2>New Lead Received via Veluno Website</h2>";
    $email_body .= "<table border='1' cellpadding='10' cellspacing='0' style='border-collapse: collapse; min-width: 400px;'>";
    $email_body .= "<tr><td><strong>Form Type</strong></td><td>" . htmlspecialchars($form_type) . "</td></tr>";
    
    if ($name) $email_body .= "<tr><td><strong>Name</strong></td><td>" . htmlspecialchars($name) . "</td></tr>";
    if ($phone) $email_body .= "<tr><td><strong>Phone</strong></td><td>" . htmlspecialchars($phone) . "</td></tr>";
    if ($email) $email_body .= "<tr><td><strong>Email</strong></td><td>" . htmlspecialchars($email) . "</td></tr>";
    if ($city) $email_body .= "<tr><td><strong>City</strong></td><td>" . htmlspecialchars($city) . "</td></tr>";
    if ($state) $email_body .= "<tr><td><strong>State</strong></td><td>" . htmlspecialchars($state) . "</td></tr>";
    if ($company_name) $email_body .= "<tr><td><strong>Company Name</strong></td><td>" . htmlspecialchars($company_name) . "</td></tr>";
    if ($business_type) $email_body .= "<tr><td><strong>Business Type</strong></td><td>" . htmlspecialchars($business_type) . "</td></tr>";
    if ($monthly_requirement) $email_body .= "<tr><td><strong>Monthly Requirement</strong></td><td>" . htmlspecialchars($monthly_requirement) . "</td></tr>";
    if ($requirement) $email_body .= "<tr><td><strong>Requirement</strong></td><td>" . htmlspecialchars($requirement) . "</td></tr>";
    if ($product_name) $email_body .= "<tr><td><strong>Interested Product</strong></td><td>" . htmlspecialchars($product_name) . "</td></tr>";
    if ($subject && $form_type == 'general_contact') $email_body .= "<tr><td><strong>Subject</strong></td><td>" . htmlspecialchars($subject) . "</td></tr>";
    if ($message) $email_body .= "<tr><td><strong>Message</strong></td><td>" . nl2br(htmlspecialchars($message)) . "</td></tr>";
    
    $email_body .= "<tr><td><strong>Submission Time</strong></td><td>" . date("Y-m-d H:i:s") . "</td></tr>";
    $email_body .= "</table></body></html>";

    // 3. Log lead to local text file (extremely helpful for local Wamp testing)
    $log_dir = "leads";
    if (!is_dir($log_dir)) {
        mkdir($log_dir, 0777, true);
    }
    
    $log_content = "====================================================\n";
    $log_content .= "TIME: " . date("Y-m-d H:i:s") . "\n";
    $log_content .= "FORM TYPE: " . $form_type . "\n";
    if ($name) $log_content .= "NAME: " . $name . "\n";
    if ($phone) $log_content .= "PHONE: " . $phone . "\n";
    if ($email) $log_content .= "EMAIL: " . $email . "\n";
    if ($city) $log_content .= "CITY: " . $city . "\n";
    if ($state) $log_content .= "STATE: " . $state . "\n";
    if ($company_name) $log_content .= "COMPANY: " . $company_name . "\n";
    if ($business_type) $log_content .= "BUSINESS TYPE: " . $business_type . "\n";
    if ($monthly_requirement) $log_content .= "MONTHLY REQ: " . $monthly_requirement . "\n";
    if ($requirement) $log_content .= "REQUIREMENT: " . $requirement . "\n";
    if ($product_name) $log_content .= "PRODUCT: " . $product_name . "\n";
    if ($message) $log_content .= "MESSAGE: " . str_replace("\n", " ", $message) . "\n";
    $log_content .= "====================================================\n\n";
    
    file_put_contents($log_dir . "/enquiry_log.txt", $log_content, FILE_APPEND);

    // 4. Try sending the email
    @mail($to, $email_subject, $email_body, $headers);

    // 5. Redirect back with status
    if ($form_type == 'catalogue_download') {
        header("Location: download-catalogue.php?download=success");
    } else {
        // Find referer or default to index
        $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'index.php';
        // Strip existing status from referer if present
        $referer = strtok($referer, '?');
        header("Location: " . $referer . "?status=success");
    }
    exit();
} else {
    // If accessed directly, redirect to homepage
    header("Location: index.php");
    exit();
}
?>
