<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'phpmailer/PHPMailer.php';
require 'phpmailer/SMTP.php';
require 'phpmailer/Exception.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Block submission if customer is not ready
if (!isset($_POST['ready']) || $_POST['ready'] !== 'yes') {
    http_response_code(400);
    echo "Order rejected: Customer not ready.";
    exit;
}

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->SMTPAuth   = true;
    $mail->Username   = 'relicmedic008@gmail.com';
    $mail->Password   = 'lnrz xyxl zmsv xdrf';
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;

    $mail->setFrom('relicmedic008@gmail.com', 'Relic Health Orders');
    $mail->addAddress('relicteeth@gmail.com');
    

    $fullName = $_POST['full_name'] ?? '';
    $phone    = $_POST['phone']     ?? '';
    $altPhone = $_POST['alt_phone'] ?? '';
    $address  = $_POST['address']   ?? '';
    $state    = $_POST['state']     ?? '';
    $quantity = $_POST['quantity']  ?? '';
    $email    = $_POST['email']     ?? '';

    switch (trim($quantity)) {
        case "1 pack":  $price = "₦22,000"; break;
        case "2 packs": $price = "₦40,000"; break;
        case "3 packs": $price = "₦60,000"; break;
        default:        $price = "N/A";
    }

    $mail->isHTML(true);
    $mail->Subject = "New Order: $quantity-Relic Health Detox Tea ($fullName)";

    $emailRow = $email
        ? '<tr style="background-color:#f2f9f4;"><th style="text-align:left;padding:12px;">📧 Email</th><td style="padding:12px;"><a href="mailto:' . htmlspecialchars($email) . '" style="color:#1e6b2e;text-decoration:none;">' . htmlspecialchars($email) . '</a></td></tr>'
        : '';

    $mail->Body = '
    <div style="font-family:Arial,sans-serif;max-width:600px;margin:auto;border:1px solid #ddd;border-radius:10px;overflow:hidden;">
      <div style="background:#1e6b2e;color:white;padding:20px;text-align:center;">
        <h2 style="margin:0;font-size:20px;">🌿 Relic Health — New Detox Tea Order</h2>
      </div>
      <div style="background:#f9f9f9;">
        <table style="width:100%;border-collapse:collapse;font-size:15px;">
          <tr style="background:#fff;">
            <th style="text-align:left;padding:12px;width:40%;">📌 Name</th>
            <td style="padding:12px;">'.htmlspecialchars($fullName).'</td>
          </tr>
          <tr style="background:#f2f9f4;">
            <th style="text-align:left;padding:12px;">📞 Phone</th>
            <td style="padding:12px;"><a href="tel:'.preg_replace('/\D+/','',$phone).'" style="color:#1e6b2e;text-decoration:none;">'.htmlspecialchars($phone).'</a></td>
          </tr>
          <tr style="background:#fff;">
            <th style="text-align:left;padding:12px;">📞 Alt Phone</th>
            <td style="padding:12px;"><a href="tel:'.preg_replace('/\D+/','',$altPhone).'" style="color:#1e6b2e;text-decoration:none;">'.htmlspecialchars($altPhone ?: '—').'</a></td>
          </tr>
          '.$emailRow.'
          <tr style="background:#fff;">
            <th style="text-align:left;padding:12px;">🏠 Address</th>
            <td style="padding:12px;">'.nl2br(htmlspecialchars($address)).'</td>
          </tr>
          <tr style="background:#f2f9f4;">
            <th style="text-align:left;padding:12px;">🌍 State</th>
            <td style="padding:12px;">'.htmlspecialchars($state).'</td>
          </tr>
          <tr style="background:#fff;">
            <th style="text-align:left;padding:12px;">📦 Quantity</th>
            <td style="padding:12px;">'.htmlspecialchars($quantity).'</td>
          </tr>
          <tr style="background:#f2f9f4;">
            <th style="text-align:left;padding:12px;">💰 Total Price</th>
            <td style="padding:12px;font-weight:bold;color:#1e6b2e;">'.$price.'</td>
          </tr>
          <tr style="background:#fff;">
            <th style="text-align:left;padding:12px;">✅ Ready to Receive</th>
            <td style="padding:12px;color:green;font-weight:bold;">YES — Deliver Now</td>
          </tr>
        </table>
      </div>
      <div style="background:#eaf5ec;padding:15px;text-align:center;font-size:13px;color:#555;">
        📬 Order submitted on '.date("F j, Y \\a\\t g:i A").'
      </div>
    </div>';

    $mail->send();
    echo "success";
    exit;

} catch (Exception $e) {
    http_response_code(500);
    echo "Mailer Error: {$mail->ErrorInfo}";
    error_log("Mailer Error: {$mail->ErrorInfo}");
}
