@using Shared.DTOs;
@using Front.Services
@using Shared.DTOs.Serch
@inject HttpClientController hc;

<div class="row">
    <div class="col-md-12">
        <Grid TItem="stuffDto" class="table table-hover table-bordered table-striped" DataProvider="DataProvider" AllowFiltering="true" Responsive="true" AllowPaging="true" PageSize="10">
            <GridColumn Sortable="false" FilterOperator="FilterOperator.Contains" TItem="stuffDto" HeaderText="شناسه" PropertyName="ID">
                @context.ID
            </GridColumn>
            <GridColumn Sortable="false" FilterOperator="FilterOperator.Contains" TItem="stuffDto" HeaderText="شرح شناسه کالا" PropertyName="DescriptionOfID">
                @context.DescriptionOfID
            </GridColumn>
            <GridColumn Sortable="false" Filterable="false" TItem="stuffDto" HeaderText="نوع شناسه کالا" PropertyName="Type">
                @context.Type
            </GridColumn>
            <GridColumn Sortable="false" Filterable="false" TItem="stuffDto" HeaderText="وضعیت" PropertyName="TaxableOrFree">
                @context.TaxableOrFree
            </GridColumn>
            <GridColumn Sortable="false" Filterable="false" TItem="stuffDto" HeaderText="نرخ" PropertyName="Vat">
                @context.Vat
            </GridColumn>
            @*     <GridColumn TItem="stuffDto" HeaderText="نرخ ارزش افزوده مبادی گمرکی" PropertyName="VatCustomPurposes">
            @context.VatCustomPurposes
            </GridColumn> *@

        </Grid>
    </div>
</div>
<div class="row">
    <div class="col-md-12">
        <a href="https://stuffid.tax.gov.ir/" target="_blank" rel="noopener noreferrer">جستجوی پیشرفته</a>
    </div>
</div>
@code {
    //  private IEnumerable<stuffDto> stuffDtos;
    // public string Value { get; set; }
    // protected override void OnInitialized()
    // {
    //     stuffDtos = new List<stuffDto>();


    // }


}
@functions {
    // public async Task Search()
    // {
    //     if (!string.IsNullOrEmpty(Value))
    //     {
    //         var rsp = await hc.Get($"stuff/Getstuff/{Value}");
    //         if (rsp.IsSuccessStatusCode)
    //         {
    //             stuffDtos = await rsp.Content.ReadFromJsonAsync<List<stuffDto>>();
    //         }
    //     }

    // }

    private async Task<GridDataProviderResult<stuffDto>> DataProvider(GridDataProviderRequest<stuffDto> request)
    {
        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 stuffDtos = new PagingDto<stuffDto>(0, 0, new List<stuffDto>());
        var rsp = await hc.Post<GridDataProviderRequestDto>($"stuff/Getstuff", itemsearch);
        if (rsp.IsSuccessStatusCode)
        {
            stuffDtos = await rsp.Content.ReadFromJsonAsync<PagingDto<stuffDto>>();
        }

        //  var result = await _customerService.GetCustomersAsync(request.Filters, request.PageNumber, request.PageSize, sortString, sortDirection, request.CancellationToken);
        return await Task.FromResult(new GridDataProviderResult<stuffDto> { Data = stuffDtos.list, TotalCount = stuffDtos.RowCount });
    }
}