<x-admin-layout title="Support Tickets">
    <div class="card">
        <div class="card-body">
            <h5 class="fw-bold mb-3">Support Tickets</h5>

            @if ($tickets->isEmpty())
                <div class="text-center text-muted py-5">
                    <i class="bi bi-life-preserver fs-1 d-block mb-2"></i>
                    No support tickets yet.
                </div>
            @else
                <div class="table-responsive">
                    <table class="table align-middle datatable">
                        <thead>
                            <tr>
                                <th>From</th>
                                <th>Company</th>
                                <th>Subject</th>
                                <th>Message</th>
                                <th>Status</th>
                                <th>Received</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach ($tickets as $ticket)
                                <tr>
                                    <td class="fw-semibold">{{ $ticket->user->name ?? '—' }}</td>
                                    <td>{{ $ticket->company->name ?? '— (Super Admin)' }}</td>
                                    <td>{{ $ticket->subject }}</td>
                                    <td style="max-width:280px;">
                                        <div class="text-truncate" title="{{ $ticket->message }}">{{ $ticket->message }}</div>
                                    </td>
                                    <td>
                                        <form method="POST" action="{{ route('admin.support-tickets.update-status', $ticket) }}">
                                            @csrf @method('PATCH')
                                            @php $color = $ticket->status === 'resolved' ? 'is-green' : 'is-amber'; @endphp
                                            <select name="status" onchange="this.form.requestSubmit()" class="form-select form-select-sm badge {{ $color }} border-0">
                                                <option value="open" @selected($ticket->status === 'open')>Open</option>
                                                <option value="resolved" @selected($ticket->status === 'resolved')>Resolved</option>
                                            </select>
                                        </form>
                                    </td>
                                    <td class="text-nowrap text-muted small">{{ $ticket->created_at->format('d M Y, H:i') }}</td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            @endif
        </div>
    </div>
</x-admin-layout>
