@using TIAM.Entities.Transfers
@using TIAM.Entities.Drivers
@using TIAM.Models.Dtos.Users
@using TIAMWebApp.Shared.Application.Interfaces
@using TIAMWebApp.Shared.Application.Utility
@inject IUserDataService NwindDataService
@inject LogToBrowserConsole logToBrowserConsole

<div class="mb-2">
    Contact Phone: @PhoneNumber
</div>
<div class="mb-2">
    Contact Phone: @EmailAddress
</div>
<DxGrid Data="DetailGridData"
        PageSize="5"
        AutoExpandAllGroupRows="true" 
        KeyboardNavigationEnabled="KeyboardNavigationEnabled"
        KeyFieldName="Id"
        ValidationEnabled="false"
        CustomizeEditModel="CustomizeEditModel"
        EditModelSaving="EditModelSaving"
        DataItemDeleting="DataItemDeleting"
        EditMode="GridEditMode.EditForm"
        ColumnResizeMode="GridColumnResizeMode.NextColumn"
        ShowFilterRow="true">    
    <Columns>
        <DxGridDataColumn FieldName="Id" GroupIndex="0" />
        <DxGridDataColumn FieldName="CarId" Width="40%" />
        <DxGridDataColumn FieldName="LicencePlate" />
    </Columns>
</DxGrid>

@code {
    [Parameter]
    public bool KeyboardNavigationEnabled { get; set; }
    [Parameter]
    public TIAM.Entities.Transfers.Transfer Customer { get; set; }

    List<TransferToDriver> DetailGridData { get; set; }

    public UserModelDtoDetail UserInfo;
    public string PhoneNumber = "";
    public string EmailAddress = "";

    protected override async Task OnInitializedAsync()
    {
        UserInfo = await NwindDataService.GetUserDetailByIdAsync(Customer.UserId);
        PhoneNumber = UserInfo.UserDto.PhoneNumber;
        EmailAddress = UserInfo.UserDto.EmailAddress;
        DetailGridData = Customer.TransferToDrivers;
        logToBrowserConsole.LogToBC($"DetailGridData: {DetailGridData.Count}");
    }

    void CustomizeEditModel(GridCustomizeEditModelEventArgs e)
    {
        if (e.IsNew)
        {
            var newDriver = (TransferToDriver)e.EditModel;
            newDriver.Id = Guid.NewGuid();
            newDriver.CarId = Guid.Empty;
            newDriver.LicencePlate = "";
            newDriver.Car = new Car();
            newDriver.Price = 0;
            newDriver.TransferId = Customer.Id;


        }
    }

    async Task 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");
        
        //await NwindDataService.UpdateEmployeeAsync((EditableEmployee)e.DataItem, (EditableEmployee)e.EditModel);

        await UpdateDataAsync();
    }
    async Task 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");
    }

}