@using Front.Services
@using Shared.DTOs
@using Shared.DTOs.Serch
@using Shared.Enums
@inject HttpClientController hc;
<Preload LoadingText="در حال بارگذاری..." />
<Grid TItem="CreditDocumentDto"
      Class="table table-hover table-bordered table-striped"
      DataProvider="DocumentDataProvider"
      AllowFiltering="true"
      AllowPaging="true"
      RowClass="GetRowClass"
      Responsive="true"
      PageSize="10">

    <GridColumn TItem="CreditDocumentDto" HeaderText="تاریخ" PropertyName="Date" Sortable="false" Filterable="false"  HeaderTextAlignment="Alignment.Center" TextAlignment="Alignment.Center">
        @context.Date
    </GridColumn>
    <GridColumn TItem="CreditDocumentDto"  HeaderText="عنوان" PropertyName="Title" Sortable="false" Filterable="false">
        @context.Title
    </GridColumn>
    <GridColumn TItem="CreditDocumentDto" HeaderText="حالت" PropertyName="typeName" Sortable="false" Filterable="false">
        @context.typeName
    </GridColumn>
    <GridColumn  TItem="CreditDocumentDto" HeaderText="مقدار" PropertyName="Value" Sortable="false" Filterable="false">
        @context.Value.ToString("N0") ريال
    </GridColumn>

</Grid>

@code {
    [Inject] protected PreloadService PreloadService { get; set; } = default!;

    private string GetRowClass(CreditDocumentDto emp)
    {
        if (emp.type == CreditDocumentType.Decrease)
            return "table-danger";
        else
            return "table-success";
    }

    private async Task<GridDataProviderResult<CreditDocumentDto>> DocumentDataProvider(GridDataProviderRequest<CreditDocumentDto> request)
    {
        PreloadService.Show(SpinnerColor.Dark);
        string sortString = "";
        SortDirection sortDirection = SortDirection.None;

        if (request.Sorting is not null && request.Sorting.Any())
        {
            // Note: Multi column sorting is not supported at this moment
            sortString = request.Sorting.FirstOrDefault()!.SortString;
            sortDirection = request.Sorting.FirstOrDefault()!.SortDirection;
        }
        var itemsearch = new GridDataProviderRequestDto()
            {
                // CancellationToken = request.CancellationToken,
                Filters = request.Filters.Select(s => new Shared.DTOs.Serch.FilterItem
                {
                    Oper = s.Operator.ToString(),
                    PropertyName = s.PropertyName,
                    Value = s.Value
                }).ToList(),
                PageNumber = request.PageNumber,
                PageSize = request.PageSize,
                //sortString = sortString,
                //    SortDirection = sortDirection.ToString()
            };
        var model = new PagingDto<CreditDocumentDto>(0, 0, new List<CreditDocumentDto>());
        var rsp = await hc.Post<GridDataProviderRequestDto>($"Orders/GetCreditDocuments", itemsearch);
        if (rsp.IsSuccessStatusCode)
        {
            model = await rsp.Content.ReadFromJsonAsync<PagingDto<CreditDocumentDto>>();
        }
        PreloadService.Hide();

        return await Task.FromResult(new GridDataProviderResult<CreditDocumentDto> { Data = model.list, TotalCount = model.RowCount });




        // if (request.Sorting is not null && request.Sorting.Any())
        // {
        //     // Note: Multi column sorting is not supported at this moment
        //     sortString = request.Sorting.FirstOrDefault()!.SortString;
        //     sortDirection = request.Sorting.FirstOrDefault()!.SortDirection;
        // }
        // var result = await _customerService.GetCustomersAsync(request.Filters, request.PageNumber, request.PageSize, sortString, sortDirection, request.CancellationToken);
        // return await Task.FromResult(new GridDataProviderResult<Customer2> { Data = result.Item1, TotalCount = result.Item2 });
    }
}
