<div class="container-fluid py-4">
    <div class="row g-4">
        @can('tasks.admin')
        <div class="col-lg-4">
            <div class="card shadow-sm">
                <div class="card-header fw-bold">
                    <i class="bi bi-plus-circle me-2"></i>Assign New Task
                </div>
                <div class="card-body">
                    <form wire:submit="addTask">
                        <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 border-primary-subtle">
                                <option value="">Select Staff Member...</option>
                                @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" placeholder="What needs to be done?">
                        </div>

                        <div class="mb-3">
                            <label class="form-label small fw-bold text-muted">DESCRIPTION / NOTES</label>
                            <textarea wire:model="description" class="form-control" rows="3" placeholder="Additional details..."></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 shadow-sm">Assign Task</button>
                    </form>
                </div>
            </div>
        </div>
        @endcan

        <div class="col-lg-8">
            <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="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>

                    <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>

                @can('tasks.edit')
                    <button type="submit" class="btn btn-primary w-100">Update Task</button>
                @endcan
            </form>
        </div>
    </div>

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