using BlazorApp1.Components;
using BlazorApp1.Models;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;

namespace BlazorApp1.Pages
{
    public partial class Inventory
    {
        [Inject]
        public IDataService DataService { get; set; }

        private int totalItem;
        public List<Item> Items { get; set; } = new List<Item>();

        [Inject]
        public IStringLocalizer<Inventory> Localizer { get; set; }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            base.OnAfterRenderAsync(firstRender);

            if (!firstRender)
            {
                return;
            }

            Items = await DataService.List(0, await DataService.Count());

            StateHasChanged();
        }
        private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
        {
            if (e.CancellationToken.IsCancellationRequested)
            {
                return;
            }
            
            if (!e.CancellationToken.IsCancellationRequested)
            {
                Items = await DataService.List(e.Page, e.PageSize);
                SortItem2(Items);
                totalItem = await DataService.Count();
            }
        }
        private List<Item> SortItem2(List<Item> i)
        {
            i.OrderBy(i => i.DisplayName).ToList();
            return i;
        }
        private void SortItem()
        {
            SortItem2((List<Item>)Items);
        }
    }
}
