@page "/productlist"
@using proevi.Models
@using proevi.Services

@inject ProductService service
@inject IJSRuntime js


<h3>Productlist</h3>
@if (productList.Count > 0)
{

    @foreach (var p in productList)
    {
       
            <div class="card">
               

              <table>
                <a>CategoryName:  @p.Name</a>
                <a class="btn btn-warning" @onclick="@(()=> ConfirmDelete(p.ProductCategoryID))">Delete</a>

              </table>
               

               
            </div>
            <div class="card-body">
                <table class="table-bordered">
                   
                    @if(p.Products is not null){
                        <thead>
                            <tr>
                                <th>Product</th>
                                <th>Name</th>
                                <th>ProductNumber</th>
                                <th>color</th>
                                <th>Weight</th>
                                <th>Size</th>
                                <th>StandardCost</th>
                                <th>ListPrice</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if(p.Products is not null)
                            {
                                foreach(var item in p.Products)
                                {
                                    <tr>
                                        <td>@item.ProductID</td>
                                        <td>@item.Name</td>
                                        <td>@item.ProductNumber</td>
                                        <td>@item.Color</td>
                                        <td>@item.Weight</td>
                                        <td>@item.Size</td>
                                        <td>@item.StandardCost</td>
                                        <td>@item.ListPrice</td>
                                    </tr>
                                }
                            }
                        </tbody>                       
                    }                   
                </table>
            </div>
        
    }
}

@code {
    List<ProductCategoryDTO> productList = new List<ProductCategoryDTO>();
    protected override async Task OnInitializedAsync()
    {
        productList = await service.Get();
    }
    public async void ConfirmDelete(int id)
    {
        var isdelted = await service.Delete(id);
        if (isdelted)
        {
            productList.RemoveAll(c => c.ProductCategoryID == id);
            StateHasChanged();
        }

    }

}
