<?php
declare(strict_types=1);

/**
 * @var \App\View\AppView $this
 * @var \Cake\Collection\CollectionInterface<\App\Model\Entity\Teacher> $rows
 * @var array<int,int> $slotCounts
 */
$list = $rows->toList();
?>
<?php $this->assign('title', 'Teachers · CandleCraft Admin'); ?>

<section class="cc-admin-page">
    <div class="cc-admin-page__head">
        <div>
            <div class="cc-eyebrow" style="margin-bottom: 12px;">Teachers</div>
            <h1>Manage teachers</h1>
        </div>
        <a class="cc-btn cc-btn--primary" href="<?= $this->Url->build(['_name' => 'admin_teacher_add']) ?>">+ Add teacher</a>
    </div>

    <table class="cc-inbox-table">
        <thead>
            <tr class="cc-inbox-row cc-inbox-row--head">
                <th scope="col">Name</th>
                <th scope="col" data-priority="2">Role</th>
                <th scope="col" data-priority="3">Rate</th>
                <th scope="col" data-priority="3">Open slots</th>
                <th scope="col" data-priority="1">Status</th>
                <th scope="col" style="text-align:right;">Action</th>
            </tr>
        </thead>
        <tbody>
        <?php if (empty($list)): ?>
            <tr><td colspan="6"><div class="cc-inbox-empty"><h3>No teachers yet.</h3></div></td></tr>
        <?php else: ?>
            <?php foreach ($list as $t):
                $editUrl = $this->Url->build(['_name' => 'admin_teacher_edit', 'id' => $t->id]);
                $openSlots = (int)($slotCounts[$t->id] ?? 0);
            ?>
                <tr class="cc-inbox-row cc-inbox-row--data">
                    <td>
                        <div class="cc-inbox-sender-name"><?= h($t->full_name) ?></div>
                        <div class="cc-inbox-sender-email">/<?= h($t->slug) ?></div>
                    </td>
                    <td data-priority="2"><?= h($t->role) ?></td>
                    <td data-priority="3">A$<?= h($t->getHourlyDollars()) ?></td>
                    <td data-priority="3"><?= $openSlots ?></td>
                    <td data-priority="1">
                        <?php if ($t->is_active): ?>
                            <span class="cc-badge cc-badge--paid">Active</span>
                        <?php else: ?>
                            <span class="cc-badge">Inactive</span>
                        <?php endif; ?>
                    </td>
                    <td style="text-align:right;">
                        <span class="cc-inbox-actions">
                            <a class="cc-btn cc-btn--ghost cc-btn--sm" href="<?= h($editUrl) ?>">Edit →</a>
                            <?= $this->Form->postLink(
                                $t->is_active ? 'Deactivate' : 'Activate',
                                ['_name' => 'admin_teacher_deactivate', 'id' => $t->id],
                                ['class' => 'cc-btn cc-btn--outline cc-btn--sm', 'confirm' => $t->is_active ? 'Hide this teacher from the public site?' : 'Make this teacher visible again?']
                            ) ?>
                        </span>
                    </td>
                </tr>
            <?php endforeach; ?>
        <?php endif; ?>
        </tbody>
    </table>
</section>
