@extends('layouts.admin', ['heading' => 'Support Tickets', 'subheading' => 'User raised support and issue tracking'])

@section('content')
    <div class="admin-card p-4">
        <form method="GET" class="grid grid-cols-1 gap-2 md:grid-cols-4">
            <input name="q" value="{{ $search }}" placeholder="Search ticket, member, mobile, email" class="rounded border p-2 md:col-span-2">
            <select name="status" class="rounded border p-2">
                <option value="all" @selected($status === 'all')>All Status</option>
                <option value="new" @selected($status === 'new')>New</option>
                <option value="contacted" @selected($status === 'contacted')>Contacted</option>
                <option value="qualified" @selected($status === 'qualified')>Qualified</option>
                <option value="converted" @selected($status === 'converted')>Resolved</option>
                <option value="closed" @selected($status === 'closed')>Closed</option>
            </select>
            <button class="rounded bg-slate-900 px-4 py-2 text-white">Filter</button>
        </form>
    </div>

    <div class="mt-4 overflow-x-auto rounded-2xl border border-slate-200 bg-white shadow-sm">
        <table class="min-w-full text-sm">
            <thead class="bg-slate-100 text-left">
                <tr>
                    <th class="p-3">Ticket</th>
                    <th class="p-3">Member</th>
                    <th class="p-3">Subject</th>
                    <th class="p-3">Priority</th>
                    <th class="p-3">Status</th>
                    <th class="p-3">Created</th>
                    <th class="p-3">Action</th>
                </tr>
            </thead>
            <tbody>
                @forelse($tickets as $ticket)
                    @php $meta = is_array($ticket->meta) ? $ticket->meta : []; @endphp
                    <tr class="border-t align-top">
                        <td class="p-3">{{ $meta['ticket_code'] ?? ('TKT-'.$ticket->id) }}</td>
                        <td class="p-3">
                            <div class="font-semibold">{{ $ticket->name }}</div>
                            <div class="text-xs text-slate-500">{{ $ticket->mobile }} | {{ $ticket->email ?: '-' }}</div>
                        </td>
                        <td class="p-3">
                            <div class="font-semibold">{{ $meta['subject'] ?? 'Support Request' }}</div>
                            <div class="text-xs text-slate-500">{{ \Illuminate\Support\Str::limit($meta['message'] ?? $ticket->remarks, 100) }}</div>
                            @if(!empty($meta['admin_note']))
                                <div class="mt-1 rounded bg-slate-100 p-2 text-xs text-slate-600">Admin note: {{ $meta['admin_note'] }}</div>
                            @endif
                        </td>
                        <td class="p-3">{{ ucfirst((string) ($meta['priority'] ?? 'medium')) }}</td>
                        <td class="p-3">{{ ucfirst($ticket->status) }}</td>
                        <td class="p-3">{{ optional($ticket->created_at)->format('d M Y H:i') }}</td>
                        <td class="p-3">
                            <form method="POST" action="{{ route('admin.support.update', $ticket->id) }}" class="space-y-2">
                                @csrf
                                <select name="status" class="w-full rounded border p-1 text-xs">
                                    <option value="new" @selected($ticket->status === 'new')>New</option>
                                    <option value="contacted" @selected($ticket->status === 'contacted')>Contacted</option>
                                    <option value="qualified" @selected($ticket->status === 'qualified')>Qualified</option>
                                    <option value="converted" @selected($ticket->status === 'converted')>Resolved</option>
                                    <option value="closed" @selected($ticket->status === 'closed')>Closed</option>
                                </select>
                                <textarea name="admin_note" rows="2" placeholder="Admin note" class="w-full rounded border p-1 text-xs">{{ $meta['admin_note'] ?? '' }}</textarea>
                                <button class="rounded bg-indigo-600 px-2 py-1 text-xs text-white">Update</button>
                            </form>
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="7" class="p-4 text-center text-slate-500">No support tickets found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

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