@page "/product/category/edit"
@page "/product/category/edit/{Id:long?}"

@inject ILogger<CategoryEdit> Logger
@* @attribute [Authorize] *@

<PageTitle>分类编辑</PageTitle>
<Title Level="4">分类信息</Title>

<Spin Spinning="pageLoading">
    <Card Title="" Class="hideborder"
          Style="margin-top: 24px;"
          BodyStyle="padding: 0 32px 40px 32px">
        <Form @ref="editform" Model="@model" LabelColSpan="5" WrapperColSpan="19" OnFinish="OnFormFinishAsync">
            @* @if (languages.Count > 1 && Id > 0)
                    {
                        <Tabs ActiveKey="@context.Language" OnTabClick="OnLanguageTabChange">
                            <TabPane Key="0">
                                <TabTemplate>
                                    <span>标准</span>
                                </TabTemplate>
                            </TabPane>
                            @foreach (var item in languages)
                            {
                                <TabPane Key="@item.Id.ToString()">
                                    <TabTemplate>
                                        <span>@item.Name @item.Id.ToString()</span>
                                    </TabTemplate>
                                </TabPane>
                            }
                        </Tabs>
                    }
                    <FormItem Label="上级菜单">
                        <SimpleSelect DefaultValue="" Style="width:300px;" @bind-Value="@context.ParentId">
                            <SelectOptions>
                                <SimpleSelectOption Value="0" Label="无上级"></SimpleSelectOption>
                                @foreach (var item in categories)
                                {
                                    <SimpleSelectOption Value="@item.Id.ToString()" Label="@item.Name"></SimpleSelectOption>
                                }
                            </SelectOptions>
                        </SimpleSelect>
                    </FormItem>
                    <FormItem Label="分类名称" Required>
                        <Input @bind-Value="@context.Name" Placeholder="分类名称" />
                    </FormItem>
                    <FormItem Label="URL缩略名" Required>
                        <Input @bind-Value="@context.Slug" Placeholder="URL缩略名" />
                    </FormItem>
                    <FormItem Label="图标">
                        <Button OnClick="HandleSetIcon">
                            @if (string.IsNullOrEmpty(formModel.Image))
                            {
                                <span>选择图标</span>
                            }
                            else
                            {
                                <span>已选图标：</span>
                            }
                        </Button> <Button Size="@ButtonSize.Large" Type="@ButtonType.Text" Icon="@formModel.Image"></Button>
                    </FormItem> *@
            <FormItem Label="Meta描述">
                <Input @bind-Value="@context.MetaDescription" Placeholder="Meta描述" />
            </FormItem>
            <FormItem Label="Meta关键词">
                <Input @bind-Value="@context.MetaKeywords" Placeholder="Meta关键词" />
            </FormItem>
            <FormItem Label="显示排序">
                <Input @bind-Value="@context.DisplayOrder" Placeholder="显示排序" />
            </FormItem>
            <FormItem Label="状态">
                <Checkbox @bind-Checked="@context.Enabled">启用</Checkbox>
            </FormItem>
            <FormItem WrapperCol="new ColLayoutParam { Span = 24, Offset = 5 }">
                <Button Type="@ButtonType.Primary" HtmlType="submit">
                    提交保存
                </Button>
            </FormItem>
        </Form>
    </Card>
</Spin>

@code {
    [SupplyParameterFromQuery]
    int? Lang { get; set; }

    [Parameter]
    public long? Id { get; set; }

    [SupplyParameterFromForm]
    CategoryModel model { get; set; } = new();
    Form<CategoryModel> editform = null!;

    bool pageLoading = true;
    List<CategoryItem> Categories = new();

    protected override void OnInitialized()
    {
        base.OnInitialized();
    }

    protected override async Task OnParametersSetAsync()
    {
        pageLoading = true;
        // await LoadCategories();
        // if (Id > 0)
        // {
        //     await LoadData();
        //     await SearchAttribute(string.Empty);
        // }
        // else
        // {
        //     editCategory = true;
        //     CategoryPath = string.Empty;
        // }
        // if (Categories.Any())
        // {
        //     BindCategory();
        // }
        pageLoading = false;
        base.OnParametersSet();
    }

    async Task LoadCategories()
    {
        var url = $"/api/category/items";
        var apiResult = await HttpService.Get<ApiResult<List<CategoryItem>>>(url);
        if (apiResult.Success)
        {
            Categories = apiResult.Data ?? new();
        }
    }

    async void OnFormFinishAsync()
    {
    }
}
