@extends('layouts.app')
@section('title', 'Cases')
@section('content')
<main class="nxl-container"><div class="nxl-content"><div class="page-header"><h5>Cases</h5></div><div class="main-content">
@if(session('success'))<div class="alert alert-success">{{ session('success') }}</div>@endif
@if($errors->any())<div class="alert alert-danger">{{ $errors->first() }}</div>@endif
<div class="card mb-4" id="caseTopFilters"><div class="card-header"><strong>Case Filters</strong></div><div class="card-body"><form method="GET" action="{{ route('cases.index') }}" class="row g-2 align-items-end">
<div class="col-md-3"><label class="form-label">Search</label><input type="search" name="q" value="{{ request('q') }}" maxlength="100" class="form-control" placeholder="Number, subject, or description"></div>
<div class="col-md-2"><label class="form-label">Status</label><select class="form-select" name="status"><option value="">All statuses</option>@foreach(['new','in_progress','waiting','resolved','closed'] as $s)<option value="{{ $s }}" @selected(request('status')===$s)>{{ ucwords(str_replace('_',' ',$s)) }}</option>@endforeach</select></div>
<div class="col-md-2"><label class="form-label">Priority</label><select class="form-select" name="priority"><option value="">All priorities</option>@foreach(['low','medium','high','urgent'] as $priority)<option value="{{ $priority }}" @selected(request('priority')===$priority)>{{ ucfirst($priority) }}</option>@endforeach</select></div>
<x-case-remote-select name="assigned_to" label="Employee" type="user" :selected="request('assigned_to')" none="All employees" column="col-md-3" />
<div class="col-md-2"><label class="form-label">Created From</label><input type="date" name="date_from" value="{{ request('date_from') }}" class="form-control"></div>
<div class="col-md-2"><label class="form-label">Created To</label><input type="date" name="date_to" value="{{ request('date_to') }}" class="form-control"></div>
<div class="col-12 d-flex justify-content-center gap-2"><button class="btn btn-primary px-4"><i class="feather-filter me-1"></i>Apply Filters</button><a href="{{ route('cases.index') }}" class="btn btn-outline-secondary px-4"><i class="feather-refresh-cw me-1"></i>Reset Filters</a></div>
</form></div></div>
<div class="card mb-4"><div class="card-header">Create Case</div><div class="card-body"><form method="POST" action="{{ route('cases.store') }}" class="row g-3">@csrf
<div class="col-md-8"><label class="form-label">Subject</label><input class="form-control" name="subject" value="{{ old('subject') }}" required></div>
<div class="col-md-4"><label class="form-label">Category</label><input class="form-control" name="category" value="{{ old('category') }}"></div>
<x-case-remote-select name="account_id" label="Account" type="account" :selected="old('account_id', request('account_id'))" />
<x-case-remote-select name="contact_id" label="Contact" type="contact" :selected="old('contact_id', request('contact_id'))" />
<x-case-remote-select name="lead_id" label="Lead" type="lead" :selected="old('lead_id', request('lead_id'))" />
<x-case-remote-select name="deal_id" label="Deal" type="deal" :selected="old('deal_id', request('deal_id'))" />
<x-case-remote-select name="assigned_to" label="Assigned Employee" type="user" :selected="old('assigned_to')" none="Unassigned" />
<div class="col-md-3"><label class="form-label">Priority</label><select class="form-select" name="priority">@foreach(['low','medium','high','urgent'] as $s)<option @selected(old('priority')===$s)>{{ $s }}</option>@endforeach</select></div>
<div class="col-md-3"><label class="form-label">Status</label><select class="form-select" name="status">@foreach(['new','in_progress','waiting','resolved','closed'] as $s)<option @selected(old('status')===$s)>{{ $s }}</option>@endforeach</select></div>
<div class="col-md-3"><label class="form-label">Due</label><input type="datetime-local" class="form-control" name="due_at" value="{{ old('due_at') }}"></div>
<div class="col-12"><textarea class="form-control" name="description" placeholder="Description">{{ old('description') }}</textarea></div><div class="col-12"><button class="btn btn-primary">Create Case</button></div>
</form></div></div>
<div class="card"><div class="card-header"><strong>Case Records</strong></div><div class="card-body table-responsive"><table class="table"><thead><tr><th>Case</th><th>Relationships</th><th>Employee</th><th>Priority</th><th>Status</th><th>Due</th><th>Activity</th></tr></thead><tbody>
@forelse($cases as $case)<tr><td><a href="{{ route('cases.show',$case) }}"><strong>{{ $case->case_number }}</strong><small class="d-block">{{ $case->subject }}</small></a></td><td>{{ $case->account?->account_name ?: $case->contact?->first_name ?: $case->lead?->first_name ?: $case->deal?->name }}</td><td>{{ $case->assignee?->name ?: 'Unassigned' }}</td><td>{{ ucfirst($case->priority) }}</td><td>{{ ucwords(str_replace('_',' ',$case->status)) }}</td><td>{{ $case->due_at?->format('M d, Y H:i') }}</td><td>{{ $case->comments_count }} comments · {{ $case->files_count }} files</td></tr>@empty<tr><td colspan="7" class="text-center text-muted">No records found.</td></tr>@endforelse
</tbody></table>{{ $cases->links() }}</div></div>
</div></div></main>
@include('cases.partials.remote-select-script')
@endsection
