<?php
require_once 'includes/config.php';
require_once 'includes/db.php';
require_once 'includes/functions.php';

// Get order ID from URL
$order_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($order_id <= 0) {
    redirect(SITE_URL . '/index.php');
}

// Check if user is logged in
if (!isLoggedIn()) {
    $_SESSION['redirect_after_login'] = $_SERVER['REQUEST_URI'];
    redirect(SITE_URL . '/auth/login.php');
}

// Get order details with user information
$stmt = $pdo->prepare("
    SELECT o.*, u.username, u.email 
    FROM orders o 
    JOIN users u ON o.user_id = u.id 
    WHERE o.id = ?
");
$stmt->execute([$order_id]);
$order = $stmt->fetch(PDO::FETCH_ASSOC);

// Check if order exists and user has permission to view it
if (!$order || ($order['user_id'] != $_SESSION['user_id'] && !isAdmin())) {
    redirect(SITE_URL . '/index.php');
}

// Get order items with product information
$stmt = $pdo->prepare("
    SELECT oi.*, p.name, p.price as product_price
    FROM order_items oi
    JOIN products p ON oi.product_id = p.id
    WHERE oi.order_id = ?
");
$stmt->execute([$order_id]);
$order_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>فاتورة #<?php echo $order['id']; ?> - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="<?php echo SITE_URL; ?>/assets/css/style.css">
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@400;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Tajawal', Arial, sans-serif;
            background-color: #fff;
            color: #333;
            margin: 0;
            padding: 20px;
        }
        
        .invoice-container {
            max-width: 800px;
            margin: 0 auto;
            background: #fff;
            padding: 30px;
            border: 1px solid #ddd;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        
        .invoice-header {
            text-align: center;
            border-bottom: 2px solid #333;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        
        .invoice-header h1 {
            margin: 0;
            color: #333;
        }
        
        .invoice-details {
            display: flex;
            justify-content: space-between;
            margin-bottom: 30px;
        }
        
        .invoice-section {
            flex: 1;
        }
        
        .invoice-section h3 {
            margin-top: 0;
            border-bottom: 1px solid #eee;
            padding-bottom: 5px;
        }
        
        .invoice-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        
        .invoice-table th,
        .invoice-table td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: left;
        }
        
        .invoice-table th {
            background-color: #f5f5f5;
        }
        
        .text-right {
            text-align: right;
        }
        
        .total-row {
            font-weight: bold;
        }
        
        .print-button {
            text-align: center;
            margin-top: 30px;
        }
        
        .print-button button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
            border-radius: 4px;
        }
        
        .print-button button:hover {
            background-color: #2980b9;
        }
        
        @media print {
            .print-button {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="invoice-container">
        <div class="invoice-header">
            <h1>فاتورة</h1>
            <p>رقم الفاتورة: <?php echo $order['id']; ?></p>
            <p>التاريخ: <?php echo date('F j, Y', strtotime($order['order_date'])); ?></p>
        </div>
        
        <div class="invoice-details">
            <div class="invoice-section">
                <h3>الفاتورة إلى</h3>
                <p><?php echo htmlspecialchars($order['username']); ?></p>
                <p><?php echo htmlspecialchars($order['email']); ?></p>
                <p><?php echo nl2br(htmlspecialchars($order['shipping_address'])); ?></p>
                <p><strong>طريقة الدفع:</strong> <?php echo htmlspecialchars($order['payment_method']); ?></p>
            </div>
            
            <div class="invoice-section">
                <h3>من</h3>
                <p><?php echo SITE_NAME; ?></p>
                <p>storeweb@example.com</p>
                <p>123 شارع المتجر، المدينة، البلد</p>
            </div>
        </div>
        
        <table class="invoice-table">
            <thead>
                <tr>
                    <th>الوصف</th>
                    <th>السعر</th>
                    <th>الكمية</th>
                    <th>الإجمالي</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($order_items as $item): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($item['name']); ?></td>
                        <td>$<?php echo number_format($item['product_price'], 2); ?></td>
                        <td><?php echo $item['quantity']; ?></td>
                        <td>$<?php echo number_format($item['price'] * $item['quantity'], 2); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="total-row">
                    <td colspan="3" class="text-right">الإجمالي الفرعي:</td>
                    <td>$<?php echo number_format($order['total_amount'], 2); ?></td>
                </tr>
                <tr class="total-row">
                    <td colspan="3" class="text-right">الضريبة (0%):</td>
                    <td>$0.00</td>
                </tr>
                <tr class="total-row">
                    <td colspan="3" class="text-right">الشحن:</td>
                    <td>$0.00</td>
                </tr>
                <tr class="total-row">
                    <td colspan="3" class="text-right">الإجمالي:</td>
                    <td>$<?php echo number_format($order['total_amount'], 2); ?></td>
                </tr>
            </tfoot>
        </table>
        
        <div class="print-button">
            <button onclick="window.print()">طباعة الفاتورة</button>
            <?php if (isLoggedIn()): ?>
                <a href="<?php echo SITE_URL; ?>/index.php" class="btn btn-outline" style="margin-left: 10px;">العودة إلى الصفحة الرئيسية</a>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>