@php
    $editing = $row !== null;
    $action  = $editing ? [$s['route'].'.update', $row->id] : $s['route'].'.store';
    $size    = $s['modalSize'] ?? 'modal-lg';
@endphp
<div class="modal-dialog {{ $size }}" role="document">
    <div class="modal-content">
        {!! Form::open(['route' => $action, 'method' => $editing ? 'PUT' : 'POST', 'class' => 'vrc-form']) !!}
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">{{ $editing ? __('messages.edit') : __('messages.add') }} — {{ $s['singular'] }}</h4>
        </div>
        <div class="modal-body">
            <div class="row">
                @foreach($s['fields'] as $f)
                    @php
                        $name = $f['name'];
                        $type = $f['type'] ?? 'text';
                        $col  = $f['col'] ?? 6;
                        $val  = $editing ? data_get($row, $name) : ($f['default'] ?? null);
                        $req  = ! empty($f['required']);
                    @endphp
                    @if($type === 'checkbox')
                        <div class="col-md-{{ $col }}">
                            <div class="form-group"><br>
                                <label><input type="checkbox" name="{{ $name }}" value="1" {{ $val ? 'checked' : '' }}> {{ $f['label'] }}</label>
                            </div>
                        </div>
                    @else
                        <div class="col-md-{{ $col }}">
                            <div class="form-group">
                                {!! Form::label($name, $f['label'] . ($req ? ' *' : '') . ':') !!}
                                @if($type === 'textarea')
                                    {!! Form::textarea($name, $val, ['class' => 'form-control', 'rows' => $f['rows'] ?? 2] + ($req ? ['required'] : [])) !!}
                                @elseif($type === 'select')
                                    {!! Form::select($name, $f['options'] ?? [], $val, ['class' => 'form-control vrc-select2', 'placeholder' => $f['placeholder'] ?? __('messages.please_select')] + ($req ? ['required'] : [])) !!}
                                @elseif($type === 'number')
                                    {!! Form::number($name, $val, ['class' => 'form-control', 'step' => $f['step'] ?? 'any', 'min' => $f['min'] ?? null] + ($req ? ['required'] : [])) !!}
                                @elseif($type === 'date')
                                    {!! Form::date($name, $val ? \Carbon\Carbon::parse($val)->format('Y-m-d') : null, ['class' => 'form-control'] + ($req ? ['required'] : [])) !!}
                                @elseif($type === 'datetime')
                                    {!! Form::input('datetime-local', $name, $val ? \Carbon\Carbon::parse($val)->format('Y-m-d\TH:i') : null, ['class' => 'form-control'] + ($req ? ['required'] : [])) !!}
                                @else
                                    {!! Form::text($name, $val, ['class' => 'form-control', 'dir' => ($f['rtl'] ?? false) ? 'rtl' : 'ltr'] + ($req ? ['required'] : [])) !!}
                                @endif
                                @if(!empty($f['help']))<span class="help-block">{{ $f['help'] }}</span>@endif
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
        <div class="modal-footer">
            <button type="submit" class="btn btn-primary">@lang('messages.save')</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">@lang('messages.close')</button>
        </div>
        {!! Form::close() !!}
    </div>
</div>
