@page "/MeasuringIn"
@using AyCode.Utils.Extensions
@using DevExpress.Blazor
@using DevExpress.Blazor.Internal
@using FruitBank.Common.Entities
@using FruitBank.Common.Enums
@using FruitBank.Common.SignalRs
@using FruitBankHybrid.Shared.Components
@using FruitBankHybrid.Shared.Services
@using AyCode.Blazor.Components.Components.CardViews
@using Mango.Nop.Core.Dtos

<h3>Áru bevételezés</h3>

<div style="margin-top: 30px;">
	<DxLoadingPanel @bind-Visible="LoadingPanelVisible"
					IsContentBlocked="true"
					ApplyBackgroundShading="true"
					IndicatorAreaVisible="true"
					IsContentVisible="true"
					IndicatorAnimationType="WaitIndicatorAnimationType.Spin"
					Text="Adatok szinkronizálása folyamatban...">

		<DxFormLayout CaptionPosition="CaptionPosition.Vertical" CssClass="w-100 measuring-form-layout">
			<DxFormLayoutItem Caption="Dátum" ColSpanXs="8" ColSpanSm="8" ColSpanMd="2"
							  CaptionCssClass="@(SelectedShipping != null && _measuringDates.Where(x => MeasurementService.DaysEqual(x.DateTime, SelectedShipping.ShippingDate)).All(x => x.IsMeasured) ? "text-success" : "")">
				<DxDateEdit CssClass="cw-320"
							DisplayFormat="m"
							Format="m"
							Context="ctxShippingDate"
							Date="@(SelectedShipping?.ShippingDate.Date ?? DateTime.Now.Date)"
							CustomDisabledDate="@OnCustomDisabledMeasuringDate"
							DateChanged="@((DateTime newValue) => OnMeasuringDateChanged(newValue))"
							InputId="deDisabledDates"
							PickerDisplayMode="DatePickerDisplayMode.Calendar">
					<DayCellTemplate>
						@{
							var cssClass = GetMeasuringDateCssClassNames(ctxShippingDate);
							if (!cssClass.IsNullOrWhiteSpace())
							{
								<a class="@GetMeasuringDateCssClassNames(ctxShippingDate)">@ctxShippingDate.Day.ToString()</a>
							}
							else
							{
								<a>@ctxShippingDate.Day.ToString()</a>
							}
						}

					</DayCellTemplate>
				</DxDateEdit>
			</DxFormLayoutItem>
		</DxFormLayout>

		<DxTabs ActiveTabIndex="(int)_activeTab" ActiveTabIndexChanged="i => _activeTab = (MeasuringTab)i" RenderMode="TabsRenderMode.OnDemand" CssClass="measuring-tabs">
			<DxTabPage Text="Napi feladatok">
				<div class="p-3">
					<MgCardView TItem="Shipping" Data="@FilteredShippings"
								ShowFilterPanel="true"
								OnCardClick="NavigateToMeasuringTab"
								ScrollToItem="@SelectedShipping"
								ItemKeySelector="s => s.Id"
								Height="calc(100vh - 220px)">
						<FilterPanel>
							<DxTagBox Data="@(new[] { MeasuringStatus.NotStarted, MeasuringStatus.Started, MeasuringStatus.Finnished })"
									  NullText="Összes státusz"
									  @bind-Values="_statusFilter"
									  ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"
									  CssClass="cw-480" />
						</FilterPanel>
						<CardTemplate>
							<div class="d-flex justify-content-between align-items-start mb-2">
								<h6 class="mb-0">@context.LicencePlate</h6>
								<span class="badge @(MeasurementService.GetMeasuringStatusBadgeCssClass(MeasurementService.GetShippingMeasuringStatus(context), hasAuditedStage: false))">@(MeasurementService.GetMeasuringStatusText(MeasurementService.GetShippingMeasuringStatus(context)))</span>
							</div>
							<div class="text-muted small mb-2">
								<strong>@context.ShippingDate.ToString("yyyy.MM.dd")</strong>
								@if (!string.IsNullOrWhiteSpace(context.CargoCompany))
								{
									<span> — @context.CargoCompany</span>
								}
							</div>
							@{
								var allItems = context.ShippingDocuments?
									.Where(sd => sd.ShippingItems is not null)
									.SelectMany(sd => sd.ShippingItems!);
							}
							@if (allItems is not null)
							{
								@foreach (var item in allItems)
								{
									<div class="@(MeasurementService.GetMeasuringStatusCssClass(item.MeasuringStatus, hasAuditedStage: false)) small">
										@item.ProductName — @item.MeasuredQuantity/@item.QuantityOnDocument rekesz
									</div>
								}
							}
							@if (!string.IsNullOrWhiteSpace(context.Comment))
							{
								<div class="text-muted small mt-2 fst-italic">
									📝 @context.Comment
								</div>
							}
						</CardTemplate>
					</MgCardView>
				</div>
			</DxTabPage>
			<DxTabPage Text="Mérés">
				<div class="p-3">
					<DxFormLayout CaptionPosition="CaptionPosition.Vertical" CssClass="w-100">
						<DxFormLayoutItem Caption="Rendszám:" ColSpanMd="2" CaptionCssClass="@(SelectedShipping?.IsAllMeasured == true ? "text-success" : "")">
							<DxComboBox Data="@NotMeasuredShippings"
										@bind-Value="@SelectedShipping"
										Text="Select licence plate"
										ValueFieldName="@nameof(Shipping.Id)"
										TextFieldName="@nameof(Shipping.LicencePlate)"
										CssClass="cw-480"
										DropDownBodyCssClass="dd-body-class"
										Context="ctxShipping"
										SelectedDataItemChanged="@((SelectedDataItemChangedEventArgs<Shipping> args) => OnSelectedShippingChanged(args))"
										InputId="cbNotMeasuredShippings">
								<ItemDisplayTemplate>
									<span class="@(ctxShipping.DataItem.IsAllMeasured ? "text-success" : "")">@ctxShipping.DisplayText</span>
								</ItemDisplayTemplate>
							</DxComboBox>
						</DxFormLayoutItem>

