@extends('layouts.app')

@section('title', 'Warranty')

@section('content')
    @php $hasFilters = (bool) array_filter($filters); @endphp

    <x-page-header
        title="Warranty"
        :subtitle="trans_choice(':count coverage period|:count coverage periods', $warranties->total(), ['count' => $warranties->total()])">

        <a href="{{ route('warranty.expiring') }}" class="btn btn-outline-secondary">
            <i class="bi bi-exclamation-triangle" aria-hidden="true"></i>Expiring Soon
        </a>

        <a href="{{ route('warranty.export', request()->query()) }}" class="btn btn-outline-secondary">
            <i class="bi bi-download" aria-hidden="true"></i>Export
        </a>
    </x-page-header>

    <div class="row g-3 mb-3">
        <div class="col-6 col-md-4 col-xl-2dot4">
            <x-stat-card
                label="Active"
                :value="$cards['active']"
                icon="shield-check"
                color="success"
                :href="route('warranty.index', ['status' => 'active'])" />
        </div>
        <div class="col-6 col-md-4 col-xl-2dot4">
            <x-stat-card
                label="Expiring in 30 Days"
                :value="$cards['expiring_30']"
                icon="hourglass-split"
                color="warning"
                :href="route('warranty.index', ['status' => 'expiring', 'days' => 30])" />
        </div>
        <div class="col-6 col-md-4 col-xl-2dot4">
            <x-stat-card
                label="Expiring in 60 Days"
                :value="$cards['expiring_60']"
                icon="hourglass-split"
                color="warning"
                :href="route('warranty.index', ['status' => 'expiring', 'days' => 60])" />
        </div>
        <div class="col-6 col-md-4 col-xl-2dot4">
            <x-stat-card
                label="Expiring in 90 Days"
                :value="$cards['expiring_90']"
                icon="hourglass-split"
                color="warning"
                :href="route('warranty.index', ['status' => 'expiring', 'days' => 90])" />
        </div>
        <div class="col-6 col-md-4 col-xl-2dot4">
            <x-stat-card
                label="Expired"
                :value="$cards['expired']"
                icon="shield-x"
                color="danger"
                :href="route('warranty.index', ['status' => 'expired'])" />
        </div>
    </div>

    <x-table.wrapper>
        <x-slot:toolbar>
            <x-filter-bar :action="route('warranty.index')" :active="$hasFilters">
                <x-filter.search :value="$filters['search']" placeholder="Search asset, provider or reference…" />

                <x-filter.select
                    name="type"
                    label="Type"
                    placeholder="Any type"
                    :selected="$filters['type']"
                    :options="\App\Enums\WarrantyType::options()" />

                <x-filter.select
                    name="status"
                    label="Status"
                    placeholder="Any status"
                    :selected="$filters['status']"
                    :options="['active' => 'Active', 'expiring' => 'Expiring Soon', 'expired' => 'Expired']" />
            </x-filter-bar>
        </x-slot:toolbar>

        <x-slot:head>
            <tr>
                <th scope="col">Asset</th>
                <th scope="col">Type</th>
                <th scope="col">Provider</th>
                <th scope="col">Coverage</th>
                <th scope="col">Status</th>
                <th scope="col" class="text-end">Actions</th>
            </tr>
        </x-slot:head>

        @forelse ($warranties as $warranty)
            <tr>
                <td>
                    <a href="{{ route('assets.show', $warranty->asset) }}" class="text-decoration-none">
                        <div class="ams-cell-code fw-semibold">{{ $warranty->asset->asset_tag }}</div>
                        <div class="ams-cell-secondary ams-truncate">{{ $warranty->asset->name }}</div>
                    </a>
                </td>
                <td>
                    <x-status-badge :status="$warranty->type" />
                    @if ($warranty->is_primary)
                        <x-badge color="primary" dot>Current</x-badge>
                    @endif
                </td>
                <td class="ams-cell-secondary">{{ $warranty->provider ?? '—' }}</td>
                <td class="ams-cell-secondary">
                    {{ $warranty->start_date->format('d M Y') }} – {{ $warranty->end_date->format('d M Y') }}
                </td>
                <td>
                    <x-status-badge :status="$warranty->status()" />
                </td>
                <td class="text-end text-nowrap">
                    <x-table.actions>
                        <a href="{{ route('assets.show', $warranty->asset) }}"
                           class="btn btn-sm btn-icon"
                           data-bs-toggle="tooltip"
                           title="View Asset">
                            <i class="bi bi-eye" aria-hidden="true"></i>
                            <span class="visually-hidden">View {{ $warranty->asset->asset_tag }}</span>
                        </a>

                        @if ($warranty->is_primary)
                            <a href="{{ route('assets.warranties.renew.create', [$warranty->asset, $warranty]) }}"
                               class="btn btn-sm btn-icon"
                               data-bs-toggle="tooltip"
                               title="Renew">
                                <i class="bi bi-arrow-repeat" aria-hidden="true"></i>
                                <span class="visually-hidden">Renew coverage for {{ $warranty->asset->asset_tag }}</span>
                            </a>
                        @endif
                    </x-table.actions>
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="6" class="p-0">
                    <x-table.empty-state
                        icon="shield-check"
                        :title="$hasFilters ? 'No coverage matches these filters' : 'No warranties recorded yet'"
                        :message="$hasFilters ? 'Try clearing the filters to see every coverage period.' : 'Add a warranty from any asset\'s detail page.'">
                        @if ($hasFilters)
                            <a href="{{ route('warranty.index') }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-lg" aria-hidden="true"></i>Clear filters
                            </a>
                        @endif
                    </x-table.empty-state>
                </td>
            </tr>
        @endforelse

        @if ($warranties->hasPages())
            <x-slot:footer>
                <x-table.count :paginator="$warranties" noun="warranty" />
                {{ $warranties->links() }}
            </x-slot:footer>
        @endif
    </x-table.wrapper>
@endsection
