<?php
require_once '../../config/database.php';

$order_id = $_GET['id'] ?? 0;

if (!$order_id) {
    die('رقم الطلب غير صحيح');
}

$database = new Database();
$db = $database->getConnection();

// Get order details
$query = "SELECT o.*, sc.name as shipping_company_name 
          FROM orders o 
          LEFT JOIN shipping_companies sc ON o.shipping_company_id = sc.id 
          WHERE o.id = ?";
$stmt = $db->prepare($query);
$stmt->execute([$order_id]);
$order = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$order) {
    die('الطلب غير موجود');
}

// Get order items
$query = "SELECT oi.*, p.name as product_name 
          FROM order_items oi 
          JOIN products p ON oi.product_id = p.id 
          WHERE oi.order_id = ?";
$stmt = $db->prepare($query);
$stmt->execute([$order_id]);
$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; ?></title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { 
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
            background: #f5f5f5;
        }
        .invoice {
            max-width: 800px;
            margin: 0 auto;
            background: white;
            padding: 40px;
            box-shadow: 0 0 20px rgba(0,0,0,0.1);
        }
        .header {
            text-align: center;
            border-bottom: 3px solid #E57393;
            padding-bottom: 20px;
            margin-bottom: 30px;
        }
        .header h1 {
            color: #E57393;
            font-size: 32px;
            margin-bottom: 5px;
        }
        .header p {
            color: #666;
            font-size: 14px;
        }
        .order-info {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 30px;
        }
        .info-box {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 8px;
            border-right: 4px solid #E57393;
        }
        .info-box h3 {
            color: #E57393;
            font-size: 14px;
            margin-bottom: 10px;
            text-transform: uppercase;
        }
        .info-box p {
            color: #333;
            line-height: 1.6;
            font-size: 14px;
        }
        .barcode {
            text-align: center;
            margin: 20px 0;
            padding: 15px;
            background: #f9f9f9;
            border-radius: 8px;
        }
        .barcode-number {
            font-size: 24px;
            font-weight: bold;
            letter-spacing: 3px;
            color: #333;
            font-family: monospace;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }
        th, td {
            padding: 12px;
            text-align: right;
            border-bottom: 1px solid #ddd;
        }
        th {
            background: #E57393;
            color: white;
            font-weight: 600;
        }
        tr:hover {
            background: #f9f9f9;
        }
        .total-section {
            margin-top: 20px;
            text-align: left;
        }
        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 10px 0;
            font-size: 16px;
        }
        .total-row.grand-total {
            border-top: 2px solid #E57393;
            font-size: 24px;
            font-weight: bold;
            color: #E57393;
            padding-top: 15px;
        }
        .cod-badge {
            display: inline-block;
            background: #E57393;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            font-size: 18px;
            font-weight: bold;
            margin-top: 10px;
        }
        .footer {
            margin-top: 40px;
            padding-top: 20px;
            border-top: 2px solid #ddd;
            text-align: center;
            color: #666;
            font-size: 12px;
        }
        @media print {
            body { background: white; padding: 0; }
            .invoice { box-shadow: none; }
            .no-print { display: none; }
        }
        .print-btn {
            position: fixed;
            top: 20px;
            left: 20px;
            background: #E57393;
            color: white;
            border: none;
            padding: 12px 24px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            box-shadow: 0 4px 6px rgba(0,0,0,0.1);
        }
        .print-btn:hover {
            background: #d15a7a;
        }
    </style>
</head>
<body>
    <button onclick="window.print()" class="print-btn no-print">
        🖨️ طباعة
    </button>

    <div class="invoice">
        <!-- Header -->
        <div class="header">
            <h1>🌸 Roz Skin</h1>
            <p>بوليصة شحن</p>
        </div>

        <!-- Barcode -->
        <div class="barcode">
            <div class="barcode-number">ORD-<?php echo str_pad($order_id, 6, '0', STR_PAD_LEFT); ?></div>
            <p style="color: #666; font-size: 12px; margin-top: 5px;">رقم الطلب</p>
        </div>

        <!-- Order Info -->
        <div class="order-info">
            <div class="info-box">
                <h3>📦 معلومات الطلب</h3>
                <p><strong>رقم الطلب:</strong> #<?php echo $order_id; ?></p>
                <p><strong>التاريخ:</strong> <?php echo date('Y-m-d H:i', strtotime($order['created_at'])); ?></p>
                <p><strong>الحالة:</strong> <?php echo $order['status']; ?></p>
                <?php if ($order['shipping_company_name']): ?>
                    <p><strong>شركة الشحن:</strong> <?php echo htmlspecialchars($order['shipping_company_name']); ?></p>
                <?php endif; ?>
                <?php if ($order['tracking_number']): ?>
                    <p><strong>رقم التتبع:</strong> <?php echo htmlspecialchars($order['tracking_number']); ?></p>
                <?php endif; ?>
            </div>

            <div class="info-box">
                <h3>👤 معلومات العميل</h3>
                <p><strong>الاسم:</strong> <?php echo htmlspecialchars($order['first_name'] . ' ' . $order['last_name']); ?></p>
                <p><strong>الهاتف:</strong> <?php echo htmlspecialchars($order['phone']); ?></p>
                <p><strong>البريد:</strong> <?php echo htmlspecialchars($order['email']); ?></p>
                <p><strong>العنوان:</strong> <?php echo htmlspecialchars($order['address'] . ', ' . $order['city']); ?></p>
                <?php if ($order['notes']): ?>
                    <p><strong>ملاحظات:</strong> <?php echo htmlspecialchars($order['notes']); ?></p>
                <?php endif; ?>
            </div>
        </div>

        <!-- Items Table -->
        <table>
            <thead>
                <tr>
                    <th>المنتج</th>
                    <th>الكمية</th>
                    <th>السعر</th>
                    <th>الإجمالي</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                    <tr>
                        <td><?php echo htmlspecialchars($item['product_name']); ?></td>
                        <td><?php echo $item['quantity']; ?></td>
                        <td>EGP <?php echo number_format($item['price'], 2); ?></td>
                        <td>EGP <?php echo number_format($item['price'] * $item['quantity'], 2); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Total Section -->
        <div class="total-section">
            <div class="total-row grand-total">
                <span>المبلغ الإجمالي:</span>
                <span>EGP <?php echo number_format($order['total'], 2); ?></span>
            </div>
            <div style="text-align: center;">
                <div class="cod-badge">💵 الدفع عند الاستلام</div>
            </div>
        </div>

        <!-- Footer -->
        <div class="footer">
            <p>شكراً لتعاملكم معنا | Roz Skin</p>
            <p>للاستفسارات: 01234567890 | info@rozskin.com</p>
        </div>
    </div>

    <script>
        // Auto print on load (optional)
        // window.onload = function() { window.print(); }
    </script>
</body>
</html>
