<?php
/** @var yii\web\View $this */
/** @var app\models\MasType $model */

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

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

<div class="page-title">
    <i class="fas fa-edit"></i> Edit Type
    <a href="<?= Url::to(['types/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 style="display:grid; grid-template-columns:1fr 340px; gap:20px; align-items:start;">

    <!-- ===== Form ===== -->
    <div class="lms-card">
        <div class="lms-card-header">
            <i class="fas fa-edit"></i> Update Type —
            <span class="type-name-badge" style="margin-left:8px;">
                <i class="fas fa-file-alt" style="color:#636e72; font-size:12px;"></i>
                <?= Html::encode($model->name) ?>
            </span>
        </div>
        <div class="lms-card-body">

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

            <div class="lms-form-group" style="margin-bottom:20px;">
                <?= $form->field($model, 'name', [
                        'options'  => ['tag' => false],
                        'template' => '<label class="lms-label" style="font-weight:600;">{label} <span style="color:#e74c3c;">*</span></label>{input}{error}',
                    ])
                    ->textInput([
                        'id'    => 'inp-type-name',
                        'class' => 'form-control',
                        'style' => 'padding-left:14px;',
                    ])
                ?>
            </div>

            <div class="lms-form-group" style="margin-bottom:24px;">
                <?= $form->field($model, 'isActive', [
                        'options'  => ['tag' => false],
                        'template' => '<label class="lms-label" style="font-weight:600;">{label}</label>{input}{error}',
                    ])
                    ->dropDownList(
                        ['Y' => 'Active', 'N' => 'Inactive'],
                        ['class' => 'form-control', 'style' => 'padding-left:14px;']
                    )
                ?>
            </div>

            <!-- Audit info (read-only) -->
            <div style="background:#f8faff; border-radius:8px; padding:12px 16px; margin-bottom:20px; font-size:13px; color:#888;">
                <div style="display:flex; gap:24px; flex-wrap:wrap;">
                    <div>
                        <i class="fas fa-user-plus" style="width:14px;"></i>
                        Created by
                        <strong style="color:#555;">
                            <?= $model->createdBy ? Html::encode($model->createdBy->getFullName()) : "User #{$model->created_by}" ?>
                        </strong>
                    </div>
                    <div>
                        <i class="fas fa-clock" style="width:14px;"></i>
                        <?= $model->created_at ? date('d M Y, H:i', strtotime($model->created_at)) : '—' ?>
                    </div>
                </div>
            </div>

            <div>
                <?= Html::submitButton(
                    '<i class="fas fa-save"></i> Update Type',
                    ['class' => 'lms-btn lms-btn-success']
                ) ?>
                <a href="<?= Url::to(['types/index']) ?>" class="lms-btn lms-btn-secondary" style="margin-left:8px;">
                    Cancel
                </a>
                <?= Html::beginForm(['types/delete', 'id' => $model->id], 'post', ['style' => 'display:inline; margin-left:8px;']) ?>
                <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>
                <button type="submit" class="lms-btn lms-btn-danger"
                        onclick="return confirm('Delete type \'<?= Html::encode($model->name) ?>\'?')">
                    <i class="fas fa-trash"></i> Delete
                </button>
                <?= Html::endForm() ?>
            </div>

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

    <!-- ===== Preview ===== -->
    <div class="lms-card">
        <div class="lms-card-header" style="background:#f8f9fa;"><i class="fas fa-eye"></i> Preview</div>
        <div class="lms-card-body" style="text-align:center; padding:30px 20px;">
            <span class="type-name-badge" style="font-size:15px; padding:8px 18px;">
                <i class="fas fa-file-alt" style="color:#636e72;"></i>
                <span id="preview-text"><?= Html::encode($model->name) ?></span>
            </span>
        </div>
    </div>

</div>

<script>
(function () {
    var inp     = document.getElementById('inp-type-name');
    var preview = document.getElementById('preview-text');
    inp.addEventListener('input', function () {
        preview.textContent = this.value.trim() || 'type name';
    });
})();
</script>
