<?php
$pageTitle = 'Clients';
$breadcrumb = [['label' => 'Clients', '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">Clients</h1>
    <a href="<?= APP_URL ?>/clients/create" class="bg-[#0A3A6B] hover:bg-blue-700 text-white font-bold py-2 px-4 rounded shadow">Add Client</a>
</div>

<div class="grid grid-cols-1 md:grid-cols-2 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 Clients</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">Active</div>
        <div class="text-3xl font-bold text-green-600"><?= htmlspecialchars($stats['active'] ?? 0) ?></div>
    </div>
</div>

<div class="bg-white rounded-lg shadow p-4 mb-6">
    <form method="GET" action="<?= APP_URL ?>/clients" class="flex flex-col md:flex-row gap-4">
        <div class="flex-grow">
            <input type="text" name="search" placeholder="Search clients..." 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="industry" class="w-full px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
                <option value="">All Industries</option>
                <?php foreach (getIndustryGroups() as $sector => $subIndustries): ?>
                    <?php if (empty($subIndustries)): ?>
                    <option value="<?= htmlspecialchars($sector) ?>" <?= ($filters['industry'] ?? '') === $sector ? 'selected' : '' ?>><?= htmlspecialchars($sector) ?></option>
                    <?php else: ?>
                    <optgroup label="<?= htmlspecialchars($sector) ?>">
                        <?php foreach ($subIndustries as $sub): ?>
                        <option value="<?= htmlspecialchars($sub) ?>" <?= ($filters['industry'] ?? '') === $sub ? 'selected' : '' ?>><?= htmlspecialchars($sub) ?></option>
                        <?php endforeach; ?>
                    </optgroup>
                    <?php endif; ?>
                <?php endforeach; ?>
            </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>

<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 class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Code</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Company</th>
                <?php if (!empty($isPlatformView)): ?><th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Organization</th><?php endif; ?>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Industry</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Contact</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">Actions</th>
            </tr>
        </thead>
        <tbody class="bg-white divide-y divide-gray-200">
            <?php if (empty($clients)): ?>
            <tr><td colspan="<?= !empty($isPlatformView) ? 7 : 6 ?>" class="px-6 py-12 text-center text-gray-500">No clients found.</td></tr>
            <?php else: ?>
                <?php foreach ($clients as $c): ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900"><?= htmlspecialchars($c['client_code'] ?? '') ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-[#0A3A6B]"><?= htmlspecialchars($c['company_name'] ?? '') ?></td>
                    <?php if (!empty($isPlatformView)): ?><td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($c['org_name'] ?? '-') ?></td><?php endif; ?>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($c['industry'] ?? '') ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <div><?= htmlspecialchars($c['contact_email'] ?? '') ?></div>
                        <div class="text-xs text-gray-400"><?= htmlspecialchars($c['phone'] ?? '') ?></div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full <?= ($c['status'] ?? '') === 'Active' ? 'bg-green-100 text-green-800' : 'bg-gray-100 text-gray-800' ?>">
                            <?= htmlspecialchars($c['status'] ?? '') ?>
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="<?= APP_URL ?>/clients/<?= $c['id'] ?>" class="text-[#0A3A6B] hover:text-blue-900 mr-3">View</a>
                        <a href="<?= APP_URL ?>/clients/<?= $c['id'] ?>/edit" class="text-gray-600 hover:text-gray-900">Edit</a>
                    </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';
?>
