<?php
function sc_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

$conferences = $conferences ?? [];
$organizations = $organizations ?? [];
$role = $role ?? '';
$q = $_GET['q'] ?? '';
$status = $_GET['status'] ?? '';
$orgFilter = (int)($_GET['org_id'] ?? 0);
?>

<style>
.ec-select-wrap{
    max-width:1180px;
    margin:30px auto;
    padding:0 16px;
}
.ec-hero{
    border-radius:28px;
    padding:32px;
    color:#fff;
    background:linear-gradient(135deg,#0f172a,#1d4ed8);
    box-shadow:0 18px 45px rgba(15,23,42,.18);
    margin-bottom:22px;
}
.ec-card{
    border:0;
    border-radius:22px;
    box-shadow:0 16px 38px rgba(15,23,42,.08);
    overflow:hidden;
}
.ec-conf{
    border:1px solid #e5e7eb;
    border-radius:18px;
    padding:18px;
    height:100%;
    background:#fff;
    transition:.2s;
}
.ec-conf:hover{
    transform:translateY(-3px);
    box-shadow:0 18px 36px rgba(15,23,42,.12);
}
.ec-badge{
    display:inline-block;
    padding:6px 10px;
    border-radius:999px;
    font-size:12px;
    font-weight:800;
    background:#dbeafe;
    color:#1e40af;
}
.ec-badge-current{
    background:#dcfce7;
    color:#166534;
}
.ec-meta{
    color:#64748b;
    font-size:13px;
}
</style>

<div class="ec-select-wrap">

    <div class="ec-hero">
        <h2 class="fw-bold mb-1">Select Conference</h2>
        <div class="opacity-75">
            اختر المؤتمر للعمل عليه. كل مؤتمر معزول حسب المؤسسة والصلاحيات.
        </div>
    </div>

    <div class="card ec-card mb-4">
        <div class="card-body">
            <form method="get" action="/easychair/select-conference" class="row g-2 align-items-end">
                <div class="col-lg-5">
                    <label class="form-label fw-bold">Search</label>
                    <input name="q" class="form-control" value="<?= sc_h($q) ?>" placeholder="Conference name, short name, organization...">
                </div>

                <?php if (($role ?? '') === 'superadmin'): ?>
                    <div class="col-lg-3">
                        <label class="form-label fw-bold">Organization</label>
                        <select name="org_id" class="form-select">
                            <option value="0">All organizations</option>
                            <?php foreach ($organizations as $o): ?>
                                <option value="<?= (int)$o['id'] ?>" <?= $orgFilter === (int)$o['id'] ? 'selected' : '' ?>>
                                    <?= sc_h($o['name'] ?? ('Org #' . $o['id'])) ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                <?php endif; ?>

                <div class="col-lg-2">
                    <label class="form-label fw-bold">Status</label>
                    <select name="status" class="form-select">
                        <option value="">All</option>
                        <?php foreach (['draft','open','reviewing','decision','published','archived'] as $st): ?>
                            <option value="<?= sc_h($st) ?>" <?= $status === $st ? 'selected' : '' ?>>
                                <?= sc_h($st) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-lg-2">
                    <button class="btn btn-primary w-100">Filter</button>
                </div>
            </form>
        </div>
    </div>

    <div class="row g-3">
        <?php foreach ($conferences as $c): ?>
            <?php
                $isCurrent = !empty($c['current_conference_id']) && (int)$c['current_conference_id'] === (int)$c['id'];
                $title = ($c['title_ar'] ?? '') ?: (($c['title_en'] ?? '') ?: ($c['short_name'] ?? 'Conference'));
            ?>
            <div class="col-lg-4 col-md-6">
                <div class="ec-conf">
                    <div class="d-flex justify-content-between gap-2 align-items-start mb-2">
                        <div>
                            <h5 class="fw-bold mb-1"><?= sc_h($title) ?></h5>
                            <div class="ec-meta"><?= sc_h($c['short_name'] ?? '') ?></div>
                        </div>
                        <?php if ($isCurrent): ?>
                            <span class="ec-badge ec-badge-current">Current</span>
                        <?php else: ?>
                            <span class="ec-badge"><?= sc_h($c['status'] ?? 'open') ?></span>
                        <?php endif; ?>
                    </div>

                    <div class="ec-meta mb-2">
                        Organization:
                        <strong><?= sc_h($c['org_name'] ?? ('Org #' . ($c['org_id'] ?? ''))) ?></strong>
                    </div>

                    <?php if (($role ?? '') === 'superadmin'): ?>
                        <div class="row text-center my-3">
                            <div class="col-6">
                                <div class="fw-bold fs-5"><?= (int)($c['submissions_count'] ?? 0) ?></div>
                                <div class="ec-meta">Submissions</div>
                            </div>
                            <div class="col-6">
                                <div class="fw-bold fs-5"><?= (int)($c['members_count'] ?? 0) ?></div>
                                <div class="ec-meta">Members</div>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="mb-3">
                            <span class="ec-badge">
                                Role: <?= sc_h($c['membership_role_code'] ?? ($_SESSION['role'] ?? 'user')) ?>
                            </span>
                        </div>
                    <?php endif; ?>

                    <form method="post" action="/easychair/select-conference/select">
                        <input type="hidden" name="conference_id" value="<?= (int)$c['id'] ?>">
                        <button class="btn btn-success w-100 rounded-pill fw-bold">
                            Select Conference
                        </button>
                    </form>
                </div>
            </div>
        <?php endforeach; ?>

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