@extends('layouts.app')

@section('title', 'Assignment — '.$assignment->asset->asset_tag)

@section('content')
    <x-page-header
        :title="$assignment->asset->asset_tag.' → '.$assignment->employee->fullName()"
        subtitle="Assignment record">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('assignments.index') }}">Assignments</a></li>
            <li class="breadcrumb-item active" aria-current="page">{{ $assignment->asset->asset_tag }}</li>
        </x-slot:breadcrumbs>

        @if ($assignment->isActive())
            <a href="{{ route('returns.create', ['assignment' => $assignment->id]) }}" class="btn btn-primary">
                <i class="bi bi-box-arrow-in-left" aria-hidden="true"></i>Record Return
            </a>
        @endif
    </x-page-header>

    <div class="row g-3">
        <div class="col-xl-8">
            <div class="d-flex flex-column gap-3">

                <x-panel title="Overview">
                    <div class="d-flex align-items-center justify-content-between mb-3 pb-3 border-bottom">
                        <x-status-badge :status="$assignment->status" class="fs-6" />
                        @if ($assignment->isOverdue())
                            <x-badge color="danger" dot>Overdue</x-badge>
                        @endif
                    </div>

                    <x-detail-list>
                        <x-detail-row label="Asset">
                            <a href="{{ route('assets.show', $assignment->asset) }}">
                                <span class="ams-cell-code">{{ $assignment->asset->asset_tag }}</span>
                                — {{ $assignment->asset->name }}
                            </a>
                        </x-detail-row>

                        <x-detail-row label="Category">
                            {{ $assignment->asset->category->qualifiedName() }}
                        </x-detail-row>

                        <x-detail-row label="Employee">
                            <a href="{{ route('employees.show', $assignment->employee) }}">
                                {{ $assignment->employee->fullName() }}
                            </a>
                            <span class="ams-cell-secondary ams-cell-code">{{ $assignment->employee->employee_code }}</span>
                        </x-detail-row>

                        <x-detail-row label="Duration">
                            {{ trans_choice(':count day|:count days', $assignment->durationInDays(), ['count' => $assignment->durationInDays()]) }}
                            {{ $assignment->isActive() ? 'so far' : 'held' }}
                        </x-detail-row>
                    </x-detail-list>
                </x-panel>

                <x-panel title="Issue">
                    <x-detail-list>
                        <x-detail-row label="Assigned On">
                            {{ $assignment->assigned_at->format(setting('date_format', 'd M Y')) }}
                        </x-detail-row>

                        <x-detail-row label="Assigned By">
                            {{ $assignment->assignedBy?->name ?? 'Unknown' }}
                        </x-detail-row>

                        <x-detail-row label="Expected Return">
                            {{ $assignment->expected_return_at?->format(setting('date_format', 'd M Y')) }}
                        </x-detail-row>

                        <x-detail-row label="Handover Location">
                            {{ $assignment->assignmentLocation?->name }}
                        </x-detail-row>

                        <x-detail-row label="Condition on Issue">
                            <x-status-badge :status="$assignment->condition_on_issue" />
                        </x-detail-row>

                        <x-detail-row label="Acknowledgement Ref">
                            <span class="ams-mono">{{ $assignment->acknowledgement_ref }}</span>
                        </x-detail-row>
                    </x-detail-list>

                    @if ($assignment->issue_notes)
                        <hr>
                        <p class="mb-0" style="white-space: pre-line;">{{ $assignment->issue_notes }}</p>
                    @endif
                </x-panel>

                @if ($assignment->status->value === 'returned')
                    <x-panel title="Return">
                        <x-detail-list>
                            <x-detail-row label="Returned On">
                                {{ $assignment->returned_at?->format(setting('date_format', 'd M Y')) }}
                            </x-detail-row>

                            <x-detail-row label="Received By">
                                {{ $assignment->receivedBy?->name ?? 'Unknown' }}
                            </x-detail-row>

                            <x-detail-row label="Condition on Return">
                                @if ($assignment->condition_on_return)
                                    <x-status-badge :status="$assignment->condition_on_return" />
                                @endif
                            </x-detail-row>
                        </x-detail-list>

                        @if ($assignment->return_notes)
                            <hr>
                            <p class="mb-0" style="white-space: pre-line;">{{ $assignment->return_notes }}</p>
                        @endif
                    </x-panel>
                @endif

            </div>
        </div>

        <div class="col-xl-4">
            <div class="d-flex flex-column gap-3">
                <x-panel title="Quick Links">
                    <div class="d-flex flex-column gap-2">
                        <a href="{{ route('assets.show', $assignment->asset) }}" class="btn btn-outline-secondary">
                            <i class="bi bi-box-seam" aria-hidden="true"></i>View Asset
                        </a>
                        <a href="{{ route('employees.show', $assignment->employee) }}" class="btn btn-outline-secondary">
                            <i class="bi bi-person" aria-hidden="true"></i>View Employee
                        </a>
                    </div>
                </x-panel>
            </div>
        </div>
    </div>
@endsection
