<?php
function org_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$organizations = $organizations ?? [];
?>

<div class="container-fluid py-3">

    <div class="card border-0 shadow-sm mb-4" style="border-radius:24px;background:linear-gradient(135deg,#0f172a,#1d4ed8);color:#fff;">
        <div class="card-body p-4 d-flex justify-content-between align-items-center">

    <div>
        <h3 class="fw-bold mb-1">
            <i class="fa fa-building"></i> المؤسسات
        </h3>
        <div class="opacity-75">
            Organization isolation, conferences, and users
        </div>
    </div>

    <?php
    /* Superadmin always allowed to create organizations */
    if (
        function_exists('is_superadmin') && is_superadmin()
        || (\App\Core\BusinessPermission::crud('organizations','create') ?? false)
    ):
    ?>

        <a href="/easychair/admin/organizations/create"
           class="btn btn-light fw-bold shadow-sm"
           style="border-radius:14px;">

            <i class="fa fa-plus"></i>
            Add Organization

        </a>

    <?php endif; ?>

</div>
    </div>

    <div class="row g-3">
        <?php foreach ($organizations as $o): ?>
            <?php
                $name = $o['name'] ?? ($o['name_ar'] ?? ($o['title'] ?? ('Organization #'.$o['id'])));
            ?>
            <div class="col-lg-4 col-md-6">
                <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-start gap-2">
                            <div>
                                <h5 class="fw-bold"><?= org_h($name) ?></h5>
                                <div class="text-muted small">Org ID: <?= (int)$o['id'] ?></div>
                            </div>
                            <span class="badge bg-primary">active</span>
                        </div>

                        <hr>

                        <div class="row text-center">
                            <div class="col-4">
                                <div class="fw-bold fs-4"><?= (int)($o['conferences_count'] ?? 0) ?></div>
                                <div class="text-muted small">Conferences</div>
                            </div>
                            <div class="col-4">
                                <div class="fw-bold fs-4"><?= (int)($o['users_count'] ?? 0) ?></div>
                                <div class="text-muted small">Users</div>
                            </div>
                            <div class="col-4">
                                <div class="fw-bold fs-4"><?= (int)($o['current_conference_id'] ?? 0) ?></div>
                                <div class="text-muted small">Current</div>
                            </div>
                        </div>

                        <div class="mt-3 d-flex gap-2 flex-wrap">
                            <a class="btn btn-sm btn-outline-primary" href="/easychair/admin/conference-home">
                                Smart Home
                            </a>
                            <a class="btn btn-sm btn-outline-secondary" href="/easychair/admin/users">
                                Users
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>

        <?php if (!$organizations): ?>
            <div class="col-12">
                <div class="alert alert-info">No organizations found.</div>
            </div>
        <?php endif; ?>
    </div>

</div>
