@page "/user/properties"
@using TIAM.Entities.ServiceProviders
@using TIAM.Resources
@using TIAMSharedUI.Pages.Components
@using TIAMSharedUI.Shared
@using TIAMWebApp.Shared.Application.Models.ClientSide.UI.WizardModels
@using TIAMWebApp.Shared.Application.Utility
@layout AdminLayout
@inject LogToBrowserConsole logToBrowserConsole
@inject IStringLocalizer<TIAMResources> localizer
<h3>Properties</h3>

<div class="container">
    <div class="row">        
        <div class=" col-12 col-xl-6">
            <div class="card glass card-admin" style="border-radius: 16px;">
                <div class="card-header py-2 px-4">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <span class="fw-bold text-body">Service providers list</span>

                        </div>
                        <div>
                            <!--div class="target-container" @onclick="@(() => EulaVisible = true)">
                                <button class="btn btn-primary">Create</button>
                            </div-->
                        </div>
                    </div>
                </div>

                <div class="card-body card-admin-body py-2 px-4">
                    <div class="d-flex flex-row mb-4 pb-2">


                        
                        @*<DxPopup CssClass="popup-demo-events"
                                 @bind-Visible="@EulaVisible"
                                 ShowFooter="true"
                                 CloseOnEscape="false"
                                 CloseOnOutsideClick="false"
                                 ShowCloseButton="true"
                                 HeaderText="@localizer.GetString(ResourceKeys.ServiceProviderTitle)"
                                 Closing="EulaPopupClosing"
                                 Closed="EulaPopupClosed">
                            <BodyContentTemplate>
                                <InputWizard Data=@myModel OnSubmit="SubmitForm" SubmitButtonText=@ResourceKeys.ButtonSave TitleResourceString=@ResourceKeys.ServiceProviderTitle></InputWizard>
                            </BodyContentTemplate>
                            <FooterContentTemplate Context="Context">
                                <div class="popup-demo-events-footer">
                                    <!--DxCheckBox CssClass="popup-demo-events-checkbox" @bind-Checked="@EulaAccepted">I accept the terms of the EULA</!--DxCheckBox-->
                                    <!--DxButton CssClass="popup-demo-events-button ms-2" RenderStyle="ButtonRenderStyle.Primary" Text="OK" Click="Context.CloseCallback" /-->
                                    <DxButton CssClass="popup-demo-events-button ms-2" RenderStyle="ButtonRenderStyle.Secondary" Text=@localizer.GetString(ResourceKeys.ButtonCancel) Click="CancelCreateClick" />
                                </div>
                            </FooterContentTemplate>
                        </DxPopup>*@

                        <DxGrid @ref="Grid"
                                Data="OrderData"
                                PageSize="12"
                                KeyFieldName="Id"
                                ValidationEnabled="false"
                                CustomizeEditModel="Grid_CustomizeEditModel"
                                EditModelSaving="Grid_EditModelSaving"
                                DataItemDeleting="Grid_DataItemDeleting"
                                EditMode="GridEditMode.EditRow"
                                KeyboardNavigationEnabled="true">
                            <Columns>
                                <DxGridCommandColumn Width="160px" />
                                <DxGridDataColumn FieldName="Id" MinWidth="80">
                                    <CellDisplayTemplate>
                                        <a class="d-block text-left" href="user/serviceprovider/@context.Value.ToString()">@context.Value</a>
                                    </CellDisplayTemplate>
                                </DxGridDataColumn>
                                <DxGridDataColumn FieldName="Name" MinWidth="80" />
                                <DxGridDataColumn FieldName="OwnerId" MinWidth="80" />

                            </Columns>
                        </DxGrid>


                    </div>

                    <div class="d-flex flex-row mb-4 pb-2">
                        <h4> Some <span class="small text-muted"> conclusion </span></h4>
                    </div>
                </div>

            </div>

        </div>
        
        <div class=" col-12 col-xl-6">
        </div>

    </div>
</div>


@code {

    IGrid Grid { get; set; }
    object? OrderData { get; set; }

    public ServiceProviderWizardModel myModel = new ServiceProviderWizardModel();

    bool EulaAccepted { get; set; }
    bool EulaVisible { get; set; }

    
    void CancelCreateClick()
    {
        
       
        EulaVisible = false;
    }
    void EulaPopupClosed()
    {
        EulaAccepted = false;

    }
    void EulaPopupClosing(PopupClosingEventArgs args)
    {
        myModel = new ServiceProviderWizardModel();
    }

    //-----------------------------------------------------------------------------------


    public async Task SubmitForm(object Result)
    {
        //await WizardProcessor.ProcessWizardAsync(Result.GetType(), Result);
        logToBrowserConsole.LogToBC($"Submitted nested form: {Result.GetType().FullName}");
    }


    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
            await Grid.StartEditRowAsync(0);
    }

    void Grid_CustomizeEditModel(GridCustomizeEditModelEventArgs e)
    {
        if (e.IsNew)
        {
            var newEmployee = (TiamServiceProvider)e.EditModel;
            newEmployee.Name = "John";
            newEmployee.OwnerId = Guid.NewGuid();
        }
    }

    async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e)
    {
        if (e.IsNew)
            //add new orderData to orderData array
            logToBrowserConsole.LogToBC("New orderData added");
        //await NwindDataService.InsertEmployeeAsync((EditableEmployee)e.EditModel);
        else
            logToBrowserConsole.LogToBC("orderData updated");
        //modify orderData where orderData.Name == e.EditModel.Name
            
            //await NwindDataService.UpdateEmployeeAsync((EditableEmployee)e.DataItem, (EditableEmployee)e.EditModel);
            
        await UpdateDataAsync();
    }
    async Task Grid_DataItemDeleting(GridDataItemDeletingEventArgs e)
    {
        //await NwindDataService.RemoveEmployeeAsync((EditableEmployee)e.DataItem);
        //remove orderData from orderData array
        logToBrowserConsole.LogToBC("orderData deleted");
        //await UpdateDataAsync();
    }
    async Task UpdateDataAsync()
    {
        //DataSource = await NwindDataService.GetEmployeesEditableAsync();
        //refresh grid
        logToBrowserConsole.LogToBC("orderData grid refreshed");
    }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        OrderData = new TiamServiceProvider[]
        {
        new TiamServiceProvider(Guid.NewGuid(), "BAT", Guid.NewGuid(), Guid.NewGuid()),
        new TiamServiceProvider(Guid.NewGuid(), "TIAM", Guid.NewGuid(), Guid.NewGuid()),
        new TiamServiceProvider(Guid.NewGuid(), "TestHotel", Guid.NewGuid(), Guid.NewGuid())
        
        };


    }

}
