<?php
/**
 * Reusable tag panel — shows current tags + inline add/remove forms.
 *
 * @var string   $entityType  'course' | 'activity'
 * @var int      $entityId
 * @var app\models\Tag[] $tags        Tags currently attached
 * @var string[] $allTagNames         All tag names for <datalist> autocomplete
 * @var string   $returnUrl           URL to redirect back to after add/remove
 */

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

$panelId = 'tag-panel-' . $entityType . '-' . $entityId;
?>

<div class="tag-panel-wrap">

    <!-- Current tags -->
    <div class="tag-panel" id="<?= $panelId ?>">
        <?php if (empty($tags)): ?>
            <span class="tag-empty-hint">No tags — type below to add one</span>
        <?php else: ?>
            <?php foreach ($tags as $tag): ?>
            <span class="tag-pill" style="background:<?= Html::encode($tag->color) ?>;">
                <i class="fas fa-tag" style="font-size:10px; opacity:.75;"></i>
                <?= Html::encode($tag->tag_name) ?>
                <form method="post" action="<?= Url::to(['tags/remove']) ?>"
                      class="tag-remove-form" style="display:inline; margin:0;">
                    <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                    <?= Html::hiddenInput('entity_type', $entityType) ?>
                    <?= Html::hiddenInput('entity_id',   $entityId) ?>
                    <?= Html::hiddenInput('tag_id',      $tag->id) ?>
                    <?= Html::hiddenInput('return_url',  $returnUrl) ?>
                    <button type="submit" class="tag-pill-remove" title="Remove '<?= Html::encode($tag->tag_name) ?>'">
                        <i class="fas fa-times"></i>
                    </button>
                </form>
            </span>
            <?php endforeach; ?>
        <?php endif; ?>
    </div>

    <!-- Add tag form (ADD_TAG_TO_ENTITY) -->
    <form method="post" action="<?= Url::to(['tags/add']) ?>" class="tag-add-form">
        <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
        <?= Html::hiddenInput('entity_type', $entityType) ?>
        <?= Html::hiddenInput('entity_id',   $entityId) ?>
        <?= Html::hiddenInput('return_url',  $returnUrl) ?>

        <input type="text"
               name="tag_name"
               class="tag-add-input"
               placeholder="Type a tag name…"
               list="taglist-<?= $panelId ?>"
               autocomplete="off"
               maxlength="100">

        <datalist id="taglist-<?= $panelId ?>">
            <?php foreach ($allTagNames as $n): ?>
                <option value="<?= Html::encode($n) ?>">
            <?php endforeach; ?>
        </datalist>

        <button type="submit" class="lms-btn lms-btn-primary lms-btn-sm">
            <i class="fas fa-plus"></i> Add Tag
        </button>

        <a href="<?= Url::to(['tags/index']) ?>"
           class="tag-manage-link" title="Manage all tags">
            <i class="fas fa-cog"></i>
        </a>
    </form>
</div>
