@extends('layout')

@section('title', __('store.quotes_title'))

@push('styles')
    @vite('resources/css/store/tables.css')
@endpush

@section('content')
<div class="container py-5">
    <div class="row">
        <div class="col-md-12">
            <x-clientarea.page-header
                icon="fas fa-file-contract"
                :title="__('store.quotes_title')"
                :subtitle="$quotes->total() . ' ' . __('store.quotes_subtitle')"
            />

            
            <div class="card border-0 shadow-sm mb-4">
                <div class="card-body py-2">
                    <div class="d-flex flex-wrap gap-2 align-items-center">
                        <span class="text-muted me-2"><i class="fas fa-filter me-1"></i></span>
                        <a href="{{ route('store.auth.quotes') }}"
                           class="btn btn-sm {{ !$currentFilter ? 'btn-primary' : 'btn-outline-secondary' }}">
                            {{ __('store.quotes_filter_all') }}
                        </a>
                        <a href="{{ route('store.auth.quotes', ['status' => 'sent']) }}"
                           class="btn btn-sm {{ $currentFilter === 'sent' ? 'btn-info' : 'btn-outline-info' }}">
                            <i class="fas fa-paper-plane me-1"></i>{{ __('store.quotes_filter_sent') }}
                        </a>
                        <a href="{{ route('store.auth.quotes', ['status' => 'accepted']) }}"
                           class="btn btn-sm {{ $currentFilter === 'accepted' ? 'btn-success' : 'btn-outline-success' }}">
                            <i class="fas fa-check-circle me-1"></i>{{ __('store.quotes_filter_accepted') }}
                        </a>
                        <a href="{{ route('store.auth.quotes', ['status' => 'rejected']) }}"
                           class="btn btn-sm {{ $currentFilter === 'rejected' ? 'btn-danger' : 'btn-outline-danger' }}">
                            <i class="fas fa-times-circle me-1"></i>{{ __('store.quotes_filter_rejected') }}
                        </a>
                        <a href="{{ route('store.auth.quotes', ['status' => 'expired']) }}"
                           class="btn btn-sm {{ $currentFilter === 'expired' ? 'btn-warning' : 'btn-outline-warning' }}">
                            <i class="fas fa-clock me-1"></i>{{ __('store.quotes_filter_expired') }}
                        </a>
                    </div>
                </div>
            </div>

            @if($quotes->count() > 0)
                <div class="card border-0 shadow-sm">
                    <div class="card-body">
                        <table class="table table-hover mb-0">
                            <thead>
                                <tr>
                                    <th>{{ __('store.quotes_quote_number') }}</th>
                                    <th>{{ __('store.quotes_subject') }}</th>
                                    <th class="d-none d-md-table-cell">{{ __('store.quotes_date') }}</th>
                                    <th class="d-none d-lg-table-cell">{{ __('store.quotes_valid_until') }}</th>
                                    <th>{{ __('store.quotes_total') }}</th>
                                    <th>{{ __('store.quotes_status') }}</th>
                                    <th class="text-end">{{ __('store.common_actions') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($quotes as $quote)
                                    <tr>
                                        <td>
                                            <strong>{{ $quote->quote_number }}</strong>
                                        </td>
                                        <td>{{ $quote->subject }}</td>
                                        <td class="d-none d-md-table-cell">
                                            <span>{{ $quote->created_at->format($dateFormat) }}</span>
                                            <br><small class="text-muted">{{ $quote->created_at->format('H:i') }}</small>
                                        </td>
                                        <td class="d-none d-lg-table-cell">
                                            @if($quote->isExpired() && $quote->status === 'sent')
                                                <span class="text-danger">{{ $quote->valid_until->format($dateFormat) }}</span>
                                                <br><small class="text-danger">{{ __('store.quotes_status_expired') }}</small>
                                            @else
                                                <span>{{ $quote->valid_until->format($dateFormat) }}</span>
                                                @if($quote->status === 'sent' && !$quote->isExpired())
                                                    @php
                                                        $daysLeft = (int) now()->diffInDays($quote->valid_until, false);
                                                    @endphp
                                                    <br><small class="text-success">
                                                        @if($daysLeft === 0)
                                                            {{ __('store.quotes_valid_for_today') }}
                                                        @else
                                                            {{ __('store.quotes_valid_for_days', ['days' => $daysLeft]) }}
                                                        @endif
                                                    </small>
                                                @endif
                                            @endif
                                        </td>
                                        <td>
                                            <strong>{{ number_format($quote->total, 2) }} {{ $quote->currency->code }}</strong>
                                        </td>
                                        <td>
                                            <span class="badge bg-{{ match($quote->status) {
                                                'sent' => 'info',
                                                'accepted' => 'success',
                                                'rejected' => 'danger',
                                                'expired' => 'warning',
                                                'converted' => 'primary',
                                                default => 'secondary'
                                            } }}">
                                                <i class="fas fa-{{ match($quote->status) {
                                                    'sent' => 'paper-plane',
                                                    'accepted' => 'check-circle',
                                                    'rejected' => 'times-circle',
                                                    'expired' => 'clock',
                                                    'converted' => 'file-invoice',
                                                    default => 'question-circle'
                                                } }} me-1"></i>
                                                {{ __('store.quotes_status_' . $quote->status) }}
                                            </span>
                                        </td>
                                        <td class="text-end">
                                            <a href="{{ route('store.auth.quotes.detail', $quote->id) }}" class="btn btn-sm btn-outline-primary">
                                                {{ __('store.quotes_view') }}
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>

                <div class="mt-3">
                    {{ $quotes->links() }}
                </div>
            @else
                <div class="card border-0 shadow-sm">
                    <div class="card-body text-center py-5">
                        <i class="fas fa-file-alt fa-3x text-muted mb-3"></i>
                        <h5>{{ __('store.quotes_no_quotes') }}</h5>
                    </div>
                </div>
            @endif
        </div>
    </div>
</div>
@endsection
