@extends('layouts.app')
@section('title', ($ticket->subject ?: ('Ticket #' . $ticket->id)))

@section('content')
<section class="content-header">
    <h1>{{ $ticket->subject ?: ('Ticket #' . $ticket->id) }}
        <span class="label label-info">{{ ucfirst(str_replace('_', ' ', $ticket->status)) }}</span>
        <a href="{{ route('vrc.tickets.index') }}" class="btn btn-default btn-sm pull-right"><i class="fa fa-arrow-left"></i> Back</a>
    </h1>
</section>

<section class="content">
    @if(session('status'))
        @php $st = session('status'); @endphp
        <div class="alert alert-{{ !empty($st['success']) ? 'success' : 'danger' }}">{{ $st['msg'] ?? '' }}</div>
    @endif

    <div class="row">
        <div class="col-md-8">
            {{-- Message thread --}}
            @component('components.widget', ['class' => 'box-primary', 'title' => 'Conversation'])
                @forelse ($messages as $m)
                    <div class="vrc-msg" style="border-bottom:1px solid #eee; padding:8px 0;">
                        <strong>
                            @if($m->author_type === 'system')
                                <i class="fa fa-cog"></i> System
                            @elseif($m->author_type === 'customer')
                                <i class="fa fa-user"></i> Customer
                            @else
                                <i class="fa fa-headset"></i> Staff
                            @endif
                        </strong>
                        <span class="text-muted pull-right" style="font-size:12px;">
                            {{ $m->created_at ? \Carbon\Carbon::parse($m->created_at)->format('Y-m-d H:i') : '' }}
                        </span>
                        <div style="margin-top:4px; white-space:pre-wrap;">{{ $m->body }}</div>
                    </div>
                @empty
                    <p class="text-muted">No messages yet.</p>
                @endforelse

                {{-- Reply form --}}
                <form method="POST" action="{{ route('vrc.tickets.messages.store', $ticket->id) }}" style="margin-top:12px;">
                    @csrf
                    <div class="form-group">
                        <label for="body">Reply</label>
                        <textarea name="body" id="body" rows="3" class="form-control" required></textarea>
                    </div>
                    <button type="submit" class="btn btn-primary"><i class="fa fa-reply"></i> Send Reply</button>
                </form>
            @endcomponent
        </div>

        <div class="col-md-4">
            {{-- Details --}}
            @component('components.widget', ['class' => 'box-default', 'title' => 'Details'])
                <p><strong>Priority:</strong> {{ ucfirst((string) $ticket->priority) ?: '—' }}</p>
                <p><strong>Category:</strong> {{ ucfirst((string) $ticket->category) ?: '—' }}</p>
                <p><strong>Source:</strong> {{ ucfirst((string) $ticket->source) ?: '—' }}</p>
                <p><strong>Assigned To:</strong> {{ $ticket->assigned_to ?: '—' }}</p>
                <p><strong>Opened:</strong> {{ $ticket->opened_at ? $ticket->opened_at->format('Y-m-d H:i') : '—' }}</p>
                @if($ticket->resolved_at)
                    <p><strong>Resolved:</strong> {{ $ticket->resolved_at->format('Y-m-d H:i') }}</p>
                @endif
                @if($vehicle)
                    <p><strong>Vehicle:</strong> {{ $vehicle->label ?? $vehicle->plate_number ?? ('#' . $vehicle->id) }}</p>
                @endif
                @if($repairOrder)
                    <p><strong>Repair Order:</strong>
                        <a href="{{ route('vrc.repair-orders.show', $repairOrder->id) }}">{{ $repairOrder->number ?: ('#' . $repairOrder->id) }}</a>
                    </p>
                @endif
                @if($ticket->ncr_id)
                    <p><span class="label label-warning">Escalated to NCR #{{ $ticket->ncr_id }}</span></p>
                @endif
                @if($ticket->description)
                    <hr>
                    <p style="white-space:pre-wrap;">{{ $ticket->description }}</p>
                @endif
            @endcomponent

            @can('vrc.ticket.manage')
            {{-- Status change --}}
            @component('components.widget', ['class' => 'box-default', 'title' => __('vrc::lang.status')])
                <form method="POST" action="{{ route('vrc.tickets.status', $ticket->id) }}">
                    @csrf
                    <div class="form-group">
                        {!! Form::select('status', collect($statuses)->mapWithKeys(fn ($s) => [$s => ucfirst(str_replace('_', ' ', $s))])->toArray(), $ticket->status, ['class' => 'form-control']) !!}
                    </div>
                    <button type="submit" class="btn btn-primary btn-block">Update Status</button>
                </form>
            @endcomponent

            {{-- Escalate to NCR --}}
            @unless($ticket->ncr_id)
            @component('components.widget', ['class' => 'box-default', 'title' => 'Escalation'])
                <form method="POST" action="{{ route('vrc.tickets.escalate', $ticket->id) }}"
                      onsubmit="return confirm('Escalate this ticket to a Non-Conformance Report?');">
                    @csrf
                    <button type="submit" class="btn btn-warning btn-block"><i class="fa fa-exclamation-triangle"></i> Escalate to NCR</button>
                </form>
            @endcomponent
            @endunless
            @endcan
        </div>
    </div>
</section>
@endsection
