<?php
/** @var array $p */
/** @var array $layout */
/** @var string $mode */
$v = function ($k, $d = '') use ($p) { return $p ? ($p[$k] ?? $d) : $d; };
$isEdit = !empty($p);
$switchUrl = $isEdit ? site_url('admin/products/switchMode/' . (int)$p['id']) : '';
$initial = json_encode($layout ?: [], JSON_UNESCAPED_UNICODE);
?>
<div class="page-head">
    <div>
        <h1><?php echo $isEdit ? '编辑产品' : '新增产品'; ?><span class="mode-badge mode-builder">可视化编辑</span></h1>
        <div class="desc">可视化编辑：拖拽文字 / 图片 / 按钮自由排版，可放置「购买 / 价格 / 规格」专用块（自动读取本产品数据）。</div>
    </div>
    <div class="mode-switch-sel">
        <label for="modeSel">编辑模式</label>
        <select id="modeSel" class="mode-sel" onchange="location.href='<?php echo $switchUrl; ?>?mode='+this.value">
            <option value="fixed" <?php echo $mode === 'fixed' ? 'selected' : ''; ?>>固定版面</option>
            <option value="builder" <?php echo $mode === 'builder' ? 'selected' : ''; ?>>可视化编辑</option>
        </select>
    </div>
</div>

<div class="admin-card">
    <form id="pbForm" method="post" action="<?php echo site_url($isEdit ? 'admin/products/update/' . $p['id'] : 'admin/products/store'); ?>" enctype="multipart/form-data">
        <?php echo csrf_field(); ?>
        <input type="hidden" name="mode" value="builder">
        <input type="hidden" name="layout" id="pbLayout">

        <div class="form-grid">
            <div class="field">
                <label>所属分类 *</label>
                <select name="category_id" required>
                    <?php foreach ($cats as $c): ?><option value="<?php echo e($c['id']); ?>" <?php echo $v('category_id') == $c['id'] ? 'selected' : ''; ?>><?php echo e($c['name']); ?></option><?php endforeach; ?>
                </select>
            </div>
            <div class="field"><label>产品名称 *</label><input name="name" value="<?php echo e($v('name')); ?>" required></div>
            <div class="field"><label>URL 标识（留空按序号生成）</label><input name="slug" value="<?php echo e($v('slug')); ?>" placeholder="留空则自动生成如 12"></div>
            <div class="field"><label>价格（元，起）</label><input name="price" type="number" step="0.01" value="<?php echo e($v('price')); ?>"></div>
        </div>

        <div class="field"><label>一句话简介</label><input name="summary" value="<?php echo e($v('summary')); ?>"></div>

        <div class="form-grid">
            <div class="field"><label>封面图（上传，可选）</label><input type="file" name="cover" accept="image/*"></div>
            <div class="field"><label>或填写图片地址/路径</label><input name="cover_url" value="<?php echo e($v('cover')); ?>" placeholder="assets/uploads/xxx.jpg 或 http(s)://"></div>
        </div>

        <div class="field">
            <label>可视化画布（拖拽排版产品详情）</label>
            <?php echo \Core\View::buffer('admin/parts/builder', ['module' => 'product', 'layout' => $layout]); ?>
        </div>

        <div class="field">
            <label>规格参数（每行一项，格式：参数名|参数值）</label>
            <textarea name="specs" placeholder="降温方式|水冷循环&#10;续航|6-8 小时"><?php echo e($specText ?? ''); ?></textarea>
        </div>

        <div class="form-grid">
            <div class="field"><label>标签（逗号分隔）</label><input name="tags" value="<?php echo e($v('tags')); ?>"></div>
            <div class="field"><label>排序（越小越靠前）</label><input name="sort_order" type="number" value="<?php echo e($v('sort_order', 0)); ?>"></div>
        </div>

        <div class="field" style="display:flex;align-items:center;gap:8px">
            <input type="checkbox" name="status" value="1" <?php echo $v('status', 1) ? 'checked' : ''; ?> id="st"> <label for="st" style="margin:0">立即上线</label>
        </div>

        <div class="form-actions">
            <button class="btn-primary" type="submit">保存</button>
            <a class="btn-ghost" href="<?php echo site_url('admin/products'); ?>">取消</a>
        </div>
    </form>
</div>

<style>
.mode-switch-sel{display:inline-flex;align-items:center;gap:8px}
.mode-switch-sel label{font-size:13px;color:#64748b}
.mode-sel{border:1px solid #e2e8f0;border-radius:10px;padding:8px 12px;font-size:14px;background:#fff;color:#334155;cursor:pointer;min-width:140px}
.mode-sel:focus{border-color:#0ea5e9;box-shadow:0 0 0 3px rgba(14,165,233,.12)}
.mode-badge{display:inline-block;margin-left:10px;padding:2px 10px;border-radius:999px;font-size:12px;font-weight:600;vertical-align:middle;line-height:1.7}
.mode-badge.mode-fixed{background:#e0f2fe;color:#0369a1}
.mode-badge.mode-builder{background:#ede9fe;color:#6d28d9}
</style>

<script>
window.__PB_INIT__   = <?php echo $initial; ?>;
window.__PB_MEDIA__  = '<?php echo site_url('admin/media'); ?>';
window.__PB_UPLOAD__ = '<?php echo site_url('admin/media/upload'); ?>';
window.__PB_CSRF__   = '<?php echo csrf_token(); ?>';
window.__PB_MODULE__  = 'product';
window.__PB_DELETE__  = '<?php echo site_url('admin/media/delete/'); ?>';
</script>
<script src="<?php echo asset('js/page-builder.js'); ?>"></script>
