<?php
session_start();
require_once '../../config/database.php';

if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
    header('Location: ../login.php');
    exit;
}

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

// جلب جميع المانيفستات
$query = "SELECT sm.*, sc.name as company_name,
          (SELECT COUNT(*) FROM manifest_orders WHERE manifest_id = sm.id) as orders_count
          FROM shipping_manifests sm
          LEFT JOIN shipping_companies sc ON sm.shipping_company_id = sc.id
          ORDER BY sm.created_at DESC";
$stmt = $db->prepare($query);
$stmt->execute();
$manifests = $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>قائمة المانيفستات</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Tajawal:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body { font-family: 'Tajawal', sans-serif; }
    </style>
</head>
<body class="bg-gray-50">
    <div class="min-h-screen p-6">
        <!-- Header -->
        <div class="max-w-7xl mx-auto mb-6">
            <div class="flex items-center justify-between">
                <div class="flex items-center gap-4">
                    <a href="../orders/index.php" class="text-gray-600 hover:text-gray-900">
                        <i class="fas fa-arrow-right text-xl"></i>
                    </a>
                    <h1 class="text-3xl font-bold">قائمة المانيفستات</h1>
                </div>
                <a href="create-manifest.php" class="bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700 font-bold">
                    <i class="fas fa-plus"></i>
                    إنشاء مانيفست جديد
                </a>
            </div>
        </div>

        <!-- Statistics -->
        <div class="max-w-7xl mx-auto grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow-sm border p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">إجمالي المانيفستات</p>
                        <p class="text-3xl font-bold text-gray-900"><?php echo count($manifests); ?></p>
                    </div>
                    <div class="w-12 h-12 bg-blue-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-file-invoice text-2xl text-blue-600"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-sm border p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">إجمالي الطلبات</p>
                        <p class="text-3xl font-bold text-gray-900">
                            <?php echo array_sum(array_column($manifests, 'total_orders')); ?>
                        </p>
                    </div>
                    <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-box text-2xl text-green-600"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-sm border p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">إجمالي المبالغ</p>
                        <p class="text-2xl font-bold text-gray-900">
                            <?php echo number_format(array_sum(array_column($manifests, 'total_amount')), 0); ?> ر.س
                        </p>
                    </div>
                    <div class="w-12 h-12 bg-purple-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-money-bill-wave text-2xl text-purple-600"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-sm border p-6">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-sm text-gray-600">الصافي</p>
                        <p class="text-2xl font-bold text-green-600">
                            <?php echo number_format(array_sum(array_column($manifests, 'net_amount')), 0); ?> ر.س
                        </p>
                    </div>
                    <div class="w-12 h-12 bg-green-100 rounded-lg flex items-center justify-center">
                        <i class="fas fa-hand-holding-usd text-2xl text-green-600"></i>
                    </div>
                </div>
            </div>
        </div>

        <!-- Manifests List -->
        <div class="max-w-7xl mx-auto bg-white rounded-lg shadow-sm border">
            <?php if (empty($manifests)): ?>
                <div class="text-center py-16 text-gray-500">
                    <i class="fas fa-inbox text-6xl mb-4"></i>
                    <p class="text-xl mb-4">لا توجد مانيفستات بعد</p>
                    <a href="create-manifest.php" class="inline-block bg-blue-600 text-white px-6 py-3 rounded-lg hover:bg-blue-700">
                        إنشاء أول مانيفست
                    </a>
                </div>
            <?php else: ?>
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50 border-b">
                            <tr>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">رقم المانيفست</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">شركة الشحن</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">المندوب</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">عدد الطلبات</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">إجمالي المبالغ</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">تكلفة الشحن</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">الصافي</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">التاريخ</th>
                                <th class="px-6 py-4 text-right text-sm font-bold text-gray-700">إجراءات</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y">
                            <?php foreach ($manifests as $manifest): ?>
                                <tr class="hover:bg-gray-50">
                                    <td class="px-6 py-4 font-bold text-blue-600">
                                        <?php echo htmlspecialchars($manifest['manifest_number']); ?>
                                    </td>
                                    <td class="px-6 py-4">
                                        <?php echo htmlspecialchars($manifest['company_name'] ?? 'غير محدد'); ?>
                                    </td>
                                    <td class="px-6 py-4">
                                        <?php echo htmlspecialchars($manifest['delegate_name'] ?? '-'); ?>
                                    </td>
                                    <td class="px-6 py-4 font-bold">
                                        <?php echo $manifest['total_orders']; ?> طلب
                                    </td>
                                    <td class="px-6 py-4 font-bold text-green-600">
                                        <?php echo number_format($manifest['total_amount'], 2); ?> ر.س
                                    </td>
                                    <td class="px-6 py-4 text-red-600">
                                        <?php echo number_format($manifest['shipping_cost'], 2); ?> ر.س
                                    </td>
                                    <td class="px-6 py-4 font-bold text-blue-600 text-lg">
                                        <?php echo number_format($manifest['net_amount'], 2); ?> ر.س
                                    </td>
                                    <td class="px-6 py-4 text-sm text-gray-600">
                                        <?php echo date('Y-m-d', strtotime($manifest['created_at'])); ?>
                                    </td>
                                    <td class="px-6 py-4">
                                        <div class="flex gap-2 flex-wrap">
                                            <a href="print-manifest.php?id=<?php echo $manifest['id']; ?>" 
                                               target="_blank"
                                               class="inline-block bg-blue-600 text-white px-3 py-2 rounded hover:bg-blue-700 text-sm"
                                               title="طباعة المانيفست">
                                                <i class="fas fa-file-alt"></i>
                                                المانيفست
                                            </a>
                                            <a href="print-labels.php?id=<?php echo $manifest['id']; ?>" 
                                               target="_blank"
                                               class="inline-block bg-green-600 text-white px-3 py-2 rounded hover:bg-green-700 text-sm"
                                               title="طباعة البوالص (4/صفحة)">
                                                <i class="fas fa-tags"></i>
                                                البوالص
                                            </a>
                                            <a href="print-thermal-labels.php?id=<?php echo $manifest['id']; ?>" 
                                               target="_blank"
                                               class="inline-block bg-orange-600 text-white px-3 py-2 rounded hover:bg-orange-700 text-sm"
                                               title="ملصقات حرارية (طابعة ملصقات)">
                                                <i class="fas fa-print"></i>
                                                حراري
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</body>
</html>
