<?php
/** @var yii\web\View $this */
/** @var app\models\CourseActivity $model */
/** @var app\models\Course $course */
/** @var app\models\Tag[] $activityTags */
/** @var string[] $allTagNames */

use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\ActiveForm;
use app\models\CourseActivity;
use app\models\EntityTag;

$this->title  = 'Edit Activity';
$activityUrl  = Url::to(['courses/edit-activity', 'id' => $model->id]);
?>

<div class="page-title">
    <i class="fas fa-edit"></i> Edit Activity
    <a href="<?= Url::to(['courses/view', 'id' => $course->id]) ?>"
       class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
        <i class="fas fa-arrow-left"></i> Back to <?= Html::encode($course->course_code) ?>
    </a>
</div>

<div style="display:grid; grid-template-columns:1fr 360px; gap:20px; align-items:start;">

<!-- ===== Activity Form ===== -->
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-edit"></i> Update Activity —
        <span class="lms-badge act-type-<?= Html::encode($model->activity_type) ?>" style="margin-left:6px;">
            <?= Html::encode($model->getTypeLabel()) ?>
        </span>
    </div>
    <div class="lms-card-body">

    <?php $form = ActiveForm::begin([
        'options'     => ['autocomplete' => 'off'],
        'fieldConfig' => ['errorOptions' => ['class' => 'field-error']],
    ]); ?>

        <div class="lms-form-row">
            <?= $form->field($model, 'activity_name', ['options' => ['class' => 'lms-form-group']])
                ->textInput(['class' => 'form-control', 'style' => 'padding-left:14px;']) ?>
        </div>

        <div style="display:grid; grid-template-columns:1fr 1fr; gap:16px;">
            <div class="lms-form-row">
                <?= $form->field($model, 'activity_type', ['options' => ['class' => 'lms-form-group']])
                    ->dropDownList(CourseActivity::TYPES, ['class' => 'form-control', 'style' => 'padding-left:14px;']) ?>
            </div>
            <div class="lms-form-row">
                <?= $form->field($model, 'activity_code', ['options' => ['class' => 'lms-form-group']])
                    ->textInput(['class' => 'form-control', 'style' => 'padding-left:14px; font-family:monospace;']) ?>
            </div>
        </div>

        <div style="display:grid; grid-template-columns:1fr 1fr; gap:16px;">
            <div class="lms-form-row">
                <?= $form->field($model, 'duration_minutes', ['options' => ['class' => 'lms-form-group']])
                    ->textInput(['class' => 'form-control', 'style' => 'padding-left:14px;', 'type' => 'number', 'min' => '0']) ?>
            </div>
            <div class="lms-form-row">
                <?= $form->field($model, 'sort_order', ['options' => ['class' => 'lms-form-group']])
                    ->textInput(['class' => 'form-control', 'style' => 'padding-left:14px;', 'type' => 'number', 'min' => '0']) ?>
            </div>
        </div>

        <div class="lms-form-row">
            <?= $form->field($model, 'description', ['options' => ['class' => 'lms-form-group']])
                ->textarea(['class' => 'form-control', 'rows' => 2, 'style' => 'padding:10px 14px;']) ?>
        </div>

        <div class="lms-form-row">
            <?= $form->field($model, 'is_active', ['options' => ['class' => 'lms-form-group']])
                ->dropDownList(['Y' => 'Active', 'N' => 'Inactive'],
                    ['class' => 'form-control', 'style' => 'padding-left:14px;']) ?>
        </div>

        <div style="margin-top:14px;">
            <?= Html::submitButton('<i class="fas fa-save"></i> Update Activity', ['class' => 'lms-btn lms-btn-success']) ?>
        </div>

    <?php ActiveForm::end(); ?>
    </div>
</div>

<!-- ===== Activity Tags Panel ===== -->
<div class="lms-card">
    <div class="lms-card-header">
        <i class="fas fa-tags"></i> Activity Tags
        <?php if (!empty($activityTags)): ?>
            <span class="lms-badge lms-badge-info" style="margin-left:8px;"><?= count($activityTags) ?></span>
        <?php endif; ?>
    </div>
    <?= $this->renderPartial('/tags/_tag_panel', [
        'entityType'  => EntityTag::TYPE_ACTIVITY,
        'entityId'    => $model->id,
        'tags'        => $activityTags,
        'allTagNames' => $allTagNames,
        'returnUrl'   => $activityUrl,
    ]) ?>
</div>

</div>
