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

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

@section('content')
    @php
        $linkedBooking = $booking ?? $quotation?->booking;
        $lockSpaces = (bool) $linkedBooking;
        $prefillItems = $quotation?->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($prefillItems)->pluck('package_id')->filter()->unique()->values()->all();
        $initialMode = count($prefillItems) === 0 ? 'packages' : (count($selectedPackageIds) ? 'packages' : 'custom');
    @endphp

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

    <x-page-header title="Create Invoice" subtitle="Creates a provisional booking/reservation with the invoice" />

    <form method="POST" action="{{ route('invoices.store') }}">
        @csrf
        @if ($quotation)
            <input type="hidden" name="quotation_id" value="{{ $quotation->id }}">
        @endif
        @if ($linkedBooking)
            <input type="hidden" name="booking_id" value="{{ $linkedBooking->id }}">
        @endif

        <x-card title="Invoice Information" class="mb-3">
            <div class="row mb-1">
                <div class="col-md-3">
                    <x-form.input label="Invoice Date" name="invoice_date" type="date" :value="old('invoice_date', now()->toDateString())" />
                </div>
                <div class="col-md-3">
                    <x-form.input label="Due Date" name="due_date" type="date" :value="old('due_date')" />
                </div>
            </div>

            @include('partials.document-event-fields', [
                'booking' => $linkedBooking,
                'quotation' => $quotation ?? null,
                'lockSpaces' => $lockSpaces,
                'sectionTitle' => 'Invoice Information',
            ])
        </x-card>

        <x-card title="Spaces" class="mb-3">
            <x-document-spaces-builder
                :spaces="$spaces"
                :booking="$linkedBooking"
                :readonly="(bool) $lockSpaces"
            />
        </x-card>

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

        <div class="d-flex gap-2">
            <button type="submit" class="btn btn-primary">Save Invoice</button>
            <a href="{{ route('invoices.index') }}" class="btn btn-outline-secondary">Cancel</a>
        </div>
    </form>
@endsection
