@php
    /** @var \App\Models\Promotion|null $promotion */
    $promotion = $promotion ?? null;
    $selProducts = old('target_products', $promotion->target_products ?? []);
    $selGroups = old('target_groups', $promotion->target_groups ?? []);
    $curTargetType = old('target_type', $promotion->target_type ?? 'all');
    $boolVal = fn ($field, $default = false) => (bool) old($field, $promotion->{$field} ?? $default);
@endphp

<x-admin.tab-nav :tabs="[
    ['id' => 'general', 'label' => __('admin/promotions.fields_general'), 'icon' => 'fas fa-info-circle'],
    ['id' => 'targeting', 'label' => __('admin/promotions.target_title'), 'icon' => 'fas fa-bullseye'],
    ['id' => 'advanced', 'label' => __('admin/promotions.fields_advanced'), 'icon' => 'fas fa-sliders-h'],
]" id="promotionTabs" />

<div class="tab-content p-4" id="promotionTabsContent">

    {{-- General --}}
    <div class="tab-pane fade show active" id="general" role="tabpanel">
        <div class="row g-3">
            <div class="col-md-6">
                <div class="mb-3">
                    <label for="code" class="form-label fw-semibold">{{ __('admin/promotions.fields_code') }} <span class="text-danger">*</span></label>
                    <input type="text" class="form-control form-control-sm @error('code') is-invalid @enderror"
                           id="code" name="code" value="{{ old('code', $promotion->code ?? '') }}" required>
                    <div class="form-text small">{{ __('admin/promotions.help_code') }}</div>
                    @error('code') <div class="invalid-feedback">{{ $message }}</div> @enderror
                </div>

                <div class="mb-3">
                    <label for="description" class="form-label fw-semibold">{{ __('admin/promotions.fields_description') }}</label>
                    <textarea class="form-control form-control-sm @error('description') is-invalid @enderror"
                              id="description" name="description" rows="3">{{ old('description', $promotion->description ?? '') }}</textarea>
                    @error('description') <div class="invalid-feedback">{{ $message }}</div> @enderror
                </div>

                <div class="mb-3">
                    <label for="status" class="form-label fw-semibold">{{ __('admin/promotions.fields_status') }} <span class="text-danger">*</span></label>
                    <select class="form-select form-select-sm @error('status') is-invalid @enderror" id="status" name="status" required>
                        <option value="active" @selected(old('status', $promotion->status ?? 'active') === 'active')>{{ __('admin/promotions.statuses_active') }}</option>
                        <option value="inactive" @selected(old('status', $promotion->status ?? 'active') === 'inactive')>{{ __('admin/promotions.statuses_inactive') }}</option>
                    </select>
                    @error('status') <div class="invalid-feedback">{{ $message }}</div> @enderror
                </div>
            </div>

            <div class="col-md-6">
                <div class="row g-3">
                    <div class="col-md-6">
                        <label for="type" class="form-label fw-semibold">{{ __('admin/promotions.fields_type') }} <span class="text-danger">*</span></label>
                        <select class="form-select form-select-sm @error('type') is-invalid @enderror" id="type" name="type" required>
                            <option value="percentage" @selected(old('type', $promotion->type ?? 'percentage') === 'percentage')>{{ __('admin/promotions.types_percentage') }}</option>
                            <option value="fixed" @selected(old('type', $promotion->type ?? 'percentage') === 'fixed')>{{ __('admin/promotions.types_fixed') }}</option>
                        </select>
                        @error('type') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-md-6" id="currencyGroup">
                        <label for="currency_id" class="form-label fw-semibold">{{ __('admin/promotions.fields_currency') }} <span class="text-danger currency-required-star">*</span></label>
                        <select class="form-select form-select-sm @error('currency_id') is-invalid @enderror" id="currency_id" name="currency_id">
                            <option value="">{{ __('admin/promotions.placeholders_select_currency') }}</option>
                            @foreach($currencies as $currency)
                                <option value="{{ $currency->id }}" @selected((int) old('currency_id', $promotion->currency_id ?? 0) === $currency->id)>{{ $currency->code }} - {{ $currency->name }}</option>
                            @endforeach
                        </select>
                        @error('currency_id') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-12">
                        <label for="amount" class="form-label fw-semibold">{{ __('admin/promotions.fields_amount') }} <span class="text-danger">*</span></label>
                        <div class="input-group input-group-sm">
                            <input type="number" class="form-control @error('amount') is-invalid @enderror"
                                   id="amount" name="amount" value="{{ old('amount', $promotion->amount ?? '') }}" step="0.01" min="0" required>
                            <span class="input-group-text" id="amount_unit">%</span>
                        </div>
                        <div class="form-text small">{{ __('admin/promotions.help_amount') }}</div>
                        @error('amount') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-md-6">
                        <label for="limit" class="form-label fw-semibold">{{ __('admin/promotions.fields_limit') }}</label>
                        <input type="number" class="form-control form-control-sm @error('limit') is-invalid @enderror"
                               id="limit" name="limit" value="{{ old('limit', $promotion->limit ?? '') }}" min="1"
                               placeholder="{{ __('admin/promotions.placeholders_unlimited') }}">
                        @error('limit') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-md-6">
                        <label for="usage_limit_per_client" class="form-label fw-semibold">{{ __('admin/promotions.fields_usage_limit_per_client') }}</label>
                        <input type="number" class="form-control form-control-sm @error('usage_limit_per_client') is-invalid @enderror"
                               id="usage_limit_per_client" name="usage_limit_per_client" value="{{ old('usage_limit_per_client', $promotion->usage_limit_per_client ?? '') }}" min="1"
                               placeholder="{{ __('admin/promotions.placeholders_unlimited') }}">
                        @error('usage_limit_per_client') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-md-6">
                        <label for="cycles" class="form-label fw-semibold">{{ __('admin/promotions.fields_cycles') }}</label>
                        <input type="number" class="form-control form-control-sm @error('cycles') is-invalid @enderror"
                               id="cycles" name="cycles" value="{{ old('cycles', $promotion->cycles ?? '') }}" min="0">
                        <div class="form-text small">{{ __('admin/promotions.help_cycles') }}</div>
                        @error('cycles') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-md-3">
                        <label for="start_date" class="form-label fw-semibold">{{ __('admin/promotions.fields_start_date') }}</label>
                        <input type="date" class="form-control form-control-sm @error('start_date') is-invalid @enderror"
                               id="start_date" name="start_date" value="{{ old('start_date', optional($promotion->start_date ?? null)->format('Y-m-d')) }}">
                        @error('start_date') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>

                    <div class="col-md-3">
                        <label for="end_date" class="form-label fw-semibold">{{ __('admin/promotions.fields_end_date') }}</label>
                        <input type="date" class="form-control form-control-sm @error('end_date') is-invalid @enderror"
                               id="end_date" name="end_date" value="{{ old('end_date', optional($promotion->end_date ?? null)->format('Y-m-d')) }}">
                        @error('end_date') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- Targeting --}}
    <div class="tab-pane fade" id="targeting" role="tabpanel">
        <div class="row g-3">
            <div class="col-12">
                <div class="card border-primary">
                    <div class="card-body p-3">
                        <label for="target_type" class="form-label fw-semibold mb-3">
                            <i class="fas fa-bullseye text-primary me-1"></i>{{ __('admin/promotions.target_type') }} <span class="text-danger">*</span>
                        </label>
                        <select class="form-select form-select-sm @error('target_type') is-invalid @enderror" id="target_type" name="target_type" required>
                            <option value="all" @selected($curTargetType === 'all')>{{ __('admin/promotions.target_types_all') }}</option>
                            <option value="specific_products" @selected($curTargetType === 'specific_products')>{{ __('admin/promotions.target_types_specific_products') }}</option>
                            <option value="specific_groups" @selected($curTargetType === 'specific_groups')>{{ __('admin/promotions.target_types_specific_groups') }}</option>
                        </select>
                        <div class="form-text small">{{ __('admin/promotions.help_target_type') }}</div>
                        @error('target_type') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>
                </div>
            </div>

            <div class="col-12" id="targetProductsGroup" style="display: none;">
                <div class="card">
                    <div class="card-body p-3">
                        <label for="target_products" class="form-label fw-semibold"><i class="fas fa-box text-info me-1"></i>{{ __('admin/promotions.target_products') }}</label>
                        <select class="form-select form-select-sm @error('target_products') is-invalid @enderror" id="target_products" name="target_products[]" multiple size="10">
                            @foreach($products as $product)
                                <option value="{{ $product->id }}" {{ in_array($product->id, $selProducts) ? 'selected' : '' }}>{{ $product->name }}</option>
                            @endforeach
                        </select>
                        <div class="form-text small">{{ __('admin/promotions.help_target_products') }}</div>
                        @error('target_products') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>
                </div>
            </div>

            <div class="col-12" id="targetGroupsGroup" style="display: none;">
                <div class="card">
                    <div class="card-body p-3">
                        <label for="target_groups" class="form-label fw-semibold"><i class="fas fa-layer-group text-success me-1"></i>{{ __('admin/promotions.target_groups') }}</label>
                        <select class="form-select form-select-sm @error('target_groups') is-invalid @enderror" id="target_groups" name="target_groups[]" multiple size="10">
                            @foreach($productGroups as $group)
                                <option value="{{ $group->id }}" {{ in_array($group->id, $selGroups) ? 'selected' : '' }}>{{ $group->name }}</option>
                            @endforeach
                        </select>
                        <div class="form-text small">{{ __('admin/promotions.help_target_groups') }}</div>
                        @error('target_groups') <div class="invalid-feedback">{{ $message }}</div> @enderror
                    </div>
                </div>
            </div>
        </div>
    </div>

    {{-- Advanced --}}
    <div class="tab-pane fade" id="advanced" role="tabpanel">
        <div class="alert alert-info alert-sm mb-3">
            <i class="fas fa-info-circle me-1"></i>{{ __('admin/promotions.advanced_description') }}
        </div>
        <div class="row g-3">
            @foreach([
                'is_combinable' => 'fields_is_combinable',
                'only_new_client' => 'fields_only_new_client',
                'only_existing_client' => 'fields_only_existing_client',
                'only_once_client' => 'fields_only_once_client',
                'only_new_orders' => 'fields_only_new_orders',
                'allow_renewal' => 'fields_allow_renewal',
                'allow_upgrades' => 'fields_allow_upgrades',
            ] as $field => $label)
                <div class="col-md-4">
                    <div class="form-check form-switch">
                        <input class="form-check-input" type="checkbox" id="{{ $field }}" name="{{ $field }}" value="1" @checked($boolVal($field))>
                        <label class="form-check-label" for="{{ $field }}">{{ __('admin/promotions.'.$label) }}</label>
                    </div>
                </div>
            @endforeach

            <div class="col-md-6">
                <label for="applies_to" class="form-label fw-semibold">{{ __('admin/promotions.fields_applies_to') }}</label>
                <input type="text" class="form-control form-control-sm @error('applies_to') is-invalid @enderror"
                       id="applies_to" name="applies_to" value="{{ old('applies_to', $promotion->applies_to ?? '') }}">
                <div class="form-text small">{{ __('admin/promotions.help_applies_to') }}</div>
                @error('applies_to') <div class="invalid-feedback">{{ $message }}</div> @enderror
            </div>

            <div class="col-md-6">
                <label for="requires_for" class="form-label fw-semibold">{{ __('admin/promotions.fields_requires_for') }}</label>
                <input type="text" class="form-control form-control-sm @error('requires_for') is-invalid @enderror"
                       id="requires_for" name="requires_for" value="{{ old('requires_for', $promotion->requires_for ?? '') }}">
                <div class="form-text small">{{ __('admin/promotions.help_requires_for') }}</div>
                @error('requires_for') <div class="invalid-feedback">{{ $message }}</div> @enderror
            </div>
        </div>
    </div>
</div>

@push('scripts')
<script>
    window.promotionForm = {
        initialTargetType: @json($curTargetType),
        currencies: @json($currencies->map(fn ($c) => ['id' => $c->id, 'code' => $c->code])),
    };
</script>
@vite('resources/js/admin/promotions/form.js')
@endpush
