<?php
$pageTitle = 'Suppliers';
$breadcrumb = [['label' => 'Suppliers', 'url' => null]];
ob_start();
?>
<div class="flex flex-col md:flex-row justify-between items-center mb-6">
    <h1 class="text-2xl font-bold text-gray-800">Suppliers</h1>
    <a href="<?= APP_URL ?>/suppliers/create" class="bg-[#0A3A6B] hover:bg-blue-700 text-white font-bold py-2 px-4 rounded shadow">Add Supplier</a>
</div>

<!-- Stats -->
<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-[#0A3A6B]">
        <div class="text-sm text-gray-500 uppercase font-semibold">Total Suppliers</div>
        <div class="text-3xl font-bold text-gray-800"><?= htmlspecialchars($stats['total'] ?? 0) ?></div>
    </div>
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-green-500">
        <div class="text-sm text-gray-500 uppercase font-semibold">Approved</div>
        <div class="text-3xl font-bold text-green-600"><?= htmlspecialchars($stats['approved'] ?? 0) ?></div>
    </div>
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-amber-500">
        <div class="text-sm text-gray-500 uppercase font-semibold">Pending Approval</div>
        <div class="text-3xl font-bold text-amber-600"><?= htmlspecialchars($stats['pending'] ?? 0) ?></div>
    </div>
    <div class="bg-white rounded-lg shadow p-5 border-l-4 border-red-500">
        <div class="text-sm text-gray-500 uppercase font-semibold">Suspended</div>
        <div class="text-3xl font-bold text-red-600"><?= htmlspecialchars($stats['suspended'] ?? 0) ?></div>
    </div>
</div>

<!-- Filters -->
<div class="bg-white rounded-lg shadow p-4 mb-6">
    <form method="GET" action="<?= APP_URL ?>/suppliers" class="flex flex-col md:flex-row gap-4">
        <div class="flex-grow">
            <input type="text" name="search" placeholder="Search suppliers..." value="<?= htmlspecialchars($filters['search'] ?? '') ?>" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
        </div>
        <div class="w-full md:w-64">
            <select name="status" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
                <option value="">All Statuses</option>
                <option value="approved" <?= ($filters['status'] ?? '') === 'approved' ? 'selected' : '' ?>>Approved</option>
                <option value="pending" <?= ($filters['status'] ?? '') === 'pending' ? 'selected' : '' ?>>Pending</option>
                <option value="suspended" <?= ($filters['status'] ?? '') === 'suspended' ? 'selected' : '' ?>>Suspended</option>
            </select>
        </div>
        <div>
            <button type="submit" class="w-full md:w-auto bg-gray-800 hover:bg-gray-700 text-white font-bold py-2 px-6 rounded">Filter</button>
        </div>
    </form>
</div>

<!-- Table -->
<div class="bg-white rounded-lg shadow overflow-hidden">
    <div class="overflow-x-auto">
    <table class="min-w-full divide-y divide-gray-200">
        <thead class="bg-gray-50">
            <tr>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Code</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Company Name</th>
                <?php if (!empty($isPlatformView)): ?><th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Organization</th><?php endif; ?>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">TIN</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Contact</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Status</th>
                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Categories</th>
                <th scope="col" class="px-6 py-3 text-right text-xs font-medium text-gray-500 uppercase tracking-wider">Actions</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            <?php if (empty($suppliers)): ?>
            <tr>
                <td colspan="<?= !empty($isPlatformView) ? 8 : 7 ?>" class="px-6 py-12 text-center text-gray-500">
                    <svg class="mx-auto h-12 w-12 text-gray-300" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
                    </svg>
                    <p class="mt-4 text-lg">No suppliers found.</p>
                </td>
            </tr>
            <?php else: ?>
                <?php foreach ($suppliers as $supplier): ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?= htmlspecialchars($supplier['code'] ?? '') ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-[#0A3A6B]"><?= htmlspecialchars($supplier['company_name'] ?? '') ?></td>
                    <?php if (!empty($isPlatformView)): ?><td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($supplier['org_name'] ?? '-') ?></td><?php endif; ?>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($supplier['tin'] ?? '') ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <div><?= htmlspecialchars($supplier['contact_email'] ?? '') ?></div>
                        <div class="text-xs text-gray-400"><?= htmlspecialchars($supplier['phone'] ?? '') ?></div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <?php 
                            $statusClass = 'bg-gray-100 text-gray-800';
                            if (($supplier['status'] ?? '') === 'approved') $statusClass = 'bg-green-100 text-green-800';
                            if (($supplier['status'] ?? '') === 'pending') $statusClass = 'bg-amber-100 text-amber-800';
                            if (($supplier['status'] ?? '') === 'suspended') $statusClass = 'bg-red-100 text-red-800';
                        ?>
                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full <?= $statusClass ?>">
                            <?= ucfirst(htmlspecialchars($supplier['status'] ?? 'unknown')) ?>
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500 truncate max-w-xs" title="<?= htmlspecialchars($supplier['categories'] ?? '') ?>"><?= htmlspecialchars($supplier['categories'] ?? '') ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="<?= APP_URL ?>/suppliers/<?= $supplier['id'] ?>" class="text-[#0A3A6B] hover:text-blue-900 mr-3">View</a>
                        <a href="<?= APP_URL ?>/suppliers/<?= $supplier['id'] ?>/edit" class="text-gray-600 hover:text-gray-900 mr-3">Edit</a>
                        <?php if (($supplier['status'] ?? '') === 'pending'): ?>
                        <form action="<?= APP_URL ?>/suppliers/<?= $supplier['id'] ?>/approve" method="POST" class="inline">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                            <button type="submit" class="text-green-600 hover:text-green-900">Approve</button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
    </div>

    <div class="px-6 py-4 border-t border-gray-200">
        <?php @include ROOT_PATH . '/modules/shared/views/partials/pagination.php'; ?>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>