@page "/products"
@attribute [Microsoft.AspNetCore.Authorization.Authorize]

@inject IDialogService DialogService
@inject NavigationManager NavigationManager
@inject ISnackbar Snackbar
@inject IUserUtility UserUtility
@inject IRestWrapper RestWrapper
@inject IBrowserViewportService BrowserViewportService

<MudStack class="h-full w-full p-8">
    <MudGrid>
        <MudItem xs="12">
            <MudStack Row="@ViewModel.IsXs.Not()" class="mb-5">
                <MudStack Row="true">
                    <MudText Typo="Typo.h4">محصولاتــــ</MudText>
                    <MudChip Color="Color.Info" Variant="Variant.Outlined">@ViewModel.TotalItems عدد</MudChip>
                </MudStack>
                @if (@ViewModel.IsXs.Not())
                {
                    <MudSpacer />
                }
                <MudStack Row="@ViewModel.IsXs.Not()">
                    <MudButton Variant="Variant.Filled"
                               DisableElevation="true"
                               StartIcon="@Icons.Material.Outlined.Add"
                               Color="Color.Secondary"
                               OnClick="@ViewModel.AddProductClicked"
                               class="w-full md:my-auto md:w-auto">افزودن محصول</MudButton>
                    <MudButton Variant="Variant.Outlined"
                               DisableElevation="true"
                               StartIcon="@Icons.Material.Outlined.Add"
                               Color="Color.Info"
                               OnClick="@ViewModel.AddFastProductClicked"
                               class="w-full md:my-auto md:w-auto">افزودن سریع محصول</MudButton>
                    <MudButton Variant="Variant.Outlined"
                               DisableElevation="true"
                               StartIcon="@Icons.Material.Outlined.Add"
                               Color="Color.Error"
                               OnClick="@ViewModel.AddDigikalaProductClicked"
                               class="w-full md:my-auto md:w-auto">افزودن محصول از دیجیکالا</MudButton>
                </MudStack>
            </MudStack>
            <MudHidden Breakpoint="Breakpoint.SmAndUp">
                <MudGrid class="mb-3" Row="true">

                    <MudItem xs="12" sm="6">
                        <MudTextField T="string" Placeholder="جست جو بر اساس نام" Adornment="Adornment.Start" 
                                      Immediate="true"
                                      Clearable="true"
                                      @bind-Value="@ViewModel.Search"
                                      AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" class="my-auto"
                                      OnAdornmentClick="@ViewModel.GetEntitiesAsync"></MudTextField>
                    </MudItem>

                    <MudItem xs="12" sm="6">
                        <MudAutocomplete class="-mt-0.5" Required="true" ToStringFunc="dto => dto.Name"
                                         T="ProductCategorySDto"
                                         Label="بر اساس دسته بندی"
                                         OnClearButtonClick="ViewModel.ClearProductCategorySearch"
                                         SearchFunc="ViewModel.SearchProductCategory"
                                         ValueChanged="ViewModel.ProductCategorySelected"
                                         Clearable="true">
                            <ProgressIndicatorInPopoverTemplate>
                                <MudList Clickable="false">
                                    <MudListItem>
                                        <div class="mx-auto flex w-full flex-row">
                                            <MudProgressCircular class="my-auto -ml-4 mr-1" Size="Size.Small" Indeterminate="true" />
                                            <p class="text-md mx-auto my-1 font-bold">منتظر بمانید</p>
                                        </div>
                                    </MudListItem>
                                </MudList>
                            </ProgressIndicatorInPopoverTemplate>
                            <ItemTemplate Context="e">
                                <p>@e.Name</p>
                            </ItemTemplate>
                        </MudAutocomplete>
                    </MudItem>
                </MudGrid>
            </MudHidden>
            <MudPaper>
                <MudDataGrid FixedFooter="true" FixedHeader="true" Striped="true"
                             T="ProductSDto" Items="@ViewModel.PageDto" CurrentPage="@ViewModel.CurrentPage"
                             RowsPerPage="20" Filterable="false" Loading="@ViewModel.IsProcessing"
                             SortMode="@SortMode.None" Groupable="false">

                    <ToolBarContent>
                        <MudGrid class="collapse md:visible">

                            <MudItem xs="12" sm="4">
                                <MudTextField T="string" Placeholder="جست جو بر اساس نام" Adornment="Adornment.Start" 
                                              Immediate="true"
                                              Clearable="true"
                                              @bind-Value="@ViewModel.Search"
                                              AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" class="my-auto"
                                              OnAdornmentClick="@ViewModel.GetEntitiesAsync"></MudTextField>
                            </MudItem>

                            <MudItem xs="12" sm="6">
                                <MudAutocomplete class="-mt-0.5" Required="true" ToStringFunc="dto => dto.Name"
                                                 T="ProductCategorySDto"
                                                 Label="بر اساس دسته بندی"
                                                 OnClearButtonClick="ViewModel.ClearProductCategorySearch"
                                                 SearchFunc="ViewModel.SearchProductCategory"
                                                 ValueChanged="ViewModel.ProductCategorySelected"
                                                 Clearable="true">
                                    <ProgressIndicatorInPopoverTemplate>
                                        <MudList Clickable="false">
                                            <MudListItem>
                                                <div class="mx-auto flex w-full flex-row">
                                                    <MudProgressCircular class="my-auto -ml-4 mr-1" Size="Size.Small" Indeterminate="true" />
                                                    <p class="text-md mx-auto my-1 font-bold">منتظر بمانید</p>
                                                </div>
                                            </MudListItem>
                                        </MudList>
                                    </ProgressIndicatorInPopoverTemplate>
                                    <ItemTemplate Context="e">
                                        <p>@e.Name</p>
                                    </ItemTemplate>
                                </MudAutocomplete>
                            </MudItem>
                            <MudItem xs="12" sm="2">
                                <MudSwitch class="mt-3" T="bool"
                                           Value="ViewModel.IsEnable"
                                           ValueChanged="async(flag)=>await ViewModel.ProductEnableChanged(flag)"
                                           Label="فقط محصولات موجود" Color="Color.Info" />
                            </MudItem>
                        </MudGrid>
                    </ToolBarContent>
                    <Columns>

                        <TemplateColumn T="ProductSDto" Title="نمایش">
                            <CellTemplate>
                                <MudCheckBox ValueChanged="delegate(bool flag) { context.Item.BeDisplayed = flag; ViewModel.DisplayedChanged(context.Item); }" Color="Color.Secondary" Value="@context.Item.BeDisplayed"></MudCheckBox>
                            </CellTemplate>
                        </TemplateColumn>
                        <PropertyColumn Title="نام محصول" Property="arg => arg.PersianName" />
                        <PropertyColumn Title="دسته بندی" Property="arg => arg.CategoryName" />
                        <TemplateColumn T="ProductSDto" Title="پیشنهاد ویژه است">
                            <CellTemplate>
                                @if (@context.Item.IsSpecial)
                                {
                                    <p>بلی</p>
                                }
                                else
                                {
                                    <p>خیر</p>

                                }
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="ProductSDto" Title="موجود است">
                            <CellTemplate>
                                @if (@context.Item.IsEnable)
                                {
                                    <p>بلی</p>
                                }
                                else
                                {
                                    <p>خیر</p>

                                }
                            </CellTemplate>
                        </TemplateColumn>

                        <TemplateColumn T="ProductSDto" Title="قیمتــ">
                            <CellTemplate>
                                <MudTextField class="-mt-2"
                                Format="N0"
                                T="double"
                                ValueChanged="async(cost) => await ViewModel.ChangeProductCostAsync(context.Item,cost)"
                                AdornmentText="ریالــ"
                                Immediate="false"
                                AdornmentIcon="@Icons.Material.Filled.Check"
                                Adornment="Adornment.End"
                                Value="context.Item.Cost"></MudTextField>
                            </CellTemplate>
                        </TemplateColumn>
                        <TemplateColumn CellClass="d-flex justify-end">
                            <CellTemplate>
                                <MudStack Row="true">
                                    <MudIconButton Icon="@Icons.Material.Filled.Edit"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   Color="@Color.Info"
                                                   OnClick="async()=>await ViewModel.EditProductClicked(context.Item)" />
                                    <MudIconButton Icon="@Icons.Material.Filled.Delete"
                                                   Size="@Size.Small"
                                                   Variant="@Variant.Outlined"
                                                   OnClick="async () => await ViewModel.DeleteProductAsync(context.Item.Id)"
                                                   Color="@Color.Error" />
                                </MudStack>
                            </CellTemplate>
                        </TemplateColumn>
                    </Columns>
                    <PagerContent>
                        <MudStack Row="true" class="w-full">

                            <MudPagination Rectangular="true" Variant="Variant.Filled" Count="@ViewModel.PageCount"
                                           SelectedChanged="@ViewModel.ChangePageAsync" class="mx-auto my-4" />
                        </MudStack>

                    </PagerContent>
                </MudDataGrid>
            </MudPaper>
        </MudItem>
    </MudGrid>
</MudStack>

@code
{
    public ProductsPageViewModel ViewModel { get; set; }
    protected override async Task OnInitializedAsync()
    {
        ViewModel = new ProductsPageViewModel(NavigationManager, Snackbar, UserUtility, RestWrapper, DialogService, BrowserViewportService);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}