@* 						<DxFormLayoutItem Caption="Partner:" ColSpanMd="3" CaptionCssClass="@(SelectedShippingDocument?.IsAllMeasured == true ? "text-success" : "")">
							<DxComboBox Data="@SelectedShipping?.ShippingDocuments"
										@bind-Value="@SelectedShippingDocument"
										Text="Select partner"
										ValueFieldName="@nameof(ShippingDocument.Id)"
										TextFieldName="@(nameof(ShippingDocument.Partner) + '.' + nameof(Partner.Name))"
										CssClass="cw-480"
										DropDownBodyCssClass="dd-body-class"
										Context="ctxShippingDocument"
										SelectedDataItemChanged="@((SelectedDataItemChangedEventArgs<ShippingDocument> args) => OnSelectedShippingDocumentChanged(args))"
										InputId="cbShippingDocument">
								<ItemDisplayTemplate>
									<span class="@(ctxShippingDocument.DataItem.IsAllMeasured ? "text-success" : "")">@ctxShippingDocument.DisplayText</span>
								</ItemDisplayTemplate>
							</DxComboBox>
						</DxFormLayoutItem> *@

						<DxFormLayoutItem Caption="Termék:" ColSpanMd="5" CaptionCssClass="@(SelectedShippingItem?.IsMeasured == true ? "text-success" : "")">
							<DxComboBox Data="@_shippingItemsDataSource"
										@bind-Value="@SelectedShippingItem"
										Text="Select item"
										ValueFieldName="@nameof(ShippingItem.Id)"
										TextFieldName="@(nameof(ShippingItem.ProductName))"
										CssClass="cw-480"
										SearchMode="ListSearchMode.AutoSearch"
										SearchFilterCondition="ListSearchFilterCondition.Contains"
										SearchTextParseMode="ListSearchTextParseMode.GroupWordsByAnd"
										ClearButtonDisplayMode="DataEditorClearButtonDisplayMode.Auto"
										DropDownTriggerMode="DropDownTriggerMode.Click"
										ListRenderMode="ListRenderMode.Entire"
										ShowDropDownButton="false"
										DropDownBodyCssClass="dd-body-class"
										Context="ctxShippingitem"
										SelectedDataItemChanged="@((SelectedDataItemChangedEventArgs<ShippingItem> args) => OnSelectedShippingItemChanged(args))"
										InputId="cbShippingItem">
								<ItemDisplayTemplate>
