@page "/produktion/cockpit"
@using DxBlazorApplication2.Components.Formen
@using DxBlazorApplication2.Services
@using DxBlazorApplication2.Services.Produktion
@using DxBlazorApplication2.Data.Produktion
@rendermode InteractiveServer
@inject FSTLService _FSTLService;
@inject PSDLService _PSDLService;
@inject BDSTService _BDSTService;
@inject TabellenService _TabellenService;
@inject NavigationManager NavigationManager
@implements IDisposable

<PageTitle>Cockpit</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="CockpitGrid" Data="@CockpitItems" ShowGroupPanel="false" TextWrapEnabled="false" AutoExpandAllGroupRows="true"
        ShowSearchBox="true" AllowSelectRowByClick="true" PageSize="100" HighlightRowOnHover="true" CustomizeElement="Grid_CustomizeElement" RowDoubleClick="Grid_OnRowDoubleClick">
    <Columns>
        <DxGridDataColumn FieldName="WWLINI" Caption="Linie" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWPANR" Caption="Auftrag" DisplayFormat="F0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWARNR" Caption="Artikel" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWARBZ" Caption="Bezeichnung" Width="300px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWCHAR" Caption="Charge" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWMHD" Caption="MHD" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWPRDT" Caption="Datum" DisplayFormat="dd.MM.yyyy" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWTIMV" Caption="Zeit von" DisplayFormat="HH:mm" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWTIMB" Caption="Zeit bis" DisplayFormat="HH:mm" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWMGPL" Caption="Plan [VSE]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWMGIS" Caption="Ist [VSE]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWGWPL" Caption="Plan [kg]" DisplayFormat="N0" Width="100px" AllowGroup="false" AllowSort="false" />
        <DxGridDataColumn FieldName="WWGWIS" 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 CockpitService _CockpitService;
    private IList<FSTLItem> FSTLItems { get; set; } = new List<FSTLItem>();
    private IList<CockpitItem> CockpitItems { get; set; } = new List<CockpitItem>();
    private DotNetObjectReference<Cockpit>? objRef;
    private readonly Logger _logger = new Logger();

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

    protected override void OnInitialized()
    {
        _logger.Log("Cockpit.OnInitialized()");
    }

    protected override void OnAfterRender(bool firstRender)
    {
        _logger.Log($"Cockpit.OnAfterRender({firstRender})");
       
    }
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        _logger.Log($"Cockpit.OnAfterRenderAsync({firstRender})");
        if (firstRender)
        {
            _CockpitService = new CockpitService(_TabellenService, _PSDLService, _BDSTService);
            FSTLItems = _FSTLService.GetItems().ToList();
            // Firma in der Combobox vorbelegen
            Firma = FSTLItems[0];
            _CockpitService.OnItemChanged += this.OnCockpitItemChanged;
            CockpitItems = _CockpitService.GetItems().Where(item => item.WWWERK == Firma.FSFIRM).ToList();
            StateHasChanged();
        }
    }

    public void Dispose()
    {
        _logger.Log("Cockpit.Dispose()");
        // Austragen, da Seite nicht mehr benutzt wird
        if (_CockpitService != null)
        {
            _CockpitService.OnItemChanged -= this.OnCockpitItemChanged;
            _CockpitService.Dispose();
        }
        objRef?.Dispose();
    }


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

    void Grid_CustomizeElement(GridCustomizeElementEventArgs e)
    {
        if (e.ElementType == GridElementType.DataCell && e.Column.Caption == "Linie")
        {
            if ((bool)e.Grid.GetRowValue(e.VisibleIndex, "StörungAktiv"))
                e.CssClass = "stoerung_aktiv";
            else
                e.CssClass = null;
        }
    }

    void Grid_OnRowDoubleClick(GridRowClickEventArgs e)
    {
        NavigationManager.NavigateTo($"/produktion/aufträgeprolinie/{e.Grid.GetRowValue(e.VisibleIndex, "WWLINI")}");
    }

    public async void OnCockpitItemChanged(object sender, CockpitServiceEventArgs args)
    {
        if (args.DataItem.WWWERK == Firma.FSFIRM)
        {
            await InvokeAsync(() =>
            {
                CockpitItems = _CockpitService.GetItems().Where(item => item.WWWERK == Firma.FSFIRM).ToList();
                CockpitGrid.Reload();
                //StateHasChanged();
            });
        }
    }

}