<?php

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

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

// Check if form data is submitted
if(isset($_POST['name']) && isset($_POST['email']) && isset($_POST['phone']) && 
   isset($_POST['delivery_method']) && isset($_POST['payment_method']) && 
   isset($_POST['order_details'])) {
    
    // Collect form data
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $delivery_method = $_POST['delivery_method'];
    $address = isset($_POST['address']) ? $_POST['address'] : ($delivery_method == 'Farm Pickup' ? 'Farm Pickup - No address required' : 'Not provided');
    $payment_method = $_POST['payment_method'];
    $order_details = $_POST['order_details'];
    $special_instructions = isset($_POST['special_instructions']) ? $_POST['special_instructions'] : 'None provided';
    
    // Generate order number
    $order_number = 'NH' . date('Ymd') . rand(100, 999);

    // Admin Email HTML with NatureHills Agriculture Farm theme
    $htmlAdmin = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>New Farm Produce Order - NatureHills Organic Farm</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f8fdf6;
            margin: 0;
            padding: 20px;
            color: #1B5E20;
        }
        .email-container {
            max-width: 700px;
            background: #ffffff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(27, 94, 32, 0.1);
            margin: auto;
            border: 1px solid #e8f5e9;
        }
        .email-header {
            background: #1B5E20;
            color: #ffffff;
            text-align: center;
            padding: 20px;
            font-size: 24px;
            font-weight: bold;
            border-radius: 6px 6px 0 0;
            margin-bottom: 20px;
        }
        .section-title {
            background: #f8fdf6;
            font-size: 18px;
            font-weight: bold;
            padding: 12px 15px;
            margin: 25px 0 15px 0;
            border-radius: 6px;
            color: #1B5E20;
            border-left: 4px solid #FF8C00;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 15px 0;
        }
        th, td {
            padding: 12px 15px;
            border-bottom: 1px solid #e8f5e9;
            text-align: left;
        }
        th {
            background: #f8fdf6;
            font-weight: 600;
            color: #1B5E20;
            width: 35%;
        }
        .leaf-icon {
            color: #ffffff;
            margin-right: 8px;
        }
        .naturehills-logo {
            color: #1B5E20;
            font-weight: bold;
            font-size: 22px;
            margin-bottom: 10px;
            text-align: center;
        }
        .highlight {
            color: #FF8C00;
            font-weight: bold;
        }
        .urgent {
            background: #fff8f0;
            border: 1px solid #FF8C00;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
        }
        .farm-icon {
            text-align: center;
            font-size: 40px;
            margin: 10px 0;
        }
        .order-item {
            background: #f8fdf6;
            padding: 8px 12px;
            margin: 5px 0;
            border-radius: 4px;
            border-left: 3px solid #1B5E20;
        }
        .order-number {
            background: #1B5E20;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            text-align: center;
            font-size: 18px;
            font-weight: bold;
            margin: 15px 0;
        }
    </style>
</head>

<body>
    <div class='email-container'>
        <div class='naturehills-logo'>NatureHills - Organic Agriculture Farm</div>
        <div class='farm-icon'>🌱</div>
        <div class='order-number'>Order #: $order_number</div>
        <div class='email-header'>
            <span class='leaf-icon'>🌿</span> New Farm Produce Order
        </div>

        <!-- Customer Information -->
        <div class='section-title'>Customer Information</div>
        <table>
            <tr><th>Name:</th><td>$name</td></tr>
            <tr><th>Email:</th><td>$email</td></tr>
            <tr><th>Phone:</th><td>$phone</td></tr>
            <tr><th>Delivery Method:</th><td>$delivery_method</td></tr>
            <tr><th>Delivery Address:</th><td>$address</td></tr>
            <tr><th>Payment Method:</th><td>$payment_method</td></tr>
        </table>

        <!-- Order Details -->
        <div class='section-title'>Order Details</div>
        <div style='padding: 0 15px; line-height: 1.6; background: #f8fdf6; border-radius: 6px; padding: 15px; margin: 15px 0;'>
            " . nl2br($order_details) . "
        </div>

        <!-- Special Instructions -->
        <div class='section-title'>Special Instructions</div>
        <table>
            <tr><th>Customer Requests:</th><td>" . nl2br($special_instructions) . "</td></tr>
        </table>

        <!-- Order Information -->
        <div class='section-title'>Order Information</div>
        <div style='padding: 0 15px; line-height: 1.6;'>
            <p><strong>Order Date:</strong> " . date('F j, Y \a\t g:i A') . "</p>
            <p><strong>Order Number:</strong> $order_number</p>
            <p>This order requires produce harvesting and preparation.</p>
        </div>

        <div class='urgent'>
            <p><strong>🌿 Action Required:</strong> Please process this farm order within 24 hours and contact customer to confirm availability and schedule.</p>
            <p><strong>Farm Team Notes:</strong> Check produce availability, harvest fresh items, and prepare for $delivery_method.</p>
        </div>
    </div>
