@extends('admin.layouts.app')
@section('title', 'الأخبار')
@section('page-title', 'إدارة الأخبار')

@section('content')

<div class="d-flex justify-content-between align-items-center mb-3">
    <form method="GET" class="d-flex gap-2">
        <input type="text" name="search" value="{{ request('search') }}"
               class="form-control form-control-sm" placeholder="بحث...">
        <select name="category" class="form-select form-select-sm" style="width:160px">
            <option value="">كل التصنيفات</option>
            <option value="news"  {{ request('category')=='news'  ? 'selected':'' }}>أخبار طبية</option>
            <option value="tips"  {{ request('category')=='tips'  ? 'selected':'' }}>نصائح صحية</option>
            <option value="research" {{ request('category')=='research' ? 'selected':'' }}>أبحاث</option>
        </select>
        <button class="btn btn-sm btn-outline-primary px-3">
            <i class="bi bi-search"></i>
        </button>
    </form>
    <a href="{{ route('admin.news.create') }}" class="btn btn-primary btn-sm">
        <i class="bi bi-plus-lg me-1"></i> خبر جديد
    </a>
</div>

<div class="card table-card">
    <div class="table-responsive">
        <table class="table mb-0">
            <thead>
                <tr>
                    <th style="width:80px">الصورة</th>
                    <th>العنوان</th>
                    <th>التصنيف</th>
                    <th>المشاهدات</th>
                    <th>الحالة</th>
                    <th>تاريخ النشر</th>
                    <th>إجراءات</th>
                </tr>
            </thead>
            <tbody>
                @forelse($news as $item)
                <tr>
                    <td>
                        @if($item->cover_image)
                            <img src="{{ asset('storage/'.$item->cover_image) }}"
                                 style="width:64px;height:44px;object-fit:cover;border-radius:6px">
                        @else
                            <div class="bg-light d-flex align-items-center justify-content-center"
                                 style="width:64px;height:44px;border-radius:6px">
                                <i class="bi bi-image text-muted"></i>
                            </div>
                        @endif
                    </td>
                    <td>
                        <div class="fw-semibold" style="max-width:280px">
                            {{ Str::limit($item->title, 60) }}
                        </div>
                        @if($item->source)
                            <small class="text-muted">المصدر: {{ $item->source }}</small>
                        @endif
                    </td>
                    <td>
                        @php
                            $cats = ['news'=>'أخبار','tips'=>'نصائح','research'=>'أبحاث'];
                        @endphp
                        <span class="badge bg-light text-dark">
                            {{ $cats[$item->category] ?? $item->category ?? '—' }}
                        </span>
                    </td>
                    <td>
                        <i class="bi bi-eye text-muted me-1"></i>{{ number_format($item->views) }}
                    </td>
                    <td>
                        @if($item->is_published)
                            <span class="badge badge-active">منشور</span>
                        @else
                            <span class="badge badge-pending">مسودة</span>
                        @endif
                    </td>
                    <td>
                        <small class="text-muted">
                            {{ $item->published_at ? $item->published_at->format('Y/m/d') : '—' }}
                        </small>
                    </td>
                    <td>
                        <div class="d-flex gap-1">
                            <a href="{{ route('admin.news.edit', $item->id) }}"
                               class="btn btn-sm btn-outline-primary" title="تعديل">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <form method="POST" action="{{ route('admin.news.publish', $item->id) }}">
                                @csrf @method('PATCH')
                                <button class="btn btn-sm btn-{{ $item->is_published ? 'outline-warning' : 'outline-success' }}"
                                        title="{{ $item->is_published ? 'إلغاء النشر' : 'نشر' }}">
                                    <i class="bi bi-{{ $item->is_published ? 'eye-slash' : 'send' }}"></i>
                                </button>
                            </form>
                            <form method="POST" action="{{ route('admin.news.destroy', $item->id) }}">
                                @csrf @method('DELETE')
                                <button class="btn btn-sm btn-outline-danger"
                                        onclick="return confirm('حذف الخبر؟')">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7" class="text-center text-muted py-5">
                        <i class="bi bi-newspaper fs-2 d-block mb-2"></i>
                        لا توجد أخبار بعد
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
    @if($news->hasPages())
    <div class="card-footer bg-transparent d-flex justify-content-between align-items-center">
        <small class="text-muted">
            عرض {{ $news->firstItem() }}–{{ $news->lastItem() }} من {{ $news->total() }}
        </small>
        {{ $news->withQueryString()->links('pagination::bootstrap-5') }}
    </div>
    @endif
</div>

@endsection
