@extends('layouts.app')

@section('title', 'Offboarding — ' . ($offboarding->user?->name ?? ''))
@section('heading', 'Offboarding')

@section('content')
@include('hr.top-submenu')

@php
    $progress = $offboarding->clearanceProgress();
    $statusLabels = [
        'in_progress' => 'In progress',
        'clearance_pending' => 'Clearance pending',
        'cleared' => 'Cleared',
        'completed' => 'Completed',
        'cancelled' => 'Cancelled',
    ];
    $categoryLabels = [
        'it' => 'IT',
        'assets' => 'Assets',
        'hr' => 'HR',
        'finance' => 'Finance',
        'docs' => 'Documents',
        'manager' => 'Manager',
    ];
@endphp

<div class="max-w-6xl space-y-6">
    <div class="flex flex-wrap items-start justify-between gap-4">
        <div>
            <h1 class="text-xl font-bold text-slate-900">{{ $offboarding->user?->name }}</h1>
            <p class="mt-1 text-sm text-slate-600">
                {{ ucfirst(str_replace('_', ' ', $offboarding->type)) }} · LWD {{ $offboarding->last_working_date?->format('d M Y') ?? '—' }}
            </p>
        </div>
        <div class="flex flex-wrap gap-2">
            @if($offboarding->user_id)
                <a href="{{ route('generated-letters.create', ['user_id' => $offboarding->user_id, 'letter_type' => 'relieving', 'last_working_date' => $offboarding->last_working_date?->format('Y-m-d')]) }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Relieving letter</a>
                <a href="{{ route('generated-letters.create', ['user_id' => $offboarding->user_id, 'letter_type' => 'experience', 'last_working_date' => $offboarding->last_working_date?->format('Y-m-d')]) }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Experience cert</a>
            @endif
            @if($offboarding->fnfSettlement)
                <a href="{{ route('fnf.show', $offboarding->fnfSettlement) }}" class="rounded-lg bg-primary px-4 py-2 text-sm font-medium text-white">F&F Worksheet</a>
            @endif
            <a href="{{ route('offboardings.index') }}" class="rounded-lg border border-slate-200 bg-white px-4 py-2 text-sm font-medium text-slate-700">Back to list</a>
        </div>
    </div>

    <div class="grid gap-4 sm:grid-cols-4">
        <x-card>
            <p class="text-xs text-slate-500">Status</p>
            <p class="mt-1 font-semibold text-slate-900">{{ $statusLabels[$offboarding->status] ?? $offboarding->status }}</p>
        </x-card>
        <x-card>
            <p class="text-xs text-slate-500">Notice period</p>
            <p class="mt-1 font-semibold text-slate-900">{{ $offboarding->notice_period_days }} days</p>
        </x-card>
        <x-card>
            <p class="text-xs text-slate-500">Clearance</p>
            <p class="mt-1 font-semibold text-slate-900">{{ $progress['completed'] }}/{{ $progress['total'] }} ({{ $progress['percent'] }}%)</p>
        </x-card>
        <x-card>
            <p class="text-xs text-slate-500">Assets assigned</p>
            <p class="mt-1 font-semibold text-slate-900">{{ $assignedAssets->count() }}</p>
        </x-card>
    </div>

    <x-card>
        <h2 class="mb-4 text-lg font-semibold text-slate-900">Exit clearance checklist</h2>
        <div class="overflow-x-auto">
            <table class="min-w-full text-sm">
                <thead>
                    <tr class="border-b border-slate-200 text-left text-slate-500">
                        <th class="pb-3 pr-4">Category</th>
                        <th class="pb-3 pr-4">Task</th>
                        <th class="pb-3 pr-4">Assignee</th>
                        <th class="pb-3 pr-4">Status</th>
                        <th class="pb-3">Update</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @foreach($offboarding->checklistItems as $item)
                        <tr>
                            <td class="py-3 pr-4 capitalize">{{ $categoryLabels[$item->category] ?? $item->category }}</td>
                            <td class="py-3 pr-4">
                                <div class="font-medium text-slate-900">{{ $item->task_name }}</div>
                                @if($item->description)
                                    <div class="text-xs text-slate-500">{{ $item->description }}</div>
                                @endif
                            </td>
                            <td class="py-3 pr-4 text-sm text-slate-600">{{ $item->assignee?->name ?? '—' }}</td>
                            <td class="py-3 pr-4 capitalize">{{ str_replace('_', ' ', $item->status) }}</td>
                            <td class="py-3">
                                <form action="{{ route('offboardings.checklist.update', $item) }}" method="post" class="flex flex-nowrap items-center gap-2">
                                    @csrf
                                    <select name="status" class="rounded border border-slate-300 text-xs js-select2">
                                        @foreach(['pending', 'completed', 'waived', 'na'] as $st)
                                            <option value="{{ $st }}" {{ $item->status === $st ? 'selected' : '' }}>{{ ucfirst($st) }}</option>
                                        @endforeach
                                    </select>
                                    <input type="text" name="notes" value="{{ $item->notes }}" placeholder="Notes" class="w-32 rounded border border-slate-300 px-2 py-1 text-xs">
                                    <button type="submit" class="rounded bg-slate-800 px-2 py-1 text-xs font-medium text-white">Save</button>
                                </form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </x-card>

    @if($assignedAssets->isNotEmpty())
        <x-card>
            <h2 class="mb-3 text-sm font-semibold text-slate-900">Assigned assets pending return</h2>
            <ul class="list-disc pl-5 text-sm text-slate-700">
                @foreach($assignedAssets as $asset)
                    <li>{{ $asset->name }} @if($asset->serial_number)({{ $asset->serial_number }})@endif</li>
                @endforeach
            </ul>
            <p class="mt-2 text-xs text-slate-500">Mark assets returned from the <a href="{{ route('assets.index') }}" class="text-primary hover:underline">Assets</a> module.</p>
        </x-card>
    @endif

    @if($offboarding->reason || $offboarding->notes)
        <x-card>
            <h2 class="mb-2 text-sm font-semibold text-slate-900">Notes</h2>
            @if($offboarding->reason)<p class="text-sm text-slate-700"><strong>Reason:</strong> {{ $offboarding->reason }}</p>@endif
            @if($offboarding->notes)<p class="mt-2 text-sm text-slate-700">{{ $offboarding->notes }}</p>@endif
        </x-card>
    @endif
</div>
@endsection
