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

<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 flex-wrap gap-3">

<div class="d-flex gap-2">

    <a class="btn btn-light rounded-pill fw-bold"
       href="/easychair/admin/conference-home">
        الصفحة الذكية
    </a>

    <?php if (in_array($_SESSION['role'] ?? '', ['superadmin','admin','org_admin'])): ?>
        <a class="btn btn-success rounded-pill fw-bold"
           href="/easychair/admin/conferences/create">
            <i class="fa fa-plus"></i> إنشاء مؤتمر
        </a>
    <?php endif; ?>

</div>

            <div>
                <h3 class="fw-bold mb-1"><i class="fa fa-briefcase"></i> إدارة المؤتمرات</h3>
                <div class="opacity-75">Organization-isolated conference manager</div>
            </div>
            <a class="btn btn-light rounded-pill fw-bold" href="/easychair/admin/conference-home">
                الصفحة الذكية
            </a>
        </div>
    </div>

    <div class="row g-3">
        <?php foreach ($conferences as $c): ?>
            <?php $isCurrent = ((int)($c['org_current_conference_id'] ?? 0) === (int)$c['id']); ?>
            <div class="col-xl-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"><?= conf_h(($c['title_ar'] ?? '') ?: ($c['title_en'] ?? '')) ?></h5>
                                <div class="text-muted small"><?= conf_h($c['short_name'] ?? '') ?> | Org <?= (int)($c['org_id'] ?? 0) ?></div>
                            </div>
                            <?php if ($isCurrent): ?>
                                <span class="badge bg-success">Current</span>
                            <?php else: ?>
                                <span class="badge bg-secondary">Other</span>
                            <?php endif; ?>
                        </div>

                        <hr>

                        <div class="row text-center mb-3">
                            <div class="col-4">
                                <div class="fw-bold fs-4"><?= (int)($c['submissions_count'] ?? 0) ?></div>
                                <div class="text-muted small">Submissions</div>
                            </div>
                            <div class="col-4">
                                <div class="fw-bold fs-4"><?= (int)($c['members_count'] ?? 0) ?></div>
                                <div class="text-muted small">Members</div>
                            </div>
                            <div class="col-4">
                                <div class="fw-bold"><?= conf_h($c['status'] ?? '') ?></div>
                                <div class="text-muted small">Status</div>
                            </div>
                        </div>

                        <form method="post" action="/easychair/admin/conferences/changeStatus" class="d-flex gap-2 mb-2">
                            <input type="hidden" name="id" value="<?= (int)$c['id'] ?>">
                            <select name="status" class="form-select form-select-sm">
                                <?php foreach (['draft','open','reviewing','decision','published','archived'] as $st): ?>
                                    <option value="<?= conf_h($st) ?>" <?= (($c['status'] ?? '') === $st) ? 'selected' : '' ?>>
                                        <?= conf_h($st) ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                            <button class="btn btn-sm btn-primary">Save</button>
                        </form>

                        <div class="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" target="_blank" href="/easychair/conference/home?id=<?= (int)$c['id'] ?>">
                                Public Page
                            </a>
                            <a class="btn btn-sm btn-outline-success" href="/easychair/admin/conferences/setCurrent?conference_id=<?= (int)$c['id'] ?>">
                                Set Current
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>

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

</div>
