@* Merged into /blog *@
@attribute [Authorize(Roles = "Administrator")]

@using BackOffice.Services.Blog

@inject IBlogPostService BlogPostService
@inject IBlogCategoryService BlogCategoryService

<div>
    <MudStack Spacing="2">
        <MudStack Row="true" Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center">
            <MudText Typo="Typo.h5">مدیریت پست‌های بلاگ</MudText>
            <MudButton Variant="Variant.Filled"
                       Color="Color.Primary"
                       StartIcon="@Icons.Material.Filled.Add"
                       OnClick="CreatePost">
                افزودن پست
            </MudButton>
        </MudStack>

        <MudStack Row="true" Spacing="2" AlignItems="AlignItems.Center">
            <MudTextField @bind-Value="_search"
                          Label="جستجو بر اساس عنوان"
                          Variant="Variant.Outlined"
                          Margin="Margin.Dense"
                          Adornment="Adornment.Start"
                          AdornmentIcon="@Icons.Material.Filled.Search"
                          Immediate="true"
                          OnBlur="ReloadAsync"
                          OnKeyDown="OnSearchKeyDown" />

            <MudSelect @bind-Value="_statusFilter"
                       Label="وضعیت"
                       Variant="Variant.Outlined"
                       Margin="Margin.Dense"
                       Style="width: 160px;">
                <MudSelectItem T="int?" Value="@( (int?)null )">همه</MudSelectItem>
                <MudSelectItem T="int?" Value="0">پیش‌نویس</MudSelectItem>
                <MudSelectItem T="int?" Value="1">منتشر شده</MudSelectItem>
                <MudSelectItem T="int?" Value="2">آرشیو شده</MudSelectItem>
            </MudSelect>

            <MudSelect @bind-Value="_categoryFilter"
                       Label="دسته‌بندی"
                       Variant="Variant.Outlined"
                       Margin="Margin.Dense"
                       Style="width: 200px;">
                <MudSelectItem T="long?" Value="@( (long?)null )">همه</MudSelectItem>
                @foreach (var cat in _categories)
                {
                    <MudSelectItem T="long?" Value="@( (long?)cat.Id )">@cat.Title</MudSelectItem>
                }
            </MudSelect>

            <MudButton Variant="Variant.Outlined"
                       Color="Color.Primary"
                       OnClick="ReloadAsync">
                اعمال فیلتر
            </MudButton>
        </MudStack>

        <MudDataGrid T="BlogPostListItemDto"
                     @ref="_grid"
                     ServerData="LoadServerData"
                     Hover="true"
                     Elevation="1"
                     Dense="true"
                     Height="calc(100vh - 240px)">
            <Columns>
                <PropertyColumn Property="x => x.Id" Title="شناسه" />
                <PropertyColumn Property="x => x.Title" Title="عنوان" />
                <PropertyColumn Property="x => x.Slug" Title="اسلاگ" />
                <PropertyColumn Property="x => x.StatusName" Title="وضعیت">
                    <CellTemplate>
                        <MudChip Color="@GetStatusColor(context.Item.Status)"
                                 Size="Size.Small">
                            @context.Item.StatusName
                        </MudChip>
                    </CellTemplate>
                </PropertyColumn>
                <PropertyColumn Property="x => x.IsFeatured" Title="ویژه">
                    <CellTemplate>
                        @if (context.Item.IsFeatured)
                        {
                            <MudIcon Icon="@Icons.Material.Filled.Star" Color="Color.Warning" Size="Size.Small" />
                        }
                    </CellTemplate>
                </PropertyColumn>
                <PropertyColumn Property="x => x.ViewCount" Title="بازدید" />
                <TemplateColumn Title="دسته‌بندی‌ها" Sortable="false">
                    <CellTemplate>
                        <MudStack Row="true" Spacing="1">
                            @foreach (var cat in context.Item.Categories)
                            {
                                <MudChip Size="Size.Small" Color="Color.Info">@cat.Title</MudChip>
                            }
                        </MudStack>
                    </CellTemplate>
                </TemplateColumn>
                <TemplateColumn Title="تاریخ ایجاد">
                    <CellTemplate>
                        @context.Item.Created.MiladiToJalali()
                    </CellTemplate>
                </TemplateColumn>
                <TemplateColumn Title="عملیات" Sortable="false">
                    <CellTemplate>
                        <MudStack Row="true" Spacing="1">
                            @if (context.Item.Status == 0)
                            {
                                <MudTooltip Text="انتشار">
                                    <MudIconButton Icon="@Icons.Material.Filled.Publish"
                                                   Size="Size.Small"
                                                   Color="Color.Success"
                                                   OnClick="@(() => PublishPost(context.Item))" />
                                </MudTooltip>
                            }
                            @if (context.Item.Status == 1)
                            {
                                <MudTooltip Text="آرشیو">
                                    <MudIconButton Icon="@Icons.Material.Filled.Archive"
                                                   Size="Size.Small"
                                                   Color="Color.Warning"
                                                   OnClick="@(() => ArchivePost(context.Item))" />
                                </MudTooltip>
                            }
                            <MudTooltip Text="ویرایش">
                                <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                               Size="Size.Small"
                                               Color="Color.Primary"
                                               OnClick="@(() => EditPost(context.Item))" />
                            </MudTooltip>
                            <MudTooltip Text="حذف">
                                <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                               Size="Size.Small"
                                               Color="Color.Error"
                                               OnClick="@(() => DeletePost(context.Item))" />
                            </MudTooltip>
                        </MudStack>
                    </CellTemplate>
                </TemplateColumn>
            </Columns>
            <NoRecordsContent>
                <MudAlert Severity="Severity.Info" Dense="true" Variant="Variant.Text" Class="my-4">موردی یافت نشد.</MudAlert>
            </NoRecordsContent>
            <PagerContent>
                <MudDataGridPager T="BlogPostListItemDto" PageSizeOptions="@(new int[] { 20, 50, 100 })"
                                  InfoFormat="سطر {first_item} تا {last_item} از {all_items}"
                                  RowsPerPageString="تعداد در صفحه" />
            </PagerContent>
        </MudDataGrid>
    </MudStack>
</div>