</body>
</html>";

    // Client Email HTML with NatureHills Agriculture Farm theme
    $htmlClient = "
<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Thank You for Your Farm Order - NatureHills</title>
    <style>
        body { 
            font-family: 'Arial', sans-serif; 
            background-color: #f8fdf6; 
            padding: 20px; 
            color: #1B5E20;
            line-height: 1.6;
        }
        .email-container { 
            max-width: 600px; 
            background: #ffffff; 
            padding: 30px; 
            margin: auto; 
            border-radius: 10px; 
            box-shadow: 0 0 15px rgba(27, 94, 32, 0.1);
            border: 1px solid #e8f5e9;
        }
        .header { 
            background: #1B5E20;
            color: #ffffff; 
            text-align: center; 
            padding: 25px; 
            border-radius: 8px;
            margin-bottom: 25px;
        }
        .header h2 {
            margin: 0;
            font-size: 28px;
        }
        .header p {
            margin: 10px 0 0;
            font-size: 16px;
        }
        .content { 
            margin: 25px 0; 
        }
        .details { 
            background: #f8fdf6; 
            padding: 20px; 
            border-radius: 8px;
            margin: 20px 0;
            border-left: 4px solid #FF8C00;
        }
        .highlight { 
            color: #FF8C00; 
            font-weight: bold; 
        }
        .order-summary { 
            width: 100%; 
            border-collapse: collapse; 
            margin: 15px 0; 
        }
        .order-summary th { 
            background: #e8f5e9; 
            text-align: left; 
            padding: 12px 15px;
            color: #1B5E20;
            width: 35%;
        }
        .order-summary td { 
            padding: 12px 15px; 
            border-bottom: 1px solid #e8f5e9; 
        }
        .footer {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e8f5e9;
            text-align: center;
            color: #666666;
        }
        .leaf-icon {
            color: #ffffff;
            margin-right: 8px;
        }
        ol {
            padding-left: 20px;
        }
        ol li {
            margin-bottom: 10px;
        }
        .naturehills-logo {
            color: #1B5E20;
            font-weight: bold;
            font-size: 22px;
            margin-bottom: 10px;
            text-align: center;
        }
        .next-steps {
            background: #fff8f0;
            border: 1px solid #FF8C00;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
        }
        .farm-icon {
            text-align: center;
            font-size: 40px;
            margin: 10px 0;
        }
        .order-confirmation {
            background: linear-gradient(135deg, #FF8C00, #1B5E20);
            color: white;
            padding: 15px;
            border-radius: 6px;
            text-align: center;
            margin: 20px 0;
        }
        .order-details-box {
            background: #f8fdf6;
            border: 1px solid #1B5E20;
            border-radius: 6px;
            padding: 15px;
            margin: 15px 0;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <div class='email-container'>
        <div class='naturehills-logo'>NatureHills - Organic Agriculture Farm</div>
        <div class='farm-icon'>🌱</div>
        <div class='order-confirmation'>
            <h3 style='margin: 0;'>Order Confirmation: #$order_number</h3>
        </div>
        <div class='header'>
            <h2><span class='leaf-icon'>🌿</span> Thank You, $name!</h2>
            <p>We've received your farm produce order</p>
        </div>
        
        <div class='content'>
            <p>Thank you for choosing NatureHills Organic Farm! Here's a summary of your fresh produce order:</p>
            
            <div class='details'>
                <h3 style='color: #1B5E20; margin-top: 0;'>Order Summary:</h3>
                <table class='order-summary'>
                    <tr><th>Name:</th><td>$name</td></tr>
                    <tr><th>Phone:</th><td>$phone</td></tr>
                    <tr><th>Delivery Method:</th><td>$delivery_method</td></tr>
                    <tr><th>Delivery Address:</th><td>$address</td></tr>
                    <tr><th>Payment Method:</th><td>$payment_method</td></tr>
                    <tr><th>Order Date:</th><td>" . date('F j, Y') . "</td></tr>
                </table>
            </div>

            <div class='order-details-box'>
                <h4 style='color: #1B5E20; margin-top: 0;'>Your Order Details:</h4>
                <p>" . nl2br($order_details) . "</p>
            </div>

            <div class='order-details-box'>
                <h4 style='color: #1B5E20; margin-top: 0;'>Special Instructions:</h4>
                <p>" . nl2br($special_instructions) . "</p>
            </div>

            <div class='next-steps'>
                <p><strong>Order Status:</strong> Your order is being processed! We'll contact you at <span class='highlight'>$phone</span> within 24 hours to confirm produce availability and schedule.</p>
                <p><strong>Farm Preparation:</strong> Our farm team is harvesting your fresh produce for maximum freshness.</p>
                <p><strong>Delivery Method:</strong> $delivery_method - " . ($delivery_method == 'Farm Pickup' ? 'Please bring this order number when picking up.' : 'We will deliver to the address provided.') . "</p>
            </div>

            <p>We're excited to provide you with fresh, organic farm produce!</p>
            
            <h3 style='color: #1B5E20;'>What to Expect Next:</h3>
            <ol>
                <li>Farm team confirmation call (within 24 hours)</li>
                <li>Produce availability confirmation</li>
                <li>Schedule confirmation for $delivery_method</li>
                <li>" . ($delivery_method == 'Farm Pickup' ? 'Pickup instructions and directions' : 'Delivery day preparation') . "</li>
                <li>Quality check and follow-up</li>
            </ol>
        </div>

        <div class='footer'>
            <p>Need immediate assistance or have questions?<br>
            Call us at <span class='highlight'>(555) 123-FARM</span> for urgent inquiries</p>
            <p style='margin-top: 15px; font-size: 14px;'>
                <span class='leaf-icon'>🌾</span> NatureHills - Growing Fresh, Organic Produce Since 1995
            </p>
        </div>
    </div>
</body>
</html>";

    $mail = new PHPMailer(true);

    try {
        // SMTP settings
        $mail->isSMTP();
        $mail->Host = 'smtp.gmail.com';
        $mail->SMTPAuth = true;
        $mail->Username = 'your_email@gmail.com';
        $mail->Password = 'Your App Password';
        $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $mail->Port = 587;

        // Sender and recipient settings for admin email
        $mail->setFrom('your_email@gmail.com', 'NatureHills Organic Farm');
        $mail->addAddress('your_email@gmail.com', 'NatureHills Organic Farm');
        
        // Content for admin
        $mail->isHTML(true);
        $mail->Subject = 'New Farm Produce Order #' . $order_number . ' from ' . $name;
        $mail->Body = $htmlAdmin;

        $mail->send();
        
        // Clear addresses and send to client
        $mail->clearAddresses();
        $mail->addAddress($email, $name);
        $mail->Subject = 'NatureHills Order Confirmation #' . $order_number;
        $mail->Body = $htmlClient;
        
        $mail->send();
        
        echo 'Farm produce order submitted successfully!';

    } catch (Exception $e) {
        echo "Error Occurred: {$mail->ErrorInfo}";
    }
} else {
    echo 'Please fill all required fields.';
}
?>