@php
    $ws = session('workspace', 'ha');
    $wsLabel = match($ws) { 'pension' => 'CPF', 'wills' => 'W&T', default => 'HA' };
    $wsBadge = match($ws) { 'pension' => 'warning text-dark', 'wills' => 'success', default => 'primary' };
@endphp
<div class="container-fluid py-4">
    <div class="row g-4">
        <div class="col-12">
            <div class="card shadow-sm mb-4">
                <div class="card-body bg-light border-bottom d-flex justify-content-between align-items-center">
                    <div class="d-flex align-items-center gap-2">
                        <div class="btn-group shadow-sm">
                            <button wire:click="$set('timeframe', 'all')" class="btn btn-sm btn-{{ $timeframe == 'all' ? 'dark' : 'outline-dark' }}">All</button>
                            <button wire:click="$set('timeframe', 'day')" class="btn btn-sm btn-{{ $timeframe == 'day' ? 'dark' : 'outline-dark' }}">Today</button>
                            <button wire:click="$set('timeframe', 'week')" class="btn btn-sm btn-{{ $timeframe == 'week' ? 'dark' : 'outline-dark' }}">This Week</button>
                        </div>
                        <span class="badge bg-{{ $wsBadge }}">{{ $wsLabel }} Tasks</span>
                    </div>

                    <select wire:model.live="userId" class="form-select form-select-sm w-auto">
                        <option value="">Full Team View</option>
                        @foreach($this->manageableUsers as $u)
                            <option value="{{ $u->id }}">{{ $u->name_first }} {{ $u->name_last }}</option>
                        @endforeach
                    </select>
                </div>

                <div class="card-body">
                    @if($groupByUser)
                        @foreach($this->tasks as $fullName => $userTasks)
                            <div class="mb-4">
                                <h6 class="fw-bold bg-secondary-subtle p-2 border rounded"><i class="bi bi-person me-2"></i>{{ $fullName }}</h6>
                                <div class="list-group list-group-flush border rounded shadow-sm">
                                    @foreach($userTasks as $task)
                                        <x-task.task-row :task="$task" :showUser="false" />
                                    @endforeach
                                </div>
                            </div>
                        @endforeach
                    @else
                        <div class="list-group list-group-flush border rounded shadow-sm">
                            @foreach($this->tasks as $task)
                                <x-task.task-row :task="$task" :showUser="true" />
                            @endforeach
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
    <div
            class="offcanvas offcanvas-end {{ $showEditOffcanvas ? 'show' : '' }}"
            tabindex="-1"
            style="{{ $showEditOffcanvas ? 'visibility: visible;' : '' }}"
    >
        <div class="offcanvas-header bg-light border-bottom">
            <h5 class="offcanvas-title fw-bold">Edit Task</h5>
            <button type="button" class="btn-close" wire:click="closeOffcanvas"></button>
        </div>
        <div class="offcanvas-body">
            <form wire:submit="updateTask">
                <div class="mb-3">
                    <label class="form-label small fw-bold text-muted">ASSIGN TO</label>
                    <select wire:model="target_user_id" class="form-select">
                        @foreach($this->manageableUsers as $u)
                            <option value="{{ $u->id }}">{{ $u->name_first }} {{ $u->name_last }}</option>
                        @endforeach
                    </select>
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold text-muted">TASK TITLE</label>
                    <input type="text" wire:model="title" class="form-control">
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold text-muted">DESCRIPTION</label>
                    <textarea wire:model="description" class="form-control" rows="4"></textarea>
                </div>

                <div class="mb-3">
                    <label class="form-label small fw-bold text-muted">DUE DATE</label>
                    <input type="datetime-local" wire:model="due_at" class="form-control">
                </div>

                <button type="submit" class="btn btn-primary w-100">Update Task</button>
            </form>
        </div>
    </div>

    @if($showEditOffcanvas)
        <div class="offcanvas-backdrop fade show" wire:click="closeOffcanvas"></div>
    @endif
</div>