@extends('layouts.epelcon')
@section('title', 'Housekeeping')

@section('content')
<div x-data="{ create: false }">
    <div class="mb-5 flex flex-wrap items-center justify-between gap-3">
        <div>
            <h1 class="text-xl font-extrabold text-slate-900 lg:text-2xl">Housekeeping</h1>
            <p class="text-sm text-slate-500">Cleaning, maintenance and inspection tasks.</p>
        </div>
        <button @click="create = true" class="rounded-xl bg-indigo-600 px-4 py-2.5 text-sm font-bold text-white shadow-lg shadow-indigo-600/25 hover:bg-indigo-700">+ New task</button>
    </div>

    @if (session('error'))
        <div class="mb-4 rounded-xl bg-rose-50 px-4 py-3 text-sm font-bold text-rose-700 ring-1 ring-rose-200">{{ session('error') }}</div>
    @endif
    @if (session('success'))
        <div class="mb-4 rounded-xl bg-emerald-50 px-4 py-3 text-sm font-bold text-emerald-700 ring-1 ring-emerald-200">{{ session('success') }}</div>
    @endif

    @if (! empty($myActive))
        <div x-data="{ started: {{ Illuminate\Support\Js::from(optional($myActive->started_at)->toIso8601String()) }}, label: '00:00', tick() { const s = Math.max(0, Math.floor((Date.now() - new Date(this.started).getTime()) / 1000)); const m = Math.floor(s / 60); const ss = s % 60; this.label = String(m).padStart(2, '0') + ':' + String(ss).padStart(2, '0'); } }"
             x-init="tick(); setInterval(() => tick(), 1000)"
             class="mb-5 rounded-2xl bg-gradient-to-br from-sky-600 to-indigo-700 p-5 text-white shadow-lg shadow-indigo-600/25">
            <div class="flex flex-wrap items-center gap-4">
                <div class="flex h-14 w-14 shrink-0 items-center justify-center rounded-2xl bg-white/20 text-xl font-extrabold">{{ $myActive->room->number ?? '?' }}</div>
                <div class="min-w-0 flex-1">
                    <p class="text-xs font-bold uppercase tracking-wide text-white/70">My current job</p>
                    <p class="text-lg font-extrabold capitalize">{{ str_replace('_', ' ', $myActive->task_type) }} · Room {{ $myActive->room->number ?? '?' }}</p>
                    <p class="mt-0.5 font-mono text-3xl font-extrabold tabular-nums" x-text="label">00:00</p>
                </div>
                <form method="POST" action="{{ route('epelcon.housekeeping.clockout', $myActive) }}" class="shrink-0">@csrf
                    <button class="rounded-2xl bg-white px-6 py-4 text-base font-extrabold text-indigo-700 shadow-lg hover:bg-slate-100">Clock out / Complete</button>
                </form>
            </div>
        </div>
    @endif

    <div class="mb-5 grid grid-cols-2 gap-3 sm:grid-cols-4">
        <div class="rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200"><p class="text-xs font-bold uppercase text-slate-400">Pending</p><p class="mt-1 text-2xl font-extrabold text-amber-600">{{ $counts['pending'] }}</p></div>
        <div class="rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200"><p class="text-xs font-bold uppercase text-slate-400">In progress</p><p class="mt-1 text-2xl font-extrabold text-sky-600">{{ $counts['in_progress'] }}</p></div>
        <div class="rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200"><p class="text-xs font-bold uppercase text-slate-400">Done today</p><p class="mt-1 text-2xl font-extrabold text-emerald-600">{{ $counts['completed'] }}</p></div>
        <div class="rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200"><p class="text-xs font-bold uppercase text-slate-400">Dirty rooms</p><p class="mt-1 text-2xl font-extrabold text-rose-600">{{ $counts['dirty_rooms'] }}</p></div>
    </div>

    {{-- Rooms status at a glance --}}
    <div class="mb-5 rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200">
        <div class="mb-3 flex flex-wrap items-center justify-between gap-2">
            <p class="text-xs font-bold uppercase text-slate-400">Rooms status</p>
            <div class="flex flex-wrap gap-3 text-[10px] font-bold text-slate-500">
                <span class="inline-flex items-center gap-1"><span class="h-2 w-2 rounded-full bg-emerald-500"></span>Available</span>
                <span class="inline-flex items-center gap-1"><span class="h-2 w-2 rounded-full bg-rose-500"></span>Occupied</span>
                <span class="inline-flex items-center gap-1"><span class="h-2 w-2 rounded-full bg-amber-500"></span>Reserved</span>
                <span class="inline-flex items-center gap-1"><span class="h-2 w-2 rounded-full bg-slate-400"></span>Maintenance</span>
            </div>
        </div>
        <div class="grid grid-cols-4 gap-2 sm:grid-cols-6 lg:grid-cols-10">
            @foreach ($rooms as $room)
                @php
                    $dot = ['available' => 'bg-emerald-500', 'occupied' => 'bg-rose-500', 'reserved' => 'bg-amber-500', 'maintenance' => 'bg-slate-400'][$room->status] ?? 'bg-slate-300';
                    $hk = ['clean' => 'bg-emerald-100 text-emerald-700', 'inspected' => 'bg-violet-100 text-violet-700', 'dirty' => 'bg-rose-100 text-rose-700', 'cleaning' => 'bg-sky-100 text-sky-700'][$room->housekeeping_status] ?? 'bg-slate-100 text-slate-500';
                @endphp
                <div class="flex flex-col items-center gap-1 rounded-xl bg-slate-50 p-2 ring-1 ring-slate-100">
                    <span class="inline-flex items-center gap-1 text-sm font-extrabold text-slate-800"><span class="h-2.5 w-2.5 rounded-full {{ $dot }}"></span>{{ $room->number }}</span>
                    <span class="rounded-full px-2 py-0.5 text-[9px] font-bold uppercase {{ $hk }}">{{ str_replace('_', ' ', $room->housekeeping_status) }}</span>
                </div>
            @endforeach
        </div>
    </div>

    <div class="no-scrollbar mb-4 flex gap-2 overflow-x-auto">
        @foreach (['' => 'All', 'pending' => 'Pending', 'in_progress' => 'In progress', 'completed' => 'Completed', 'verified' => 'Verified'] as $value => $label)
            <a href="{{ route('epelcon.housekeeping.index', array_filter(['status' => $value])) }}"
               class="shrink-0 rounded-full px-4 py-2 text-xs font-bold {{ request('status', '') === $value ? 'bg-slate-900 text-white' : 'bg-white text-slate-600 ring-1 ring-slate-200' }}">{{ $label }}</a>
        @endforeach
    </div>

    <div class="space-y-3">
        @forelse ($tasks as $task)
            @php
                $prio = ['urgent' => 'bg-rose-100 text-rose-700', 'high' => 'bg-amber-100 text-amber-700', 'normal' => 'bg-slate-100 text-slate-600', 'low' => 'bg-slate-100 text-slate-400'][$task->priority];
                $stat = ['pending' => 'bg-amber-100 text-amber-700', 'in_progress' => 'bg-sky-100 text-sky-700', 'completed' => 'bg-emerald-100 text-emerald-700', 'verified' => 'bg-violet-100 text-violet-700'][$task->status];
            @endphp
            <div class="rounded-2xl bg-white p-4 shadow-sm ring-1 ring-slate-200">
                <div class="flex flex-wrap items-center gap-3">
                    <div class="flex h-11 w-11 shrink-0 items-center justify-center rounded-xl bg-indigo-50 font-bold text-indigo-700">{{ $task->room->number }}</div>
                    <div class="min-w-0 flex-1">
                        <p class="font-bold capitalize text-slate-900">{{ str_replace('_', ' ', $task->task_type) }} <span class="ml-1 rounded-full px-2 py-0.5 text-[10px] font-bold uppercase {{ $prio }}">{{ $task->priority }}</span></p>
                        <p class="text-xs text-slate-500">{{ $task->room->roomType->name ?? '' }} · {{ $task->assignee->name ?? 'Unassigned' }} · {{ $task->scheduled_for?->format('D j M') }}{{ $task->notes ? ' — ' . Str::limit($task->notes, 60) : '' }}</p>
                        @if ($task->status === 'in_progress' && $task->started_at)
                            <p class="mt-0.5 text-xs font-bold text-sky-600">Started {{ $task->started_at->format('g:i A') }} · {{ $task->elapsedMinutes() }} min elapsed</p>
                        @elseif ($task->status === 'completed' && $task->duration_minutes !== null)
                            <p class="mt-0.5 text-xs font-bold text-emerald-600">Done in {{ $task->duration_minutes }} min</p>
                        @endif
                    </div>
                    <span class="rounded-full px-2.5 py-1 text-[10px] font-bold uppercase {{ $stat }}">{{ str_replace('_', ' ', $task->status) }}</span>
                    <div class="flex flex-wrap gap-1.5">
                        @if ($task->status === 'pending' && ! $task->started_at)
                            <form method="POST" action="{{ route('epelcon.housekeeping.clockin', $task) }}">@csrf
                                <button class="rounded-lg bg-emerald-600 px-4 py-2 text-xs font-bold text-white hover:bg-emerald-700">Clock in / Start</button></form>
                        @elseif ($task->status === 'in_progress')
                            <form method="POST" action="{{ route('epelcon.housekeeping.clockout', $task) }}">@csrf
                                <button class="rounded-lg bg-indigo-600 px-4 py-2 text-xs font-bold text-white hover:bg-indigo-700">Clock out / Complete</button></form>
                        @endif
                        @if ($task->status === 'completed' && auth()->user()->hasRole('admin', 'manager', 'supervisor'))
                            <form method="POST" action="{{ route('epelcon.housekeeping.status', $task) }}">@csrf<input type="hidden" name="status" value="verified">
                                <button class="rounded-lg bg-violet-600 px-3 py-1.5 text-xs font-bold text-white hover:bg-violet-700">Verify</button></form>
                        @endif
                        @if (auth()->user()->hasRole('admin', 'manager', 'supervisor'))
                            <form method="POST" action="{{ route('epelcon.housekeeping.destroy', $task) }}" onsubmit="return confirm('Delete this task?')">@csrf
                                <button class="rounded-lg bg-rose-50 px-3 py-1.5 text-xs font-bold text-rose-500 hover:bg-rose-100">✕</button></form>
                        @endif
                    </div>
                </div>
            </div>
        @empty
            <div class="rounded-2xl bg-white p-10 text-center text-slate-400 shadow-sm ring-1 ring-slate-200">No housekeeping tasks. ✨</div>
        @endforelse
    </div>

    <div class="mt-5">{{ $tasks->links() }}</div>

    {{-- Create modal --}}
    <div x-cloak x-show="create" class="fixed inset-0 z-50 flex items-end justify-center sm:items-center">
        <div class="absolute inset-0 bg-slate-900/50 backdrop-blur-sm" @click="create = false"></div>
        <div x-show="create" x-transition class="relative w-full max-w-md rounded-t-3xl bg-white p-6 shadow-2xl sm:rounded-3xl">
            <h2 class="mb-4 text-lg font-extrabold">New housekeeping task</h2>
            <form method="POST" action="{{ route('epelcon.housekeeping.store') }}" class="space-y-4">@csrf
                <label class="block"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Room</span>
                    <select name="room_id" required class="w-full rounded-xl border-slate-200 text-sm">
                        @foreach ($rooms as $room)<option value="{{ $room->id }}">Room {{ $room->number }} ({{ str_replace('_', ' ', $room->housekeeping_status) }})</option>@endforeach
                    </select></label>
                <div class="grid grid-cols-2 gap-4">
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Task</span>
                        <select name="task_type" class="w-full rounded-xl border-slate-200 text-sm">
                            @foreach (['cleaning', 'turndown', 'deep_clean', 'maintenance', 'inspection', 'laundry'] as $t)<option value="{{ $t }}">{{ ucfirst(str_replace('_', ' ', $t)) }}</option>@endforeach
                        </select></label>
                    <label><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Priority</span>
                        <select name="priority" class="w-full rounded-xl border-slate-200 text-sm">
                            @foreach (['normal', 'low', 'high', 'urgent'] as $p)<option value="{{ $p }}">{{ ucfirst($p) }}</option>@endforeach
                        </select></label>
                </div>
                <label class="block"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Assign to</span>
                    <select name="assigned_to" class="w-full rounded-xl border-slate-200 text-sm">
                        <option value="">— Anyone —</option>
                        @foreach ($housekeepers as $hk)<option value="{{ $hk->id }}">{{ $hk->name }}</option>@endforeach
                    </select></label>
                <label class="block"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Date</span>
                    <input type="date" name="scheduled_for" value="{{ today()->toDateString() }}" required class="w-full rounded-xl border-slate-200 text-sm"></label>
                <label class="block"><span class="mb-1 block text-xs font-bold uppercase text-slate-500">Notes</span>
                    <textarea name="notes" rows="2" class="w-full rounded-xl border-slate-200 text-sm"></textarea></label>
                <div class="flex justify-end gap-2">
                    <button type="button" @click="create = false" class="rounded-xl px-4 py-2.5 text-sm font-bold text-slate-500">Cancel</button>
                    <button class="rounded-xl bg-indigo-600 px-5 py-2.5 text-sm font-bold text-white">Create task</button>
                </div>
            </form>
        </div>
    </div>
</div>
@endsection
