@extends('admin.layout')

@section('title', ($row ? 'Edit ' : 'Add ') . $config['title'])

@section('body')
<div class="shell">
    @include('admin.partials.sidebar')
    <main class="main">
        <div class="topbar">
            <div><h1>{{ $row ? 'Edit' : 'Add' }} {{ $config['title'] }}</h1><div class="crumb">Professional RapidSewa admin form</div></div>
            <div class="actions">
                <a class="btn btn-gray" href="{{ route('admin.module.index', $module) }}">Back to List</a>
                @include('admin.partials.logout')
            </div>
        </div>
        @if ($errors->any()) <div class="error">{{ $errors->first() }}</div> @endif
        <section class="panel">
            <form method="post" action="{{ $row ? route('admin.module.update', [$module, $row->id]) : route('admin.module.store', $module) }}">
                @csrf
                @if ($row) @method('PUT') @endif
                <div class="form-grid">
                    @foreach ($config['fields'] as $field => $meta)
                        @php
                            $type = $meta['type'] ?? 'text';
                            $value = old($field, $row->{$field} ?? '');
                        @endphp
                        <div class="{{ in_array($type, ['textarea','editor'], true) ? 'full' : '' }}">
                            <label>{{ $meta['label'] ?? ucwords(str_replace('_', ' ', $field)) }}</label>
                            @if ($type === 'select')
                                <select name="{{ $field }}">
                                    @foreach (($meta['options'] ?? []) as $option)
                                        <option value="{{ $option }}" @selected($value == $option)>{{ ucwords(str_replace('_', ' ', $option)) }}</option>
                                    @endforeach
                                </select>
                            @elseif ($type === 'textarea' || $type === 'editor')
                                <textarea class="{{ $type === 'editor' ? 'editor' : '' }}" name="{{ $field }}">{{ $value }}</textarea>
                            @elseif ($type === 'checkbox')
                                <input type="hidden" name="{{ $field }}" value="0">
                                <label style="display:flex;gap:8px;align-items:center"><input style="width:auto" type="checkbox" name="{{ $field }}" value="1" @checked((bool) $value)> Enabled</label>
                            @else
                                <input type="{{ $type }}" name="{{ $field }}" value="{{ $value }}">
                            @endif
                        </div>
                    @endforeach
                </div>
                <p style="margin-top:18px"><button type="submit">{{ $row ? 'Update' : 'Save' }}</button></p>
            </form>
        </section>
    </main>
</div>
@endsection
