@extends('admin.layouts.app')

@section('title', 'Мэдээ')
@section('page_title', 'Мэдээ')

@section('content')

<div class="flex items-center justify-between mb-6">
    <form method="GET">
        <input type="text" name="search" value="{{ request('search') }}" placeholder="Хайх..." class="px-4 py-2 border border-gray-200 rounded-lg outline-none">
    </form>
    <a href="{{ route('admin.news.create') }}" class="bg-nsma-red text-white font-semibold px-4 py-2 rounded-lg">+ Шинэ мэдээ</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">
            <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($articles as $a)
                <tr class="border-b border-gray-100">
                    <td class="px-4 py-3">
                        <div class="flex items-center gap-3">
                            @if($a->cover_image) <img src="{{ $a->coverImageUrl() }}" class="w-12 h-12 rounded object-cover"> @endif
                            <span class="font-semibold">{{ $a->title }}</span>
                        </div>
                    </td>
                    <td class="px-4 py-3 text-sm text-gray-600">{{ $a->category }}</td>
                    <td class="px-4 py-3 text-sm">{{ $a->author->name ?? '-' }}</td>
                    <td class="px-4 py-3 text-sm text-gray-500">{{ $a->published_at?->format('Y-m-d') ?? '-' }}</td>
                    <td class="px-4 py-3">
                        <span class="text-xs px-2 py-1 rounded {{ $a->is_published ? 'bg-green-100 text-green-700' : 'bg-yellow-100 text-yellow-700' }}">
                            {{ $a->is_published ? 'Нийтлэгдсэн' : 'Ноорог' }}
                        </span>
                    </td>
                    <td class="px-4 py-3 text-right">
                        <a href="{{ route('admin.news.edit', $a) }}" class="px-3 py-1 text-xs bg-nsma-dark text-white rounded">Засах</a>
                        <form action="{{ route('admin.news.destroy', $a) }}" 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">{{ $articles->links() }}</div>

@endsection
