<div>
    <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3 mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('إنشاء خطة دفع') }}</h1>
    </div>

    @if(session('success'))
    <div class="mb-4 p-3 bg-green-50 border border-green-200 rounded-lg text-green-700 text-sm">{{ session('success') }}</div>
    @endif

    <div class="grid grid-cols-1 lg:grid-cols-2 gap-3 sm:gap-4">
        <!-- Configuration -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800 mb-4">{{ __('إعدادات الخطة') }}</h3>

            <div class="space-y-4">
                <div>
                    <label class="block text-sm text-gray-600 mb-1">{{ __('الفاتورة') }}</label>
                    <select wire:model.live="invoiceId" class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                        <option value="">{{ __('اختر فاتورة') }}</option>
                        @foreach($invoices as $inv)
                        <option value="{{ $inv->id }}">
                            {{ $inv->number }} — {{ format_money($inv->due_amount) }}
                        </option>
                        @endforeach
                    </select>
                </div>

                @if($invoice)
                <div class="p-3 bg-blue-50 rounded-lg text-sm">
                    <p>{{ __('إجمالي الفاتورة') }}: <strong dir="ltr">{{ format_money($invoice->total_amount) }}</strong></p>
                    <p>{{ __('المتبقي') }}: <strong dir="ltr">{{ format_money($invoice->due_amount) }}</strong></p>
                </div>
                @endif

                <div>
                    <label class="block text-sm text-gray-600 mb-1">{{ __('دفعة مقدمة (ج.م)') }}</label>
                    <input type="number" wire:model.live.debounce.500ms="downPayment" dir="ltr" min="0"
                           class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                </div>

                <div>
                    <label class="block text-sm text-gray-600 mb-1">{{ __('عدد الأقساط') }}</label>
                    <input type="number" wire:model.live="installmentCount" dir="ltr" min="2" max="24"
                           class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                </div>

                <div>
                    <label class="block text-sm text-gray-600 mb-1">{{ __('التكرار') }}</label>
                    <select wire:model.live="frequency" class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                        <option value="weekly">{{ __('أسبوعي') }}</option>
                        <option value="biweekly">{{ __('كل أسبوعين') }}</option>
                        <option value="monthly">{{ __('شهري') }}</option>
                    </select>
                </div>

                <div>
                    <label class="block text-sm text-gray-600 mb-1">{{ __('تاريخ أول قسط') }}</label>
                    <input type="date" wire:model.live="startDate" dir="ltr"
                           class="w-full rounded-lg border-gray-300 text-sm py-2.5">
                </div>
            </div>
        </div>

        <!-- Preview -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800 mb-4">{{ __('معاينة الأقساط') }}</h3>

            @if(count($installments) > 0)
            <div class="space-y-2">
                @if($downPayment > 0)
                <div class="flex items-center justify-between p-3 bg-green-50 rounded-lg">
                    <span class="text-sm text-gray-700">{{ __('دفعة مقدمة') }}</span>
                    <span class="text-sm font-medium text-green-700" dir="ltr">{{ format_money($downPayment * 100) }}</span>
                </div>
                @endif

                @foreach($installments as $inst)
                <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                    <div>
                        <span class="text-sm text-gray-700">{{ __('قسط') }} {{ $inst['number'] }}</span>
                        <span class="text-xs text-gray-500 ms-2" dir="ltr">{{ $inst['due_date'] }}</span>
                    </div>
                    <span class="text-sm font-medium text-gray-800" dir="ltr">{{ format_money($inst['amount']) }}</span>
                </div>
                @endforeach

                <div class="pt-3 mt-3 border-t border-gray-200 flex items-center justify-between">
                    <span class="text-sm font-medium text-gray-700">{{ __('الإجمالي') }}</span>
                    <span class="text-sm font-bold text-gray-900" dir="ltr">
                        {{ format_money(($downPayment * 100) + collect($installments)->sum('amount')) }}
                    </span>
                </div>
            </div>

            <button wire:click="save" wire:loading.attr="disabled"
                    class="w-full mt-6 px-4 sm:px-6 py-2.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 disabled:opacity-50 text-sm font-medium">
                <span wire:loading.remove wire:target="save">{{ __('إنشاء خطة الدفع') }}</span>
                <span wire:loading wire:target="save">{{ __('جارٍ الإنشاء...') }}</span>
            </button>
            @else
            <p class="text-sm text-gray-500 text-center py-8">{{ __('اختر فاتورة لمعاينة الأقساط') }}</p>
            @endif
        </div>
    </div>
</div>
