<?php
/**
 * Order Confirmation Page
 * Digital Product Store
 */

require_once __DIR__ . '/config/database.php';
require_login();

$order_id = isset($_GET['order_id']) ? (int)$_GET['order_id'] : 0;

if (!$order_id) {
    set_flash_message('Invalid order.', 'error');
    header('Location: ' . BASE_URL . '/index.php');
    exit;
}

$order = get_order_by_id($order_id);

if (!$order || $order['user_id'] != get_current_user_id()) {
    set_flash_message('Order not found.', 'error');
    header('Location: ' . BASE_URL . '/index.php');
    exit;
}

$order_items = get_order_items($order_id);

$page_title = 'Order Confirmation - ' . $order['order_number'];
include __DIR__ . '/header.php';
?>

<section class="py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-lg-8">
                <div class="text-center mb-5">
                    <i class="fas fa-check-circle fa-5x text-success mb-3"></i>
                    <h1>Thank You!</h1>
                    <p class="lead">Your order has been placed successfully.</p>
                </div>
                
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <i class="fas fa-receipt me-2"></i>Order Summary
                    </div>
                    <div class="card-body">
                        <div class="row mb-4">
                            <div class="col-md-6">
                                <h6>Order Information</h6>
                                <p class="mb-1"><strong>Order Number:</strong> <?php echo $order['order_number']; ?></p>
                                <p class="mb-1"><strong>Date:</strong> <?php echo format_date($order['created_at'], 'M d, Y h:i A'); ?></p>
                                <p class="mb-1"><strong>Payment Method:</strong> <?php echo ucfirst($order['payment_method']); ?></p>
                                <p class="mb-0"><strong>Payment Status:</strong> 
                                    <span class="badge bg-<?php echo $order['payment_status'] === 'paid' ? 'success' : 'warning'; ?>">
                                        <?php echo ucfirst($order['payment_status']); ?>
                                    </span>
                                </p>
                            </div>
                            <div class="col-md-6">
                                <h6>Customer Information</h6>
                                <p class="mb-1"><strong>Name:</strong> <?php echo htmlspecialchars($order['customer_name']); ?></p>
                                <p class="mb-1"><strong>Email:</strong> <?php echo htmlspecialchars($order['customer_email']); ?></p>
                                <p class="mb-0"><strong>Phone:</strong> <?php echo htmlspecialchars($order['customer_phone'] ?: 'N/A'); ?></p>
                            </div>
                        </div>
                        
                        <h6>Order Items</h6>
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Product</th>
                                        <th class="text-center">Qty</th>
                                        <th class="text-end">Price</th>
                                        <th class="text-end">Total</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($order_items as $item): ?>
                                    <tr>
                                        <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                                        <td class="text-center"><?php echo $item['quantity']; ?></td>
                                        <td class="text-end"><?php echo format_price($item['product_price']); ?></td>
                                        <td class="text-end"><?php echo format_price($item['subtotal']); ?></td>
                                    </tr>
                                    <?php endforeach; ?>
                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="3" class="text-end"><strong>Subtotal</strong></td>
                                        <td class="text-end"><?php echo format_price($order['subtotal']); ?></td>
                                    </tr>
                                    <?php if ($order['tax'] > 0): ?>
                                    <tr>
                                        <td colspan="3" class="text-end"><strong>Tax</strong></td>
                                        <td class="text-end"><?php echo format_price($order['tax']); ?></td>
                                    </tr>
                                    <?php endif; ?>
                                    <?php if ($order['shipping'] > 0): ?>
                                    <tr>
                                        <td colspan="3" class="text-end"><strong>Shipping</strong></td>
                                        <td class="text-end"><?php echo format_price($order['shipping']); ?></td>
                                    </tr>
                                    <?php endif; ?>
                                    <?php if ($order['discount'] > 0): ?>
                                    <tr>
                                        <td colspan="3" class="text-end"><strong>Discount</strong></td>
                                        <td class="text-end text-danger">-<?php echo format_price($order['discount']); ?></td>
                                    </tr>
                                    <?php endif; ?>
                                    <tr>
                                        <td colspan="3" class="text-end"><h5>Total</h5></td>
                                        <td class="text-end"><h5 class="text-primary"><?php echo format_price($order['total']); ?></h5></td>
                                    </tr>
                                </tfoot>
                            </table>
                        </div>
                    </div>
                </div>
                
                <?php if ($order['payment_status'] === 'paid'): ?>
                <!-- Downloads Section -->
                <div class="card mb-4">
                    <div class="card-header bg-success text-white">
                        <i class="fas fa-download me-2"></i>Your Downloads
                    </div>
                    <div class="card-body">
                        <p class="text-muted mb-3">Your payment has been confirmed. You can now download your products.</p>
                        
                        <div class="list-group">
                            <?php foreach ($order_items as $item): 
                                $download_link = create_download_link($order_id, $item['product_id'], get_current_user_id());
                            ?>
                            <div class="list-group-item d-flex justify-content-between align-items-center">
                                <div>
                                    <h6 class="mb-1"><?php echo htmlspecialchars($item['product_name']); ?></h6>
                                    <small class="text-muted">Digital Download</small>
                                </div>
                                <?php if ($download_link['success']): ?>
                                <a href="<?php echo $download_link['url']; ?>" class="btn btn-success btn-sm">
                                    <i class="fas fa-download me-2"></i>Download
                                </a>
                                <?php endif; ?>
                            </div>
                            <?php endforeach; ?>
                        </div>
                        
                        <div class="alert alert-info mt-3 mb-0">
                            <i class="fas fa-info-circle me-2"></i>
                            You can also access your downloads from your <a href="<?php echo BASE_URL; ?>/account/orders.php">account dashboard</a>.
                        </div>
                    </div>
                </div>
                <?php else: ?>
                <!-- Pending Payment -->
                <div class="card mb-4">
                    <div class="card-header bg-warning text-dark">
                        <i class="fas fa-clock me-2"></i>Payment Pending
                    </div>
                    <div class="card-body">
                        <p>Your order is pending payment. Once payment is confirmed, you will be able to download your products.</p>
                        
                        <?php if ($order['payment_method'] === 'eft' || $order['payment_method'] === 'bank_transfer'): ?>
                        <div class="alert alert-info">
                            <h6><i class="fas fa-university me-2"></i>Bank Transfer Instructions</h6>
                            <p class="mb-2">Please complete your payment using the bank details below:</p>
                            <?php
                            $eft = get_payment_gateway('eft');
                            $config = $eft['config'] ?? [];
                            ?>
                            <ul class="mb-0">
                                <li><strong>Bank:</strong> <?php echo htmlspecialchars($config['bank_name'] ?? ''); ?></li>
                                <li><strong>Account:</strong> <?php echo htmlspecialchars($config['account_number'] ?? ''); ?></li>
                                <li><strong>Reference:</strong> <?php echo $order['order_number']; ?></li>
                            </ul>
                        </div>
                        <?php elseif ($order['payment_method'] === 'bitcoin'): ?>
                        <div class="alert alert-info">
                            <h6><i class="fab fa-bitcoin me-2"></i>Bitcoin Payment</h6>
                            <p>Please send the payment to the Bitcoin address provided. Include your order number as reference.</p>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
                <?php endif; ?>
                
                <div class="text-center">
                    <a href="<?php echo BASE_URL; ?>/index.php" class="btn btn-primary me-2">
                        <i class="fas fa-home me-2"></i>Continue Shopping
                    </a>
                    <a href="<?php echo BASE_URL; ?>/account/orders.php" class="btn btn-outline-primary">
                        <i class="fas fa-list me-2"></i>View My Orders
                    </a>
                </div>
            </div>
        </div>
    </div>
</section>

<?php include __DIR__ . '/footer.php'; ?>
