<?php
/** @var yii\web\View $this */
/** @var app\models\MasType[] $types */
/** @var string $filter  'Y' | 'N' | '' */

use yii\helpers\Html;
use yii\helpers\Url;

$this->title = 'Types';

$activeCount   = count(array_filter($types, fn($t) => $t->isActive === 'Y'));
$inactiveCount = count($types) - $activeCount;
?>

<div class="page-title">
    <i class="fas fa-layer-group"></i> Types
    <a href="<?= Url::to(['types/create']) ?>" class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
        <i class="fas fa-plus"></i> New Type
    </a>
</div>

<!-- ===== Summary stats ===== -->
<div style="display:flex; gap:14px; margin-bottom:20px; flex-wrap:wrap;">
    <a href="<?= Url::to(['types/index']) ?>"
       class="type-stat-pill <?= $filter === '' ? 'type-stat-pill-active' : '' ?>">
        <span class="type-stat-num"><?= count($types) ?: MasType::find()->count() ?></span>
        <span>All</span>
    </a>
    <a href="<?= Url::to(['types/index', 'status' => 'Y']) ?>"
       class="type-stat-pill <?= $filter === 'Y' ? 'type-stat-pill-active' : '' ?>" style="border-color:#27ae60;">
        <span class="type-stat-num" style="color:#27ae60;"><?= $activeCount ?></span>
        <span style="color:#27ae60;">Active</span>
    </a>
    <a href="<?= Url::to(['types/index', 'status' => 'N']) ?>"
       class="type-stat-pill <?= $filter === 'N' ? 'type-stat-pill-active' : '' ?>" style="border-color:#e74c3c;">
        <span class="type-stat-num" style="color:#e74c3c;"><?= $inactiveCount ?></span>
        <span style="color:#e74c3c;">Inactive</span>
    </a>
</div>

<!-- ===== Types table ===== -->
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-layer-group"></i>
        <?php if ($filter === 'Y'): ?>Active Types
        <?php elseif ($filter === 'N'): ?>Inactive Types
        <?php else: ?>All Types
        <?php endif; ?>
        <span class="lms-badge lms-badge-info" style="margin-left:10px;"><?= count($types) ?></span>
    </div>

    <?php if (empty($types)): ?>
        <div style="padding:40px; text-align:center; color:#aaa;">
            <i class="fas fa-layer-group" style="font-size:40px; display:block; margin-bottom:12px;"></i>
            No types found. <a href="<?= Url::to(['types/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>Type Name</th>
                    <th style="width:100px; text-align:center;">Status</th>
                    <th style="width:140px;">Created By</th>
                    <th style="width:160px;">Created At</th>
                    <th style="width:160px;">Updated At</th>
                    <th style="width:140px; text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($types as $t): ?>
                <tr class="<?= $t->isActive === 'N' ? 'row-inactive' : '' ?>">
                    <td style="text-align:center; color:#aaa; font-size:13px;"><?= $t->id ?></td>
                    <td>
                        <span class="type-name-badge">
                            <i class="fas fa-file-alt" style="color:#636e72; font-size:12px;"></i>
                            <?= Html::encode($t->name) ?>
                        </span>
                    </td>
                    <td style="text-align:center;">
                        <?= Html::beginForm(['types/toggle', 'id' => $t->id], 'post', ['style' => 'display:inline;']) ?>
                        <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                        <button type="submit"
                                class="lms-badge <?= $t->isActive === 'Y' ? 'lms-badge-success' : 'lms-badge-danger' ?>"
                                style="border:none; cursor:pointer; padding:4px 10px;"
                                title="Click to toggle">
                            <?= $t->isActive === 'Y' ? 'Active' : 'Inactive' ?>
                        </button>
                        <?= Html::endForm() ?>
                    </td>
                    <td style="font-size:13px; color:#777;">
                        <?php
                        $creator = $t->createdBy;
                        echo $creator ? Html::encode($creator->getFullName()) : '<span style="color:#ccc;">—</span>';
                        ?>
                    </td>
                    <td style="font-size:13px; color:#888;">
                        <?= $t->created_at ? date('d M Y, H:i', strtotime($t->created_at)) : '—' ?>
                    </td>
                    <td style="font-size:13px; color:#888;">
                        <?= $t->updated_at ? date('d M Y, H:i', strtotime($t->updated_at)) : '—' ?>
                    </td>
                    <td style="text-align:center; white-space:nowrap;">
                        <a href="<?= Url::to(['types/update', 'id' => $t->id]) ?>"
                           class="lms-btn lms-btn-primary lms-btn-sm" title="Edit">
                            <i class="fas fa-edit"></i>
                        </a>
                        <?= Html::beginForm(['types/delete', 'id' => $t->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 type \'<?= Html::encode($t->name) ?>\'?')">
                            <i class="fas fa-trash"></i>
                        </button>
                        <?= Html::endForm() ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
