<?php
// ECWID Order Confirmation Page
// Configuration
$store_id = '10569756';
$secret_token = 'secret_qv9VpA4YrCV5ytSWzMkat9FNZRweWL1W';

// Get order number from URL
$order_id = isset($_GET['order']) ? $_GET['order'] : null;

if (!$order_id) {
    die('Order number not provided.');
}

// Call ECWID API to get order details
$api_url = "https://app.ecwid.com/api/v3/{$store_id}/orders/{$order_id}";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Authorization: Bearer {$secret_token}"
]);

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($http_code !== 200) {
    die('Order not found or API error.');
}

$order = json_decode($response, true);

// Extract order details
$customer_name = $order['billingPerson']['name'] ?? 'Customer';
$customer_email = $order['email'] ?? '';
$order_number = $order['orderNumber'] ?? $order_id;
$order_date = date('F j, Y', strtotime($order['createDate'] ?? 'now'));
$order_total = number_format($order['total'] ?? 0, 2);
$shipping_method = $order['shippingOption']['shippingMethodName'] ?? 'Standard';

// Extract product details
$item = $order['items'][0] ?? [];
$product_name = $item['name'] ?? 'Poster';
$poster_size = '';
$assurance_proof = '';
$file_url = '';
$file_name = '';

// Parse selected options
if (isset($item['selectedOptions'])) {
    foreach ($item['selectedOptions'] as $option) {
        if ($option['name'] === 'Poster Size (Portrait or Landscape)') {
            $poster_size = $option['value'] ?? '';
        }
        if ($option['name'] === 'Assurance Proof') {
            $assurance_proof = $option['value'] ?? '';
        }
        if ($option['type'] === 'FILES' && isset($option['files'][0])) {
            $file_url = $option['files'][0]['url'] ?? '';
            $file_name = $option['files'][0]['name'] ?? 'poster.pdf';
        }
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Order Confirmed - #<?php echo htmlspecialchars($order_number); ?></title>
    <style>
        * { box-sizing: border-box; margin: 0; padding: 0; }
        body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif; background: #f5f5f5; color: #333; line-height: 1.6; }
        .container { max-width: 800px; margin: 0 auto; padding: 20px; }
        .header { background: #2c5aa0; color: white; padding: 30px; text-align: center; border-radius: 8px 8px 0 0; }
        .header h1 { font-size: 28px; margin-bottom: 10px; }
        .header .checkmark { font-size: 60px; margin-bottom: 15px; }
        .content { background: white; padding: 30px; border-radius: 0 0 8px 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
        .section { margin-bottom: 25px; padding-bottom: 25px; border-bottom: 1px solid #eee; }
        .section:last-child { border-bottom: none; margin-bottom: 0; padding-bottom: 0; }
        .section h2 { font-size: 18px; color: #2c5aa0; margin-bottom: 15px; }
        .detail-row { display: flex; justify-content: space-between; padding: 8px 0; }
        .detail-row .label { color: #666; }
        .detail-row .value { font-weight: 500; text-align: right; }
        .pdf-preview { width: 100%; height: 500px; border: 1px solid #ddd; border-radius: 4px; }
        .no-preview { background: #f9f9f9; padding: 40px; text-align: center; border-radius: 4px; }
        .btn { display: inline-block; background: #2c5aa0; color: white; padding: 12px 25px; text-decoration: none; border-radius: 4px; margin-top: 15px; }
        .btn:hover { background: #234a8a; }
        .footer { text-align: center; padding: 20px; color: #666; font-size: 14px; }
        @media (max-width: 600px) { .detail-row { flex-direction: column; } .detail-row .value { text-align: left; margin-top: 3px; } }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <div class="checkmark">✓</div>
            <h1>Order Confirmed!</h1>
            <p>Thank you for your order, <?php echo htmlspecialchars($customer_name); ?>!</p>
        </div>
        
        <div class="content">
            <div class="section">
                <h2>Order Details</h2>
                <div class="detail-row">
                    <span class="label">Order Number</span>
                    <span class="value">#<?php echo htmlspecialchars($order_number); ?></span>
                </div>
                <div class="detail-row">
                    <span class="label">Order Date</span>
                    <span class="value"><?php echo htmlspecialchars($order_date); ?></span>
                </div>
                <div class="detail-row">
                    <span class="label">Product</span>
                    <span class="value"><?php echo htmlspecialchars($product_name); ?></span>
                </div>
                <div class="detail-row">
                    <span class="label">Poster Size</span>
                    <span class="value"><?php echo htmlspecialchars($poster_size); ?></span>
                </div>
                <div class="detail-row">
                    <span class="label">Assurance Proof</span>
                    <span class="value"><?php echo htmlspecialchars($assurance_proof); ?></span>
                </div>
                <div class="detail-row">
                    <span class="label">Shipping Method</span>
                    <span class="value"><?php echo htmlspecialchars($shipping_method); ?></span>
                </div>
                <div class="detail-row">
                    <span class="label">Order Total</span>
                    <span class="value">$<?php echo htmlspecialchars($order_total); ?></span>
                </div>
            </div>
            
            <div class="section">
                <h2>Your Poster Preview</h2>
                <?php if ($file_url): ?>
                    <p style="margin-bottom: 15px; color: #666;">File: <?php echo htmlspecialchars($file_name); ?></p>
                    <iframe src="<?php echo htmlspecialchars($file_url); ?>" class="pdf-preview"></iframe>
                    <br>
                    <a href="<?php echo htmlspecialchars($file_url); ?>" class="btn" target="_blank">Download PDF</a>
                <?php else: ?>
                    <div class="no-preview">
                        <p>No file preview available.</p>
                    </div>
                <?php endif; ?>
            </div>
            
            <div class="section">
                <h2>What's Next?</h2>
                <p>We've received your poster file and will begin processing your order shortly. You will receive an email confirmation at <strong><?php echo htmlspecialchars($customer_email); ?></strong> with tracking information once your order ships.</p>
                <p style="margin-top: 15px;">Questions? Contact us at <a href="mailto:access@posterpresentations.com">access@posterpresentations.com</a> or call 510-649-3001.</p>
            </div>
        </div>
        
        <div class="footer">
            <p>&copy; <?php echo date('Y'); ?> PosterPresentations.com - Canterbury Media Services, Inc.</p>
        </div>
    </div>
</body>
</html>

