@extends('admin.layouts.layout')
@section('title')درخواست‌های پشتیبانی@endsection

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">

    <div class="d-flex align-items-center justify-content-between mb-4">
        <div>
            <h4 class="mb-1">درخواست‌های پشتیبانی</h4>
            <p class="text-muted mb-0">لیست تمام تیکت‌های کاربران</p>
        </div>
        <a href="{{ route('admin.tickets.new') }}" class="btn btn-primary">
            <i class="ti ti-plus me-1"></i> تیکت جدید
        </a>
    </div>

    @if(session('success'))
        <div class="alert alert-success alert-dismissible mb-4">
            {{ session('success') }}
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    @endif

    {{-- Filters --}}
    <div class="card mb-4">
        <div class="card-body py-3">
            <form method="GET" action="{{ route('admin.tickets.index') }}">
                <div class="row g-2 align-items-center">
                    <div class="col-md-4">
                        <input type="text" name="q" class="form-control"
                               placeholder="شماره تیکت، موضوع، نام یا موبایل..." value="{{ request('q') }}">
                    </div>
                    <div class="col-md-3">
                        <select name="status" class="form-select">
                            <option value="">همه وضعیت‌ها</option>
                            @foreach($statuses as $val => $label)
                            <option value="{{ $val }}" {{ request('status') === $val ? 'selected' : '' }}>{{ $label }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select name="department_id" class="form-select">
                            <option value="">همه دپارتمان‌ها</option>
                            @foreach($departments as $dept)
                            <option value="{{ $dept->id }}" {{ (string) request('department_id') === (string) $dept->id ? 'selected' : '' }}>{{ $dept->name }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="col-auto d-flex gap-2">
                        <button type="submit" class="btn btn-primary"><i class="ti ti-search ti-xs"></i></button>
                        @if(request('q') || request('status') || request('department_id'))
                        <a href="{{ route('admin.tickets.index') }}" class="btn btn-outline-secondary">پاک کردن</a>
                        @endif
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="table-responsive">
            <table class="table border-top align-middle mb-0">
                <thead class="table-light">
                    <tr>
                        <th>شناسه</th>
                        <th>موضوع</th>
                        <th>کاربر</th>
                        <th>موبایل</th>
                        <th>دپارتمان</th>
                        <th>پاسخ‌ها</th>
                        <th>وضعیت</th>
                        <th>آخرین به‌روزرسانی</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($tickets as $ticket)
                        <tr>
                            <td><span class="badge bg-label-secondary font-monospace" dir="ltr">{{ $ticket->name }}</span></td>
                            <td>
                                <a href="{{ route('admin.tickets.show', $ticket->name) }}" class="fw-medium text-body">
                                    {{ $ticket->subject }}
                                </a>
                            </td>
                            <td>{{ optional($ticket->user)->first_name }} {{ optional($ticket->user)->last_name }}</td>
                            <td><small class="text-muted" dir="ltr">{{ optional($ticket->user)->mobile }}</small></td>
                            <td>
                                @if($ticket->department)
                                    <span class="text-muted">{{ $ticket->department->name }}</span>
                                @else <span class="text-muted">—</span> @endif
                            </td>
                            <td><span class="badge bg-label-secondary">{{ $ticket->replies_count }}</span></td>
                            <td><span class="badge bg-label-{{ $ticket->statusColor() }}">{{ $ticket->statusLabel() }}</span></td>
                            <td><small class="text-muted">{{ $ticket->shamsi_updated ?: $ticket->shamsi_created }}</small></td>
                            <td>
                                <a href="{{ route('admin.tickets.show', $ticket->name) }}" class="btn btn-sm btn-icon btn-outline-primary">
                                    <i class="ti ti-eye ti-xs"></i>
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="9" class="text-center py-5 text-muted">
                                <i class="ti ti-ticket-off fs-3 d-block mb-2"></i>
                                هیچ تیکتی ثبت نشده است.
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        @if($tickets->hasPages())
        <div class="card-footer d-flex justify-content-between align-items-center">
            <span class="text-muted small">{{ $tickets->total() }} تیکت</span>
            {{ $tickets->links() }}
        </div>
        @endif
    </div>

</div>
@endsection
