<?php
require __DIR__ . '/../../layouts/header.php';

$lang = function_exists('current_lang') ? current_lang() : 'ar';

function ch($v) {
    return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8');
}

function cp($path) {
    if (!$path) return '';
    if (function_exists('base_path')) {
        return base_path($path);
    }
    return '/easychair/' . ltrim($path, '/');
}

$title = ($lang === 'ar')
    ? (($conf['title_ar'] ?? '') ?: ($conf['title_en'] ?? ''))
    : (($conf['title_en'] ?? '') ?: ($conf['title_ar'] ?? ''));

$brief = ($lang === 'ar')
    ? (($conf['brief_ar'] ?? '') ?: ($conf['description_ar'] ?? ''))
    : (($conf['brief_en'] ?? '') ?: ($conf['description_en'] ?? ''));

$location = ($lang === 'ar')
    ? (($conf['location_ar'] ?? '') ?: ($conf['venue_ar'] ?? ''))
    : (($conf['location_en'] ?? '') ?: ($conf['venue_en'] ?? ''));

$logo = !empty($conf['logo_path']) ? cp('storage/' . $conf['logo_path']) : '';
$featured = !empty($conf['featured_image']) ? cp('storage/' . $conf['featured_image']) : '';

$slides = $byType['hero_slide'] ?? [];
$panels = $byType['panel'] ?? [];
$topics = $byType['topic'] ?? [];
$sections = $byType['section_html'] ?? [];
$datesBlocks = $byType['important_date'] ?? [];

$isSubmissionOpen = true;

if (!empty($conf['submission_start']) && strtotime($conf['submission_start']) > time()) {
    $isSubmissionOpen = false;
}

if (!empty($conf['submission_end']) && strtotime($conf['submission_end']) < time()) {
    $isSubmissionOpen = false;
}

if (($conf['cfp_status'] ?? 'open') === 'closed') {
    $isSubmissionOpen = false;
}
?>

