<?php
function re_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$rows = $rows ?? [];
$q = $_GET['q'] ?? '';
$complete = $_GET['complete'] ?? '';
?>

<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">
            <h3 class="fw-bold mb-1">
                <i class="fa fa-user-check"></i>
                Reviewer Expertise Search
            </h3>
            <div class="opacity-75">
                Search reviewers by major, specialization, expertise, keywords, tracks, and profile completion.
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <form method="get" action="/easychair/admin/reviewer-expertise" class="row g-2 align-items-end">
                <div class="col-md-7">
                    <label class="form-label fw-bold">Search expertise</label>
                    <input name="q" value="<?= re_h($q) ?>" class="form-control" placeholder="AI, cybersecurity, NLP, education, software engineering...">
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Profile Status</label>
                    <select name="complete" class="form-select">
                        <option value="">All</option>
                        <option value="1" <?= $complete === '1' ? 'selected' : '' ?>>Complete only</option>
                        <option value="0" <?= $complete === '0' ? 'selected' : '' ?>>Incomplete only</option>
                    </select>
                </div>

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

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>Reviewer</th>
                            <th>Profile</th>
                            <th>Expertise</th>
                            <th>Capacity</th>
                            <th>Assignments</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php foreach ($rows as $r): ?>
                        <tr>
                            <td>
                                <strong><?= re_h(trim(($r['first_name'] ?? '') . ' ' . ($r['last_name'] ?? '')) ?: ($r['email'] ?? '')) ?></strong>
                                <div class="text-muted small"><?= re_h($r['email'] ?? '') ?></div>
                                <div class="small">
                                    <span class="badge bg-primary"><?= re_h($r['role_code'] ?? '') ?></span>
                                    <?php if (!empty($r['reviewer_score'])): ?>
                                        <span class="badge bg-info text-dark">Score: <?= (int)$r['reviewer_score'] ?></span>
                                    <?php endif; ?>
                                </div>
                            </td>

                            <td>
                                <span class="badge <?= !empty($r['is_profile_complete']) ? 'bg-success' : 'bg-warning text-dark' ?>">
                                    <?= !empty($r['is_profile_complete']) ? 'Complete' : 'Incomplete' ?>
                                </span>
                                <div class="text-muted small"><?= re_h($r['affiliation'] ?? '') ?></div>
                                <div class="text-muted small"><?= re_h($r['academic_rank'] ?? '') ?></div>
                            </td>

                            <td style="min-width:360px;">
                                <div><strong>Major:</strong> <?= re_h($r['major'] ?? '') ?></div>
                                <div><strong>Specialization:</strong> <?= re_h($r['specialization'] ?? '') ?></div>
                                <div class="small mt-2">
                                    <strong>Keywords:</strong>
                                    <?= re_h(mb_substr((string)($r['review_keywords'] ?: $r['keywords'] ?: ''), 0, 180)) ?>
                                </div>
                                <div class="small mt-1">
                                    <strong>Expertise:</strong>
                                    <?= re_h(mb_substr((string)($r['reviewer_expertise'] ?: $r['research_interests'] ?: ''), 0, 220)) ?>
                                </div>
                            </td>

                            <td><?= (int)($r['reviewer_capacity'] ?? 0) ?></td>
                            <td><?= (int)($r['assignments_count'] ?? 0) ?></td>

                            <td>
                                <?php $userRow = $r; require BASE_PATH . '/app/views/admin/partials/user_profile_action.php'; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>

                    <?php if (!$rows): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">No reviewers found.</td>
                        </tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
