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

<section class="hero-section p-5 mb-4 rounded-4 shadow-sm">
    <h1 class="display-6 fw-bold mb-2"><?= e(trans('paper_archive')) ?></h1>
    <p class="lead mb-0"><?= e(trans('archive_results')) ?></p>
</section>

<div class="card border-0 shadow-sm rounded-4 mb-4">
    <div class="card-body">
        <form method="get" action="<?= e(base_path('archive')) ?>" class="row g-3">
            <div class="col-md-4">
                <label class="form-label"><?= e(trans('filter_by_conference')) ?></label>
                <select name="conference_id" class="form-select rounded-3">
                    <option value="0"><?= e(trans('archive_all_conferences')) ?></option>
                    <?php foreach ($conferences as $c): ?>
                        <option value="<?= e((string) $c['id']) ?>" <?= (int) $selectedConferenceId === (int) $c['id'] ? 'selected' : '' ?>>
                            <?= e(current_lang() === 'ar' ? (string) $c['title_ar'] : (string) $c['title_en']) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>

            <div class="col-md-5">
                <label class="form-label"><?= e(trans('search_keyword')) ?></label>
                <input type="text" name="keyword" class="form-control rounded-3" value="<?= e((string) $keyword) ?>">
            </div>

            <div class="col-md-3 d-flex align-items-end">
                <button type="submit" class="btn btn-primary rounded-3 w-100"><?= e(trans('search_papers')) ?></button>
            </div>
        </form>

        <?php if (has_permission('archive.export')): ?>
            <div class="d-flex gap-2 flex-wrap mt-3">
                <a href="<?= e(base_path('archive/export-csv?conference_id=' . (int) $selectedConferenceId . '&keyword=' . urlencode((string) $keyword))) ?>" class="btn btn-outline-secondary rounded-3"><?= e(trans('export_csv')) ?></a>
                <?php if ((int) $selectedConferenceId > 0): ?>
                    <a href="<?= e(base_path('archive/export-zip?conference_id=' . (int) $selectedConferenceId)) ?>" class="btn btn-outline-dark rounded-3"><?= e(trans('export_zip')) ?></a>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<div class="card border-0 shadow-sm rounded-4">
    <div class="card-body">
        <?php if (!empty($rows)): ?>
            <div class="row g-4">
                <?php foreach ($rows as $row): ?>
                    <div class="col-md-6">
                        <div class="border rounded-4 p-3 h-100">
                            <h3 class="h5 fw-bold"><?= e((string) $row['submission_title']) ?></h3>
                            <div class="small text-muted mb-2"><?= e((string) $row['submission_no']) ?></div>
                            <div class="small mb-1"><strong><?= e(trans('conference')) ?>:</strong> <?= e(current_lang() === 'ar' ? (string) $row['title_ar'] : (string) $row['title_en']) ?></div>
                            <div class="small mb-1"><strong><?= e(trans('tracks')) ?>:</strong> <?= e(current_lang() === 'ar' ? (string) $row['name_ar'] : (string) $row['name_en']) ?></div>
                            <div class="small mb-1"><strong><?= e(trans('doi')) ?>:</strong> <?= e((string) ($row['doi'] ?? '')) ?></div>
                            <div class="small mb-3"><strong><?= e(trans('published_at')) ?>:</strong> <?= e((string) ($row['published_at'] ?? '')) ?></div>
                            <div class="d-flex gap-2 flex-wrap">
                                <a href="<?= e(base_path('paper?id=' . (int) $row['paper_id'])) ?>" class="btn btn-outline-primary rounded-3 btn-sm"><?= e(trans('paper_page')) ?></a>
                                <a href="<?= e(base_path('conference/archive?slug=' . urlencode((string) $row['slug']))) ?>" class="btn btn-outline-secondary rounded-3 btn-sm"><?= e(trans('conference_archive')) ?></a>
                            </div>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>
        <?php else: ?>
            <div class="alert alert-info rounded-4 mb-0"><?= e(trans('no_published_papers')) ?></div>
        <?php endif; ?>
    </div>
</div>

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