<?php
function pc_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$statsByRole = $statsByRole ?? [];
$overall = $overall ?? [];
$rows = $rows ?? [];
$roles = $roles ?? [];
$success = $_SESSION['flash_success'] ?? '';
unset($_SESSION['flash_success']);

$total = (int)($overall['total_users'] ?? 0);
$complete = (int)($overall['complete_users'] ?? 0);
$incomplete = (int)($overall['incomplete_users'] ?? 0);
$pct = $total > 0 ? round(($complete / $total) * 100, 1) : 0;
?>

<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>
                <h3 class="fw-bold mb-1"><i class="fa fa-chart-pie"></i> Profile Completion Dashboard</h3>
                <div class="opacity-75">Track academic profile readiness for authors, reviewers, chairs, and organizers.</div>
            </div>
            <a class="btn btn-light rounded-pill px-4"
               onclick="return confirm('Recalculate profile completion for all conference users?')"
               href="/easychair/admin/profile-completion/recalculate">
                <i class="fa fa-rotate"></i>
                Recalculate
            </a>
        </div>
    </div>

    <?php if ($success): ?>
        <div class="alert alert-success"><?= pc_h($success) ?></div>
    <?php endif; ?>

    <div class="row g-3 mb-4">
        <div class="col-md-3">
            <div class="card border-0 shadow-sm" style="border-radius:20px;">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold"><?= $total ?></div>
                    <div class="text-muted">Total Users</div>
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card border-0 shadow-sm" style="border-radius:20px;">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold text-success"><?= $complete ?></div>
                    <div class="text-muted">Complete</div>
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card border-0 shadow-sm" style="border-radius:20px;">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold text-danger"><?= $incomplete ?></div>
                    <div class="text-muted">Incomplete</div>
                </div>
            </div>
        </div>

        <div class="col-md-3">
            <div class="card border-0 shadow-sm" style="border-radius:20px;">
                <div class="card-body text-center">
                    <div class="fs-2 fw-bold"><?= $pct ?>%</div>
                    <div class="text-muted">Completion Rate</div>
                </div>
            </div>
        </div>
    </div>

    <div class="card border-0 shadow-sm mb-4" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">Completion by Role</h5>

            <div class="row g-3">
                <?php foreach ($statsByRole as $s): ?>
                    <?php
                    $rtotal = (int)($s['total_users'] ?? 0);
                    $rcomplete = (int)($s['complete_users'] ?? 0);
                    $rpct = $rtotal > 0 ? round(($rcomplete / $rtotal) * 100, 1) : 0;
                    ?>
                    <div class="col-md-4">
                        <div class="border rounded-4 p-3">
                            <div class="d-flex justify-content-between mb-2">
                                <strong><?= pc_h($s['role_name'] ?? $s['role_code']) ?></strong>
                                <span><?= $rpct ?>%</span>
                            </div>
                            <div class="progress" style="height:10px;">
                                <div class="progress-bar" role="progressbar" style="width: <?= $rpct ?>%"></div>
                            </div>
                            <div class="small text-muted mt-2">
                                Complete: <?= $rcomplete ?> / <?= $rtotal ?> |
                                Incomplete: <?= (int)($s['incomplete_users'] ?? 0) ?>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>

                <?php if (!$statsByRole): ?>
                    <div class="col-12"><div class="alert alert-info">No role data found.</div></div>
                <?php endif; ?>
            </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/profile-completion" class="row g-2 align-items-end">
                <div class="col-md-4">
                    <label class="form-label fw-bold">Search</label>
                    <input name="q" value="<?= pc_h($_GET['q'] ?? '') ?>" class="form-control" placeholder="email, name, major, specialization...">
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Role</label>
                    <select name="role" class="form-select">
                        <option value="">All roles</option>
                        <?php foreach ($roles as $r): ?>
                            <option value="<?= pc_h($r['code']) ?>" <?= (($_GET['role'] ?? '') === $r['code']) ? 'selected' : '' ?>>
                                <?= pc_h(($r['name'] ?? '') . ' / ' . ($r['code'] ?? '')) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>

                <div class="col-md-3">
                    <label class="form-label fw-bold">Status</label>
                    <select name="status" class="form-select">
                        <option value="">All</option>
                        <option value="complete" <?= (($_GET['status'] ?? '') === 'complete') ? 'selected' : '' ?>>Complete</option>
                        <option value="incomplete" <?= (($_GET['status'] ?? '') === 'incomplete') ? 'selected' : '' ?>>Incomplete</option>
                    </select>
                </div>

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

    <div class="card border-0 shadow-sm" style="border-radius:22px;">
        <div class="card-body p-4">
            <h5 class="fw-bold mb-3">User Profiles</h5>

            <div class="table-responsive">
                <table class="table table-bordered align-middle">
                    <thead class="table-dark">
                    <tr>
                        <th>User</th>
                        <th>Role</th>
                        <th>Profile Status</th>
                        <th>Academic Fields</th>
                        <th>Updated</th>
                        <th>Open</th>
                    </tr>
                    </thead>

                    <tbody>
                    <?php foreach ($rows as $r): ?>
                        <tr>
                            <td>
                                <strong><?= pc_h(trim(($r['first_name'] ?? '') . ' ' . ($r['last_name'] ?? '')) ?: ($r['email'] ?? '')) ?></strong>
                                <div class="text-muted small"><?= pc_h($r['email'] ?? '') ?></div>
                            </td>

                            <td>
                                <span class="badge bg-primary"><?= pc_h($r['role_code'] ?? '') ?></span>
                            </td>

                            <td>
                                <?php if (!empty($r['is_profile_complete'])): ?>
                                    <span class="badge bg-success">Complete</span>
                                <?php else: ?>
                                    <span class="badge bg-danger">Incomplete</span>
                                <?php endif; ?>
                            </td>

                            <td class="small">
                                <div><strong>Affiliation:</strong> <?= pc_h($r['affiliation'] ?? '') ?></div>
                                <div><strong>Major:</strong> <?= pc_h($r['major'] ?? '') ?></div>
                                <div><strong>Specialization:</strong> <?= pc_h($r['specialization'] ?? '') ?></div>
                                <div><strong>Education:</strong> <?= pc_h($r['education_level'] ?? '') ?></div>
                            </td>

                            <td><?= pc_h($r['updated_at'] ?? '') ?></td>

                            <td>
                                <a class="btn btn-sm btn-outline-primary rounded-pill"
                                   href="/easychair/admin/user-profile?id=<?= (int)$r['user_id'] ?>">
                                    Profile
                                </a>
                            </td>
                        </tr>
                    <?php endforeach; ?>

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

                </table>
            </div>
        </div>
    </div>

</div>
