@using Website.Components.Alerts

<AlertBox ID="product-branches" Group="productpage" />

<div class="mb-3">
    <div class="d-flex align-items-center mb-1">
        <div>
            <label class="form-label">Select Branch</label>
        </div>        
        <div class="ms-auto">
            <button class="btn btn-sm btn-primary" @onclick="AddBranch">Add New Branch</button>
        </div>
    </div>
    
    <select class="form-select" @bind="SelectedBranchId">
        @foreach (var branch in Product.Branches)
        {
            <option value="@branch.Id">@branch.Name</option>
        }
    </select>
</div>

@if (Branch != null)
{
    <div class="mb-3 card">
        <div class="card-header fs-5">
            Overview
        </div>
        <div class="card-body">
            <EditForm Model="@Branch" OnValidSubmit="SubmitAsync">
                <div class="mb-3">
                    <label class="form-label">Name</label>
                    <InputText class="form-control" @bind-Value="Branch.Name" />
                </div>

                <div class="mb-3">
                    <label class="form-label">Description</label>
                    <InputText class="form-control" @bind-Value="Branch.Description" />
                </div>

                <div class="mb-3 form-check">
                    <InputCheckbox @bind-Value="Branch.IsEnabled" class="form-check-input" id="branchIsEnabledCheck" />
                    <label class="form-check-label" for="branchIsEnabledCheck">Is Enabled</label>
                </div>

                <DataAnnotationsValidator />
                <ValidationSummary />

                <div class="mb-3">
                    <div class="float-right">
                        @if (isLoading)
                        {
                            <LoadingButton />
                        }
                        else
                        {
                            @if (Branch.Id == default)
                            {
                                <button type="submit" class="btn btn-primary">Add</button>
                            }
                            else
                            {
                                <button type="submit" class="btn btn-primary">Update</button>
                            }
                        }
                    </div>
                </div>
            </EditForm>
        </div>
        
    </div>
    

    @if (Branch.Versions != null)
    {
        <div class="card mb-3">
            <div class="card-header fs-5">
                Versions (@Versions.Count)
            </div>
            <div class="card-body p-0">
                <table class="table table-bordered mb-0">
                    <thead>
                        <tr>
                            <th>Version</th>
                            <th>File Name</th>
                            <th>Downloads</th>
                            <th>Create Date</th>
                            <th>Options</th>
                        </tr>
                    </thead>
                    <tbody>
                        <Virtualize Items="Versions" Context="version">
                            <tr>
                                <td>@version.Name</td>
                                <td>@version.FileName</td>
                                <td>@version.DownloadsCount</td>
                                <td>@version.CreateDate</td>
                                <td>
                                    <a class="btn btn-sm btn-outline-primary" href="api/versions/download/@version.Id?shouldCount=false" target="_blank"><i class="bi bi-download"></i></a>
                                    <button class="btn btn-sm btn-outline-success" @onclick="() => EditVersionAsync(version)">
                                        <i class="bi bi-pencil-square"></i>
                                    </button>
                                    <button class="btn btn-sm btn-outline-secondary" @onclick="() => TogglePluginAsync(version)">
                                        @if (version.IsEnabled)
                                        {
                                            <i class="bi bi-toggle-on"></i>
                                        }
                                        else
                                        {
                                            <i class="bi bi-toggle-off"></i>
                                        }
                                    </button>
                                </td>
                            </tr>
                        </Virtualize>
                    </tbody>
                </table>
            </div>
        </div>
    }
}

<EditVersionModal @ref="EditVersionModal" />




