@extends('layouts.app')

@section('title', 'Quotations')

@section('content')
    <x-breadcrumbs :items="[
        ['label' => 'Dashboard', 'url' => route('dashboard')],
        ['label' => 'Quotations'],
    ]" />

    <x-page-header title="Quotations" subtitle="Manage quotations">
        @can('create', \App\Models\Quotation::class)
            <a href="{{ route('quotations.create') }}" class="btn btn-primary btn-sm">
                <i class="bi bi-plus-lg me-1"></i> Create Quotation
            </a>
        @endcan
    </x-page-header>

    <div class="vem-filter-bar">
        <form method="GET" action="{{ route('quotations.index') }}" class="row g-3 align-items-end">
            <div class="col-md-4">
                <label class="form-label" for="search">Search</label>
                <input type="search" name="search" id="search" class="form-control form-control-sm" value="{{ request('search') }}" placeholder="Number or customer...">
            </div>
            <div class="col-md-3">
                <label class="form-label" for="status">Status</label>
                <select name="status" id="status" class="form-select form-select-sm">
                    <option value="">All statuses</option>
                    @foreach (\App\Enums\QuotationStatus::cases() as $status)
                        <option value="{{ $status->value }}" @selected(request('status') === $status->value)>{{ $status->label() }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-5 d-flex gap-2">
                <button type="submit" class="btn btn-primary btn-sm">Filter</button>
                <a href="{{ route('quotations.index') }}" class="btn btn-outline-secondary btn-sm">Reset</a>
            </div>
        </form>
    </div>

    <x-card flush>
        @if ($quotations->isEmpty())
            <x-empty-state icon="file-earmark-text" title="No quotations yet" description="Create quotations for customers and events.">
                @can('create', \App\Models\Quotation::class)
                    <a href="{{ route('quotations.create') }}" class="btn btn-primary btn-sm">Create Quotation</a>
                @endcan
            </x-empty-state>
        @else
            <x-table :headers="['Number', 'Customer', 'Date', 'Status', 'Total', 'Actions']">
                @foreach ($quotations as $quotation)
                    <tr>
                        <td><a href="{{ route('quotations.show', $quotation) }}" class="fw-medium">{{ $quotation->quotation_number }}</a></td>
                        <td>{{ $quotation->customer->name ?? '—' }}</td>
                        <td><x-datetime :value="$quotation->quotation_date" /></td>
                        <td><x-badge :status="$quotation->status->value" :label="$quotation->status->label()" /></td>
                        <td><x-money :amount="$quotation->grand_total" :currency="$quotation->currency" /></td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{{ route('quotations.show', $quotation) }}" class="btn btn-outline-primary">View</a>
                                @can('update', $quotation)
                                    @if ($quotation->isEditable())
                                        <a href="{{ route('quotations.edit', $quotation) }}" class="btn btn-outline-secondary">Edit</a>
                                    @endif
                                @endcan
                                @can('convert', $quotation)
                                    @if ($quotation->status === \App\Enums\QuotationStatus::Accepted)
                                        <form method="POST" action="{{ route('quotations.convert', $quotation) }}" class="d-inline">
                                            @csrf
                                            <button type="submit" class="btn btn-success">Convert</button>
                                        </form>
                                    @endif
                                @endcan
                            </div>
                        </td>
                    </tr>
                @endforeach
            </x-table>
            @if ($quotations->hasPages())
                <div class="p-3 border-top">{{ $quotations->links() }}</div>
            @endif
        @endif
    </x-card>
@endsection