<style>
.conf-hero{
    border-radius:30px;
    overflow:hidden;
    background:linear-gradient(135deg,#0f172a,#1d4ed8);
    color:#fff;
    box-shadow:0 24px 70px rgba(15,23,42,.18);
}
.conf-hero-inner{
    padding:38px;
    background:
        linear-gradient(90deg,rgba(15,23,42,.90),rgba(15,23,42,.55)),
        url('<?= ch($featured) ?>') center/cover no-repeat;
}
.conf-logo{
    max-width:150px;
    max-height:90px;
    object-fit:contain;
    background:#fff;
    border-radius:18px;
    padding:10px;
}
.conf-pill{
    display:inline-flex;
    align-items:center;
    gap:7px;
    border-radius:999px;
    padding:8px 13px;
    background:rgba(255,255,255,.14);
    color:#fff;
    font-weight:700;
    margin:3px;
}
.smart-card{
    border:0;
    border-radius:22px;
    box-shadow:0 16px 45px rgba(15,23,42,.08);
}
.kpi-num{
    font-size:30px;
    font-weight:900;
    color:#0b2a6f;
}
.timeline{
    display:flex;
    flex-wrap:wrap;
    gap:10px;
}
.timeline .step{
    border-radius:999px;
    padding:8px 13px;
    font-weight:800;
    border:1px solid #dbeafe;
    background:#eff6ff;
    color:#1e3a8a;
}
.timeline .step.off{
    background:#f1f5f9;
    color:#64748b;
    border-color:#e2e8f0;
}
</style>

<div class="conf-hero mb-4">
    <div class="conf-hero-inner">
        <div class="d-flex align-items-center gap-4 flex-wrap">
            <?php if ($logo): ?>
                <img src="<?= ch($logo) ?>" class="conf-logo">
            <?php endif; ?>

            <div class="flex-grow-1">
                <div class="mb-2">
                    <span class="conf-pill">
                        <i class="fa-solid fa-building-columns"></i>
                        <?= ch($conf['org_name'] ?? '') ?>
                    </span>
                    <span class="conf-pill">
                        <i class="fa-solid fa-tag"></i>
                        <?= ch($conf['short_name'] ?? '') ?>
                    </span>
                    <span class="conf-pill">
                        <i class="fa-solid fa-circle-dot"></i>
                        <?= ch($conf['status'] ?? '') ?>
                    </span>
                </div>

                <h1 class="fw-bold mb-2"><?= ch($title) ?></h1>

                <?php if ($brief): ?>
                    <div class="lead" style="max-width:900px;opacity:.92"><?= nl2br(ch($brief)) ?></div>
                <?php endif; ?>

                <div class="mt-3">
                    <?php if (!empty($conf['start_date']) || !empty($conf['end_date'])): ?>
                        <span class="conf-pill">
                            <i class="fa-solid fa-calendar-days"></i>
                            <?= ch(($conf['start_date'] ?? '') . ' → ' . ($conf['end_date'] ?? '')) ?>
                        </span>
                    <?php endif; ?>

                    <?php if ($location): ?>
                        <span class="conf-pill">
                            <i class="fa-solid fa-location-dot"></i>
                            <?= ch($location) ?>
                        </span>
                    <?php endif; ?>
                </div>

                <div class="mt-4 d-flex gap-2 flex-wrap">
                    <?php if ($isSubmissionOpen): ?>
                        <a class="btn btn-light btn-lg rounded-pill fw-bold" href="<?= ch(cp('submit')) ?>?org_id=<?= (int)($conf['org_id'] ?? 0) ?>&conference_id=<?= (int)($conf['id'] ?? 0) ?>"
                            <i class="fa-solid fa-file-circle-plus me-2"></i>
                            <?= ch($lang === 'ar' ? 'تقديم بحث' : 'Submit Paper') ?>
                        </a>
                    <?php else: ?>
                        <button class="btn btn-secondary btn-lg rounded-pill fw-bold" disabled>
                            <i class="fa-solid fa-lock me-2"></i>
                            <?= ch($lang === 'ar' ? 'التقديم مغلق' : 'Submission Closed') ?>
                        </button>
                    <?php endif; ?>

                    <a class="btn btn-outline-light btn-lg rounded-pill fw-bold" href="<?= ch(cp('proceedings?conference_id='.(int)$conf['id'])) ?>">
                        <i class="fa-solid fa-book-open me-2"></i>
                        <?= ch($lang === 'ar' ? 'الوقائع' : 'Proceedings') ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row g-3 mb-4">
    <div class="col-md-2 col-6">
        <div class="card smart-card"><div class="card-body">
            <div class="text-muted small"><?= ch($lang==='ar'?'التقديمات':'Submissions') ?></div>
            <div class="kpi-num"><?= (int)($kpi['submissions'] ?? 0) ?></div>
        </div></div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card smart-card"><div class="card-body">
            <div class="text-muted small"><?= ch($lang==='ar'?'قيد التحكيم':'Under Review') ?></div>
            <div class="kpi-num"><?= (int)($kpi['under_review'] ?? 0) ?></div>
        </div></div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card smart-card"><div class="card-body">
            <div class="text-muted small"><?= ch($lang==='ar'?'المقبول':'Accepted') ?></div>
            <div class="kpi-num"><?= (int)($kpi['accepted'] ?? 0) ?></div>
        </div></div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card smart-card"><div class="card-body">
            <div class="text-muted small"><?= ch($lang==='ar'?'المنشور':'Published') ?></div>
            <div class="kpi-num"><?= (int)($kpi['published'] ?? 0) ?></div>
        </div></div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card smart-card"><div class="card-body">
            <div class="text-muted small"><?= ch($lang==='ar'?'المحكمون':'Reviewers') ?></div>
            <div class="kpi-num"><?= (int)($kpi['reviewers'] ?? 0) ?></div>
        </div></div>
    </div>
    <div class="col-md-2 col-6">
        <div class="card smart-card"><div class="card-body">
            <div class="text-muted small"><?= ch($lang==='ar'?'التوزيعات':'Assignments') ?></div>
            <div class="kpi-num"><?= (int)($kpi['assignments'] ?? 0) ?></div>
        </div></div>
    </div>
</div>

<?php if (!empty($workflow)): ?>
<div class="card smart-card mb-4">
    <div class="card-body">
        <h5 class="fw-bold mb-3">
            <i class="fa-solid fa-diagram-project me-2"></i>
            <?= ch($lang==='ar'?'سير عمل المؤتمر':'Conference Workflow') ?>
        </h5>
        <div class="timeline">
            <?php foreach ($workflow as $w): ?>
                <?php
                $wt = ($lang === 'ar')
                    ? (($w['title_ar'] ?? '') ?: ($w['stage'] ?? ''))
                    : (($w['title_en'] ?? '') ?: ($w['stage'] ?? ''));
                ?>
                <span class="step <?= empty($w['is_enabled']) ? 'off' : '' ?>">
                    <?= ch($wt) ?>
                </span>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($slides)): ?>
