@extends('layouts.admin', ['title' => 'تذاكر الدعم | Admin'])

@php
    $statusLabels = [
        'open' => 'مفتوحة',
        'pending_customer' => 'بانتظار العميل',
        'resolved' => 'تم الحل',
        'closed' => 'مغلقة',
    ];
    $priorityLabels = [
        'low' => 'منخفضة',
        'normal' => 'عادية',
        'high' => 'عالية',
    ];
@endphp

@section('content')
    <section class="lc-page-shell">
        <div class="section-heading">
            <div>
                <span>Support Desk</span>
                <h1>مكتب الدعم الفني</h1>
            </div>
            <a class="secondary-button" href="{{ route('admin.dashboard') }}">لوحة الإدارة</a>
        </div>

        <div class="surface mt-6 p-5">
            <div class="ac-panel-head">
                <div>
                    <h2>حالة التذاكر</h2>
                    <p>تابع المفتوح، المنتظر رد العميل، والأولوية العالية من مكان واحد.</p>
                </div>
            </div>
            <div class="ac-status-strip support-strip">
                <a class="ac-status-chip teal {{ $status === '' ? 'active' : '' }}" href="{{ route('admin.support-tickets.index') }}">
                    <span>كل التذاكر</span><strong>{{ $summary['total'] }}</strong>
                </a>
                <a class="ac-status-chip amber {{ $status === 'open' ? 'active' : '' }}" href="{{ route('admin.support-tickets.index', ['status' => 'open']) }}">
                    <span>مفتوحة</span><strong>{{ $summary['open'] }}</strong>
                </a>
                <a class="ac-status-chip blue {{ $status === 'pending_customer' ? 'active' : '' }}" href="{{ route('admin.support-tickets.index', ['status' => 'pending_customer']) }}">
                    <span>بانتظار العميل</span><strong>{{ $summary['pending_customer'] }}</strong>
                </a>
                <a class="ac-status-chip green {{ $status === 'resolved' ? 'active' : '' }}" href="{{ route('admin.support-tickets.index', ['status' => 'resolved']) }}">
                    <span>تم الحل</span><strong>{{ $summary['resolved'] }}</strong>
                </a>
                <a class="ac-status-chip red {{ $priority === 'high' ? 'active' : '' }}" href="{{ route('admin.support-tickets.index', ['priority' => 'high']) }}">
                    <span>أولوية عالية</span><strong>{{ $summary['high'] }}</strong>
                </a>
            </div>
        </div>

        <form class="surface mt-5 grid gap-3 p-4 md:grid-cols-[1fr_220px_220px_auto]" method="GET">
            <input name="q" value="{{ $query }}" placeholder="بحث برقم التذكرة أو الموضوع أو العميل">
            <select name="status">
                <option value="">كل الحالات</option>
                @foreach($statusLabels as $value => $label)
                    <option value="{{ $value }}" @selected($status === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <select name="priority">
                <option value="">كل الأولويات</option>
                @foreach($priorityLabels as $value => $label)
                    <option value="{{ $value }}" @selected($priority === $value)>{{ $label }}</option>
                @endforeach
            </select>
            <button class="secondary-button" type="submit">تصفية</button>
        </form>

        <div class="ac-ticket-board">
            @forelse($tickets as $ticket)
                <a href="{{ route('admin.support-tickets.show', $ticket) }}" class="ac-ticket-card priority-{{ $ticket->priority }}">
                    <div class="ac-ticket-top">
                        <span class="ac-ticket-number">{{ $ticket->ticket_number }}</span>
                        <span class="ac-badge status-{{ $ticket->status }}">{{ $statusLabels[$ticket->status] ?? $ticket->status }}</span>
                    </div>
                    <h3>{{ $ticket->subject }}</h3>
                    <div class="ac-ticket-user">
                        <span>{{ mb_strtoupper(mb_substr($ticket->user?->display_name ?? $ticket->user?->name ?? 'G', 0, 1)) }}</span>
                        <div>
                            <strong>{{ $ticket->user?->display_name ?? $ticket->user?->name ?? 'Guest' }}</strong>
                            <small>{{ $ticket->user?->email ?? 'بدون بريد' }}</small>
                        </div>
                    </div>
                    <div class="ac-ticket-foot">
                        <span class="ac-badge priority-{{ $ticket->priority }}">{{ $priorityLabels[$ticket->priority] ?? $ticket->priority }}</span>
                        <small>{{ $ticket->last_activity_at?->diffForHumans() ?? $ticket->updated_at?->diffForHumans() }}</small>
                    </div>
                </a>
            @empty
                <div class="ac-empty">لا توجد تذاكر مطابقة للفلاتر الحالية.</div>
            @endforelse
        </div>

        <div class="mt-4">{{ $tickets->links() }}</div>
    </section>
@endsection
