@extends('layouts.admin', ['title' => $ticket->ticket_number.' | 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>{{ $ticket->ticket_number }}</span>
                <h1>{{ $ticket->subject }}</h1>
            </div>
            <div class="ac-actions">
                <a class="secondary-button" href="{{ route('admin.support-tickets.index') }}">كل التذاكر</a>
                @if($ticket->user)
                    <a class="secondary-button info" href="{{ route('admin.customers.show', $ticket->user) }}">ملف العميل</a>
                @endif
            </div>
        </div>

        @if(session('status'))
            <div class="ac-alert success">{{ session('status') }}</div>
        @endif
        @if($errors->any())
            <div class="ac-alert danger">{{ $errors->first() }}</div>
        @endif

        <div class="ac-ticket-detail-layout">
            <section class="surface ac-ticket-thread">
                <div class="ac-panel-head">
                    <div>
                        <h2>محادثة التذكرة</h2>
                        <p>{{ $ticket->messages->count() }} رسالة · آخر نشاط {{ $ticket->last_activity_at?->diffForHumans() ?? $ticket->updated_at?->diffForHumans() }}</p>
                    </div>
                    <span class="ac-badge status-{{ $ticket->status }}">{{ $statusLabels[$ticket->status] ?? $ticket->status }}</span>
                </div>

                <div class="ac-message-list">
                    @forelse($ticket->messages as $message)
                        <article class="ac-message {{ $message->sender_type === 'admin' ? 'admin' : 'customer' }}">
                            <div class="ac-message-avatar">
                                {{ $message->sender_type === 'admin' ? 'A' : mb_strtoupper(mb_substr($message->sender?->display_name ?? $message->sender?->name ?? 'C', 0, 1)) }}
                            </div>
                            <div class="ac-message-bubble">
                                <header>
                                    <strong>{{ $message->sender_type === 'admin' ? 'فريق الإدارة' : ($message->sender?->display_name ?? $message->sender?->name ?? 'Customer') }}</strong>
                                    <span>{{ $message->created_at->format('Y-m-d H:i') }}</span>
                                </header>
                                <p>{{ $message->body }}</p>
                            </div>
                        </article>
                    @empty
                        <div class="ac-empty">لا توجد رسائل في هذه التذكرة بعد.</div>
                    @endforelse
                </div>
            </section>

            <aside class="ac-ticket-side">
                <form method="POST" action="{{ route('admin.support-tickets.update', $ticket) }}" class="surface ac-ticket-control">
                    @csrf
                    @method('PATCH')
                    <div class="ac-panel-head">
                        <div>
                            <h2>إدارة التذكرة</h2>
                            <p>غيّر الحالة أو الأولوية واكتب ردًا للعميل.</p>
                        </div>
                    </div>

                    <div class="ac-ticket-customer">
                        <span>{{ mb_strtoupper(mb_substr($ticket->user?->display_name ?? $ticket->user?->name ?? 'C', 0, 1)) }}</span>
                        <div>
                            <strong>{{ $ticket->user?->display_name ?? $ticket->user?->name ?? 'Customer' }}</strong>
                            <small>{{ $ticket->user?->email }}</small>
                        </div>
                    </div>

                    <div class="ac-mini-stats">
                        <div><span>الحالة</span><b>{{ $statusLabels[$ticket->status] ?? $ticket->status }}</b></div>
                        <div><span>الأولوية</span><b>{{ $priorityLabels[$ticket->priority] ?? $ticket->priority }}</b></div>
                        <div><span>الرسائل</span><b>{{ $ticket->messages->count() }}</b></div>
                    </div>

                    <label class="form-field">
                        <span>الحالة</span>
                        <select name="status">
                            @foreach($statusLabels as $value => $label)
                                <option value="{{ $value }}" @selected($ticket->status === $value)>{{ $label }}</option>
                            @endforeach
                        </select>
                    </label>

                    <label class="form-field">
                        <span>الأولوية</span>
                        <select name="priority">
                            @foreach($priorityLabels as $value => $label)
                                <option value="{{ $value }}" @selected($ticket->priority === $value)>{{ $label }}</option>
                            @endforeach
                        </select>
                    </label>

                    <label class="form-field">
                        <span>رد جديد للعميل</span>
                        <textarea name="last_message" rows="6" placeholder="اكتب رد واضح ومختصر للعميل...">{{ old('last_message') }}</textarea>
                    </label>

                    <button class="primary-button" type="submit">حفظ وإرسال التحديث</button>
                </form>
            </aside>
        </div>
    </section>
@endsection
