@extends('layouts.app', ['includeFlatpickr' => true])

@section('title', 'Edit Invoice')

@section('content')
    @php
        $itemSeed = $invoice->items->map(fn ($item) => [
            'description' => $item->description,
            'quantity' => $item->quantity,
            'unit_price' => $item->unit_price,
            'charge_basis' => $item->charge_basis->value,
            'guest_count' => $item->guest_count,
            'billable_days' => $item->billable_days,
            'billable_hours' => $item->billable_hours,
            'discount_amount' => $item->discount_amount,
            'tax_rate' => $item->tax_rate,
            'product_id' => $item->product_id,
            'package_id' => $item->package_id,
        ])->all();
        $selectedPackageIds = collect($itemSeed)->pluck('package_id')->filter()->unique()->values()->all();
        $initialMode = count($selectedPackageIds) ? 'packages' : 'custom';
    @endphp

    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Invoices', 'url' => route('invoices.index')],
        ['label' => $invoice->invoice_number, 'url' => route('invoices.show', $invoice)],
        ['label' => 'Edit'],
    ]" />

    <x-page-header title="Edit Invoice" :subtitle="$invoice->invoice_number" />

    <x-card>
        <form method="POST" action="{{ route('invoices.update', $invoice) }}">
            @csrf
            @method('PUT')

            <div class="row">
                <div class="col-md-6">
                    <x-form.select label="Customer" name="customer_id" :options="$customers->pluck('name', 'id')" :selected="$invoice->customer_id" required placeholder="Select customer" />
                </div>
                <div class="col-md-3">
                    <x-form.input label="Invoice Date" name="invoice_date" type="date" :value="old('invoice_date', $invoice->invoice_date?->format('Y-m-d'))" />
                </div>
                <div class="col-md-3">
                    <x-form.input label="Due Date" name="due_date" type="date" :value="old('due_date', $invoice->due_date?->format('Y-m-d'))" />
                </div>
                <div class="col-md-3">
                    <x-form.input label="Guest Count" name="guest_count" type="number" :value="old('guest_count', $invoice->guest_count)" min="0" />
                </div>
            </div>

            <x-space-availability-picker :spaces="$spaces" :booking="$invoice->booking" :disabled="(bool) $invoice->booking_id" />

            <x-document-pricing-builder
                :packages="$packages"
                :items="old('items', $itemSeed)"
                initial-mode="{{ old('pricing_mode', $initialMode) }}"
                :selected-package-ids="$selectedPackageIds"
                :currency="app(\App\Services\SettingsService::class)->get('currency', 'KES')"
            />

            <div class="d-flex gap-2 mt-4">
                <button type="submit" class="btn btn-primary">Update Invoice</button>
                <a href="{{ route('invoices.show', $invoice) }}" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </form>
    </x-card>
@endsection
