<?php
/** @var yii\web\View $this */
/** @var array[] $tags  raw rows: id, tag_name, tag_slug, color, is_active, course_count, activity_count, total_count */

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

$this->title = 'Tags';
?>

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

<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-tags"></i> All Tags
        <span class="lms-badge lms-badge-info" style="margin-left:10px;"><?= count($tags) ?> total</span>
    </div>

    <?php if (empty($tags)): ?>
        <div style="padding:40px; text-align:center; color:#aaa;">
            <i class="fas fa-tags" style="font-size:40px; display:block; margin-bottom:12px;"></i>
            No tags yet. <a href="<?= Url::to(['tags/create']) ?>">Create the first one.</a>
        </div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="lms-table">
            <thead>
                <tr>
                    <th style="width:200px;">Tag</th>
                    <th style="width:160px;">Slug</th>
                    <th style="width:80px; text-align:center;">Color</th>
                    <th style="width:100px; text-align:center;">Courses</th>
                    <th style="width:100px; text-align:center;">Activities</th>
                    <th style="width:90px; text-align:center;">Total Used</th>
                    <th style="width:80px;">Status</th>
                    <th style="width:110px; text-align:center;">Actions</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($tags as $t): ?>
                <tr>
                    <td>
                        <a href="<?= Url::to(['tags/view', 'id' => $t['id']]) ?>"
                           class="tag-pill" style="background:<?= Html::encode($t['color']) ?>; text-decoration:none;">
                            <i class="fas fa-tag" style="font-size:10px; opacity:.8;"></i>
                            <?= Html::encode($t['tag_name']) ?>
                        </a>
                    </td>
                    <td>
                        <code style="font-size:12px; color:#888; background:#f5f5f5; padding:2px 6px; border-radius:4px;">
                            <?= Html::encode($t['tag_slug']) ?>
                        </code>
                    </td>
                    <td style="text-align:center;">
                        <span style="display:inline-block; width:24px; height:24px; border-radius:50%;
                                     background:<?= Html::encode($t['color']) ?>;
                                     border:2px solid rgba(0,0,0,.1); vertical-align:middle;"
                              title="<?= Html::encode($t['color']) ?>"></span>
                        <span style="font-size:11px; color:#aaa; margin-left:4px;"><?= Html::encode($t['color']) ?></span>
                    </td>
                    <td style="text-align:center;">
                        <?php $cc = (int)$t['course_count']; ?>
                        <span class="lms-badge <?= $cc > 0 ? 'lms-badge-info' : '' ?>"
                              style="<?= $cc === 0 ? 'background:#eee;color:#bbb;' : '' ?>">
                            <?= $cc ?>
                        </span>
                    </td>
                    <td style="text-align:center;">
                        <?php $ac = (int)$t['activity_count']; ?>
                        <span class="lms-badge <?= $ac > 0 ? '' : '' ?>"
                              style="background:<?= $ac > 0 ? '#9b59b6' : '#eee' ?>; color:<?= $ac > 0 ? '#fff' : '#bbb' ?>;">
                            <?= $ac ?>
                        </span>
                    </td>
                    <td style="text-align:center;">
                        <?php $tc = (int)$t['total_count']; ?>
                        <strong style="color:<?= $tc > 0 ? '#333' : '#ccc' ?>;"><?= $tc ?></strong>
                    </td>
                    <td>
                        <span class="lms-badge <?= $t['is_active'] === 'Y' ? 'lms-badge-success' : 'lms-badge-danger' ?>">
                            <?= $t['is_active'] === 'Y' ? 'Active' : 'Inactive' ?>
                        </span>
                    </td>
                    <td style="text-align:center; white-space:nowrap;">
                        <a href="<?= Url::to(['tags/view', 'id' => $t['id']]) ?>"
                           class="lms-btn lms-btn-primary lms-btn-sm" title="View usage">
                            <i class="fas fa-eye"></i>
                        </a>
                        <a href="<?= Url::to(['tags/update', 'id' => $t['id']]) ?>"
                           class="lms-btn lms-btn-secondary lms-btn-sm" title="Edit">
                            <i class="fas fa-edit"></i>
                        </a>
                        <?= Html::beginForm(['tags/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 tag \'<?= Html::encode($t['tag_name']) ?>\' from all entities?')">
                            <i class="fas fa-trash"></i>
                        </button>
                        <?= Html::endForm() ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>
