<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

// Include PHPMailer files
require 'vendor/autoload.php'; // For Composer
// OR require 'path-to-phpmailer/src/PHPMailer.php';
// require 'path-to-phpmailer/src/Exception.php';
// require 'path-to-phpmailer/src/SMTP.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['Submit'])) {
    // Replace with your email address
    $to = "sales@tmcards.com";
    $subject = "Quatation Submission - Promotional Playing Cards Com";

    // Sanitize and validate input
    $paper = htmlspecialchars($_POST['paper'] ?? 'Not Provided');
    $quantity = htmlspecialchars($_POST['quantity'] ?? 'Not Provided');
    $standardsize = htmlspecialchars($_POST['standardsize'] ?? 'Not Provided');
    $customsize = htmlspecialchars($_POST['customsize'] ?? 'Not Provided');
    $cardstodeck = htmlspecialchars($_POST['cardstodeck'] ?? 'Not Provided');
    $cardface = htmlspecialchars($_POST['cardface'] ?? 'Not Provided');
    $frontcolors = htmlspecialchars($_POST['frontcolors'] ?? 'Not Provided');
    $backcolors = htmlspecialchars($_POST['backcolors'] ?? 'Not Provided');
    $specialcolor = htmlspecialchars($_POST['specialcolor'] ?? 'Not Provided');
    $coating = htmlspecialchars($_POST['coating'] ?? 'Not Provided');
    $radio = htmlspecialchars($_POST['radio'] ?? 'Not Selected');
    $check = isset($_POST['check']) ? htmlspecialchars(implode(', ', $_POST['check'])) : 'None';
    $corners = htmlspecialchars($_POST['corners'] ?? 'Not Provided');
    $instnsheetpaper = htmlspecialchars($_POST['instnsheetpaper'] ?? 'Not Provided');
    $sheetprintingoptions = htmlspecialchars($_POST['sheetprintingoptions'] ?? 'Not Provided');
    $sheetcolors = htmlspecialchars($_POST['sheetcolors'] ?? 'Not Provided');
    $packaging = htmlspecialchars($_POST['packaging'] ?? 'Not Provided');
    $leathercases = htmlspecialchars($_POST['leathercases'] ?? 'Not Provided');
    $custompackaging = htmlspecialchars($_POST['custompackaging'] ?? 'Not Provided');
    $wrapping = htmlspecialchars($_POST['wrapping'] ?? 'Not Provided');
    $displaypackoption = htmlspecialchars($_POST['displaypackoption'] ?? 'Not Provided');
    $deliverymode = htmlspecialchars($_POST['deliverymode'] ?? 'Not Provided');
    $deliverytime = htmlspecialchars($_POST['deliverytime'] ?? 'Not Provided');
    ///////////

    $firstName = filter_var($_POST['name'] ?? '', FILTER_SANITIZE_STRING);
    $lastName = filter_var($_POST['lname'] ?? '', FILTER_SANITIZE_STRING);
    $email = filter_var($_POST['email'] ?? '', FILTER_VALIDATE_EMAIL);
    $phone = filter_var($_POST['phone'] ?? '', FILTER_SANITIZE_STRING);
    $address = filter_var($_POST['address'] ?? '', FILTER_SANITIZE_STRING);
    $city = filter_var($_POST['city'] ?? '', FILTER_SANITIZE_STRING);
    $state = filter_var($_POST['state'] ?? '', FILTER_SANITIZE_STRING);
    $zip = filter_var($_POST['zip'] ?? '', FILTER_SANITIZE_STRING);
    $country = filter_var($_POST['country'] ?? '', FILTER_SANITIZE_STRING);
    $captchaEntered = filter_var($_POST['captcha_entered'] ?? '', FILTER_SANITIZE_STRING);
    $captchaTotal = filter_var($_POST['captcha_total'] ?? '', FILTER_SANITIZE_STRING);
    // Create the email body
    $emailBody = "
    <html>
    <head>
        <title>Specification Details</title>
    </head>
    <body>
        <h1>Specification Details</h1>
        <p><strong>Plastic Coated Paper:</strong> $paper</p>
        <p><strong>Quantity:</strong> $quantity</p>
        <p><strong>Size Specifications:</strong> $standardsize</p>
        <p><strong>Custom Size and Shape:</strong> $customsize</p>
        <p><strong>Cards per Deck:</strong> $cardstodeck</p>
        <p><strong>Card Faces:</strong> $cardface</p>
        <p><strong>Front Colors:</strong> $frontcolors</p>
        <p><strong>Back Colors:</strong> $backcolors</p>
        <p><strong>Pantone Special Color Requirement:</strong> $specialcolor</p>
        <p><strong>Coating Finish:</strong> $coating</p>
        <p><strong>Feel of the Cards:</strong> $radio</p>
        <p><strong>Gold Foil Options:</strong> $check</p>
        <p><strong>Corners:</strong> $corners</p>
        <p><strong>Instruction Sheet Paper:</strong> $instnsheetpaper</p>
        <p><strong>Sheet Printing Options:</strong> $sheetprintingoptions</p>
        <p><strong>Sheet Colors:</strong> $sheetcolors</p>
        <p><strong>Packaging:</strong> $packaging</p>
        <p><strong>Leather Cases:</strong> $leathercases</p>
        <p><strong>Custom Packaging:</strong> $custompackaging</p>
        <p><strong>Wrapping:</strong> $wrapping</p>
        <p><strong>Display Pack Option:</strong> $displaypackoption</p>
        <p><strong>Delivery Mode:</strong> $deliverymode</p>
        <p><strong>Delivery Time:</strong> $deliverytime</p>
        <br>
        <hr>
        <h1>Personal Details</h1>
            <p><strong>First Name:</strong> $firstName</p>
            <p><strong>Last Name:</strong> $lastName</p>
            <p><strong>Email:</strong> $email</p>
            <p><strong>Phone:</strong> $phone</p>
            <p><strong>Address:</strong> $address</p>
            <p><strong>City:</strong> $city</p>
            <p><strong>State:</strong> $state</p>
            <p><strong>Zip Code:</strong> $zip</p>
            <p><strong>Country:</strong> $country</p>
    </body>
    </html>";

    // Send email using PHPMailer
    $mail = new PHPMailer(true);

    try {
        // Server settings
        $mail->isSMTP();
        $mail->Host = "smtp.sendgrid.net"; // Replace with your SMTP server
        $mail->SMTPAuth = true;
        $mail->Username = 'apikey'; // Replace with your email
        $mail->Password = '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'; // Replace with your email password
        // $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        // $mail->Port = 587;

        // Email content
        $mail->setFrom('sales@tmcards.com', 'Quatation form Promotional Playing Cards Com');
        $mail->addAddress($to); // Add recipient
        $mail->addReplyTo('sales@tmcards.com');

        $mail->isHTML(true);
        $mail->Subject = $subject;
        $mail->Body = $emailBody;

        // Send the email
        $mail->send();
        header('location:thank-you.php');
    } catch (Exception $e) {
        echo "Failed to send the email. Error: {$mail->ErrorInfo}";
    }
} else {
    echo "Invalid request.";
}
