<x-app-layout title="Shift templates">
    <div class="space-y-6">
        <div class="flex flex-wrap items-start justify-between gap-4">
            <div>
                <p class="text-sm text-slate-500">
                    <a href="{{ route('care.nursing.services') }}" class="text-indigo-600 hover:text-indigo-800">Nursing Services</a>
                    <span class="text-slate-300">/</span>
                    Shifts
                </p>
                <h1 class="mt-1 text-2xl font-semibold text-slate-900">Shift templates</h1>
                <p class="mt-1 text-sm text-slate-500">Organization-wide duty periods used on unit rosters.</p>
            </div>
        </div>

        <div class="overflow-hidden rounded-2xl border border-slate-200 bg-white">
            <table class="min-w-full text-sm">
                <thead class="bg-slate-50 text-left text-xs uppercase text-slate-500">
                    <tr>
                        <th class="px-4 py-3">Label</th>
                        <th class="px-4 py-3">Code</th>
                        <th class="px-4 py-3">Hours</th>
                        <th class="px-4 py-3">Status</th>
                        <th class="px-4 py-3"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-50">
                    @foreach ($shifts as $shift)
                        <tr>
                            <td class="px-4 py-3">
                                <span class="mr-2 inline-block h-2.5 w-2.5 rounded-full" style="background: {{ $shift->color ?? '#64748b' }}"></span>
                                <span class="font-medium">{{ $shift->label }}</span>
                            </td>
                            <td class="px-4 py-3 font-mono text-xs text-slate-500">{{ $shift->code }}</td>
                            <td class="px-4 py-3 whitespace-nowrap">{{ $shift->timeLabel() }}</td>
                            <td class="px-4 py-3">
                                @if ($shift->is_active)
                                    <span class="text-emerald-700">Active</span>
                                @else
                                    <span class="text-slate-400">Inactive</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-right">
                                <details class="relative inline-block text-left">
                                    <summary class="cursor-pointer text-indigo-600 hover:text-indigo-800">Edit</summary>
                                    <form method="POST" action="{{ route('care.shifts.update', $shift) }}" class="absolute right-0 z-10 mt-2 w-72 rounded-xl border border-slate-200 bg-white p-4 shadow-lg">
                                        @csrf
                                        @method('PUT')
                                        <div class="space-y-3">
                                            <div>
                                                <label class="block text-xs font-medium uppercase text-slate-500">Label</label>
                                                <input type="text" name="label" value="{{ old('label', $shift->label) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                            </div>
                                            <div class="grid grid-cols-2 gap-2">
                                                <div>
                                                    <label class="block text-xs font-medium uppercase text-slate-500">Start</label>
                                                    <input type="time" name="start_time" value="{{ old('start_time', substr((string) $shift->start_time, 0, 5)) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                                </div>
                                                <div>
                                                    <label class="block text-xs font-medium uppercase text-slate-500">End</label>
                                                    <input type="time" name="end_time" value="{{ old('end_time', substr((string) $shift->end_time, 0, 5)) }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                                                </div>
                                            </div>
                                            <div>
                                                <label class="block text-xs font-medium uppercase text-slate-500">Color</label>
                                                <input type="color" name="color" value="{{ old('color', $shift->color ?? '#64748b') }}" class="mt-1 h-9 w-full rounded-lg border border-slate-300">
                                            </div>
                                            <label class="flex items-center gap-2 text-sm text-slate-700">
                                                <input type="checkbox" name="is_active" value="1" @checked(old('is_active', $shift->is_active)) class="rounded border-slate-300">
                                                Active
                                            </label>
                                            <button type="submit" class="btn-primary w-full">Save</button>
                                        </div>
                                    </form>
                                </details>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        <div class="rounded-2xl border border-slate-200 bg-white p-6">
            <h2 class="text-base font-semibold text-slate-900">Add shift</h2>
            <form method="POST" action="{{ route('care.shifts.store') }}" class="mt-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-5">
                @csrf
                <div>
                    <label class="block text-xs font-medium uppercase text-slate-500">Code</label>
                    <input type="text" name="code" value="{{ old('code') }}" required placeholder="e.g. long_day" class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @error('code')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-xs font-medium uppercase text-slate-500">Label</label>
                    <input type="text" name="label" value="{{ old('label') }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                    @error('label')<p class="mt-1 text-xs text-red-600">{{ $message }}</p>@enderror
                </div>
                <div>
                    <label class="block text-xs font-medium uppercase text-slate-500">Start</label>
                    <input type="time" name="start_time" value="{{ old('start_time', '07:00') }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                </div>
                <div>
                    <label class="block text-xs font-medium uppercase text-slate-500">End</label>
                    <input type="time" name="end_time" value="{{ old('end_time', '19:00') }}" required class="mt-1 w-full rounded-lg border-slate-300 text-sm">
                </div>
                <div class="flex items-end">
                    <button type="submit" class="btn-primary w-full">Create</button>
                </div>
            </form>
        </div>
    </div>
</x-app-layout>
