<?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';

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

$page_title = 'Manage Parcels - Admin';
require_once __DIR__ . '/../../includes/trackheader.php';

$parcel_model = new Parcel($pdo);
$page = $_GET['page'] ?? 1;
$page = max(1, intval($page));
$limit = 20;
$offset = ($page - 1) * $limit;

$parcels = $parcel_model->getAll($limit, $offset);
$total = $parcel_model->getCount();
$total_pages = ceil($total / $limit);
?>

<section class="min-h-screen bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Header -->
        <div class="flex flex-col md:flex-row justify-between items-center mb-8 gap-4">
            <div>
                <h1 class="text-4xl font-bold text-gray-900">Manage Parcels</h1>
                <p class="text-gray-600 mt-2">Total: <?php echo $total; ?> parcels</p>
            </div>
            <div class="flex gap-3">
                <a href="/routex/admin/dashboard" class="px-6 py-3 bg-white text-gray-700 border-2 border-gray-300 rounded-lg font-semibold hover:bg-gray-50 transition-all duration-300">
                    <i class="fas fa-arrow-left mr-2"></i>Dashboard
                </a>
                <a href="/routex/admin/parcel-create" class="btn-primary">
                    <i class="fas fa-plus mr-2"></i>Create Parcel
                </a>
            </div>
        </div>

        <!-- Parcels Table -->
        <div class="bg-white rounded-xl shadow-md overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-100 border-b">
                        <tr>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Tracking Code</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Receiver</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Status</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Location</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Created</th>
                            <th class="px-6 py-4 text-left text-sm font-semibold text-gray-900">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($parcels as $parcel): ?>
                        <tr class="border-b hover:bg-gray-50">
                            <td class="px-6 py-4">
                                <code class="bg-gray-100 px-3 py-1 rounded text-sm font-mono"><?php echo htmlspecialchars($parcel['tracking_code']); ?></code>
                            </td>
                            <td class="px-6 py-4">
                                <div>
                                    <p class="font-semibold text-gray-900"><?php echo htmlspecialchars($parcel['receiver_name']); ?></p>
                                    <p class="text-sm text-gray-600"><?php echo htmlspecialchars($parcel['receiver_email'] ?? ''); ?></p>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <span class="<?php echo Utils::getStatusColor($parcel['status']); ?> status-badge">
                                    <?php echo Utils::getStatusLabel($parcel['status']); ?>
                                </span>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">
                                <?php echo htmlspecialchars($parcel['current_location'] ?? 'N/A'); ?>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">
                                <?php echo Utils::formatDate($parcel['created_at']); ?>
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex space-x-2">
                                    <a href="/routex/admin/parcel-edit?id=<?php echo $parcel['id']; ?>" class="text-purple-600 hover:text-purple-800 font-semibold">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="/routex/track?code=<?php echo urlencode($parcel['tracking_code']); ?>" target="_blank" class="text-blue-600 hover:text-blue-800 font-semibold">
                                        <i class="fas fa-external-link-alt"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
            <div class="px-6 py-4 border-t flex justify-center space-x-2">
                <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                <a href="/routex/admin/parcels?page=<?php echo $i; ?>" class="<?php echo $i === $page ? 'btn-primary' : 'px-4 py-2 border-2 border-gray-300 rounded-lg hover:border-purple-600'; ?>">
                    <?php echo $i; ?>
                </a>
                <?php endfor; ?>
            </div>
            <?php endif; ?>
        </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>
