<?php
declare(strict_types=1);

/**
 * @var \App\View\AppView $this
 * @var iterable<\App\Model\Entity\TeacherApplication> $rows
 * @var array<string, int> $metrics
 * @var string $status
 */
$rowList = is_object($rows) && method_exists($rows, 'toList') ? $rows->toList() : iterator_to_array($rows);

$chips = [
    ''          => ['All',       (int)($metrics['total'] ?? 0)],
    'new'       => ['New',       (int)($metrics['new'] ?? 0)],
    'reviewing' => ['Reviewing', (int)($metrics['reviewing'] ?? 0)],
    'approved'  => ['Approved',  (int)($metrics['approved'] ?? 0)],
    'rejected'  => ['Rejected',  (int)($metrics['rejected'] ?? 0)],
];
?>
<?php $this->assign('title', 'Teacher applications · CandleCraft Admin'); ?>

<section class="cc-admin-page">
    <div class="cc-admin-page__head">
        <div>
            <div class="cc-eyebrow" style="margin-bottom: 12px;">Applications</div>
            <h1>Teacher applications</h1>
        </div>
        <div class="cc-admin-page__stats">
            <?php foreach (['new' => 'New', 'reviewing' => 'Reviewing', 'approved' => 'Approved', 'rejected' => 'Rejected'] as $k => $l): ?>
                <div class="cc-admin-stat"><strong><?= (int)($metrics[$k] ?? 0) ?></strong><span><?= h($l) ?></span></div>
            <?php endforeach; ?>
        </div>
    </div>

    <div class="cc-inbox-controls">
        <div class="cc-inbox-chips">
            <?php foreach ($chips as $value => [$label, $count]):
                $isOn = $status === $value;
                $href = $this->Url->build(['_name' => 'admin_applications', '?' => array_filter(['status' => $value ?: null])]);
            ?>
                <a class="cc-inbox-chip<?= $isOn ? ' is-on' : '' ?>" href="<?= h($href) ?>">
                    <?= h($label) ?>
                    <span class="cc-inbox-chip__count"><?= (int)$count ?></span>
                </a>
            <?php endforeach; ?>
        </div>
    </div>

    <table class="cc-inbox-table">
        <thead>
            <tr class="cc-inbox-row cc-inbox-row--head">
                <th scope="col" style="width: 24px;"></th>
                <th scope="col">Applicant</th>
                <th scope="col" data-priority="2">Craft</th>
                <th scope="col" data-priority="3">Years</th>
                <th scope="col" data-priority="2">Submitted</th>
                <th scope="col" data-priority="1">Status</th>
                <th scope="col" style="text-align:right;">Action</th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($rowList)): ?>
            <tr><td colspan="7"><div class="cc-inbox-empty"><h3>No applications yet.</h3></div></td></tr>
        <?php else: ?>
            <?php foreach ($rowList as $a):
                $isNew = $a->status === 'new';
                $viewUrl = $this->Url->build(['_name' => 'admin_application_view', 'id' => $a->id]);
                $submitted = $a->created?->format('d M · H:i') ?? '';
            ?>
                <tr class="cc-inbox-row cc-inbox-row--data<?= $isNew ? ' cc-inbox-row--new' : '' ?>" onclick="window.location='<?= h($viewUrl) ?>'">
                    <td><?php if ($isNew): ?><span class="cc-inbox-row__newdot"></span><?php endif; ?></td>
                    <td>
                        <div class="cc-inbox-sender-name"><?= h($a->full_name) ?></div>
                        <div class="cc-inbox-sender-email"><?= h($a->email) ?></div>
                    </td>
                    <td data-priority="2"><?= h(ucfirst((string)$a->craft)) ?></td>
                    <td data-priority="3"><?= $a->years_experience !== null ? (int)$a->years_experience : '—' ?></td>
                    <td data-priority="2" class="cc-inbox-submitted"><?= h($submitted) ?></td>
                    <td data-priority="1"><span class="cc-badge cc-badge--<?= h((string)$a->status) ?>"><?= h(ucfirst((string)$a->status)) ?></span></td>
                    <td style="text-align:right;">
                        <a class="cc-btn cc-btn--ghost cc-btn--sm" href="<?= h($viewUrl) ?>" onclick="event.stopPropagation();">Open →</a>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
    </table>

    <?php $pagingParams = $this->Paginator->params(); ?>
    <nav class="cc-paginator">
        <div class="cc-paginator__info">
            Page <?= (int)($pagingParams['page'] ?? 1) ?> of <?= max(1, (int)($pagingParams['pageCount'] ?? 1)) ?>
        </div>
        <ul class="cc-paginator__pages">
            <?= $this->Paginator->prev('‹ Prev') ?>
            <?= $this->Paginator->numbers(['modulus' => 4]) ?>
            <?= $this->Paginator->next('Next ›') ?>
        </ul>
    </nav>
</section>
