@extends('layouts.app')

@section('title', 'Warranty — Expiring Soon')

@section('content')
    <x-page-header title="Expiring Soon" subtitle="Every coverage period grouped by urgency.">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('warranty.index') }}">Warranty</a></li>
            <li class="breadcrumb-item active" aria-current="page">Expiring Soon</li>
        </x-slot:breadcrumbs>

        <a href="{{ route('warranty.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-list-ul" aria-hidden="true"></i>Full Register
        </a>
    </x-page-header>

    <div class="d-flex flex-column gap-3">
        @foreach ([
            ['title' => 'Expiring Within 30 Days', 'tone' => 'danger', 'icon' => 'exclamation-triangle', 'items' => $critical, 'empty' => 'Nothing due for renewal in the next 30 days.'],
            ['title' => 'Expiring in 31–90 Days', 'tone' => 'warning', 'icon' => 'hourglass-split', 'items' => $upcoming, 'empty' => 'Nothing due for renewal between 31 and 90 days out.'],
            ['title' => 'Already Expired', 'tone' => 'danger', 'icon' => 'shield-x', 'items' => $expired, 'empty' => 'No expired coverage on file.'],
        ] as $section)
            <x-panel :title="$section['title'].' ('.$section['items']->count().')'" :tone="$section['items']->isNotEmpty() && $section['title'] !== 'Expiring in 31–90 Days' ? 'danger' : null">
                @if ($section['items']->isEmpty())
                    <p class="text-secondary mb-0">{{ $section['empty'] }}</p>
                @else
                    <div class="ams-table-scroll ams-scroll-thin">
                        <table class="table table-sm align-middle mb-0">
                            <thead>
                                <tr>
                                    <th scope="col">Asset</th>
                                    <th scope="col">Type</th>
                                    <th scope="col">Provider</th>
                                    <th scope="col">Expires</th>
                                    <th scope="col" class="text-end">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($section['items'] as $warranty)
                                    <tr>
                                        <td>
                                            <a href="{{ route('assets.show', $warranty->asset) }}" class="text-decoration-none">
                                                <span class="ams-cell-code">{{ $warranty->asset->asset_tag }}</span>
                                                {{ $warranty->asset->name }}
                                            </a>
                                        </td>
                                        <td class="ams-cell-secondary">{{ $warranty->type->label() }}</td>
                                        <td class="ams-cell-secondary">{{ $warranty->provider ?? '—' }}</td>
                                        <td class="ams-cell-secondary">{{ $warranty->end_date->format('d M Y') }}</td>
                                        <td class="text-end">
                                            @if ($warranty->is_primary)
                                                <a href="{{ route('assets.warranties.renew.create', [$warranty->asset, $warranty]) }}" class="btn btn-sm btn-outline-secondary">
                                                    <i class="bi bi-arrow-repeat" aria-hidden="true"></i>Renew
                                                </a>
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                @endif
            </x-panel>
        @endforeach
    </div>
@endsection
