@extends('layouts.app')
@section('title','Tasks')
@section('content')
@php
    $filters = [];
    if(request()->filled('search')) $filters[] = ['label'=>'Search','value'=>request('search'),'removeUrl'=>route('tasks.index',request()->except(['search','page']))];
    if(request()->filled('status')) $filters[] = ['label'=>'Status','value'=>str((string)request('status'))->replace('_',' ')->headline(),'removeUrl'=>route('tasks.index',request()->except(['status','page']))];
    if(request()->filled('branch_id')) {
        $branch = $branches->firstWhere('id',(int)request('branch_id'));
        $filters[] = ['label'=>'Branch','value'=>$branch?->name ?? request('branch_id'),'removeUrl'=>route('tasks.index',request()->except(['branch_id','page']))];
    }
    if(request()->filled('assigned_to')) {
        $assignee = $staff->firstWhere('id',(int)request('assigned_to'));
        $filters[] = ['label'=>'Assignee','value'=>$assignee?->name ?? request('assigned_to'),'removeUrl'=>route('tasks.index',request()->except(['assigned_to','page']))];
    }
@endphp

<x-ui.page-toolbar title="Tasks" description="Assigned operational work, collection follow-ups and management actions." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Tasks']]">
    <x-slot:actions>
        @can('calendar.view')<a class="mf-btn" href="{{ route('calendar.index') }}">Calendar</a>@endcan
        @can('tasks.create')<a class="mf-btn mf-btn-primary" href="{{ route('tasks.create') }}">New Task</a>@endcan
    </x-slot:actions>
</x-ui.page-toolbar>

<div class="grid gap-3 md:grid-cols-4 mb-4">
@foreach(['pending'=>'Pending','in_progress'=>'In progress','completed'=>'Completed','cancelled'=>'Cancelled'] as $key=>$label)
<div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">{{ $label }}</div><div class="mt-1 text-xl font-semibold">{{ $summary[$key] ?? 0 }}</div></div>
@endforeach
</div>

<x-ui.quick-status-tabs :tabs="[
    ['label'=>'All','value'=>''],
    ['label'=>'Pending','value'=>'pending','count'=>$summary['pending'] ?? 0],
    ['label'=>'In progress','value'=>'in_progress','count'=>$summary['in_progress'] ?? 0],
    ['label'=>'Completed','value'=>'completed','count'=>$summary['completed'] ?? 0],
    ['label'=>'Cancelled','value'=>'cancelled','count'=>$summary['cancelled'] ?? 0],
]" />

<x-ui.table-toolbar table-id="tasks-table" search-placeholder="Search tasks…" :filter-names="['branch_id','assigned_to']" :columns="[
    'task'=>'Task', 'due'=>'Due', 'category'=>'Category', 'priority'=>'Priority', 'assigned'=>'Assigned', 'branch'=>'Branch', 'status'=>'Status'
]" export-filename="tasks.csv">
    <x-slot:filters>
        <select name="branch_id" class="mf-filter-control" aria-label="Filter by branch">
            <option value="">All branches</option>
            @foreach($branches as $branch)<option value="{{ $branch->id }}" @selected((string)request('branch_id')===(string)$branch->id)>{{ $branch->name }}</option>@endforeach
        </select>
        <select name="assigned_to" class="mf-filter-control" aria-label="Filter by assignee">
            <option value="">All assignees</option>
            @foreach($staff as $user)<option value="{{ $user->id }}" @selected((string)request('assigned_to')===(string)$user->id)>{{ $user->name }}</option>@endforeach
        </select>
    </x-slot:filters>
</x-ui.table-toolbar>

<x-ui.applied-filter-chips :filters="$filters" :clear-url="route('tasks.index')" />

<div class="mf-card"><div class="mf-table-frame border-0"><table id="tasks-table" class="mf-table" data-standard-table>
<thead><tr>
    <th data-table-column="task">Task</th>
    <th data-table-column="due">Due</th>
    <th data-table-column="category">Category</th>
    <th data-table-column="priority">Priority</th>
    <th data-table-column="assigned">Assigned</th>
    <th data-table-column="branch">Branch</th>
    <th data-table-column="status">Status</th>
    <th></th>
</tr></thead>
<tbody>@forelse($tasks as $task)<tr>
<td data-table-column="task"><div class="font-medium">{{ $task->title }}</div><div class="text-xs text-muted-foreground line-clamp-1">{{ $task->description ?: 'No description' }}</div></td>
<td data-table-column="due">{{ $task->due_at?->format('d M Y H:i') ?? 'No due date' }}</td>
<td data-table-column="category">{{ str($task->category)->replace('_',' ')->headline() }}</td>
<td data-table-column="priority">{{ str($task->priority)->headline() }}</td>
<td data-table-column="assigned">{{ $task->assignee?->name ?? 'Unassigned' }}</td>
<td data-table-column="branch">{{ $task->branch?->name }}</td>
<td data-table-column="status"><span class="mf-badge {{ $task->status==='completed'?'mf-badge-success':($task->status==='cancelled'?'mf-badge-danger':($task->status==='in_progress'?'mf-badge-info':'mf-badge-warning')) }}">{{ str($task->status)->replace('_',' ')->headline() }}</span></td>
<td class="text-right"><a class="mf-btn" href="{{ route('tasks.show',$task) }}">Open</a></td>
</tr>@empty<tr><td colspan="8" class="text-center text-muted-foreground">No tasks match the selected filters.</td></tr>@endforelse</tbody>
</table></div></div>
<div class="mt-4">{{ $tasks->links() }}</div>
@endsection