@php
    $assignment = $assignment ?? null;
@endphp

<x-app-layout title="{{ $assignment ? 'Edit unit assignment' : 'Add unit assignment' }}">
    <div class="mx-auto max-w-xl">
        <h1 class="text-xl font-semibold text-slate-900">{{ $assignment ? 'Edit unit assignment' : 'Add unit assignment' }}</h1>
        <p class="mt-1 text-sm text-slate-500">Home or float placement on a unit. Day/evening/night duty is scheduled separately on the shift roster.</p>

        <form method="POST"
              action="{{ $assignment ? route('care.staff-assignments.update', $assignment) : route('care.staff-assignments.store') }}"
              class="mt-6 space-y-4 rounded-2xl border border-slate-200 bg-white p-6">
            @csrf
            @if ($assignment) @method('PUT') @endif

            <div>
                <label class="block text-sm font-medium">Staff member</label>
                <select name="member_id" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">Select…</option>
                    @foreach ($members as $member)
                        <option value="{{ $member->id }}" @selected((int) old('member_id', $assignment?->member_id ?? $selectedMemberId) === (int) $member->id)>
                            {{ $memberLabels[$member->id] ?? $member->user_ref }}
                        </option>
                    @endforeach
                </select>
                @error('member_id')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>

            <div>
                <label class="block text-sm font-medium">Assignment kind</label>
                <select name="kind" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @foreach ($kinds as $value => $label)
                        <option value="{{ $value }}" @selected(old('kind', $assignment?->kind ?? 'primary') === $value)>{{ $label }}</option>
                    @endforeach
                </select>
                <p class="mt-1 text-xs text-slate-500">Temporary requires a care unit (e.g. float nurse covering Emergency today).</p>
            </div>

            <div>
                <label class="block text-sm font-medium">Department <span class="font-normal text-slate-400">(optional if unit selected)</span></label>
                <select name="department_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">—</option>
                    @foreach ($departments as $department)
                        <option value="{{ $department->id }}" @selected((int) old('department_id', $assignment?->department_id) === (int) $department->id)>
                            {{ $department->labelForSelect() }}
                        </option>
                    @endforeach
                </select>
            </div>

            <div>
                <label class="block text-sm font-medium">Care unit</label>
                <select name="care_unit_id" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">—</option>
                    @foreach ($units as $unit)
                        <option value="{{ $unit->id }}"
                                data-department="{{ $unit->department_id }}"
                                @selected((int) old('care_unit_id', $assignment?->care_unit_id ?? request('care_unit_id')) === (int) $unit->id)>
                            {{ $unit->labelForSelect() }}
                        </option>
                    @endforeach
                </select>
                @error('care_unit_id')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
            </div>

            <div>
                <label class="block text-sm font-medium">Post / grade</label>
                <select name="assignment_role" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    <option value="">—</option>
                    @foreach ($assignmentRoles as $value => $label)
                        <option value="{{ $value }}" @selected(old('assignment_role', $assignment?->assignment_role) === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div class="grid gap-4 sm:grid-cols-2">
                <div>
                    <label class="block text-sm font-medium">Starts on</label>
                    <input type="date" name="starts_on" required value="{{ old('starts_on', $assignment?->starts_on?->format('Y-m-d') ?? now()->toDateString()) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                </div>
                <div>
                    <label class="block text-sm font-medium">Ends on <span class="font-normal text-slate-400">(open-ended OK)</span></label>
                    <input type="date" name="ends_on" value="{{ old('ends_on', $assignment?->ends_on?->format('Y-m-d')) }}" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                </div>
            </div>

            <div>
                <label class="block text-sm font-medium">Status</label>
                <select name="status" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @foreach ($statuses as $value => $label)
                        <option value="{{ $value }}" @selected(old('status', $assignment?->status ?? 'active') === $value)>{{ $label }}</option>
                    @endforeach
                </select>
            </div>

            <div>
                <label class="block text-sm font-medium">Notes</label>
                <textarea name="notes" rows="2" class="mt-1 w-full rounded-lg border-slate-300 text-sm">{{ old('notes', $assignment?->notes) }}</textarea>
            </div>

            <button type="submit" class="btn-primary w-full">{{ $assignment ? 'Save changes' : 'Create unit assignment' }}</button>
        </form>

        @if ($assignment)
            <form method="POST" action="{{ route('care.staff-assignments.destroy', $assignment) }}" class="mt-4" onsubmit="return confirm('Remove this unit assignment?')">
                @csrf @method('DELETE')
                <button type="submit" class="text-sm font-medium text-red-600 hover:text-red-800">Remove unit assignment</button>
            </form>
        @endif
    </div>
</x-app-layout>
