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

$pageData = $pageData ?? [];
$conference = $pageData['conference'] ?? null;
$reviewers = $reviewers ?? [];
$needsConferenceSelection = $needsConferenceSelection ?? false;

$confTitle = '';

if ($conference) {
    $confTitle = ($conference['title_ar'] ?? '') ?: (($conference['title_en'] ?? '') ?: ('Conference #' . $conference['id']));
}
?>

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

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body d-flex justify-content-between align-items-center flex-wrap gap-3">
            <div>
                <h3 class="fw-bold mb-1">المحكمون</h3>
                <?php if ($conference): ?>
                    <div class="text-muted">
                        <?= rv_h($confTitle) ?>
                        <?php if (!empty($conference['short_name'])): ?>
                            — <?= rv_h($conference['short_name']) ?>
                        <?php endif; ?>
                    </div>
                <?php else: ?>
                    <div class="text-muted">قائمة المحكمين حسب المؤتمر الحالي</div>
                <?php endif; ?>
            </div>

            <div class="d-flex gap-2 flex-wrap">
                <a class="btn btn-outline-secondary" href="/easychair/select-conference">تغيير المؤتمر</a>
                <a class="btn btn-primary" href="/easychair/admin/users">إدارة المستخدمين</a>
            </div>
        </div>
    </div>

    <?php if ($needsConferenceSelection): ?>

        <div class="alert alert-warning shadow-sm" style="border-radius:18px;">
            يرجى اختيار مؤتمر أولاً لعرض المحكمين المرتبطين به.
            <a href="/easychair/select-conference" class="alert-link">اختيار مؤتمر</a>
        </div>

    <?php else: ?>

        <div class="row g-3 mb-4">
            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100" style="border-radius:18px;">
                    <div class="card-body">
                        <div class="text-muted small">إجمالي المحكمين</div>
                        <div class="display-6 fw-bold"><?= (int)($pageData['total_reviewers'] ?? 0) ?></div>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100" style="border-radius:18px;">
                    <div class="card-body">
                        <div class="text-muted small">مرتبطون بدور reviewer</div>
                        <div class="display-6 fw-bold text-success"><?= (int)($pageData['mapped_reviewers'] ?? 0) ?></div>
                    </div>
                </div>
            </div>

            <div class="col-md-4">
                <div class="card border-0 shadow-sm h-100" style="border-radius:18px;">
                    <div class="card-body">
                        <div class="text-muted small">من التوزيعات فقط</div>
                        <div class="display-6 fw-bold text-warning"><?= (int)($pageData['assignment_only_reviewers'] ?? 0) ?></div>
                    </div>
                </div>
            </div>
        </div>

        <?php if (empty($reviewers)): ?>

            <div class="alert alert-info shadow-sm" style="border-radius:18px;">
                لا يوجد محكمون لهذا المؤتمر حالياً.
            </div>

        <?php else: ?>

            <div class="card border-0 shadow-sm" style="border-radius:22px;">
                <div class="card-body">
                    <div class="table-responsive">
                        <table class="table table-bordered bg-white align-middle">
                            <thead class="table-dark">
                                <tr>
                                    <th>#</th>
                                    <th>الاسم</th>
                                    <th>البريد</th>
                                    <th>الهاتف</th>
                                    <th>الوظيفة</th>
                                    <th>التخصص</th>
                                    <th>النقاط</th>
                                    <th>التوزيعات</th>
                                    <th>المكتملة</th>
                                    <th>المعلقة</th>
                                    <th>المصدر</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php foreach($reviewers as $r): ?>
                                <tr>
                                    <td><?= (int)$r['id'] ?></td>
                                    <td class="fw-bold"><?= rv_h(trim(($r['first_name'] ?? '') . ' ' . ($r['last_name'] ?? ''))) ?></td>
                                    <td><?= rv_h($r['email'] ?? '') ?></td>
                                    <td><?= rv_h($r['mobile'] ?? '-') ?></td>
                                    <td><?= rv_h($r['job_title'] ?? '-') ?></td>
                                    <td style="max-width:260px;">
                                        <div class="small text-muted" style="white-space:pre-line"><?= rv_h($r['expertise'] ?? '-') ?></div>
                                    </td>
                                    <td><span class="badge bg-secondary"><?= rv_h($r['reviewer_score'] ?? '-') ?></span></td>
                                    <td><span class="badge bg-primary"><?= (int)($r['assigned_count'] ?? 0) ?></span></td>
                                    <td><span class="badge bg-success"><?= (int)($r['completed_count'] ?? 0) ?></span></td>
                                    <td><span class="badge bg-warning text-dark"><?= (int)($r['pending_count'] ?? 0) ?></span></td>
                                    <td>
                                        <?php $source = (string)($r['source_label'] ?? ''); ?>
                                        <?php if ($source === 'assignment_only'): ?>
                                            <span class="badge bg-warning text-dark">assignment only</span>
                                        <?php elseif (strpos($source, 'conference_role') !== false): ?>
                                            <span class="badge bg-success">conference role</span>
                                        <?php else: ?>
                                            <span class="badge bg-light text-dark"><?= rv_h($source ?: '-') ?></span>
                                        <?php endif; ?>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>

                    <div class="text-muted small mt-3">
                        ملاحظة: assignment only يعني أن المحكم لديه توزيعات في هذا المؤتمر، ولكن لا يوجد له صف reviewer في user_conferences.
                    </div>
                </div>
            </div>

        <?php endif; ?>

    <?php endif; ?>

</div>
