<section class="report-toolbar">
    <div>
        <p class="eyebrow"><?= e(trans('common.administrator')) ?></p>
        <h1><?= e(trans('admin.reports_heading')) ?></h1>
        <p class="support-copy"><?= e(trans('admin.reports_copy')) ?></p>
    </div>
</section>

<section class="card table-card">
    <form method="get" action="<?= e(base_url('/admin/reports')) ?>" class="search-form">
        <label>
            <span><?= e(trans('common.ui_language')) ?></span>
            <select name="lang">
                <?php foreach ($languages as $languageCode => $languageLabel): ?>
                    <option value="<?= e($languageCode) ?>" <?= current_locale() === $languageCode ? 'selected' : '' ?>><?= e($languageLabel) ?></option>
                <?php endforeach; ?>
            </select>
        </label>
        <input type="text" name="search" value="<?= e($search) ?>" placeholder="<?= e(trans('admin.reports_search_placeholder')) ?>">
        <button class="ghost-button" type="submit"><?= e(trans('common.search')) ?></button>
    </form>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th><?= e(trans('common.reference')) ?></th>
                    <th><?= e(trans('admin.table_participant')) ?></th>
                    <th><?= e(trans('admin.table_contact')) ?></th>
                    <th><?= e(trans('admin.table_primary_area')) ?></th>
                    <th><?= e(trans('admin.table_generated')) ?></th>
                    <th><?= e(trans('common.actions')) ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if ($reports === []): ?>
                    <tr>
                        <td colspan="6"><?= e(trans('admin.reports_empty')) ?></td>
                    </tr>
                <?php endif; ?>

                <?php foreach ($reports as $report): ?>
                    <tr>
                        <td><?= e($report['reference']) ?></td>
                        <td><?= e($report['participant']['full_name']) ?></td>
                        <td>
                            <?= e($report['participant']['email']) ?><br>
                            <?= e($report['participant']['phone']) ?>
                        </td>
                        <td>
                            <?= e($report['primary_area']['name'] ?? trans('common.not_available')) ?><br>
                            <small><?= e(($report['primary_area']['score'] ?? 0) . ' / ' . ($report['primary_area']['max_score'] ?? 0)) ?></small>
                        </td>
                        <td><?= e($report['generated_at']) ?></td>
                        <td class="actions-cell">
                            <a class="text-link" href="<?= e(base_url('/admin/reports/view?ref=' . urlencode($report['reference']))) ?>"><?= e(trans('common.open')) ?></a>
                            <a class="text-link" href="<?= e(base_url('/admin/reports/download?ref=' . urlencode($report['reference']))) ?>"><?= e(trans('common.download_pdf')) ?></a>
                            <form method="post" action="<?= e(base_url('/admin/reports/delete')) ?>" onsubmit="return confirm(<?= js_value(trans('admin.delete_report_confirm')) ?>);">
                                <input type="hidden" name="_csrf" value="<?= e(csrf_token()) ?>">
                                <input type="hidden" name="reference" value="<?= e($report['reference']) ?>">
                                <button class="text-link destructive-link" type="submit"><?= e(trans('common.delete')) ?></button>
                            </form>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>