<?php
$pageTitle = 'Organizations';
$breadcrumb = [['label' => 'Organizations', 'url' => null]];
ob_start();
?>
<div class="flex justify-between items-center mb-6">
    <h1 class="text-2xl font-bold text-gray-800">Organizations</h1>
</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</div>
        <div class="text-3xl font-bold text-gray-800"><?= (int)($counts['total'] ?? 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 Review</div>
        <div class="text-3xl font-bold text-amber-600"><?= (int)($counts['pending'] ?? 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"><?= (int)($counts['approved'] ?? 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">Rejected</div>
        <div class="text-3xl font-bold text-red-600"><?= (int)($counts['rejected'] ?? 0) ?></div>
    </div>
</div>

<!-- Filters -->
<div class="bg-white rounded-lg shadow p-4 mb-6">
    <form method="GET" action="<?= APP_URL ?>/admin/organizations" class="flex gap-4">
        <select name="status" onchange="this.form.submit()" class="w-full md:w-64 px-4 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-[#0A3A6B]">
            <option value="">All Statuses</option>
            <option value="pending" <?= $currentStatus === 'pending' ? 'selected' : '' ?>>Pending Review</option>
            <option value="approved" <?= $currentStatus === 'approved' ? 'selected' : '' ?>>Approved</option>
            <option value="rejected" <?= $currentStatus === 'rejected' ? 'selected' : '' ?>>Rejected</option>
        </select>
    </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 class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Organization</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Admin Contact</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Plan</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Users</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-left text-xs font-medium text-gray-500 uppercase tracking-wider">Requested</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($organizations)): ?>
            <tr>
                <td colspan="7" class="px-6 py-12 text-center text-gray-500">No organizations found.</td>
            </tr>
            <?php else: ?>
                <?php foreach ($organizations as $org): ?>
                <tr class="hover:bg-gray-50">
                    <td class="px-6 py-4 whitespace-nowrap text-sm font-bold text-[#0A3A6B]"><?= htmlspecialchars($org['name']) ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                        <div><?= htmlspecialchars($org['admin_name'] ?? '-') ?></div>
                        <div class="text-xs text-gray-400"><?= htmlspecialchars($org['admin_email'] ?? '') ?></div>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars($org['plan_name'] ?? '-') ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= (int)($org['user_count'] ?? 0) ?></td>
                    <td class="px-6 py-4 whitespace-nowrap">
                        <?php
                            $statusClass = 'bg-gray-100 text-gray-800';
                            if ($org['status'] === 'approved') $statusClass = 'bg-green-100 text-green-800';
                            if ($org['status'] === 'pending') $statusClass = 'bg-amber-100 text-amber-800';
                            if ($org['status'] === 'rejected') $statusClass = 'bg-red-100 text-red-800';
                        ?>
                        <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full <?= $statusClass ?>">
                            <?= ucfirst(htmlspecialchars($org['status'])) ?>
                        </span>
                    </td>
                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"><?= htmlspecialchars(date('M j, Y', strtotime($org['created_at']))) ?></td>
                    <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
                        <a href="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>" class="text-[#0A3A6B] hover:text-blue-900 mr-3">View</a>
                        <a href="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/edit" class="text-gray-600 hover:text-gray-900 mr-3">Edit</a>
                        <?php if ($org['status'] === 'pending'): ?>
                        <form action="<?= APP_URL ?>/admin/organizations/<?= $org['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 mr-3">Approve</button>
                        </form>
                        <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/reject" method="POST" class="inline mr-3" onsubmit="return confirm('Reject this organization?');">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                            <button type="submit" class="text-red-600 hover:text-red-900">Reject</button>
                        </form>
                        <?php elseif ($org['status'] === 'rejected'): ?>
                        <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/approve" method="POST" class="inline mr-3">
                            <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 elseif ((int)$org['is_active'] === 1): ?>
                        <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/suspend" method="POST" class="inline mr-3" onsubmit="return confirm('Suspend this organization? Its users will lose access immediately.');">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                            <button type="submit" class="text-amber-600 hover:text-amber-900">Suspend</button>
                        </form>
                        <?php else: ?>
                        <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/reactivate" method="POST" class="inline mr-3">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                            <button type="submit" class="text-green-600 hover:text-green-900">Reactivate</button>
                        </form>
                        <?php endif; ?>
                        <?php if ((int)$org['is_active'] === 0): ?>
                        <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/delete" method="POST" class="inline" onsubmit="return confirm('Permanently delete this organization and ALL its data (users, projects, invoices, contractors, suppliers)? This cannot be undone.');">
                            <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                            <button type="submit" class="text-red-600 hover:text-red-900">Delete</button>
                        </form>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            <?php endif; ?>
        </tbody>
    </table>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