<div id="confCarousel" class="carousel slide mb-4" data-bs-ride="carousel">
    <div class="carousel-inner rounded-4 overflow-hidden">
        <?php foreach ($slides as $i=>$s): ?>
            <?php
            $st = ($lang==='ar') ? ($s['title_ar'] ?? '') : ($s['title_en'] ?? '');
            $sc = ($lang==='ar') ? ($s['body_ar'] ?? '') : ($s['body_en'] ?? '');
            $img = !empty($s['image_path']) ? cp('storage/'.$s['image_path']) : '';
            ?>
            <div class="carousel-item <?= $i===0?'active':'' ?>">
                <?php if ($img): ?>
                    <img src="<?= ch($img) ?>" class="d-block w-100" style="height:360px;object-fit:cover;">
                <?php else: ?>
                    <div style="height:360px;background:linear-gradient(135deg,#0b2a6f,#1146b3)"></div>
                <?php endif; ?>

                <div class="carousel-caption d-none d-md-block text-start" style="background:rgba(0,0,0,.50);border-radius:18px;padding:18px">
                    <?php if ($st): ?><h5 class="fw-bold"><?= ch($st) ?></h5><?php endif; ?>
                    <?php if ($sc): ?><div><?= nl2br(ch($sc)) ?></div><?php endif; ?>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($datesBlocks)): ?>
<div class="card smart-card mb-4">
    <div class="card-body">
        <h5 class="fw-bold mb-3">
            <i class="fa-solid fa-calendar-check me-2"></i>
            <?= ch($lang==='ar'?'مواعيد مهمة':'Important Dates') ?>
        </h5>
        <div class="row g-3">
            <?php foreach ($datesBlocks as $d): ?>
                <div class="col-md-6">
                    <div class="border rounded-4 p-3 h-100">
                        <div class="fw-bold">
                            <?= ch($lang==='ar' ? ($d['title_ar'] ?? '') : ($d['title_en'] ?? '')) ?>
                        </div>
                        <div class="text-muted small" style="white-space:pre-line">
                            <?= ch($lang==='ar' ? ($d['body_ar'] ?? '') : ($d['body_en'] ?? '')) ?>
                        </div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($panels)): ?>
<div class="row g-3 mb-4">
    <?php foreach ($panels as $p): ?>
        <?php
        $pt = ($lang==='ar') ? ($p['title_ar'] ?? '') : ($p['title_en'] ?? '');
        $pb = ($lang==='ar') ? ($p['body_ar'] ?? '') : ($p['body_en'] ?? '');
        $img = !empty($p['image_path']) ? cp('storage/'.$p['image_path']) : '';
        $link = $p['link_url'] ?? '';
        ?>
        <div class="col-md-4">
            <div class="card smart-card h-100">
                <?php if ($img): ?>
                    <img src="<?= ch($img) ?>" class="rounded-top-4" style="height:170px;object-fit:cover">
                <?php endif; ?>
                <div class="card-body">
                    <h5 class="fw-bold"><?= ch($pt) ?></h5>
                    <div class="text-muted small" style="white-space:pre-line"><?= ch($pb) ?></div>
                    <?php if ($link): ?>
                        <a class="btn btn-sm btn-outline-primary rounded-pill mt-3" href="<?= ch($link) ?>" target="_blank">
                            <?= ch($lang==='ar'?'المزيد':'More') ?>
                        </a>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>
