@page "/produktion/aufträge"
@using DxBlazorApplication2.Components.Formen
@using DxBlazorApplication2.Services.Produktion
@using DxBlazorApplication2.Data.Produktion
@rendermode InteractiveServer
@inject FSTLService _FSTLService;
@inject PSDLService _PSDLService;
@implements IDisposable

<PageTitle>Produktionsaufträge</PageTitle>

<div class="border" style="padding: 20px">
    <h4>Firma</h4>
<DxComboBox Data="@FSTLItems" TextFieldName="FSNAME" @bind-Value="@Firma" CssClass="cw-480" InputId="cbOverview"
            SelectedDataItemChanged="(SelectedDataItemChangedEventArgs<FSTLItem> args) => OnSelectedFirmaChanged(args)" />
</div>


<DxGrid @ref="PSDLGrid" Data="@PSDLItems" ShowGroupPanel="true" TextWrapEnabled="false" AutoExpandAllGroupRows="true"
            ShowSearchBox="true" AllowSelectRowByClick="true" PageSize="15" HighlightRowOnHover="true">
    <Columns>
        <DxGridDataColumn FieldName="PDLINI" Caption="Linie" GroupIndex="0" Width="100px" FilterMenuButtonDisplayMode="GridFilterMenuButtonDisplayMode.Always" />
        <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" />
<!--        <DxGridDataColumn FieldName="PDFIRM" Caption="Firma" Visible="false" FilterRowValue="@Firma.FSFIRM" /> -->
    </Columns>
</DxGrid>

@code 
{
    private IList<FSTLItem> FSTLItems { get; set; } = new List<FSTLItem>();
    private IList<PSDLItem> PSDLItems { get; set; } = new List<PSDLItem>();
    private DotNetObjectReference<Aufträge>? objRef;

    IGrid PSDLGrid { get; set; }
    private FSTLItem Firma;

    protected override void OnInitialized()
    {
        FSTLItems = _FSTLService.GetItems().ToList();
        // Firma in der Combobox vorbelegen
        Firma = FSTLItems[0];
        this._PSDLService.OnItemChanged += this.OnPSDLItemChanged;
        PSDLItems = _PSDLService.GetItems().Where(item => item.PDFIRM == Firma.FSFIRM).ToList();
    }

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


    void OnSelectedFirmaChanged(SelectedDataItemChangedEventArgs<FSTLItem> args)
    {
        // Nur aktualisieren, wenn der Benutzer geändert hat
        if (args.ChangeSource == SelectionChangeSource.UserAction)
        {
            Firma = args.DataItem;
            PSDLItems = _PSDLService.GetItems().Where(item => item.PDFIRM == args.DataItem.FSFIRM).ToList();
        }
    }

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