<?php
require_once __DIR__ . '/../../config/database.php';
require_once __DIR__ . '/../../app/Utils.php';
require_once __DIR__ . '/../../app/Models/Admin.php';
require_once __DIR__ . '/../../app/Models/Parcel.php';
require_once __DIR__ . '/../../app/Models/Review.php';

// Check admin login BEFORE including header
if (!Admin::isLoggedIn()) {
    Utils::redirect('/routex/admin/login');
}

$page_title = 'Admin Dashboard - RoutexShipping';
require_once __DIR__ . '/../../includes/trackheader.php';

$parcel_model = new Parcel($pdo);
$review_model = new Review($pdo);
$admin_model = new Admin($pdo);

$stats = $parcel_model->getStats();
$current_admin = $admin_model->getCurrentAdmin();
$recent_parcels = $parcel_model->getAll(5, 0);
?>

<section class="min-h-screen bg-gray-100 py-8">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <!-- Page Header -->
        <div class="mb-8 flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
            <div>
                <h2 class="text-2xl font-bold text-gray-800">Dashboard</h2>
                <p class="text-gray-600">Welcome back, <?php echo htmlspecialchars($current_admin['full_name'] ?? $current_admin['username']); ?>!</p>
            </div>
            <div class="flex gap-3">
                <a href="/routex/admin/logout" class="bg-red-600 hover:bg-red-700 text-white px-4 py-2 rounded-lg shadow transition flex items-center">
                    <i class="fas fa-sign-out-alt mr-2"></i>Logout
                </a>
                <a href="/routex/admin/parcel-create" class="bg-blue-600 hover:bg-blue-700 text-white px-4 py-2 rounded-lg shadow transition flex items-center">
                    <i class="fas fa-plus mr-2"></i>Create Parcel
                </a>
            </div>
        </div>

        <!-- Stats Cards -->
        <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <!-- Total Parcels -->
            <div class="bg-blue-600 rounded-xl shadow-lg p-6 relative overflow-hidden text-white transition transform hover:-translate-y-1">
                <div class="relative z-10">
                    <p class="text-blue-100 text-sm font-medium mb-1">Total Parcels</p>
                    <h3 class="text-3xl font-bold"><?php echo $stats['total']; ?></h3>
                    <a href="/routex/admin/parcels" class="inline-block mt-4 text-sm text-blue-100 hover:text-white bg-blue-700 bg-opacity-50 px-3 py-1 rounded-full transition">View All <i class="fas fa-arrow-right ml-1 text-xs"></i></a>
                </div>
                <i class="fas fa-box absolute -right-4 -bottom-4 text-9xl text-white opacity-10 transform rotate-12"></i>
            </div>

            <!-- Delivered -->
            <div class="bg-green-600 rounded-xl shadow-lg p-6 relative overflow-hidden text-white transition transform hover:-translate-y-1">
                <div class="relative z-10">
                    <p class="text-green-100 text-sm font-medium mb-1">Delivered</p>
                    <h3 class="text-3xl font-bold"><?php echo $stats['by_status']['delivered'] ?? 0; ?></h3>
                    <a href="/routex/admin/parcels?status=delivered" class="inline-block mt-4 text-sm text-green-100 hover:text-white bg-green-700 bg-opacity-50 px-3 py-1 rounded-full transition">View All <i class="fas fa-arrow-right ml-1 text-xs"></i></a>
                </div>
                <i class="fas fa-check-circle absolute -right-4 -bottom-4 text-9xl text-white opacity-10 transform rotate-12"></i>
            </div>

            <!-- In Transit -->
            <div class="bg-yellow-500 rounded-xl shadow-lg p-6 relative overflow-hidden text-white transition transform hover:-translate-y-1">
                <div class="relative z-10">
                    <p class="text-yellow-100 text-sm font-medium mb-1">In Transit</p>
                    <h3 class="text-3xl font-bold"><?php echo $stats['by_status']['in_transit'] ?? 0; ?></h3>
                    <a href="/routex/admin/parcels?status=in_transit" class="inline-block mt-4 text-sm text-yellow-100 hover:text-white bg-yellow-600 bg-opacity-50 px-3 py-1 rounded-full transition">View All <i class="fas fa-arrow-right ml-1 text-xs"></i></a>
                </div>
                <i class="fas fa-truck absolute -right-4 -bottom-4 text-9xl text-white opacity-10 transform rotate-12"></i>
            </div>

            <!-- Total Reviews -->
            <div class="bg-purple-600 rounded-xl shadow-lg p-6 relative overflow-hidden text-white transition transform hover:-translate-y-1">
                <div class="relative z-10">
                    <p class="text-purple-100 text-sm font-medium mb-1">Total Reviews</p>
                    <h3 class="text-3xl font-bold"><?php echo $review_model->getCount(); ?></h3>
                    <a href="/routex/admin/reviews" class="inline-block mt-4 text-sm text-purple-100 hover:text-white bg-purple-700 bg-opacity-50 px-3 py-1 rounded-full transition">View All <i class="fas fa-arrow-right ml-1 text-xs"></i></a>
                </div>
                <i class="fas fa-star absolute -right-4 -bottom-4 text-9xl text-white opacity-10 transform rotate-12"></i>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
            <!-- Recent Parcels Table (Spans 2 cols) -->
            <div class="lg:col-span-2 bg-white rounded-xl shadow-md border border-gray-100 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-100 flex justify-between items-center bg-gray-50">
                    <h3 class="font-bold text-gray-800">Recent Shipments</h3>
                    <a href="/routex/admin/parcels" class="text-sm text-blue-600 hover:text-blue-800 font-medium">View All</a>
                </div>
                <div class="overflow-x-auto">
                    <table class="w-full">
                        <thead class="bg-gray-50">
                            <tr>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Tracking ID</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Receiver</th>
                                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                                <th class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Action</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-100">
                            <?php foreach ($recent_parcels as $parcel): ?>
                            <tr class="hover:bg-gray-50 transition">
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="font-mono text-sm font-semibold text-blue-600">#<?php echo htmlspecialchars($parcel['tracking_code']); ?></span>
                                    <div class="text-xs text-gray-500 mt-1"><?php echo Utils::formatDate($parcel['created_at']); ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <div class="text-sm font-medium text-gray-900"><?php echo htmlspecialchars($parcel['receiver_name']); ?></div>
                                    <div class="text-xs text-gray-500"><?php echo htmlspecialchars($parcel['receiver_country']); ?></div>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap">
                                    <span class="px-3 py-1 inline-flex text-xs leading-5 font-semibold rounded-full <?php echo Utils::getStatusColor($parcel['status']); ?>">
                                        <?php echo Utils::getStatusLabel($parcel['status']); ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                                    <a href="/routex/admin/parcel-edit?id=<?php echo $parcel['id']; ?>" class="text-gray-400 hover:text-blue-600 transition">
                                        <i class="fas fa-edit text-lg"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Quick Actions / Admin Menu (Spans 1 col) -->
            <div class="space-y-8">
                <!-- Admin Menu Card -->
                <div class="bg-white rounded-xl shadow-md border border-gray-100 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-100 bg-gray-50">
                        <h3 class="font-bold text-gray-800">Quick Navigation</h3>
                    </div>
                    <div class="p-4 grid grid-cols-1 gap-2">
                        <a href="/routex/admin/parcel-create" class="flex items-center p-3 rounded-lg hover:bg-blue-50 text-gray-700 hover:text-blue-600 transition group">
                            <div class="w-10 h-10 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center mr-3 group-hover:bg-blue-600 group-hover:text-white transition">
                                <i class="fas fa-plus"></i>
                            </div>
                            <span class="font-medium">Create Shipment</span>
                        </a>
                        <a href="/routex/admin/parcels" class="flex items-center p-3 rounded-lg hover:bg-purple-50 text-gray-700 hover:text-purple-600 transition group">
                            <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-3 group-hover:bg-purple-600 group-hover:text-white transition">
                                <i class="fas fa-list"></i>
                            </div>
                            <span class="font-medium">All Shipments</span>
                        </a>
                        <a href="/routex/admin/reviews" class="flex items-center p-3 rounded-lg hover:bg-yellow-50 text-gray-700 hover:text-yellow-600 transition group">
                            <div class="w-10 h-10 rounded-full bg-yellow-100 text-yellow-600 flex items-center justify-center mr-3 group-hover:bg-yellow-600 group-hover:text-white transition">
                                <i class="fas fa-star"></i>
                            </div>
                            <span class="font-medium">Reviews</span>
                        </a>
                        <a href="/routex/admin/email-logs" class="flex items-center p-3 rounded-lg hover:bg-green-50 text-gray-700 hover:text-green-600 transition group">
                            <div class="w-10 h-10 rounded-full bg-green-100 text-green-600 flex items-center justify-center mr-3 group-hover:bg-green-600 group-hover:text-white transition">
                                <i class="fas fa-envelope"></i>
                            </div>
                            <span class="font-medium">Email Logs</span>
                        </a>
                        <a href="/routex/admin/settings" class="flex items-center p-3 rounded-lg hover:bg-gray-100 text-gray-700 hover:text-gray-900 transition group">
                            <div class="w-10 h-10 rounded-full bg-gray-200 text-gray-600 flex items-center justify-center mr-3 group-hover:bg-gray-600 group-hover:text-white transition">
                                <i class="fas fa-cog"></i>
                            </div>
                            <span class="font-medium">System Settings</span>
                        </a>
                        <a href="/routex/admin/profile" class="flex items-center p-3 rounded-lg hover:bg-teal-50 text-gray-700 hover:text-teal-600 transition group">
                            <div class="w-10 h-10 rounded-full bg-teal-100 text-teal-600 flex items-center justify-center mr-3 group-hover:bg-teal-600 group-hover:text-white transition">
                                <i class="fas fa-user"></i>
                            </div>
                            <span class="font-medium">My Profile</span>
                        </a>
                    </div>
                </div>

                <!-- Mini Chart Placeholder or Info -->
                <div class="bg-gradient-to-br from-blue-600 to-blue-800 rounded-xl shadow-lg p-6 text-white">
                    <h3 class="font-bold text-lg mb-2">System Status</h3>
                    <p class="text-blue-100 text-sm mb-4">Your system is running smoothly. All services are operational.</p>
                    <div class="flex items-center justify-between text-sm">
                        <span>Server Time</span>
                        <span class="font-mono bg-blue-900 bg-opacity-50 px-2 py-1 rounded"><?php echo date('H:i:s'); ?></span>
                    </div>
                </div>
                </a>
            </div>
        </div>
    </div>
</section>

<footer class="bg-white border-t mt-20 no-print">
  <div class="max-w-7xl mx-auto px-4 py-6 text-center text-sm text-slate-500">
    © 2026 RoutexShipping. All rights reserved.
  </div>
</footer>
