@extends('layouts.app')

@section('title', 'Corporate Expiry Matrix')

@section('content')
<div class="space-y-6">
    <div class="flex items-center justify-between">
        <div>
            <h1 class="text-xl font-bold text-slate-900 dark:text-white">Corporate Employee Expiry Matrix</h1>
            <p class="text-xs text-slate-500">Corporate client employee passports, residence visas, work permits, and Emirates ID expiry matrix.</p>
        </div>
        <a href="{{ route('app.corporate.employees.index') }}" class="px-4 py-2 bg-slate-200 dark:bg-slate-700 text-slate-700 dark:text-slate-200 font-semibold text-xs rounded-xl">Back to Employees</a>
    </div>

    <!-- Corporate Expiry Table (Requirement 52) -->
    <div class="bg-white dark:bg-slate-800 rounded-3xl border border-slate-200 dark:border-slate-700/60 shadow-sm overflow-hidden text-xs">
        <div class="overflow-x-auto">
            <table class="w-full text-left rtl:text-right">
                <thead class="bg-slate-50 dark:bg-slate-900/60 border-b border-slate-200 dark:border-slate-700 text-slate-500 uppercase tracking-wider font-bold">
                    <tr>
                        <th class="px-6 py-4">Corporate Employee</th>
                        <th class="px-6 py-4">Company Client</th>
                        <th class="px-6 py-4">Document Type</th>
                        <th class="px-6 py-4">Document #</th>
                        <th class="px-6 py-4">Expiry Date</th>
                        <th class="px-6 py-4">Days Left</th>
                        <th class="px-6 py-4 text-right rtl:text-left">Renewal Opportunity</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-200 dark:divide-slate-700/60 font-medium">
                    @forelse($expiringDocs as $doc)
                        <tr class="hover:bg-slate-50 dark:hover:bg-slate-700/30">
                            <td class="px-6 py-4 font-bold text-slate-900 dark:text-white">{{ $doc->corporateEmployee->full_name }}</td>
                            <td class="px-6 py-4 text-slate-700 dark:text-slate-300 font-semibold">{{ $doc->corporateEmployee->company ? $doc->corporateEmployee->company->name_en : 'N/A' }}</td>
                            <td class="px-6 py-4 uppercase font-bold text-sky-600 text-[10px]">{{ str_replace('_', ' ', $doc->document_type) }}</td>
                            <td class="px-6 py-4 font-mono text-slate-500">{{ $doc->document_number ?? 'N/A' }}</td>
                            <td class="px-6 py-4 font-mono font-bold text-slate-800 dark:text-slate-200">{{ $doc->expiry_date->format('d M Y') }}</td>
                            <td class="px-6 py-4">
                                @php $days = now()->diffInDays($doc->expiry_date, false); @endphp
                                <span class="px-2.5 py-0.5 rounded-full text-[10px] font-bold {{ $days <= 30 ? 'bg-rose-100 text-rose-700' : 'bg-amber-100 text-amber-700' }}">
                                    {{ $days < 0 ? 'EXPIRED' : $days . ' Days' }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-right rtl:text-left">
                                @if($opportunitySourceIds->has($doc->id))
                                    <a href="{{ route('app.renewals.show', $opportunitySourceIds->get($doc->id)) }}" class="inline-flex px-3 py-1 bg-sky-600 text-white font-bold text-[10px] rounded-lg">Open Renewal</a>
                                @elseif(auth()->user()->hasPermission('renewals.manage'))
                                    <form method="POST" action="{{ route('app.corporate.expiries.renewal', $doc) }}">
                                        @csrf
                                        <button class="px-3 py-1 bg-emerald-600 text-white font-bold text-[10px] rounded-lg">+ Create Renewal</button>
                                    </form>
                                @else
                                    <span class="text-[10px] font-semibold text-slate-400">Renewal manager required</span>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-6 py-8 text-center text-slate-400 font-semibold">
                                No corporate client employee documents expiring within the next 90 days.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
