<?php
function aup_h($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}
$user = $user ?? [];
$profile = $profile ?? [];
$fullName = trim((string)($user['first_name'] ?? '') . ' ' . (string)($user['last_name'] ?? ''));
if ($fullName === '') {
    $fullName = (string)($user['email'] ?? 'User');
}
?>

<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 align-items-center gap-3 flex-wrap">
            <?php if (!empty($profile['profile_image'])): ?>
                <img src="/easychair/storage/<?= aup_h($profile['profile_image']) ?>" style="width:86px;height:86px;border-radius:50%;object-fit:cover;border:3px solid #fff;">
            <?php endif; ?>

            <div>
                <h3 class="fw-bold mb-1"><?= aup_h($fullName) ?></h3>
                <div class="opacity-75"><?= aup_h($user['email'] ?? '') ?> | <?= aup_h($user['role_code'] ?? '') ?></div>
                <div class="mt-2">
                    <span class="badge <?= !empty($profile['is_profile_complete']) ? 'bg-success' : 'bg-warning text-dark' ?>">
                        <?= !empty($profile['is_profile_complete']) ? 'Complete Profile' : 'Incomplete Profile' ?>
                    </span>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4">

        <?php
        $sections = [
            'Affiliation' => ['affiliation','affiliation_ar','college','department','academic_rank'],
            'Education' => ['major','specialization','education_level','highest_degree','degree_university','graduation_year'],
            'Research IDs' => ['orcid','scopus_id','google_scholar','personal_website','linkedin_url'],
            'Research Profile' => ['research_interests','keywords','bio','bio_ar'],
            'Reviewer Profile' => ['reviewer_expertise','review_keywords','preferred_tracks','reviewer_capacity','conflict_of_interest'],
            'Contact' => ['phone','mobile','country','city'],
        ];
        ?>

        <?php foreach ($sections as $title => $fields): ?>
            <div class="col-xl-6">
                <div class="card border-0 shadow-sm h-100" style="border-radius:22px;">
                    <div class="card-body p-4">
                        <h5 class="fw-bold mb-3"><?= aup_h($title) ?></h5>
                        <table class="table table-bordered align-middle">
                            <?php foreach ($fields as $f): ?>
                                <tr>
                                    <th style="width:180px;"><?= aup_h($f) ?></th>
                                    <td><?= nl2br(aup_h($profile[$f] ?? '')) ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </table>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>

        <div class="col-12">
            <div class="card border-0 shadow-sm" style="border-radius:22px;">
                <div class="card-body p-4">
                    <h5 class="fw-bold mb-3">Files</h5>
                    <?php if (!empty($profile['cv_file'])): ?>
                        <a target="_blank" class="btn btn-outline-primary" href="/easychair/storage/<?= aup_h($profile['cv_file']) ?>">Open CV</a>
                    <?php else: ?>
                        <div class="alert alert-info">No CV uploaded.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>

    </div>
</div>