@* 								<div class="combobox-item-template">
									<img src="/images/ok_green_lt.jpg" alt="" />
									<span class="icon counter-icon" aria-hidden="true"></span>
									<div class="combobox-item-template-text">
										<span>@ctxShippingitem.DisplayText</span>
									</div>
								</div> *@
								<span class="@(ctxShippingitem.DataItem.IsMeasured ? "text-success" : "")">@ctxShippingitem.DisplayText)</span>
								</ItemDisplayTemplate>
								<Buttons>
									<DxEditorButton IconCssClass="editor-icon editor-icon-add" Text="R" Tooltip="Adatok frissítése..."
													Click="() => OnOrdersRefreshClick()" />
								</Buttons>
							</DxComboBox>
						</DxFormLayoutItem>

						<DxFormLayoutItem Caption="Partner:" ColSpanMd="3" CaptionCssClass="@(SelectedShippingItem?.ShippingDocument?.IsAllMeasured == true ? "text-success" : "")">
							@{
								var sd = SelectedShippingItem?.ShippingDocument;
								if (sd != null)
								{
									var partnerName = sd.Partner?.Name;

									<span class="@(sd.IsAllMeasured == true ? " text-success" : "")">
										@($"{partnerName}, {sd.ShippingItems?.Count(si => si.IsMeasured) ?? 0}/{sd.ShippingItems?.Count ?? 0} kész.")
									</span>
								}
							}
						</DxFormLayoutItem>
					</DxFormLayout>

					<div style="margin-top: 50px;">
						@if (SelectedShippingItem is { ProductId: > 0 })
						{
							<h3 style="margin-bottom: 30px;" class="@(SelectedShippingItem.IsMeasured && SelectedShippingItem.ShippingItemPallets!.All(x => x.IsMeasuredAndValid(SelectedShippingItem.IsMeasurable)) ? "text-success" : "")">
								#@(SelectedShippingItem.ProductId). @(SelectedShippingItem.ProductName)
							</h3>

							<DxFormLayout Data="@SelectedShippingItem" CaptionPosition="CaptionPosition.Vertical" CssClass="w-100">
								<DxFormLayoutItem Context="ctxShippingItemFromLayoutItem" ColSpanMd="12">
									@for (var index = 0; index < (SelectedShippingItem?.ShippingItemPallets?.Count ?? 0); index++)
									{
										var localI = index + 1;
										var currentShippingItemPallet = SelectedShippingItem!.ShippingItemPallets![index];

										<PalletItemComponent IsMeasurable="@SelectedShippingItem.IsMeasurable"
															 MeasuringIndex="@localI"
															 PalletItem="@currentShippingItemPallet"
															 ProductId="@SelectedShippingItem.ProductId!.Value"
															 AddOrUpdateSignalRTag="SignalRTags.AddOrUpdateMeasuredShippingItemPallet"
															 OnPalletItemSaved="pallet => OnShippingItemPalletSaved(pallet)"
															 OnPalletItemValueChanged="pallet => OnShippingItemPalletValueChanged(pallet, SelectedShippingItem)">
										</PalletItemComponent>
									}
								</DxFormLayoutItem>

								<DxFormLayoutItem Context="vfdfgfd" ColSpanMd="12" BeginRow="true">
									<DxFormLayout CssClass="w-100">
										<DxFormLayoutItem ColSpanMd="1" BeginRow="false"><strong>TOTAL:</strong></DxFormLayoutItem>
										<DxFormLayoutItem ColSpanMd="2" BeginRow="false" Visible="@(SelectedShippingItem.IsMeasurable)" />
										<DxFormLayoutItem ColSpanMd="2" BeginRow="false" Visible="@(SelectedShippingItem.IsMeasurable)" />
										<DxFormLayoutItem ColSpanMd="2" BeginRow="false"><strong>Rekesz: @(SelectedShippingItem.MeasuredQuantity) db</strong></DxFormLayoutItem>
										<DxFormLayoutItem ColSpanMd="2" BeginRow="false" Visible="@(SelectedShippingItem.IsMeasurable)"><strong>Br: @(SelectedShippingItem.MeasuredGrossWeight) kg</strong></DxFormLayoutItem>
										<DxFormLayoutItem ColSpanMd="1" BeginRow="false" Visible="@(SelectedShippingItem.IsMeasurable)"><strong>Net: @(SelectedShippingItem.MeasuredNetWeight) kg</strong></DxFormLayoutItem>
										<DxFormLayoutItem ColSpanMd="1" BeginRow="false" />
									</DxFormLayout>
								</DxFormLayoutItem>

								@* <DxFormLayoutItem ColSpanMd="12" BeginRow="true">
							<ValidationSummary/>
						</DxFormLayoutItem> *@

								@if (!_errorText.IsNullOrWhiteSpace())
								{
									<DxFormLayoutItem ColSpanMd="12" BeginRow="true">
										<text>HIBA! @_errorText</text>
									</DxFormLayoutItem>
									//_errorText = string.Empty;
								}
							</DxFormLayout>
						}
					</div>
				</div>
			</DxTabPage>
		</DxTabs>
	</DxLoadingPanel>
</div>

@code {
}
