<?php
/** @var yii\web\View $this */
/** @var app\models\Category $model */
/** @var app\models\Category[] $parents */

use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;
use yii\widgets\ActiveForm;

$this->title = 'Edit Category';
?>

<div class="page-title">
    <i class="fas fa-edit"></i> Edit Category
    <a href="<?= Url::to(['categories/index']) ?>" class="lms-btn lms-btn-primary lms-btn-sm" style="margin-left:auto;">
        <i class="fas fa-arrow-left"></i> Back
    </a>
</div>

<div class="lms-card" style="max-width:600px;">
    <div class="lms-card-header">
        <i class="fas fa-edit"></i> Update &mdash;
        <code style="background:#f0f4ff; padding:2px 8px; border-radius:4px; font-size:13px;">
            <?= Html::encode($model->category_id) ?>
        </code>
    </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, 'category_id', [
                'options' => ['class' => 'lms-form-group'],
            ])->textInput([
                'class'    => 'form-control',
                'style'    => 'padding-left:14px;',
                'readonly' => true,
            ])->hint('Category ID cannot be changed after creation.') ?>
        </div>

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

        <div class="lms-form-row">
            <?= $form->field($model, 'parent_id', [
                'options' => ['class' => 'lms-form-group'],
            ])->dropDownList(
                ArrayHelper::map($parents, 'category_id', fn($c) => "{$c->category_id} — {$c->category_name}"),
                [
                    'class'  => 'form-control',
                    'style'  => 'padding-left:14px;',
                    'prompt' => '— None (Root Category) —',
                ]
            )->hint('Self and descendants are excluded to prevent circular references.') ?>
        </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 Category', ['class' => 'lms-btn lms-btn-success']) ?>
        </div>

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