<?php
/** @var yii\web\View $this */
/** @var common\models\Testimonial $model */
/** @var bool $isNew */

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

$this->title = $isNew ? 'Add Testimonial' : 'Edit Testimonial';
?>

<div style="display:flex;align-items:center;gap:.75rem;margin-bottom:2rem">
    <a href="<?= Url::to(['/webcms/testimonial/index']) ?>" class="btn-cms-secondary">
        <i class="bi bi-arrow-left"></i> Back
    </a>
    <h2 style="font-size:1.3rem;font-weight:700;color:#0f172a;margin:0">
        <?= $isNew ? 'Add New Testimonial' : 'Edit Testimonial' ?>
    </h2>
</div>

<div style="max-width:680px">
    <div class="cms-card">
        <div class="cms-card-header">
            <h5><?= $isNew ? 'New Testimonial' : 'Edit — ' . Html::encode($model->name) ?></h5>
        </div>
        <div class="cms-card-body">
            <?php if ($model->hasErrors()): ?>
            <div style="background:#fef2f2;border:1px solid #fecaca;color:#dc2626;border-radius:10px;padding:.85rem 1.1rem;font-size:.85rem;margin-bottom:1.5rem">
                <i class="bi bi-exclamation-triangle me-1"></i>
                Please fix the errors below before saving.
                <ul style="margin:.5rem 0 0;padding-left:1.2rem">
                    <?php foreach ($model->getFirstErrors() as $err): ?>
                    <li><?= Html::encode($err) ?></li>
                    <?php endforeach; ?>
                </ul>
            </div>
            <?php endif; ?>

            <form method="POST" action="<?= $isNew ? Url::to(['/webcms/testimonial/create']) : Url::to(['/webcms/testimonial/update', 'id' => $model->id]) ?>">
                <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>

                <div style="display:grid;gap:1.3rem">
                    <!-- Name -->
                    <div>
                        <label class="form-label-cms" for="t_name">Client Name <span style="color:#ef4444">*</span></label>
                        <input type="text"
                               id="t_name"
                               name="Testimonial[name]"
                               class="cms-input"
                               value="<?= Html::encode($model->name) ?>"
                               placeholder="e.g. Priya M."
                               required>
                        <div class="form-hint">First name and last initial is fine (e.g. Priya M.)</div>
                    </div>

                    <!-- Role -->
                    <div>
                        <label class="form-label-cms" for="t_role">Role / Description</label>
                        <input type="text"
                               id="t_role"
                               name="Testimonial[role]"
                               class="cms-input"
                               value="<?= Html::encode($model->role) ?>"
                               placeholder="e.g. Weight Loss Client">
                        <div class="form-hint">Shown below the client name (e.g. Sports Nutrition Client, Medical Nutrition Client)</div>
                    </div>

                    <!-- Quote -->
                    <div>
                        <label class="form-label-cms" for="t_quote">Testimonial Quote <span style="color:#ef4444">*</span></label>
                        <textarea id="t_quote"
                                  name="Testimonial[quote]"
                                  class="cms-input"
                                  rows="4"
                                  placeholder="What did the client say about their experience?"
                                  required><?= Html::encode($model->quote) ?></textarea>
                        <div class="form-hint">Write in the client's own words. Do not include opening/closing quotes — they are added automatically.</div>
                    </div>

                    <!-- Avatar Letter + Sort Order in one row -->
                    <div style="display:grid;grid-template-columns:1fr 1fr;gap:1rem">
                        <div>
                            <label class="form-label-cms" for="t_avatar">Avatar Letter</label>
                            <input type="text"
                                   id="t_avatar"
                                   name="Testimonial[avatar_letter]"
                                   class="cms-input"
                                   value="<?= Html::encode($model->avatar_letter) ?>"
                                   placeholder="Auto"
                                   maxlength="1">
                            <div class="form-hint">Leave blank to auto-use first letter of name</div>
                        </div>
                        <div>
                            <label class="form-label-cms" for="t_order">Sort Order</label>
                            <input type="number"
                                   id="t_order"
                                   name="Testimonial[sort_order]"
                                   class="cms-input"
                                   value="<?= (int)$model->sort_order ?>"
                                   min="0"
                                   max="9999">
                            <div class="form-hint">Lower number = shown first (0, 1, 2…)</div>
                        </div>
                    </div>

                    <!-- Active toggle -->
                    <div style="display:flex;align-items:center;gap:.85rem;padding:.9rem 1rem;background:#f8fafc;border-radius:10px;border:1px solid #e2e8f0">
                        <input type="hidden" name="Testimonial[is_active]" value="0">
                        <input type="checkbox"
                               id="t_active"
                               name="Testimonial[is_active]"
                               value="1"
                               style="width:18px;height:18px;accent-color:#2d6a4f;cursor:pointer"
                               <?= $model->is_active || $isNew ? 'checked' : '' ?>>
                        <label for="t_active" style="cursor:pointer;font-weight:600;font-size:.9rem;color:#0f172a;margin:0">
                            Show on website
                        </label>
                        <span style="font-size:.78rem;color:#94a3b8;margin-left:.25rem">Uncheck to hide without deleting</span>
                    </div>

                    <!-- Buttons -->
                    <div style="display:flex;gap:.75rem;padding-top:.5rem">
                        <button type="submit" class="btn-cms-primary" style="padding:.75rem 2rem;font-size:.95rem">
                            <i class="bi bi-check2-circle me-1"></i>
                            <?= $isNew ? 'Add Testimonial' : 'Save Changes' ?>
                        </button>
                        <a href="<?= Url::to(['/webcms/testimonial/index']) ?>" class="btn-cms-secondary">
                            Cancel
                        </a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
