@extends('layouts.app')

@section('title', 'Assignments')

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

    <x-page-header
        title="Assignments"
        :subtitle="trans_choice(':count assignment|:count assignments', $assignments->total(), ['count' => $assignments->total()])">

        <a href="{{ route('returns.index') }}" class="btn btn-outline-secondary">
            <i class="bi bi-box-arrow-in-left" aria-hidden="true"></i>Returns
        </a>

        <a href="{{ route('assignments.create') }}" class="btn btn-primary">
            <i class="bi bi-box-arrow-right" aria-hidden="true"></i>Assign Asset
        </a>
    </x-page-header>

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

                <x-filter.select
                    name="status"
                    label="Status"
                    placeholder="Any status"
                    :selected="$filters['status']"
                    :options="\App\Enums\AssignmentStatus::options()" />
            </x-filter-bar>
        </x-slot:toolbar>

        <x-slot:head>
            <tr>
                <th scope="col">Employee</th>
                <th scope="col">Asset</th>
                <th scope="col">Assigned On</th>
                <th scope="col">Returned On</th>
                <th scope="col">Status</th>
                <th scope="col" class="text-end">Actions</th>
            </tr>
        </x-slot:head>

        @forelse ($assignments as $assignment)
            <tr>
                <td>
                    <a href="{{ route('employees.show', $assignment->employee) }}" class="text-decoration-none">
                        <div class="ams-cell-primary">{{ $assignment->employee->fullName() }}</div>
                        <div class="ams-cell-secondary ams-cell-code">{{ $assignment->employee->employee_code }}</div>
                    </a>
                </td>

                <td>
                    <a href="{{ route('assets.show', $assignment->asset) }}" class="text-decoration-none">
                        <div class="ams-cell-code fw-semibold">{{ $assignment->asset->asset_tag }}</div>
                        <div class="ams-cell-secondary ams-truncate">{{ $assignment->asset->name }}</div>
                    </a>
                </td>

                <td class="ams-cell-secondary">
                    {{ $assignment->assigned_at->format(setting('date_format', 'd M Y')) }}
                </td>

                <td class="ams-cell-secondary">
                    @if ($assignment->returned_at)
                        {{ $assignment->returned_at->format(setting('date_format', 'd M Y')) }}
                    @else
                        —
                    @endif
                </td>

                <td>
                    <x-status-badge :status="$assignment->status" />
                    @if ($assignment->isOverdue())
                        <x-badge color="danger" dot>Overdue</x-badge>
                    @endif
                </td>

                <td class="text-end text-nowrap">
                    <x-table.actions>
                        <a href="{{ route('assignments.show', $assignment) }}"
                           class="btn btn-sm btn-icon"
                           data-bs-toggle="tooltip"
                           title="View">
                            <i class="bi bi-eye" aria-hidden="true"></i>
                            <span class="visually-hidden">View assignment</span>
                        </a>

                        @if ($assignment->isActive())
                            <a href="{{ route('returns.create', ['assignment' => $assignment->id]) }}"
                               class="btn btn-sm btn-icon"
                               data-bs-toggle="tooltip"
                               title="Return">
                                <i class="bi bi-box-arrow-in-left" aria-hidden="true"></i>
                                <span class="visually-hidden">Return {{ $assignment->asset->asset_tag }}</span>
                            </a>
                        @endif
                    </x-table.actions>
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="6" class="p-0">
                    <x-table.empty-state
                        icon="box-arrow-right"
                        :title="$hasFilters ? 'No assignments match these filters' : 'No assignments yet'"
                        :message="$hasFilters ? 'Try clearing the filters to see every assignment.' : 'Assign the first asset to an employee to start building history.'">
                        @if ($hasFilters)
                            <a href="{{ route('assignments.index') }}" class="btn btn-outline-secondary">
                                <i class="bi bi-x-lg" aria-hidden="true"></i>Clear filters
                            </a>
                        @else
                            <a href="{{ route('assignments.create') }}" class="btn btn-primary">
                                <i class="bi bi-box-arrow-right" aria-hidden="true"></i>Assign Asset
                            </a>
                        @endif
                    </x-table.empty-state>
                </td>
            </tr>
        @endforelse

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