@page
@model AsadiTools.Pages.Admin.Blog.AdminBlogIndexModel
@{ ViewData["Title"] = "مدیریت بلاگ"; Layout = "_AdminLayout"; }

<div class="p-6 md:p-8">
    <div class="flex items-center justify-between mb-8">
        <h1 class="text-2xl font-extrabold text-gray-900">مدیریت بلاگ</h1>
        <a href="/Admin/Blog/Create"
           class="bg-blue-700 text-white px-5 py-2.5 rounded-xl font-bold hover:bg-blue-800 transition-colors flex items-center gap-2">
            ✏️ نوشته جدید
        </a>
    </div>

    @if (!Model.Posts.Any())
    {
        <div class="text-center py-20 text-gray-400">
            <div class="text-5xl mb-4">📝</div>
            <p>هنوز مقاله‌ای ثبت نشده</p>
        </div>
    }
    else
    {
        <div class="bg-white rounded-2xl border border-gray-100 overflow-hidden">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 border-b">
                    <tr>
                        <th class="p-4 text-right font-medium text-gray-500">عنوان</th>
                        <th class="p-4 text-right font-medium text-gray-500 hidden md:table-cell">برچسب‌ها</th>
                        <th class="p-4 text-right font-medium text-gray-500">وضعیت</th>
                        <th class="p-4 text-right font-medium text-gray-500 hidden lg:table-cell">تاریخ</th>
                        <th class="p-4 text-right font-medium text-gray-500">عملیات</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-50">
                    @foreach (var post in Model.Posts)
                    {
                        <tr class="hover:bg-gray-50">
                            <td class="p-4">
                                <div class="font-medium text-gray-900 line-clamp-1">@post.Title</div>
                                <div class="text-xs text-gray-400 font-mono mt-0.5">@post.EffectiveSlug</div>
                            </td>
                            <td class="p-4 hidden md:table-cell">
                                <div class="flex flex-wrap gap-1">
                                    @foreach (var tag in post.TagList.Take(3))
                                    {
                                        <span class="text-xs bg-blue-50 text-blue-600 px-1.5 py-0.5 rounded">@tag</span>
                                    }
                                </div>
                            </td>
                            <td class="p-4">
                                <form method="post" asp-page-handler="TogglePublish" class="inline">
                                    @Html.AntiForgeryToken()
                                    <input type="hidden" name="id" value="@post.Id" />
                                    <button type="submit"
                                            class="text-xs px-2.5 py-1 rounded-full font-medium border transition-colors @(post.IsPublished ? "bg-green-100 text-green-700 border-green-200 hover:bg-green-200" : "bg-gray-100 text-gray-500 border-gray-200 hover:bg-gray-200")">
                                        @(post.IsPublished ? "✅ منتشر" : "⏸ پیش‌نویس")
                                    </button>
                                </form>
                            </td>
                            <td class="p-4 hidden lg:table-cell text-gray-400 text-xs">
                                @SiteData.ToJalali(post.CreatedAt)
                            </td>
                            <td class="p-4">
                                <div class="flex items-center gap-2">
                                    <a href="/Admin/Blog/Edit?id=@post.Id"
                                       class="text-blue-600 hover:underline text-xs font-medium">ویرایش</a>
                                    @if (post.IsPublished)
                                    {
                                        <a href="/blog/@post.EffectiveSlug" target="_blank"
                                           class="text-gray-400 hover:text-gray-600 text-xs">مشاهده</a>
                                    }
                                    <form method="post" asp-page-handler="Delete"
                                          onsubmit="return confirm('این مقاله حذف شود؟')" class="inline">
                                        @Html.AntiForgeryToken()
                                        <input type="hidden" name="id" value="@post.Id" />
                                        <button type="submit" class="text-red-400 hover:text-red-600 text-xs">حذف</button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    }
</div>
