<?php
/** @var array $c */
/** @var array $layout */
/** @var string $mode */
$v = function ($k, $d = '') use ($c) { return $c ? ($c[$k] ?? $d) : $d; };
$isEdit = !empty($c);
$switchUrl = $isEdit ? site_url('admin/cases/switchMode/' . (int)$c['id']) : '';
$initial = json_encode($layout ?: [], JSON_UNESCAPED_UNICODE);
$coverVal = $v('cover');
?>
<div class="page-head">
    <div>
        <h1><?php echo $isEdit ? '编辑客户案例' : '新增客户案例'; ?><span class="mode-badge mode-builder">可视化编辑</span></h1>
        <div class="desc">可视化编辑：拖拽文字 / 图片 / 按钮自由排版，保存后前台按此布局整页展示。</div>
    </div>
    <?php if ($isEdit): ?>
    <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>
    <?php else: ?>
    <div class="mode-switch-sel">
        <label for="modeSel">排版方式</label>
        <select id="modeSel" class="mode-sel">
            <option value="fixed" <?php echo $mode === 'fixed' ? 'selected' : ''; ?>>固定版面</option>
            <option value="builder" <?php echo $mode === 'builder' ? 'selected' : ''; ?>>可视化编辑</option>
        </select>
    </div>
    <?php endif; ?>
</div>

<div class="admin-card">
    <form id="pbForm" method="post" action="<?php echo site_url($isEdit ? 'admin/cases/update/' . $c['id'] : 'admin/cases/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="field"><label>案例标题 *</label><input name="title" value="<?php echo e($v('title')); ?>" required></div>
        <div class="form-grid">
            <div class="field"><label>客户名称</label><input name="customer" value="<?php echo e($v('customer')); ?>"></div>
            <div class="field"><label>所属行业</label><input name="industry" value="<?php echo e($v('industry')); ?>"></div>
        </div>
        <div class="form-grid">
            <div class="field"><label>服务时间</label><input type="date" name="published_at" value="<?php echo e($v('published_at', date('Y-m-d'))); ?>"></div>
            <div class="field"><label>排序权重</label><input type="number" name="sort_order" value="<?php echo e($v('sort_order', 0)); ?>" placeholder="越大越靠前"></div>
        </div>
        <div class="field"><label>摘要</label><input name="summary" value="<?php echo e($v('summary')); ?>"></div>
        <div class="field"><label>URL 标识（留空按序号生成）</label><input name="slug" value="<?php echo e($v('slug')); ?>" placeholder="留空则自动生成如 12"></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($coverVal); ?>" placeholder="assets/uploads/xxx.jpg 或 http(s)://"></div>
        </div>

        <div class="field">
            <label>可视化画布（拖拽排版案例详情）</label>
            <?php echo \Core\View::buffer('admin/parts/builder', ['module' => 'case', 'layout' => $layout]); ?>
        </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/cases'); ?>">取消</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__  = 'case';
window.__PB_DELETE__  = '<?php echo site_url('admin/media/delete/'); ?>';
</script>
<script src="<?php echo asset('js/page-builder.js'); ?>"></script>
