@using Syncfusion.Blazor.Inputs
@using Syncfusion.Blazor.Navigations
@using Syncfusion.Blazor.Grids
@using Microsoft.AspNetCore.Components

@page "/"

<SfGrid DataSource="@_assemblies" AllowPaging="true" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })">
    <GridTemplates>
        <DetailTemplate Context="assemblyDetailContext">
            @{
                var assemblyDetails = (Assembly) assemblyDetailContext;
                var assemblyDocuments = _documents.Where(d => d.AssemblyId == assemblyDetails.Id);
            }
            <SfTab HeaderPlacement="HeaderPosition.Top">
                <TabItems>
                    <TabItem>
                        <ChildContent>
                            <TabHeader Text="Documents"></TabHeader>
                        </ChildContent>
                        <ContentTemplate>
                            <div>
                                <SfGrid DataSource="@assemblyDocuments" AllowPaging="true" Toolbar="@(new List<string> { "Add", "Edit", "Delete", "Cancel", "Update" })">
                                    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="@EditMode.Dialog">
                                        <Template Context="assemblyDocumentEditContext">
                                            @{
                                                var assemblyDocument = (Document) assemblyDocumentEditContext;
                                            }
                                            <div class="form-group" style="margin: 6px;">
                                                <label>Name</label>
                                                <SfTextBox @bind-value="@(assemblyDocument.Name)"></SfTextBox>
                                            </div>
                                            <div class="form-group" style="margin: 6px;">
                                                <label>Description</label>
                                                <SfTextBox @bind-value="@(assemblyDocument.Description)"></SfTextBox>
                                            </div>
                                            <div class="form-group" style="margin: 6px;">
                                                <label>Filepath</label>
                                                <SfTextBox @bind-value="@(assemblyDocument.FilePath)"></SfTextBox>
                                            </div>
                                        </Template>
                                    </GridEditSettings>
                                    <GridEvents OnActionBegin="args => AssemblyDocumentActionBeginHandler(args, assemblyDetails)" TValue="Document"></GridEvents>
                                    <GridColumns>
                                        <GridColumn Field=@nameof(Document.Id) Visible="false" IsPrimaryKey="true" IsIdentity="true"></GridColumn>
                                        <GridColumn Field=@nameof(Document.Name) HeaderText="Name"></GridColumn>
                                        <GridColumn Field=@nameof(Document.Description) HeaderText="Description"></GridColumn>
                                    </GridColumns>
                                </SfGrid>
                            </div>
                        </ContentTemplate>
                    </TabItem>
                </TabItems>
            </SfTab>
        </DetailTemplate>
    </GridTemplates>
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" Mode="@EditMode.Dialog">
        <Template Context="assemblyContext">
            @{
                var assembly = (Assembly) assemblyContext;
            }
            <div class="form-group" style="margin: 6px;">
                <label>Name</label>
                <SfTextBox @bind-value="@(assembly.Name)"></SfTextBox>
            </div>
            <div class="form-group" style="margin: 6px;">
                <label>Description</label>
                <SfTextBox @bind-value="@(assembly.Description)"></SfTextBox>
            </div>
        </Template>
    </GridEditSettings>
    <GridEvents OnActionBegin="ActionBeginHandler" OnActionComplete="ActionCompleteHandler" TValue="Assembly"></GridEvents>
    <GridColumns>
        <GridColumn Field=@nameof(Assembly.Id) Visible="false" IsPrimaryKey="true" IsIdentity="true"></GridColumn>
        <GridColumn Field=@nameof(Assembly.Name) HeaderText="Name"></GridColumn>
        <GridColumn Field=@nameof(Assembly.Description) HeaderText="Description"></GridColumn>
    </GridColumns>
</SfGrid>

@code
{
    IList<Assembly> _assemblies = new List<Assembly>
    {
        new Assembly { Id = new Guid("fc7506f7-a5ec-4ea6-b06f-b089f3efe8d6"), Name = "Tires", Description = "The tire assembly" },
        new Assembly { Id = new Guid("f761e7a4-ee64-4505-9dde-24cf14d813c0"), Name = "Body", Description = "The body assembly" },
        new Assembly { Id = new Guid("cb6ec76a-0acd-482f-99cc-d20941c98e86"), Name = "Suspension", Description = "The suspension assembly" },
    };

    IList<Document> _documents = new List<Document>();
    
    private void AssemblyDocumentActionBeginHandler(ActionEventArgs<Document> args, Assembly assembly)
    {
        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
        {
            if (args.Action == "Add")
            {
                args.Data.AssemblyId = assembly.Id;
                _documents.Add(args.Data);
            }
        }

        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
        {
            if (args.Action == "Edit")
            {
                //Edit logic
            }
        }

        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))
        {
            _documents.Remove(args.Data);
        }
    }
    
    private void ActionBeginHandler(Syncfusion.Blazor.Grids.ActionEventArgs<Assembly> args)
    {
        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
        {
            if (args.Action == "Add")
            {
                _assemblies.Add(args.Data);
            }
        } 

        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
        {
            if (args.Action == "Edit")
            {
                //Edit logic
            }
        }

        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Delete))
        {
            _assemblies.Remove(args.Data);
        }
    }

    private void ActionCompleteHandler(ActionEventArgs<Assembly> args)
    {
        if (args.RequestType.Equals(Syncfusion.Blazor.Grids.Action.Save))
        {
            //Update logic            
        }
    }


    public class Document
    {
        public Guid Id { get; set; }
        public required string Name { get; set; }
        public string? Description { get; set; }
        public required string FilePath { get; set; }
        
        public Guid AssemblyId { get; set; }
    }
    
    public class Assembly
    {
        public Guid Id { get; set; }
        public required string Name { get; set; }
        public string? Description { get; set; }
    }
    
}
