@extends('layouts.app')
@section('title',($record->exists?'Edit ':'Add ').$config['singular'])
@section('page_title',($record->exists?'Edit ':'Add ').$config['singular'])
@section('content')
<div class="page-head"><div><h1>{{ $record->exists?'Edit':'Add' }} {{ $config['singular'] }}</h1><p>Complete the required fields and save the record.</p></div><a class="btn btn-secondary" href="{{ route('masters.index',$module) }}">Back</a></div>
<form method="post" action="{{ $record->exists ? route('masters.update',[$module,$record->id]) : route('masters.store',$module) }}">@csrf @if($record->exists)@method('put')@endif
<div class="card"><div class="card-body"><div class="form-grid">
@foreach($config['fields'] as $name=>$field)
    @php($type=$field['type'] ?? 'text')
    @php($value=old($name,$record->{$name} ?? ($field['default'] ?? null)))
    @if($type==='checkbox')
        <div class="checkbox"><input id="{{ $name }}" type="checkbox" name="{{ $name }}" value="1" @checked((bool)$value)><label for="{{ $name }}">{{ $field['label'] }}</label></div>
    @else
        <div class="field {{ $type==='textarea'?'full':'' }}"><label for="{{ $name }}">{{ $field['label'] }}</label>
        @if($type==='textarea')<textarea class="control" id="{{ $name }}" name="{{ $name }}">{{ $value }}</textarea>
        @elseif(in_array($type,['select','relation']))<select class="control" id="{{ $name }}" name="{{ $name }}"><option value="">Select...</option>@foreach($options[$name] ?? [] as $key=>$label)<option value="{{ $key }}" @selected((string)$value===(string)$key)>{{ $label }}</option>@endforeach</select>
        @else<input class="control" id="{{ $name }}" type="{{ $type }}" name="{{ $name }}" value="{{ $value }}" @if(isset($field['step']))step="{{ $field['step'] }}"@endif></input>@endif
        @error($name)<div class="invalid">{{ $message }}</div>@enderror</div>
    @endif
@endforeach
</div></div><div class="card-head" style="justify-content:flex-end"><a class="btn btn-secondary" href="{{ route('masters.index',$module) }}">Cancel</a><button class="btn btn-primary">Save {{ $config['singular'] }}</button></div></div>
</form>
@endsection
