@using Shared.DTOs
<Modal @ref="modal" />

<Grid @ref="grid" TItem="InvoiceItemDTO"
      AllowRowClick="true"
      AllowSorting="true"
      Class="table table-hover"
      DataProvider="DataProvider"
      AllowPaging="true"
      PageSize="10"
      OnRowClick="OnRowClick"
      Responsive="true">

    <GridColumn TItem="InvoiceItemDTO" HeaderText="شناسه">
        @context.ID
    </GridColumn>
   @*  <GridColumn TItem="InvoiceItemDTO" HeaderText="کد">
        @context.CODID
    </GridColumn> *@
    <GridColumn TItem="InvoiceItemDTO" HeaderText="کالا">
        @context.sstt
    </GridColumn>
    <GridColumn TItem="InvoiceItemDTO" HeaderText="واحد">
        @context.mu
    </GridColumn>
    <GridColumn TItem="InvoiceItemDTO" HeaderText="تعداد">
        @context.am.ToString().Split('٫')[0]
    </GridColumn>
    <GridColumn TItem="InvoiceItemDTO" HeaderText="مبلغ واحد">
        @context.fee.ToString("N0")
    </GridColumn>
    <GridColumn TItem="InvoiceItemDTO" HeaderText="نرخ مالیات">
        @context.vra
    </GridColumn>
    <GridColumn TItem="InvoiceItemDTO" HeaderText="مبلغ مالیات">
        @context.vam?.ToString("N0")
    </GridColumn>
    <GridColumn TItem="InvoiceItemDTO" HeaderText="تخفیف">
        @context.dis?.ToString("N0")
    </GridColumn>
@*     <GridColumn TItem="InvoiceItemDTO" HeaderText="مبلغ بعد از تخفیف">
        @context.adis
    </GridColumn> *@
    <GridColumn TItem="InvoiceItemDTO" HeaderText="مبلغ کل">
        @context.tsstam?.ToString("N0")
    </GridColumn>
</Grid>


@code {
    Grid<InvoiceItemDTO> grid = default!;
    [Parameter] public int InvoiceID { get; set; }
    [Parameter] public bool IsDeleted { get; set; }
    [Parameter] public EventCallback<string> OnMultipleOfThree { get; set; }
    [Parameter] public IEnumerable<InvoiceItemDTO> InvoiceItems { get; set; }
    private Modal modal = default!;
    private async Task<GridDataProviderResult<InvoiceItemDTO>> DataProvider(GridDataProviderRequest<InvoiceItemDTO> request)
    {
        if (InvoiceItems is null) // pull employees only one time for client-side filtering, sorting, and paging
            InvoiceItems = GetInvoiceItems(); // call a service or an API to pull the employees

        return await Task.FromResult(request.ApplyTo(InvoiceItems));
    }
    protected override async Task OnParametersSetAsync()
    {
        if(grid != null)
            await grid.RefreshDataAsync();
        await base.OnParametersSetAsync();
    }
    private async Task OnRowClick(GridRowEventArgs<InvoiceItemDTO> args)
    {
       if (!IsDeleted)
        {
            var parameters = new Dictionary<string, object>();


            parameters.Add("itemDTO", args.Item);
            parameters.Add("InvoiceID", InvoiceID);
            parameters.Add("OnMultipleOfThree", EventCallback.Factory.Create<ActionInResultComponent>(this, CallBack));
            await modal.ShowAsync<CUSComponent.InvoiceItem>(title: "ویرایش اطلاعات", parameters: parameters);

        }
      
    }
  
    public async Task CallBack(ActionInResultComponent result)
    {
        string msg = "";
        if (result.Action == ComponentAction.add)
        {
            if (result.Status == ComponentStatus.success)
                msg="آیتم جدید با موفقیت اضافه شد";

        }
        else if (result.Action == ComponentAction.update)
        {
            if (result.Status == ComponentStatus.success)
                msg="اطلاعات آیتم با موفقیت ویرایش شد";
        }
        else if (result.Action == ComponentAction.delete)
        {
            if (result.Status == ComponentStatus.success)
               msg="آیتم با موفقیت حذف شد";
        }
        await OnMultipleOfThree.InvokeAsync(msg);
        // if (result.Status == ComponentStatus.success)
        //     await LoadCod(1);

        await modal.HideAsync();
    }
    private IEnumerable<InvoiceItemDTO> GetInvoiceItems()
    {
        return new List<InvoiceItemDTO>();
       
    }

}