@extends('layouts.admin')

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h4 class="card-title">Ticket #{{ $ticket->ticket_number }}</h4>
                    <a href="{{ route('admin.support-tickets.index') }}" class="btn btn-sm btn-secondary">Back to Tickets</a>
                </div>
                <div class="card-body">
                    @if(session('success'))
                        <div class="alert alert-success alert-dismissible fade show" role="alert">
                            {{ session('success') }}
                            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                        </div>
                    @endif

                    <div class="mb-4">
                        <h5>{{ $ticket->subject }}</h5>
                        @php
                            $statusBadges = ['open' => 'warning', 'in_progress' => 'info', 'resolved' => 'success', 'closed' => 'secondary'];
                            $priorityBadges = ['low' => 'success', 'medium' => 'warning', 'high' => 'danger', 'urgent' => 'dark'];
                        @endphp
                        <span class="badge bg-{{ $statusBadges[$ticket->status] ?? 'secondary' }}">{{ str_replace('_', ' ', ucfirst($ticket->status)) }}</span>
                        <span class="badge bg-{{ $priorityBadges[$ticket->priority] ?? 'secondary' }}">{{ ucfirst($ticket->priority) }}</span>
                    </div>

                    <div class="card mb-4">
                        <div class="card-header">
                            <strong>Original Message</strong> <small class="text-muted">by {{ $ticket->user->name ?? $ticket->user->email }} on {{ $ticket->created_at->format('M d, Y h:i A') }}</small>
                        </div>
                        <div class="card-body">
                            {{ nl2br(e($ticket->message)) }}
                        </div>
                    </div>

                    <h5 class="mb-3">Conversation</h5>
                    <div class="mb-4">
                        @forelse($ticket->messages as $message)
                            <div class="card mb-2 {{ $message->is_admin ? 'border-primary' : '' }}">
                                <div class="card-header {{ $message->is_admin ? 'bg-primary text-white' : 'bg-light' }}">
                                    <strong>{{ $message->is_admin ? 'Admin' : ($message->user->name ?? $message->user->email) }}</strong>
                                    <small class="float-end">{{ $message->created_at->format('M d, Y h:i A') }}</small>
                                </div>
                                <div class="card-body">
                                    {{ nl2br(e($message->message)) }}
                                </div>
                            </div>
                        @empty
                            <p class="text-muted">No replies yet.</p>
                        @endforelse
                    </div>

                    @if($ticket->status !== 'closed' && $ticket->status !== 'resolved')
                    <div class="card">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Reply to Ticket</h5>
                        </div>
                        <div class="card-body">
                            <form action="{{ route('admin.support-tickets.reply', $ticket) }}" method="POST">
                                @csrf
                                <div class="mb-3">
                                    <label for="message" class="form-label">Your Reply</label>
                                    <textarea class="form-control @error('message') is-invalid @enderror" id="message" name="message" rows="4" required>{{ old('message') }}</textarea>
                                    @error('message')
                                        <div class="invalid-feedback">{{ $message }}</div>
                                    @enderror
                                </div>
                                <button type="submit" class="btn btn-primary">Send Reply</button>
                            </form>
                        </div>
                    </div>
                    @endif

                    <div class="card mt-3">
                        <div class="card-header">
                            <h5 class="card-title mb-0">Update Status</h5>
                        </div>
                        <div class="card-body">
                            <form action="{{ route('admin.support-tickets.update-status', $ticket) }}" method="POST">
                                @csrf
                                <div class="row">
                                    <div class="col-md-6 mb-3">
                                        <label for="status" class="form-label">Status</label>
                                        <select class="form-control" id="status" name="status">
                                            <option value="open" {{ $ticket->status == 'open' ? 'selected' : '' }}>Open</option>
                                            <option value="in_progress" {{ $ticket->status == 'in_progress' ? 'selected' : '' }}>In Progress</option>
                                            <option value="resolved" {{ $ticket->status == 'resolved' ? 'selected' : '' }}>Resolved</option>
                                            <option value="closed" {{ $ticket->status == 'closed' ? 'selected' : '' }}>Closed</option>
                                        </select>
                                    </div>
                                    <div class="col-md-6 mb-3">
                                        <label for="priority" class="form-label">Priority</label>
                                        <select class="form-control" id="priority" name="priority">
                                            <option value="low" {{ $ticket->priority == 'low' ? 'selected' : '' }}>Low</option>
                                            <option value="medium" {{ $ticket->priority == 'medium' ? 'selected' : '' }}>Medium</option>
                                            <option value="high" {{ $ticket->priority == 'high' ? 'selected' : '' }}>High</option>
                                            <option value="urgent" {{ $ticket->priority == 'urgent' ? 'selected' : '' }}>Urgent</option>
                                        </select>
                                    </div>
                                </div>
                                <button type="submit" class="btn btn-warning">Update Status</button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
