@extends('admin.layouts.app')

@section('title', 'Захиалгууд')
@section('page_title', 'Захиалгууд')

@section('content')

<div class="flex flex-wrap items-center gap-2 mb-6">
    @php
        $tabs = ['' => 'Бүгд', 'submitted' => 'Илгээгдсэн', 'reviewing' => 'Хянагдаж буй', 'in_progress' => 'Хийгдэж буй', 'completed' => 'Дууссан', 'cancelled' => 'Цуцлагдсан'];
        $countMap = ['' => 'all', 'submitted' => 'submitted', 'reviewing' => 'reviewing', 'in_progress' => 'in_progress', 'completed' => 'completed', 'cancelled' => 'cancelled'];
    @endphp
    @foreach($tabs as $key => $label)
        <a href="{{ route('admin.orders.index', array_filter(['status' => $key])) }}"
           class="px-4 py-2 rounded-lg text-sm font-semibold transition {{ request('status', '') === $key ? 'bg-nsma-red text-white' : 'bg-white border border-gray-200' }}">
            {{ $label }} ({{ $statusCounts[$countMap[$key]] ?? 0 }})
        </a>
    @endforeach
</div>

<form method="GET" class="flex items-center gap-2 mb-6">
    <input type="hidden" name="status" value="{{ request('status') }}">
    <input type="text" name="search" value="{{ request('search') }}" placeholder="Захиалгын дугаар, гарчиг..." class="px-4 py-2 border border-gray-200 rounded-lg outline-none w-72">
    <select name="priority" class="px-4 py-2 border border-gray-200 rounded-lg outline-none">
        <option value="">Бүх ач холбогдол</option>
        <option value="urgent" @selected(request('priority') === 'urgent')>Яаралтай</option>
        <option value="high" @selected(request('priority') === 'high')>Өндөр</option>
        <option value="normal" @selected(request('priority') === 'normal')>Хэвийн</option>
        <option value="low" @selected(request('priority') === 'low')>Бага</option>
    </select>
    <button class="px-4 py-2 bg-nsma-dark text-white rounded-lg">Шүүх</button>
</form>

<div class="bg-white rounded-2xl border border-gray-100 overflow-hidden">
    <table class="w-full">
        <thead class="bg-gray-50 border-b">
            <tr>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">№</th>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">Захиалга</th>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">Харилцагч</th>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">Үйлчилгээ</th>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">Эцсийн өдөр</th>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">Ач холбогдол</th>
                <th class="text-left px-4 py-3 text-xs font-semibold uppercase">Төлөв</th>
                <th class="text-right px-4 py-3 text-xs font-semibold uppercase"></th>
            </tr>
        </thead>
        <tbody>
            @forelse($orders as $o)
                <tr class="border-b border-gray-100 hover:bg-gray-50">
                    <td class="px-4 py-3 text-xs font-mono">{{ $o->order_number }}</td>
                    <td class="px-4 py-3">
                        <div class="font-semibold text-sm truncate max-w-xs">{{ $o->title }}</div>
                        <div class="text-xs text-gray-500">{{ $o->created_at->format('Y-m-d') }}</div>
                    </td>
                    <td class="px-4 py-3 text-sm">{{ $o->client->name ?? '-' }}</td>
                    <td class="px-4 py-3 text-sm">{{ $o->service->title ?? '-' }}</td>
                    <td class="px-4 py-3 text-sm">{{ $o->deadline?->format('Y-m-d') ?? '-' }}</td>
                    <td class="px-4 py-3 text-xs">
                        @php $pColors = ['urgent' => 'red', 'high' => 'orange', 'normal' => 'gray', 'low' => 'gray']; @endphp
                        <span class="px-2 py-1 rounded bg-{{ $pColors[$o->priority] ?? 'gray' }}-100 text-{{ $pColors[$o->priority] ?? 'gray' }}-700">{{ ucfirst($o->priority) }}</span>
                    </td>
                    <td class="px-4 py-3"><span class="text-xs px-2 py-1 rounded-full bg-{{ $o->statusColor() }}-100 text-{{ $o->statusColor() }}-700">{{ $o->statusLabel() }}</span></td>
                    <td class="px-4 py-3 text-right">
                        <a href="{{ route('admin.orders.show', $o) }}" class="px-3 py-1 text-xs bg-nsma-red text-white rounded">Үзэх</a>
                    </td>
                </tr>
            @empty
                <tr><td colspan="8" class="text-center py-12 text-gray-500">Захиалга байхгүй байна.</td></tr>
            @endforelse
        </tbody>
    </table>
</div>

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

@endsection
