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

<div class="mb-4">
    <h1 class="fw-bold mb-2"><?= e(trans('search_results')) ?></h1>
    <p class="text-muted mb-0"><?= e(trans('search_query')) ?>: <?= e($q !== '' ? $q : '-') ?></p>
</div>

<?php
$hasResults = !empty($results['users']) || !empty($results['conferences']) || !empty($results['submissions']) || !empty($results['papers']);
?>

<?php if (!$hasResults): ?>
    <div class="alert alert-info rounded-4"><?= e(trans('no_search_results')) ?></div>
<?php else: ?>

    <?php if (!empty($results['users'])): ?>
        <div class="card border-0 shadow-sm rounded-4 mb-4">
            <div class="card-body">
                <h3 class="h5 fw-bold mb-3"><?= e(trans('search_users')) ?></h3>
                <div class="list-compact">
                    <?php foreach ($results['users'] as $row): ?>
                        <div class="item">
                            <div class="fw-bold"><?= e(trim((string) $row['first_name'] . ' ' . (string) $row['last_name'])) ?></div>
                            <div class="text-muted"><?= e((string) $row['email']) ?></div>
                            <span class="badge-soft"><?= e((string) $row['role_code']) ?></span>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!empty($results['conferences'])): ?>
        <div class="card border-0 shadow-sm rounded-4 mb-4">
            <div class="card-body">
                <h3 class="h5 fw-bold mb-3"><?= e(trans('search_conferences')) ?></h3>
                <div class="list-compact">
                    <?php foreach ($results['conferences'] as $row): ?>
                        <div class="item">
                            <div class="fw-bold"><?= e(current_lang() === 'ar' ? (string) $row['title_ar'] : (string) $row['title_en']) ?></div>
                            <div class="text-muted"><?= e((string) $row['short_name']) ?> — <?= e((string) $row['status']) ?></div>
                            <a href="<?= e(base_path('conference?slug=' . urlencode((string) $row['slug']))) ?>" class="btn btn-sm btn-outline-primary rounded-3 mt-2"><?= e(trans('conference_details')) ?></a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!empty($results['submissions'])): ?>
        <div class="card border-0 shadow-sm rounded-4 mb-4">
            <div class="card-body">
                <h3 class="h5 fw-bold mb-3"><?= e(trans('search_submissions')) ?></h3>
                <div class="list-compact">
                    <?php foreach ($results['submissions'] as $row): ?>
                        <div class="item">
                            <div class="fw-bold"><?= e((string) $row['title']) ?></div>
                            <div class="text-muted"><?= e((string) $row['submission_no']) ?> — <?= e((string) $row['status']) ?></div>
                            <div class="small mb-2"><?= e(current_lang() === 'ar' ? (string) $row['title_ar'] : (string) $row['title_en']) ?></div>
                            <a href="<?= e(base_path('submissions/show?id=' . (int) $row['id'])) ?>" class="btn btn-sm btn-outline-primary rounded-3"><?= e(trans('view_submission')) ?></a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

    <?php if (!empty($results['papers'])): ?>
        <div class="card border-0 shadow-sm rounded-4">
            <div class="card-body">
                <h3 class="h5 fw-bold mb-3"><?= e(trans('search_published_papers')) ?></h3>
                <div class="list-compact">
                    <?php foreach ($results['papers'] as $row): ?>
                        <div class="item">
                            <div class="fw-bold"><?= e((string) $row['submission_title']) ?></div>
                            <div class="text-muted"><?= e((string) $row['submission_no']) ?></div>
                            <div class="small mb-1"><?= e(current_lang() === 'ar' ? (string) $row['title_ar'] : (string) $row['title_en']) ?></div>
                            <div class="small mb-2"><?= e((string) ($row['doi'] ?? '')) ?></div>
                            <a href="<?= e(base_path('paper?id=' . (int) $row['paper_id'])) ?>" class="btn btn-sm btn-outline-primary rounded-3"><?= e(trans('paper_page')) ?></a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>
    <?php endif; ?>

<?php endif; ?>

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