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

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

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

    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Quotations', 'url' => route('quotations.index')],
        ['label' => $quotation->quotation_number, 'url' => route('quotations.show', $quotation)],
        ['label' => 'Edit'],
    ]" />

    <x-page-header title="Edit Quotation" :subtitle="$quotation->quotation_number" />

    <x-card>
        <form method="POST" action="{{ route('quotations.update', $quotation) }}">
            @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="$quotation->customer_id" required placeholder="Select customer" />
                </div>
                <div class="col-md-3">
                    <x-form.input label="Quotation Date" name="quotation_date" type="date" :value="old('quotation_date', $quotation->quotation_date?->format('Y-m-d'))" />
                </div>
                <div class="col-md-3">
                    <x-form.input label="Expires At" name="expires_at" type="date" :value="old('expires_at', $quotation->expires_at?->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', $quotation->guest_count)" min="0" />
                </div>
            </div>

            <x-space-availability-picker :spaces="$spaces" :booking="$quotation->booking" :disabled="(bool) $quotation->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 Quotation</button>
                <a href="{{ route('quotations.show', $quotation) }}" class="btn btn-outline-secondary">Cancel</a>
            </div>
        </form>
    </x-card>
@endsection
