@extends('layouts.app')

@section('title', 'Assign Asset')
@section('container-class', 'ams-container--narrow')

@section('content')
    <x-page-header title="Assign Asset" subtitle="Issue an asset to an employee.">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('assignments.index') }}">Assignments</a></li>
            <li class="breadcrumb-item active" aria-current="page">Assign</li>
        </x-slot:breadcrumbs>
    </x-page-header>

    <form method="POST" action="{{ route('assignments.store') }}">
        @csrf

        <div class="card">
            <div class="card-body">

                <x-form.fieldset legend="Who and What" hint="The asset being issued and the employee receiving it.">
                    @if ($asset)
                        <input type="hidden" name="asset_id" value="{{ $asset->id }}">
                        <div class="ams-field">
                            <label class="form-label">Asset</label>
                            <div class="ams-check-card is-checked">
                                <span class="ams-tile ams-tile--sm ams-tile--primary" aria-hidden="true">
                                    <i class="bi bi-{{ $asset->status->icon() }}"></i>
                                </span>
                                <div class="min-width-0">
                                    <div class="fw-semibold ams-cell-code">{{ $asset->asset_tag }}</div>
                                    <div class="ams-text-xs text-secondary ams-truncate">{{ $asset->name }}</div>
                                </div>
                            </div>
                        </div>
                    @else
                        <x-form.select
                            name="asset_id"
                            label="Asset"
                            placeholder="Choose an available asset"
                            required
                            :options="$assets->mapWithKeys(fn ($a) => [$a->id => $a->asset_tag.' — '.$a->name])->all()" />

                        @if ($assets->isEmpty())
                            <p class="ams-text-xs text-secondary">
                                No available assets. <a href="{{ route('assets.create') }}">Add an asset first.</a>
                            </p>
                        @endif
                    @endif

                    @if ($employee)
                        <input type="hidden" name="employee_id" value="{{ $employee->id }}">
                        <div class="ams-field">
                            <label class="form-label">Employee</label>
                            <div class="ams-check-card is-checked">
                                <span class="ams-avatar" aria-hidden="true">{{ $employee->initials() }}</span>
                                <div class="min-width-0">
                                    <div class="fw-semibold ams-truncate">{{ $employee->fullName() }}</div>
                                    <div class="ams-text-xs text-secondary ams-truncate">{{ $employee->employee_code }}</div>
                                </div>
                            </div>
                        </div>
                    @else
                        <x-form.select
                            name="employee_id"
                            label="Employee"
                            placeholder="Choose an employee"
                            required
                            :options="$employees->mapWithKeys(fn ($e) => [$e->id => $e->fullName().' — '.$e->employee_code])->all()" />

                        @if ($employees->isEmpty())
                            <p class="ams-text-xs text-secondary">
                                No employees are eligible to receive assets right now.
                            </p>
                        @endif
                    @endif
                </x-form.fieldset>

                <x-form.fieldset legend="Handover Details">
                    <x-form.date name="assigned_at" label="Assigned On" :value="old('assigned_at', now()->toDateString())" required />
                    <x-form.date name="expected_return_at" label="Expected Return Date" :value="old('expected_return_at')" help="Leave blank for a permanent issue." />

                    <x-form.select
                        name="condition_on_issue"
                        label="Condition on Issue"
                        required
                        :selected="old('condition_on_issue', $asset?->condition->value)"
                        :options="\App\Enums\AssetCondition::options()" />

                    <x-form.select
                        name="assignment_location_id"
                        label="Handover Location"
                        placeholder="Not recorded"
                        :selected="old('assignment_location_id')"
                        :options="$locations->pluck('name', 'id')->all()" />

                    <x-form.input name="acknowledgement_ref" label="Acknowledgement Reference" :value="old('acknowledgement_ref')" help="Signed handover form reference, if one exists." />

                    <x-form.textarea name="issue_notes" label="Issue Notes" :value="old('issue_notes')" :rows="3" />
                </x-form.fieldset>

            </div>
        </div>

        <div class="d-flex justify-content-end gap-2 mt-3">
            <a href="{{ $asset ? route('assets.show', $asset) : route('assignments.index') }}" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">
                <i class="bi bi-box-arrow-right" aria-hidden="true"></i>Assign Asset
            </button>
        </div>
    </form>
@endsection
