<?php
/** @var yii\web\View $this */
/** @var array $grouped  section => SiteContent[] */

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

$this->title = 'Edit Text Content';

$sectionLabels = [
    'hero'     => ['bi-image',         'Hero Section',     'The large banner area at the top of your homepage.'],
    'about'    => ['bi-person-circle', 'About Section',    'Your bio and philosophy shown on the homepage and About page.'],
    'settings' => ['bi-gear',          'Site Settings',    'Phone number, email, hours, and social media handles used throughout the site.'],
];
?>

<div style="display:flex;align-items:center;justify-content:space-between;margin-bottom:2rem">
    <div>
        <h2 style="font-size:1.4rem;font-weight:700;color:#0f172a;margin:0">Edit Text Content</h2>
        <p style="color:#64748b;font-size:.875rem;margin:.3rem 0 0">Changes appear on your website immediately after saving.</p>
    </div>
</div>

<form method="POST" action="<?= Url::to(['/webcms/default/content']) ?>">
    <?= Html::hiddenInput(Yii::$app->request->csrfParam, Yii::$app->request->csrfToken) ?>

    <?php foreach ($grouped as $section => $rows): ?>
    <?php $meta = $sectionLabels[$section] ?? ['bi-folder', ucfirst($section), '']; ?>
    <div class="cms-card" style="margin-bottom:1.5rem">
        <div class="cms-card-header">
            <div style="display:flex;align-items:center;gap:.7rem">
                <div style="width:36px;height:36px;border-radius:9px;background:#f0faf4;display:flex;align-items:center;justify-content:center;flex-shrink:0">
                    <i class="bi <?= $meta[0] ?>" style="color:#2d6a4f"></i>
                </div>
                <div>
                    <h5 style="margin:0"><?= Html::encode($meta[1]) ?></h5>
                    <?php if ($meta[2]): ?>
                    <div style="font-size:.75rem;color:#94a3b8;margin-top:.1rem"><?= Html::encode($meta[2]) ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
        <div class="cms-card-body">
            <div style="display:grid;gap:1.4rem">
                <?php foreach ($rows as $row): ?>
                <div>
                    <label class="form-label-cms" for="field_<?= Html::encode($row->content_key) ?>">
                        <?= Html::encode($row->label) ?>
                    </label>
                    <?php
                    $isLong = strlen($row->content_value ?? '') > 120
                           || in_array($row->content_key, ['hero_subtext', 'about_bio_1', 'about_bio_2', 'about_philosophy'], true);
                    ?>
                    <?php if ($isLong): ?>
                    <textarea id="field_<?= Html::encode($row->content_key) ?>"
                              name="content[<?= Html::encode($row->content_key) ?>]"
                              class="cms-input"
                              rows="<?= str_contains($row->content_key, 'bio') ? 5 : 3 ?>"><?= Html::encode($row->content_value ?? '') ?></textarea>
                    <?php else: ?>
                    <input type="text"
                           id="field_<?= Html::encode($row->content_key) ?>"
                           name="content[<?= Html::encode($row->content_key) ?>]"
                           class="cms-input"
                           value="<?= Html::encode($row->content_value ?? '') ?>">
                    <?php endif; ?>
                    <div class="form-hint">Key: <code><?= Html::encode($row->content_key) ?></code></div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>
    <?php endforeach; ?>

    <div style="display:flex;align-items:center;gap:1rem;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> Save All Changes
        </button>
        <a href="<?= Url::to(['/site/index']) ?>" target="_blank" class="btn-cms-secondary">
            <i class="bi bi-eye me-1"></i> Preview Website
        </a>
    </div>
</form>
