@extends('admin.layouts.app')

@section('title', 'Үйлчилгээ')
@section('page_title', 'Үйлчилгээ')

@section('content')

<div class="flex items-center justify-between mb-6">
    <form method="GET" class="flex items-center gap-2">
        <input type="text" name="search" value="{{ request('search') }}" placeholder="Хайх..." class="px-4 py-2 border border-gray-200 rounded-lg outline-none focus:border-nsma-red">
        <select name="status" class="px-4 py-2 border border-gray-200 rounded-lg outline-none">
            <option value="">Бүх төлөв</option>
            <option value="active" @selected(request('status') === 'active')>Идэвхтэй</option>
            <option value="inactive" @selected(request('status') === 'inactive')>Идэвхгүй</option>
        </select>
        <button class="px-4 py-2 bg-nsma-dark text-white rounded-lg">Шүүх</button>
    </form>
    <a href="{{ route('admin.services.create') }}" class="bg-nsma-red text-white font-semibold px-4 py-2 rounded-lg hover:bg-red-600">+ Шинэ үйлчилгээ</a>
</div>

<div class="bg-white rounded-2xl border border-gray-100 overflow-hidden">
    <table class="w-full">
        <thead class="bg-gray-50 border-b border-gray-200">
            <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-right px-4 py-3 text-xs font-semibold uppercase">Үйлдэл</th>
            </tr>
        </thead>
        <tbody>
            @forelse($services as $s)
                <tr class="border-b border-gray-100 hover:bg-gray-50">
                    <td class="px-4 py-3 text-sm">{{ $s->sort_order }}</td>
                    <td class="px-4 py-3">
                        <div class="flex items-center gap-3">
                            @if($s->iconImageUrl())
                                <img src="{{ $s->iconImageUrl() }}" class="w-10 h-10 rounded-lg object-cover">
                            @endif
                            <div>
                                <div class="font-semibold">{{ $s->title }}</div>
                                <div class="text-xs text-gray-500">{{ $s->slug }}</div>
                            </div>
                        </div>
                    </td>
                    <td class="px-4 py-3 text-sm text-gray-600 max-w-xs truncate">{{ $s->short_description }}</td>
                    <td class="px-4 py-3"><input type="checkbox" {{ $s->show_on_home ? 'checked' : '' }} disabled class="rounded"></td>
                    <td class="px-4 py-3">
                        <button data-toggle="{{ $s->id }}" class="text-xs px-3 py-1 rounded-full {{ $s->is_active ? 'bg-green-100 text-green-700' : 'bg-gray-100 text-gray-600' }}">
                            {{ $s->is_active ? 'Идэвхтэй' : 'Идэвхгүй' }}
                        </button>
                    </td>
                    <td class="px-4 py-3 text-right">
                        <a href="{{ route('admin.services.edit', $s) }}" class="inline-block px-3 py-1 text-xs bg-nsma-dark text-white rounded">Засах</a>
                        <form action="{{ route('admin.services.destroy', $s) }}" method="POST" class="inline" onsubmit="return confirm('Устгах уу?')">
                            @csrf @method('DELETE')
                            <button class="px-3 py-1 text-xs bg-red-500 text-white rounded">Устгах</button>
                        </form>
                    </td>
                </tr>
            @empty
                <tr><td colspan="6" class="text-center py-12 text-gray-500">Үйлчилгээ нэмээгүй байна.</td></tr>
            @endforelse
        </tbody>
    </table>
</div>

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

@push('scripts')
<script>
    document.querySelectorAll('[data-toggle]').forEach(btn => {
        btn.addEventListener('click', async () => {
            const id = btn.dataset.toggle;
            const r = await window.ajaxJson(`/admin/services/${id}/toggle`, { method: 'POST' });
            if (r.ok) { window.showToast(r.data.message); location.reload(); }
        });
    });
</script>
@endpush

@endsection