<?php endif; ?>

<?php if (!empty($committees)): ?>
<div class="card smart-card mb-4">
    <div class="card-body">
        <h5 class="fw-bold mb-3">
            <i class="fa-solid fa-users me-2"></i>
            <?= ch($lang==='ar'?'اللجان':'Committees') ?>
        </h5>
        <div class="row g-3">
            <?php foreach ($committees as $m): ?>
                <?php
                $name = ($lang==='ar') ? (($m['full_name_ar'] ?? '') ?: ($m['full_name_en'] ?? '')) : (($m['full_name_en'] ?? '') ?: ($m['full_name_ar'] ?? ''));
                $titleM = ($lang==='ar') ? (($m['title_ar'] ?? '') ?: ($m['title_en'] ?? '')) : (($m['title_en'] ?? '') ?: ($m['title_ar'] ?? ''));
                $img = !empty($m['image_path']) ? cp('storage/'.$m['image_path']) : '';
                ?>
                <div class="col-md-3 col-sm-6">
                    <div class="border rounded-4 p-3 h-100 text-center">
                        <?php if ($img): ?>
                            <img src="<?= ch($img) ?>" class="rounded-circle mb-2" style="width:78px;height:78px;object-fit:cover">
                        <?php endif; ?>
                        <div class="fw-bold"><?= ch($name) ?></div>
                        <div class="text-muted small"><?= ch($titleM) ?></div>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($sessions)): ?>
<div class="card smart-card mb-4">
    <div class="card-body">
        <h5 class="fw-bold mb-3">
            <i class="fa-solid fa-clock me-2"></i>
            <?= ch($lang==='ar'?'البرنامج العلمي':'Program') ?>
        </h5>
        <div class="table-responsive">
            <table class="table table-bordered align-middle">
                <thead class="table-light">
                    <tr>
                        <th><?= ch($lang==='ar'?'التاريخ':'Date') ?></th>
                        <th><?= ch($lang==='ar'?'الوقت':'Time') ?></th>
                        <th><?= ch($lang==='ar'?'الجلسة':'Session') ?></th>
                        <th><?= ch($lang==='ar'?'القاعة':'Room') ?></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($sessions as $s): ?>
                    <?php
                    $sessionTitle = ($lang==='ar') ? (($s['title_ar'] ?? '') ?: ($s['title_en'] ?? '')) : (($s['title_en'] ?? '') ?: ($s['title_ar'] ?? ''));
                    ?>
                    <tr>
                        <td><?= ch($s['session_date'] ?? '') ?></td>
                        <td><?= ch(($s['start_time'] ?? '') . ' - ' . ($s['end_time'] ?? '')) ?></td>
                        <td class="fw-bold"><?= ch($sessionTitle) ?></td>
                        <td><?= ch($s['room_name'] ?? '') ?></td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php endif; ?>

<?php foreach ($sections as $s): ?>
    <div class="card smart-card mb-4">
        <div class="card-body">
            <h5 class="fw-bold">
                <?= ch($lang==='ar' ? ($s['title_ar'] ?? '') : ($s['title_en'] ?? '')) ?>
            </h5>
            <div class="text-muted" style="white-space:pre-line">
                <?= ch($lang==='ar' ? ($s['body_ar'] ?? '') : ($s['body_en'] ?? '')) ?>
            </div>
        </div>
    </div>
<?php endforeach; ?>

<?php require __DIR__ . '/../../layouts/footer.php'; ?>
