@page "/produktion/aufträgeprolinie/{LINI}"
@using DxBlazorApplication2.Components.Formen
@using DxBlazorApplication2.Services.Produktion
@using DxBlazorApplication2.Data.Produktion
@rendermode InteractiveServer
@inject TabellenService _tabellenService;
@inject PSDLService _PSDLService;
@implements IDisposable

<PageTitle>Produktionsaufträge</PageTitle>

<div class="border" style="padding: 20px">
    <h4>Linie</h4>
<DxComboBox Data="@Linien" TextFieldName="TABKEY" @bind-Value="@Linie" CssClass="cw-480" InputId="cbOverview"
            SelectedDataItemChanged="(SelectedDataItemChangedEventArgs<TabellenItem> args) => OnSelectedLinieChanged(args)"
                SearchMode="@ListSearchMode.AutoSearch"
                SearchFilterCondition="@ListSearchFilterCondition.Contains">
        <Columns>
            <DxListEditorColumn FieldName="TABKEY" Caption="Linie" />
            <DxListEditorColumn FieldName="TABTEXT" Caption="Bezeichnung" />
        </Columns>
    </DxComboBox>
</div>


<DxGrid @ref="PSDLGrid" Data="@PSDLItems" ShowGroupPanel="false" TextWrapEnabled="false" AutoExpandAllGroupRows="true"
            ShowSearchBox="true" AllowSelectRowByClick="true" PageSize="15" HighlightRowOnHover="true">
    <Columns>
        <DxGridDataColumn FieldName="PDPANR" Caption="Auftrag" DisplayFormat="F0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDARNR" Caption="Artikel" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDARBZ" Caption="Bezeichnung" Width="300px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDCHAR" Caption="Charge" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDMHD" Caption="MHD" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDPRDT" Caption="Datum" DisplayFormat="dd.MM.yyyy" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDTIMV" Caption="Zeit von" DisplayFormat="HH:mm" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDTIMB" Caption="Zeit bis" DisplayFormat="HH:mm" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDMGPL" Caption="Plan [VSE]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDMGIS" Caption="Ist [VSE]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDGWPL" Caption="Plan [kg]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="PDGWIS" Caption="Ist [kg]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
    </Columns>
</DxGrid>

@code 
{
    [Parameter]
    public string LINI { get; set; } = "";

    private IList<TabellenItem> Linien { get; set; }
    private IList<PSDLItem> PSDLItems { get; set; } = new List<PSDLItem>();
    private DotNetObjectReference<Aufträge>? objRef;

    IGrid PSDLGrid { get; set; }
    private TabellenItem Linie = new TabellenItem();

    protected override void OnInitialized()
    {
        Linien = _tabellenService.GetTabellenItems("LILINI");
        // Linie in der Combobox vorbelegen
        Linie = Linien.FirstOrDefault(item => item.TABKEY == LINI);
        this._PSDLService.OnItemChanged += this.OnPSDLItemChanged;
        PSDLItems = _PSDLService.GetItems().Where(item => item.PDLINI == Linie.TABKEY).ToList();
    }

    public void Dispose()
    {
        // Austragen, da Seite nicht mehr benutzt wird
        this._PSDLService.OnItemChanged -= this.OnPSDLItemChanged;
        objRef?.Dispose();
    }


    void OnSelectedLinieChanged(SelectedDataItemChangedEventArgs<TabellenItem> args)
    {
        // Nur aktualisieren, wenn der Benutzer geändert hat
        if (args.ChangeSource == SelectionChangeSource.UserAction)
        {
            Linie = args.DataItem;
            PSDLItems = _PSDLService.GetItems().Where(item => item.PDLINI == Linie.TABKEY).ToList();
        }
    }

    public async void OnPSDLItemChanged(object sender, PSDLServiceEventArgs args)
    {
        if (args.DataItem.PDLINI == Linie.TABKEY)
        {
            await InvokeAsync(() =>
            {
                PSDLItems = _PSDLService.GetItems().Where(item => item.PDLINI == Linie.TABKEY).ToList();
                PSDLGrid.Reload();
            });
        }
    }

}