@page  "/StaffGrade"

@inject HttpClient Http
@using AntDesign.TableModels
@inject MessageService _message
@using System.ComponentModel
@using wispro.sp.share

@if (staffGrades == null)
{
    <center><Spin /></center>

}
else
{
    <div>
        <Breadcrumb>
            <BreadcrumbItem>
                <Icon Type="home"></Icon>
            </BreadcrumbItem>
            <BreadcrumbItem>
                <a><Icon Type="user"></Icon><span>代理人系数</span></a>
            </BreadcrumbItem>
        </Breadcrumb>
    </div>
    <p />
    <AntDesign.Table @ref="table" 
                     TItem="wispro.sp.entity.StaffGrade"
                     Loading="_loading"
                     DataSource="@staffGrades"
                     Total="_total"
                     @bind-SelectedRows="selectedRows"
                     OnRow="OnRow"
                     Bordered=@true
                     Size=@TableSize.Middle>
        <ChildContent>
            <Selection Key="@(context.Id.ToString())" />
            <AntDesign.Column Title="序号" TData="int" @bind-Field="@context.Id">

            </AntDesign.Column>
            <AntDesign.Column Title="代理人等级" @bind-Field="@context.Grade" Sortable Filterable />
            <AntDesign.Column Title="代理人系数" @bind-Field="@context.Coefficient" Sortable Filterable />
            
            <ActionColumn>
                <Space>
                    <SpaceItem><Button Danger OnClick="()=>Edit(context.Id)">编辑</Button></SpaceItem>
                </Space>
            </ActionColumn>
        </ChildContent>
        
    </AntDesign.Table>

}


<Modal Title="修改"
       Visible="@_visible"
       OnOk="@HandleOk"
       OnCancel="@HandleCancel">

    <Form Model="EditingStaff" LabelColSpan="6"
          WrapperColSpan="16">
        @if (_isAdd) { 
            <FormItem Label="代理人等级">
                <Input @bind-Value="@context.Grade" />
            </FormItem>
        }
        else
        {
            <FormItem Label="代理人等级">
                <span>@context.Grade</span>
            </FormItem>
        }
        
        <FormItem Label="等级系数">
            <Input @bind-Value="@context.Coefficient" />
        </FormItem>

    </Form>

</Modal>

<style>
    .my-custom-pagination {
        margin: 15px 0;
    }

        .my-custom-pagination .ant-pagination-item,
        .my-custom-pagination .ant-pagination-item-link {
            border-radius: 100%;
        }
</style>

@using System.Text.Json;
@code {
    private List<wispro.sp.entity.StaffGrade> staffGrades;
    IEnumerable<wispro.sp.entity.StaffGrade> selectedRows;
    ITable table;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;
    bool _loading = false;

    wispro.sp.entity.StaffGrade EditingStaff = null;
    bool _visible = false;
    bool _isAdd = false;

    protected override async Task OnInitializedAsync()
    {

        _loading = true;

        List<wispro.sp.entity.StaffGrade> data = await Http.GetFromJsonAsync<List<wispro.sp.entity.StaffGrade>>($"http://localhost:39476/api/StaffGrade/GetAll");

        _loading = false;
        staffGrades = data;
        _total = data.Count;

    }

    private void HandlePageChange(PaginationEventArgs args)
    {
        if (_pageIndex != args.Page)
        {
            _pageIndex = args.Page;
        }

        if (_pageSize != args.PageSize)
        {
            _pageSize = args.PageSize;
        }
    }


    Dictionary<string, object> OnRow(RowData<wispro.sp.entity.StaffGrade> row)
    {
        Dictionary<string, object> ret = new Dictionary<string, object>();

        ret.Add("id", row.Data.Id);
        ret.Add("onclick", ((Action)delegate
        {
            //_message.Info($"row {row.Data.Grade} was clicked");
        }));


        return ret;
    }

    public void RemoveSelection(int id)
    {
        //var selected = selectedRows.Where(x => x.Id != id);
        //selectedRows = selected;
    }

    private void Delete(int id)
    {
        var rList = staffGrades.Where(x => x.Id == id).ToList();

        if (rList.Count() > 0)
        {
            EditingStaff = rList[0];

            _visible = true;
        }
        else
        {

        }
        _total = staffGrades.Count;
    }

    private void Edit(int id)
    {
        var rList = staffGrades.Where(x => x.Id == id).ToList();

        if (rList.Count() > 0)
        {
            EditingStaff = rList[0];

            _visible = true;
        }
        else
        {

        }
        _total = staffGrades.Count;
    }

    private async Task HandleOk(MouseEventArgs e)
    {

        var data = await Http.PostAsJsonAsync<wispro.sp.entity.StaffGrade>($"http://localhost:39476/api/StaffGrade/Save", EditingStaff);

        if (data.IsSuccessStatusCode)
        {
            ApiSaveResponse result = await data.Content.ReadFromJsonAsync<ApiSaveResponse>();
            await Task.Delay(1000);

            if (result.Success)
            {
                _message.Success("数据已保存!");
            }
            else
            {
                _message.Error($"{result.ErrorMessage}");
            }

        }
        else
        {
            _message.Error($"请求发生错误 {data.StatusCode}");
        }

        _visible = false;
    }

    private void HandleCancel(MouseEventArgs e)
    {
        Console.WriteLine(e);
        _visible = false;
    }
}
