<?php
    $repeaterRaw = old('repeater_raw');
    if ($repeaterRaw === null) {
        $repeaterRaw = collect($section->repeater_items ?? [])->map(fn ($i) => trim(($i['title'] ?? '') . '|' . ($i['text'] ?? '')))->implode("\n");
    }
    $usedInOld = old('used_in', $section->used_in ?? []);
    $imageRaw = old('image_url', $section->image_url ?? '');
    $imagePreview = '';
    if (!empty($imageRaw)) {
        $imagePreview = str_starts_with($imageRaw, 'http://') || str_starts_with($imageRaw, 'https://')
            ? $imageRaw
            : asset('storage/' . ltrim($imageRaw, '/'));
    }
?>

<div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
    <div class="lg:col-span-2 space-y-5">
        <section class="bg-white rounded-xl border border-gray-200 p-5">
            <h3 class="text-base font-semibold mb-4">Section Content</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                <div>
                    <label class="text-sm font-medium">Section Name *</label>
                    <input name="name" value="<?php echo e(old('name', $section->name ?? '')); ?>" class="w-full mt-1 rounded border-gray-300" required>
                </div>
                <div>
                    <label class="text-sm font-medium">Section Type *</label>
                    <select name="section_type" class="w-full mt-1 rounded border-gray-300" required>
                        <?php $__currentLoopData = ['hero'=>'Hero','cta'=>'CTA','grid'=>'Grid','form'=>'Form','faq'=>'FAQ','stats'=>'Stats','feature_list'=>'Feature List','services_block'=>'Services Block']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $value => $label): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                            <option value="<?php echo e($value); ?>" <?php echo e(old('section_type', $section->section_type ?? '') === $value ? 'selected' : ''); ?>><?php echo e($label); ?></option>
                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                    </select>
                </div>
                <div>
                    <label class="text-sm font-medium">Title</label>
                    <input name="title" value="<?php echo e(old('title', $section->title ?? '')); ?>" class="w-full mt-1 rounded border-gray-300">
                </div>
                <div>
                    <label class="text-sm font-medium">Subtitle</label>
                    <input name="subtitle" value="<?php echo e(old('subtitle', $section->subtitle ?? '')); ?>" class="w-full mt-1 rounded border-gray-300">
                </div>
                <div class="md:col-span-2">
                    <label class="text-sm font-medium">Text</label>
                    <textarea name="text" rows="4" class="w-full mt-1 rounded border-gray-300"><?php echo e(old('text', $section->text ?? '')); ?></textarea>
                </div>
                <div>
                    <label class="text-sm font-medium">Image URL</label>
                    <input name="image_url" value="<?php echo e(old('image_url', $section->image_url ?? '')); ?>" class="w-full mt-1 rounded border-gray-300">
                    <label class="text-xs text-gray-500 block mt-2">Or Upload Image</label>
                    <input type="file" name="image_file" accept="image/*" class="w-full mt-1 text-sm rounded border-gray-300">
                    <?php if($imagePreview): ?>
                        <img src="<?php echo e($imagePreview); ?>" alt="Section image" class="mt-2 h-20 w-full max-w-xs rounded object-cover border border-gray-200">
                    <?php endif; ?>
                </div>
                <div>
                    <label class="text-sm font-medium">Icon (class/text)</label>
                    <input name="icon" value="<?php echo e(old('icon', $section->icon ?? '')); ?>" class="w-full mt-1 rounded border-gray-300">
                </div>
                <div>
                    <label class="text-sm font-medium">Button Label</label>
                    <input name="button_label" value="<?php echo e(old('button_label', $section->button_label ?? '')); ?>" class="w-full mt-1 rounded border-gray-300">
                </div>
                <div>
                    <label class="text-sm font-medium">Button URL</label>
                    <input name="button_url" value="<?php echo e(old('button_url', $section->button_url ?? '')); ?>" class="w-full mt-1 rounded border-gray-300">
                </div>
                <div class="md:col-span-2">
                    <label class="text-sm font-medium">Repeater Items</label>
                    <p class="text-xs text-gray-500 mt-1">One line per item. Format: <code>title|text</code></p>
                    <textarea name="repeater_raw" rows="6" class="w-full mt-1 rounded border-gray-300 font-mono text-sm"><?php echo e($repeaterRaw); ?></textarea>
                </div>
            </div>
        </section>
    </div>

    <div class="space-y-5">
        <section class="bg-white rounded-xl border border-gray-200 p-5">
            <h3 class="text-base font-semibold mb-4">Usage</h3>
            <label class="inline-flex items-center gap-2 text-sm mb-3">
                <input type="checkbox" name="is_global" value="1" <?php echo e(old('is_global', $section->is_global ?? true) ? 'checked' : ''); ?>>
                Global (Reusable)
            </label>
            <div class="mb-3">
                <label class="text-sm font-medium">Used In Pages</label>
                <select name="used_in[]" multiple class="w-full mt-1 rounded border-gray-300 h-44">
                    <?php $__currentLoopData = $pages; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $page): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                        <option value="<?php echo e($page->slug); ?>" <?php echo e(in_array($page->slug, $usedInOld, true) ? 'selected' : ''); ?>><?php echo e($page->name); ?> (<?php echo e($page->slug); ?>)</option>
                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                </select>
            </div>
            <div class="mb-3">
                <label class="text-sm font-medium">Sort Order</label>
                <input type="number" min="0" name="sort_order" value="<?php echo e(old('sort_order', $section->sort_order ?? 0)); ?>" class="w-full mt-1 rounded border-gray-300">
            </div>
            <label class="inline-flex items-center gap-2 text-sm">
                <input type="checkbox" name="is_active" value="1" <?php echo e(old('is_active', $section->is_active ?? true) ? 'checked' : ''); ?>>
                Active
            </label>
        </section>

        <div class="bg-white rounded-xl border border-gray-200 p-4 flex gap-2">
            <button type="submit" class="flex-1 bg-blue-600 hover:bg-blue-700 text-white text-sm font-semibold px-4 py-2.5 rounded-lg">Save</button>
            <a href="<?php echo e(route('admin.sections.index')); ?>" class="px-4 py-2.5 rounded-lg border border-gray-300 text-sm text-gray-700">Cancel</a>
        </div>
    </div>
</div>
<?php /**PATH C:\xampp\htdocs\gps-migration\resources\views\admin\sections\_form.blade.php ENDPATH**/ ?>