@extends('layouts.app')

@section('title', $canManage ? 'Promotions' : 'My Promotions')
@section('heading', $canManage ? 'Promotions' : 'My Promotions')

@section('content')
@if($canManage)
@include('hr.top-submenu')
@endif

    <div class="mb-5 grid grid-cols-1 gap-4 sm:grid-cols-2 xl:grid-cols-4">
        <x-kpi-card
            :title="$canManage ? 'Total promotions' : 'My promotions'"
            :value="$kpis['total'] ?? 0"
            accent="violet"
            :subtitle="$canManage ? 'Company promotion records' : 'Promotions on your file'"
        />
    </div>

    <x-card>
        <div class="mb-4 flex justify-between items-center">
            <p class="text-slate-600">
                @if($canManage)
                    Increments and promotions.
                @else
                    Your promotion history.
                @endif
            </p>
            @if($canManage)
            <a href="{{ route('promotions.create') }}" class="inline-flex items-center rounded-lg bg-slate-800 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700"><i class="bi bi-plus-lg me-1"></i>Add Promotion</a>
            @endif
        </div>
        <x-table>
            <x-slot name="head">
                <tr>
                    @if($canManage)
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Employee</th>
                    @endif
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">From → To</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Department</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Notes</th>
                </tr>
            </x-slot>
            @forelse($promotions as $p)
                <tr>
                    @if($canManage)
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-900">{{ $p->user?->name ?? '—' }}</td>
                    @endif
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $p->fromDesignation?->name ?? '—' }} → {{ $p->toDesignation?->name ?? '—' }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $p->fromDepartment?->name ?? '—' }} → {{ $p->toDepartment?->name ?? '—' }}</td>
                    <td class="whitespace-nowrap px-4 py-3 text-sm text-slate-600">{{ $p->promotion_date?->format('M d, Y') }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ \Illuminate\Support\Str::limit((string) ($p->notes ?? '—'), 80) }}</td>
                </tr>
            @empty
                <tr><td colspan="{{ $canManage ? 5 : 4 }}" class="px-4 py-8 text-center text-sm text-slate-500">No promotions yet.</td></tr>
            @endforelse
        </x-table>
        @if($promotions->hasPages())<div class="mt-4">{{ $promotions->links() }}</div>@endif
    </x-card>
@endsection
