@props([
    'action',
    'method' => 'POST',
    'submitLabel',
    'typeOptions' => [],
])

@php
    $optionRows = old('options', array_fill(0, 5, ['value_key' => '', 'label' => '']));
@endphp

<form method="POST" action="{{ $action }}" novalidate>
    @csrf
    @if (strtoupper($method) !== 'POST')
        @method($method)
    @endif

    <div class="card">
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-4">
                    <x-ui.form.input
                        name="code"
                        :label="__('inventory.attributes.fields.code')"
                        :value="old('code')"
                        :placeholder="__('inventory.attributes.placeholders.code')"
                        required
                    />
                </div>
                <div class="col-md-4">
                    <x-ui.form.input
                        name="name"
                        :label="__('inventory.attributes.fields.name')"
                        :value="old('name')"
                        :placeholder="__('inventory.attributes.placeholders.name')"
                        required
                    />
                </div>
                <div class="col-md-4">
                    <x-ui.form.select
                        name="type"
                        :label="__('inventory.attributes.fields.type')"
                        :options="$typeOptions"
                        :selected="old('type')"
                        required
                    />
                </div>
            </div>

            <hr class="my-4">

            <p class="text-muted small mb-3">{{ __('inventory.attributes.options_hint') }}</p>
            @error('options')
                <div class="alert alert-danger py-2">{{ $message }}</div>
            @enderror

            <div class="table-responsive">
                <table class="table table-sm align-middle mb-0">
                    <thead>
                        <tr>
                            <th>{{ __('inventory.attributes.fields.option_key') }}</th>
                            <th>{{ __('inventory.attributes.fields.option_label') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($optionRows as $index => $row)
                            <tr>
                                <td>
                                    <input
                                        type="text"
                                        name="options[{{ $index }}][value_key]"
                                        class="form-control form-control-sm @error('options.'.$index.'.value_key') is-invalid @enderror"
                                        value="{{ $row['value_key'] ?? '' }}"
                                        placeholder="{{ __('inventory.attributes.placeholders.option_key') }}"
                                    >
                                </td>
                                <td>
                                    <input
                                        type="text"
                                        name="options[{{ $index }}][label]"
                                        class="form-control form-control-sm @error('options.'.$index.'.label') is-invalid @enderror"
                                        value="{{ $row['label'] ?? '' }}"
                                        placeholder="{{ __('inventory.attributes.placeholders.option_label') }}"
                                    >
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="d-flex justify-content-end gap-2 mt-4">
                <x-ui.buttons.secondary :href="route('inventory.attributes.index')">
                    {{ __('common.cancel') }}
                </x-ui.buttons.secondary>
                <x-ui.buttons.primary type="submit">{{ $submitLabel }}</x-ui.buttons.primary>
            </div>
        </div>
    </div>
</form>
