<?php
$pageTitle = $org['name'];
$breadcrumb = [
    ['label' => 'Organizations', 'url' => APP_URL . '/admin/organizations'],
    ['label' => $org['name'], 'url' => null],
];
ob_start();

$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';
?>
<div class="flex justify-between items-center mb-6">
    <div>
        <h1 class="text-2xl font-bold text-gray-800"><?= htmlspecialchars($org['name']) ?></h1>
        <div class="mt-1 flex items-center gap-2">
            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full <?= $statusClass ?>">
                <?= ucfirst(htmlspecialchars($org['status'])) ?>
            </span>
            <?php if ((int)$org['is_active'] === 0 && $org['status'] === 'approved'): ?>
            <span class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">Suspended</span>
            <?php endif; ?>
        </div>
    </div>
    <div class="flex gap-3">
        <a href="<?= APP_URL ?>/admin/organizations" class="text-gray-600 hover:text-gray-900">Back to List</a>
        <a href="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/edit" class="bg-[#0A3A6B] hover:bg-blue-700 text-white font-bold py-2 px-4 rounded shadow">Edit</a>
    </div>
</div>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <!-- Organization Details -->
    <div class="lg:col-span-2 bg-white rounded-lg shadow overflow-hidden">
        <div class="bg-gray-50 px-6 py-4 border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">Organization Details</h3>
        </div>
        <div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Industry</div>
                <div class="text-sm text-gray-800 mt-1"><?= htmlspecialchars($org['industry'] ?? '-') ?></div>
            </div>
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Plan</div>
                <div class="text-sm text-gray-800 mt-1"><?= htmlspecialchars($org['plan_name'] ?? '-') ?></div>
            </div>
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Email</div>
                <div class="text-sm text-gray-800 mt-1"><?= htmlspecialchars($org['email'] ?? '-') ?></div>
            </div>
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Phone</div>
                <div class="text-sm text-gray-800 mt-1"><?= htmlspecialchars($org['phone'] ?? '-') ?></div>
            </div>
            <div class="md:col-span-2">
                <div class="text-xs text-gray-500 uppercase font-semibold">Address</div>
                <div class="text-sm text-gray-800 mt-1"><?= nl2br(htmlspecialchars($org['address'] ?? '-')) ?></div>
            </div>
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Subscription</div>
                <div class="text-sm text-gray-800 mt-1"><?= ucfirst(htmlspecialchars($org['subscription_status'] ?? '-')) ?></div>
            </div>
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Requested</div>
                <div class="text-sm text-gray-800 mt-1"><?= htmlspecialchars(date('M j, Y g:ia', strtotime($org['created_at']))) ?></div>
            </div>
            <?php if (!empty($org['approved_at'])): ?>
            <div>
                <div class="text-xs text-gray-500 uppercase font-semibold">Approved</div>
                <div class="text-sm text-gray-800 mt-1"><?= htmlspecialchars(date('M j, Y g:ia', strtotime($org['approved_at']))) ?></div>
            </div>
            <?php endif; ?>
        </div>

        <!-- Admin Contact -->
        <div class="bg-gray-50 px-6 py-4 border-t border-b border-gray-200">
            <h3 class="text-lg font-medium text-gray-900">Admin Contact</h3>
        </div>
        <div class="p-6">
            <?php if ($adminUser): ?>
            <div class="text-sm text-gray-800 font-medium"><?= htmlspecialchars($adminUser['first_name'] . ' ' . $adminUser['last_name']) ?></div>
            <div class="text-sm text-gray-500"><?= htmlspecialchars($adminUser['email']) ?></div>
            <div class="text-sm text-gray-500"><?= htmlspecialchars($adminUser['phone'] ?? '') ?></div>
            <?php else: ?>
            <div class="text-sm text-gray-500">No admin user found for this organization.</div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Stats & Actions -->
    <div class="flex flex-col gap-6">
        <div class="bg-white rounded-lg shadow p-6">
            <h3 class="text-lg font-medium text-gray-900 mb-4">Usage</h3>
            <div class="grid grid-cols-2 gap-4 text-center">
                <div><div class="text-2xl font-bold text-gray-800"><?= (int)($counts['users'] ?? 0) ?></div><div class="text-xs text-gray-500 uppercase">Users</div></div>
                <div><div class="text-2xl font-bold text-gray-800"><?= (int)($counts['projects'] ?? 0) ?></div><div class="text-xs text-gray-500 uppercase">Projects</div></div>
                <div><div class="text-2xl font-bold text-gray-800"><?= (int)($counts['clients'] ?? 0) ?></div><div class="text-xs text-gray-500 uppercase">Clients</div></div>
                <div><div class="text-2xl font-bold text-gray-800"><?= (int)($counts['contractors'] ?? 0) ?></div><div class="text-xs text-gray-500 uppercase">Contractors</div></div>
                <div><div class="text-2xl font-bold text-gray-800"><?= (int)($counts['suppliers'] ?? 0) ?></div><div class="text-xs text-gray-500 uppercase">Suppliers</div></div>
                <div><div class="text-2xl font-bold text-gray-800"><?= (int)($counts['invoices'] ?? 0) ?></div><div class="text-xs text-gray-500 uppercase">Invoices</div></div>
            </div>
        </div>

        <div class="bg-white rounded-lg shadow p-6">
            <h3 class="text-lg font-medium text-gray-900 mb-4">Actions</h3>
            <div class="flex flex-col gap-3">
                <?php if ($org['status'] === 'pending'): ?>
                <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/approve" method="POST">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                    <button type="submit" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded shadow">Approve</button>
                </form>
                <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/reject" method="POST" onsubmit="return confirm('Reject this organization?');">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                    <button type="submit" class="w-full bg-red-600 hover:bg-red-700 text-white font-bold py-2 px-4 rounded shadow">Reject</button>
                </form>
                <?php elseif ($org['status'] === 'rejected'): ?>
                <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/approve" method="POST">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                    <button type="submit" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded shadow">Approve</button>
                </form>
                <?php elseif ((int)$org['is_active'] === 1): ?>
                <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/suspend" method="POST" 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="w-full bg-amber-500 hover:bg-amber-600 text-white font-bold py-2 px-4 rounded shadow">Suspend</button>
                </form>
                <?php else: ?>
                <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/reactivate" method="POST">
                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                    <button type="submit" class="w-full bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded shadow">Reactivate</button>
                </form>
                <?php endif; ?>

                <?php if ((int)$org['is_active'] === 0): ?>
                <form action="<?= APP_URL ?>/admin/organizations/<?= $org['id'] ?>/delete" method="POST" 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="w-full bg-white border border-red-600 text-red-600 hover:bg-red-50 font-bold py-2 px-4 rounded">Delete Permanently</button>
                </form>
                <?php else: ?>
                <p class="text-xs text-gray-400 text-center">Suspend this organization before it can be deleted.</p>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
