@inject ISnackbar Snackbar
@inject IRestWrapper RestWrapper
@inject IUserUtility UserUtility
@inject IDialogService DialogService

<MudDialog DisableSidePadding="true" class="mx-auto">
    <DialogContent>
        <MudContainer class="max-h-[30rem]" Style="overflow-y: scroll">
            <MudStack>
                <MudDivider class="-mt-3" />
                <MudStack Spacing="0">
                    <MudText Typo="Typo.h6"><b>اطلاعات کلی</b></MudText>
                    <MudText Typo="Typo.caption">اطلاعات کلی روش ارسال را به دقت وارد کنید</MudText>
                </MudStack>

                <MudGrid>
                    <MudItem sm="12" md="6">
                        <MudTextField T="string" Label="نام" @bind-Value="@ViewModel.PageDto.Title" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>
                    <MudItem sm="12" md="6">
                        <MudTextField T="string" Label="آدرس صفحه" @bind-Value="@ViewModel.PageDto.Slug" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>

                    <MudItem sm="12" md="6">
                        <MudSelect T="bool" @bind-Value="@ViewModel.PageDto.Indexing" Label="ایا صفحه ایندکس شور ؟" ToStringFunc="b=>b.ToPersianString()" Variant="Variant.Outlined" AnchorOrigin="Origin.BottomCenter">
                            <MudSelectItem T="bool" Value="false" />
                            <MudSelectItem T="bool" Value="true" />
                        </MudSelect>
                    </MudItem>

                </MudGrid>

                <MudGrid>
                    <MudItem sm="12" md="6" lg="4">
                        <MudTextField T="string" Label="عنوان سکشن" @bind-Value="@ViewModel.Section.Title" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>
                    <MudItem sm="12" md="6" lg="4">
                        <MudTextField T="string" Label="عنوان دکمه" @bind-Value="@ViewModel.Section.CTAText" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>
                    <MudItem sm="12" md="6" lg="4">
                        <MudTextField T="string" Label="آدرس دکمه" @bind-Value="@ViewModel.Section.CTARoute" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>
                    <MudItem sm="12">
                        <MudTextField T="string" Label="توضیحاتــ" @bind-Value="@ViewModel.Section.Description" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>
                    <MudItem sm="12" md="4">

                        <MudSelect Variant="Variant.Outlined" T="BasePageSectionType"
                                   ToStringFunc="type => type.ToDisplay()"
                                   Label="نوع سکشن"
                                   @bind-Value="@ViewModel.Section.Type">
                            @foreach (var state in Enum.GetValues(typeof(BasePageSectionType)).Cast<BasePageSectionType>())
                            {
                                <MudSelectItem T="BasePageSectionType" Value="state"></MudSelectItem>
                            }
                        </MudSelect>
                    </MudItem>
                    <MudItem sm="12" md="6">
                        <MudTextField T="string" Label="کوئری" @bind-Value="@ViewModel.Section.Query" Variant="Variant.Outlined"></MudTextField>
                    </MudItem>
                    <MudItem sm="12" md="2">
                        <BaseButtonUi class="w-64 rounded-md mt-3" IsProcessing="@ViewModel.IsProcessing"
                                      Icon="@Icons.Material.Outlined.Check"
                                      Variant="Variant.Filled" Color="Color.Success"
                                      Content="افزودن" OnClickCallback="@ViewModel.AddSection" />
                    </MudItem>

                    <MudItem sm="12">

                        <MudDataGrid Items="@ViewModel.PageDto.Sections"
                                     T="BasePageSection"
                                     Elevation="0"
                                     Outlined="true"
                                     Bordered="true"
                                     Striped="true"
                                     Filterable="false"
                                     SortMode="@SortMode.None"
                                     Groupable="false">
                            <Columns>
                                <PropertyColumn T="BasePageSection" TProperty="string" Property="x => x.Title" Title="عنوان" />
                                <PropertyColumn T="BasePageSection" TProperty="string" Property="x => x.CTAText" Title="عنوان دکمه" />
                                <PropertyColumn T="BasePageSection" TProperty="string" Property="x => x.CTARoute" Title="ادرس دکمه" />
                                <PropertyColumn T="BasePageSection" TProperty="BasePageSectionType" Property="x => x.Type" Title="نوع سکشن" />
                                <PropertyColumn T="BasePageSection" TProperty="string" Property="x => x.Query" Title="کوئری" />
                                <TemplateColumn T="BasePageSection" CellClass="d-flex justify-end">
                                    <CellTemplate>
                                        <MudStack Row>
                                            <MudButton DisableElevation="true"
                                                       Size="@Size.Small"
                                                       Variant="@Variant.Filled"
                                                       OnClick="()=>ViewModel.PageDto.Sections.Remove(context.Item)"
                                                       Color="@Color.Error"
                                                       StartIcon="@Icons.Material.Outlined.Delete">حذف</MudButton>
                                        </MudStack>
                                    </CellTemplate>
                                </TemplateColumn>
                            </Columns>
                        </MudDataGrid>
                    </MudItem>
                </MudGrid>
            </MudStack>
        </MudContainer>
    </DialogContent>
    <DialogActions>
        <MudStack Row="true" class="w-full mx-4 mb-2">

            @if (ViewModel.IsEditing)
            {
                <BaseButtonUi class="w-64 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Check"
                              Variant="Variant.Filled" Color="Color.Success"
                              Content="ثبت ویرایش" OnClickCallback="@ViewModel.SubmitEditAsync" />
            }
            else
            {
                <BaseButtonUi class="w-64 rounded-md" IsProcessing="@ViewModel.IsProcessing"
                              Icon="@Icons.Material.Outlined.Check"
                              Variant="Variant.Filled" Color="Color.Success"
                              Content="تایید" OnClickCallback="@ViewModel.SubmitCreateAsync" />
            }
            <MudSpacer />
            <MudButton Variant="Variant.Outlined" Size="Size.Large" Color="Color.Error" OnClick="@ViewModel.Cancel">بستن</MudButton>
        </MudStack>
    </DialogActions>
</MudDialog>
@code {

    [CascadingParameter]
    MudDialogInstance MudDialog { get; set; }

    [Parameter]
    public BasePageSDto? Page { get; set; }

    public PageActionDialogBoxViewModel ViewModel { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (Page == null)
            ViewModel = new PageActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog);
        else
            ViewModel = new PageActionDialogBoxViewModel(Snackbar, RestWrapper, UserUtility, DialogService, MudDialog, Page);
        await ViewModel.InitializeAsync();
        await base.OnInitializedAsync();
    }
}