@php
    $record = $record ?? null;
    $method = $method ?? 'POST';
@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-6">
                    <x-ui.form.input
                        name="code"
                        :label="__('organization.fields.code')"
                        :value="old('code', $record?->code)"
                        required
                    />
                </div>
                <div class="col-md-6">
                    <x-ui.form.input
                        name="name"
                        :label="__('organization.fields.name')"
                        :value="old('name', $record?->name)"
                        required
                    />
                </div>

                @if ($entity === 'section' || $entity === 'team')
                    <div class="col-md-6">
                        <x-ui.form.select
                            name="department_id"
                            :label="__('organization.fields.department')"
                            :options="$departmentOptions ?? []"
                            :selected="old('department_id', $record?->department_id)"
                        />
                    </div>
                @endif

                @if ($entity === 'team')
                    <div class="col-md-6">
                        <x-ui.form.select
                            name="section_id"
                            :label="__('organization.fields.section')"
                            :options="$sectionOptions ?? []"
                            :selected="old('section_id', $record?->section_id)"
                        />
                    </div>
                @endif

                @if ($entity === 'job_grade')
                    <div class="col-md-4">
                        <x-ui.form.input
                            name="level"
                            type="number"
                            min="1"
                            :label="__('organization.fields.level')"
                            :value="old('level', $record?->level ?? 1)"
                            required
                        />
                    </div>
                    <div class="col-md-4">
                        <x-ui.form.input
                            name="min_salary"
                            type="number"
                            step="0.01"
                            min="0"
                            :label="__('organization.fields.min_salary')"
                            :value="old('min_salary', $record?->min_salary)"
                        />
                    </div>
                    <div class="col-md-4">
                        <x-ui.form.input
                            name="max_salary"
                            type="number"
                            step="0.01"
                            min="0"
                            :label="__('organization.fields.max_salary')"
                            :value="old('max_salary', $record?->max_salary)"
                        />
                    </div>
                @endif

                @if ($entity === 'work_location')
                    <div class="col-md-6">
                        <x-ui.form.select
                            name="branch_id"
                            :label="__('organization.fields.branch')"
                            :options="$branchOptions ?? []"
                            :selected="old('branch_id', $record?->branch_id)"
                        />
                    </div>
                    <div class="col-md-6">
                        <x-ui.form.input
                            name="address"
                            :label="__('organization.fields.address')"
                            :value="old('address', $record?->address)"
                        />
                    </div>
                @endif

                <div class="col-12">
                    <x-ui.form.field
                        name="description"
                        :label="__('organization.fields.description')"
                        optional
                    >
                        <textarea
                            id="description"
                            name="description"
                            rows="3"
                            class="form-control @error('description') is-invalid @enderror"
                            placeholder="{{ __('organization.hints.description_optional') }}"
                        >{{ old('description', $record?->description) }}</textarea>
                    </x-ui.form.field>
                </div>

                <div class="col-md-4">
                    <x-ui.form.select
                        name="is_active"
                        :label="__('organization.fields.status')"
                        :options="[
                            1 => __('organization.status.active'),
                            0 => __('organization.status.inactive'),
                        ]"
                        :selected="old('is_active', $record?->is_active ?? 1)"
                        required
                    />
                </div>
            </div>
        </div>
        <div class="card-footer d-flex justify-content-end gap-2">
            <x-ui.buttons.secondary :href="route('hr.'.$route.'.index')">
                {{ __('common.cancel') }}
            </x-ui.buttons.secondary>
            <x-ui.buttons.primary type="submit">
                {{ __('common.save') }}
            </x-ui.buttons.primary>
        </div>
    </div>
</form>
