<?php
/** @var yii\web\View $this */
/** @var app\models\ColourScheme[] $schemes */
/** @var string $filter */

use yii\helpers\Html;
use yii\helpers\Url;
use app\models\ColourScheme;

$this->title = 'Colour Schemes';

$counts = [];
foreach (ColourScheme::TYPE_LABELS as $k => $label) {
    $counts[$k] = count(array_filter($schemes, fn($s) => $s->type === $k));
}
?>

<div class="page-title">
    <i class="fas fa-palette"></i> Colour Schemes
    <a href="<?= Url::to(['colour-scheme/create']) ?>" class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
        <i class="fas fa-plus"></i> New Scheme
    </a>
</div>

<!-- ===== Filter pills ===== -->
<div style="display:flex; gap:12px; margin-bottom:20px; flex-wrap:wrap;">
    <a href="<?= Url::to(['colour-scheme/index']) ?>"
       class="cs-filter-pill <?= $filter === '' ? 'cs-filter-pill-active' : '' ?>">
        <span class="cs-filter-num"><?= count($schemes) ?: ColourScheme::find()->count() ?></span>
        <span>All</span>
    </a>
    <?php foreach (ColourScheme::TYPE_LABELS as $key => $label): ?>
    <a href="<?= Url::to(['colour-scheme/index', 'type' => $key]) ?>"
       class="cs-filter-pill <?= $filter === $key ? 'cs-filter-pill-active' : '' ?>">
        <span class="cs-filter-num"><?= $counts[$key] ?></span>
        <span><?= $label ?></span>
    </a>
    <?php endforeach; ?>
</div>

<!-- ===== Table ===== -->
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-palette"></i>
        <?php if ($filter && isset(ColourScheme::TYPE_LABELS[$filter])): ?>
            <?= ColourScheme::TYPE_LABELS[$filter] ?> Colours
        <?php else: ?>
            All Colour Schemes
        <?php endif; ?>
        <span class="lms-badge lms-badge-info" style="margin-left:10px;"><?= count($schemes) ?></span>
    </div>

    <?php if (empty($schemes)): ?>
        <div style="padding:40px; text-align:center; color:#aaa;">
            <i class="fas fa-palette" style="font-size:40px; display:block; margin-bottom:12px;"></i>
            No colour schemes found. <a href="<?= Url::to(['colour-scheme/create']) ?>">Add the first one.</a>
        </div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="lms-table">
            <thead>
                <tr>
                    <th style="width:60px; text-align:center;">#</th>
                    <th style="width:120px;">Type</th>
                    <th>Item</th>
                    <th>Field / Description</th>
                    <th style="width:140px; text-align:center;">Colour</th>
                    <th style="width:160px;">Updated At</th>
                    <th style="width:120px; text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($schemes as $s): ?>
                <tr>
                    <td style="text-align:center; color:#aaa; font-size:13px;"><?= $s->id ?></td>
                    <td>
                        <span class="cs-type-badge cs-type-<?= strtolower($s->type) ?>">
                            <?= Html::encode($s->getTypeLabel()) ?>
                        </span>
                    </td>
                    <td style="font-weight:500;"><?= Html::encode($s->item) ?></td>
                    <td style="color:#666; font-size:13px;"><?= Html::encode($s->field) ?></td>
                    <td style="text-align:center;">
                        <div class="cs-colour-cell">
                            <span class="cs-swatch" style="background:<?= Html::encode($s->colour) ?>;"></span>
                            <code class="cs-hex"><?= Html::encode($s->colour) ?></code>
                        </div>
                    </td>
                    <td style="font-size:13px; color:#888;">
                        <?= $s->updated_at ? date('d M Y, H:i', strtotime($s->updated_at)) : '—' ?>
                    </td>
                    <td style="text-align:center; white-space:nowrap;">
                        <a href="<?= Url::to(['colour-scheme/update', 'id' => $s->id]) ?>"
                           class="lms-btn lms-btn-primary lms-btn-sm" title="Edit">
                            <i class="fas fa-edit"></i>
                        </a>
                        <?= Html::beginForm(['colour-scheme/delete', 'id' => $s->id], 'post', ['style' => 'display:inline;']) ?>
                        <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                        <button type="submit" class="lms-btn lms-btn-danger lms-btn-sm" title="Delete"
                                onclick="return confirm('Delete colour scheme \'<?= Html::encode($s->item) ?>\'?')">
                            <i class="fas fa-trash"></i>
                        </button>
                        <?= Html::endForm() ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
