@using AyCode.Blazor.Components.Components.Grids
@using AyCode.Utils.Extensions
@using FruitBank.Common
@using FruitBank.Common.Dtos
@using FruitBank.Common.Entities
@using FruitBank.Common.Models
@using FruitBankHybrid.Shared.Databases
@using FruitBankHybrid.Shared.Services.SignalRs

@inject FruitBankSignalRClient FruitBankSignalRClient
@inject LoggedInModel LoggedInModel

<MgGridBase @ref="gridOrderItemPallet" Data="ShippingItemPallets" IsMasterGrid="IsMasterGrid" AutoSaveLayoutName="GridShippingItemPallets"
			PageSize="@(IsMasterGrid ? 15 : 50)" ShowFilterRow="IsMasterGrid" ShowGroupPanel="IsMasterGrid"
			AutoExpandAllGroupRows="false"
			ColumnResizeMode="GridColumnResizeMode.NextColumn"
			FilterMenuButtonDisplayMode="@(IsMasterGrid ? GridFilterMenuButtonDisplayMode.Never : GridFilterMenuButtonDisplayMode.Always)">
	<Columns>
		<DxGridDataColumn FieldName="Id" Width="125" />
		<DxGridDataColumn FieldName="ShippingItemId" Caption="siId" Width="125" />
		<DxGridDataColumn FieldName="ShippingItem.ShippingId" Caption="sId" Width="125" />
		<DxGridDataColumn Name="ProductId" FieldName="ShippingItem.ProductId" Caption="pId" Width="125" Visible="false" />
		<DxGridDataColumn Name="ProductName" FieldName="ShippingItem.ProductDto.Name" Caption="ProductName" Visible="false" />

		<DxGridDataColumn FieldName="PalletWeight" />
		<DxGridDataColumn FieldName="TareWeight" />

		<DxGridDataColumn FieldName="TrayQuantity" />
		<DxGridDataColumn FieldName="GrossWeight" />
		<DxGridDataColumn FieldName="NetWeight" />

		<DxGridDataColumn FieldName="IsMeasured" ReadOnly="true" />
		
		<MgGridDataColumn FieldName="@nameof(ShippingItemPallet.ModifierId)" ReadOnly="true" UrlLink=@(FruitBankConstClient.BaseUrl + "/Admin/Customer/Edit/{ModifierId}") />
		<DxGridDataColumn FieldName="@nameof(ShippingItemPallet.Modified)" ReadOnly="true" DisplayFormat="yyyy.MM.dd hh:mm" />

		<DxGridCommandColumn Visible="!IsMasterGrid" Width="120"></DxGridCommandColumn>
	</Columns>
	<GroupSummary>
		<DxGridSummaryItem SummaryType="GridSummaryItemType.Sum"
						   FieldName="TrayQuantity"
						   FooterColumnName="TrayQuantity" />
		<DxGridSummaryItem SummaryType="GridSummaryItemType.Sum"
						   FieldName="NetWeight"
						   FooterColumnName="NetWeight" />
	</GroupSummary>
</MgGridBase>

@code {
	public MgGridBase gridOrderItemPallet;

	[Inject] public required DatabaseClient Database { get; set; }
	//[Inject] public required ObjectLock ObjectLock{ get; set; }

	[Parameter] public bool IsMasterGrid { get; set; } = false;
	[Parameter] public List<ShippingItemPallet>? ShippingItemPallets { get; set; }

	protected override async Task OnInitializedAsync()
	{
		await ReloadDataFromDb(false);
	}

	private async Task ReloadDataFromDb(bool forceReload = false)
	{
		if (!IsMasterGrid) return;

		using (await ObjectLock.GetSemaphore<ShippingItemPallet>().UseWaitAsync())
		{
			ShippingItemPallets ??= [];

			if (ShippingItemPallets.Count == 0 || forceReload)
			{
				ShippingItemPallets.Clear();

				//TODO: A ShippingItemPallet-eknek SignalR Endpoint! - J.
				ShippingItemPallets.AddRange(await FruitBankSignalRClient.GetShippingItemPallets() ?? []);

				if (ShippingItemPallets.Any(sip => sip.ShippingItem?.ProductDto != null))
				{
					gridOrderItemPallet.BeginUpdate();

					gridOrderItemPallet.GetColumns().FirstOrDefault(x => x.Name == "ProductId")!.Visible = true;
					gridOrderItemPallet.GetColumns().FirstOrDefault(x => x.Name == "ProductName")!.Visible = true;

					gridOrderItemPallet.EndUpdate();
				}
			}
		}

		if (forceReload)
			gridOrderItemPallet?.Reload();
	}

	protected override Task OnAfterRenderAsync(bool firstRender)
	{
		if (firstRender)
		{
			// if (OrderItemPallets != null && OrderItemPallets.Any(oip => oip.OrderItemDto?.ProductDto != null))
			// {
			// 	gridOrderItemPallet.BeginUpdate();
				
			// 	gridOrderItemPallet.GetColumns().FirstOrDefault(x => x.Name == "ProductId")!.Visible = true;
			// 	gridOrderItemPallet.GetColumns().FirstOrDefault(x => x.Name == "ProductName")!.Visible = true;
					
			// 	gridOrderItemPallet.EndUpdate();
			// }
		}

		return base.OnAfterRenderAsync(firstRender);
	}

}

@*     List<GenericAttribute> GenericAttributes { get; set; }
    List<OrderItemPallet> OrderItemPallets { get; set; }
    OrderDto OrderDto { get; set; }
    bool IsMeasured
    bool IsMeasurable
    int TrayQuantity
    double NetWeight
    double GrossWeight

        public Guid OrderItemGuid { get; set; }
        public int OrderId { get; set; }
        public int ProductId { get; set; }
        public int Quantity { get; set; }

        public decimal UnitPriceInclTax { get; set; }
        public decimal UnitPriceExclTax { get; set; }

        public decimal PriceInclTax { get; set; }
        public decimal PriceExclTax { get; set; }

        public string AttributesXml { get; set; }
        public decimal? ItemWeight { get; set; }

        public string ProductName => ProductDto?.Name ?? "ProductDto is null!!!";

        public TProductDto? ProductDto { get; set; }
 *@