@extends('layouts.admin')

@section('title', 'Tickets')

@section('content')
    <div class="d-flex flex-wrap justify-content-between align-items-center gap-1 mb-2">
        <div><span class="loot-status success">Support desk</span><h1 class="loot-section-title mt-1 mb-25">Tickets</h1><p class="loot-muted mb-0">Reply to users and track support status.</p></div>
    </div>
    <div class="loot-admin-grid mb-2">
        <div class="loot-admin-card"><span>Open</span><strong>{{ $stats['open'] }}</strong></div>
        <div class="loot-admin-card"><span>Pending</span><strong>{{ $stats['pending'] }}</strong></div>
        <div class="loot-admin-card"><span>Closed</span><strong>{{ $stats['closed'] }}</strong></div>
        <div class="loot-admin-card"><span>Total</span><strong>{{ $stats['total'] }}</strong></div>
    </div>
    <section class="user-page-card">
        <form method="GET" class="admin-leads-filters">
            <div class="admin-filter-search"><i data-feather="search"></i><input class="form-control" name="search" value="{{ request('search') }}" placeholder="Search tickets or users"></div>
            <select class="form-select" name="status">
                <option value="">All statuses</option>
                @foreach(['open', 'pending', 'closed'] as $status)<option value="{{ $status }}" @selected(request('status') === $status)>{{ ucfirst($status) }}</option>@endforeach
            </select>
            <button class="btn btn-loot">Apply</button><a href="{{ route('admin.tickets') }}" class="btn btn-outline-light">Reset</a>
        </form>
        <form id="ticketBulk" data-bulk-form="tickets" method="POST" action="{{ route('admin.tickets.bulk') }}" class="admin-bulk-bar mb-1">
            @csrf
            <label class="admin-select-all"><input class="form-check-input" type="checkbox" data-bulk-select-all="tickets"> Select page</label>
            <select class="form-select" name="action" required>
                <option value="">Bulk actions</option><option value="open">Mark open</option><option value="pending">Mark pending</option><option value="close">Close</option><option value="delete">Delete</option>
            </select>
            <button class="btn btn-outline-light" data-bulk-submit="tickets" disabled>Apply</button>
            <span class="loot-muted" data-bulk-count="tickets">0 selected</span>
            <button type="button" class="btn btn-outline-light ms-auto" data-bulk-export="tickets" data-export-url="{{ route('admin.export.tickets', request()->query()) }}"><i data-feather="download" class="me-50"></i>Export CSV</button>
        </form>
        <div class="loot-leader-list">
            @forelse($tickets as $ticket)
                <div class="ticket-select-row">
                    <input class="form-check-input" type="checkbox" name="ids[]" value="{{ $ticket->id }}" form="ticketBulk" data-bulk-item="tickets" aria-label="Select ticket {{ $ticket->id }}">
                    <a class="loot-leader-row ticket-row" href="{{ route('admin.tickets.show', $ticket) }}">
                        <span class="loot-leader-rank">#{{ $ticket->id }}</span>
                        <img src="{{ $ticket->user->avatar() }}" alt="{{ $ticket->user->username }}">
                        <span class="loot-leader-name"><strong>{{ $ticket->subject }}</strong><small>{{ $ticket->user->username }} / {{ $ticket->category }} / {{ $ticket->priority }}</small></span>
                        <span class="loot-status {{ $ticket->status === 'closed' ? '' : 'success' }}">{{ ucfirst($ticket->status) }}</span>
                    </a>
                </div>
            @empty
                <p class="loot-muted mb-0">No tickets yet.</p>
            @endforelse
        </div>
        <div class="mt-2">{{ $tickets->links() }}</div>
    </section>
@endsection
