@using Bit.Core.AdminConsole.Enums.Provider
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Bit.Admin.Enums
@using Bit.Core.Enums
@inject Bit.Admin.Services.IAccessControlService AccessControlService
@model ProviderViewModel

@{
    var canUnlinkFromProvider = AccessControlService.UserHasPermission(Permission.Provider_Edit);
}

@await Html.PartialAsync("_ProviderScripts")
@await Html.PartialAsync("_ProviderOrganizationScripts")

<h2>Provider Organizations</h2>
<div class="row">
    <div class="col-sm">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th style="width: 50%;">Name</th>
                        <th style="width: 50%;">Status</th>
                        <th>
                            @if (Model.Provider.Type == ProviderType.Reseller)
                            {
                                <div class="float-end text-nowrap">
                                    <a asp-controller="Providers" asp-action="CreateOrganization" asp-route-providerId="@Model.Provider.Id" class="btn btn-sm btn-primary text-decoration-none">New Organization</a>
                                    <a asp-controller="Providers" asp-action="AddExistingOrganization" asp-route-id="@Model.Provider.Id" class="btn btn-sm btn-outline-primary text-decoration-none">Add Existing Organization</a>
                                </div>
                            }
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @if (!Model.ProviderOrganizations.Any())
                    {
                        <tr>
                            <td colspan="6">No results to list.</td>
                        </tr>
                    }
                    else
                    {
                        @foreach (var providerOrganization in Model.ProviderOrganizations)
                        {
                            <tr>
                                <td class="align-middle">
                                    <a asp-controller="Organizations" asp-action="Edit" asp-route-id="@providerOrganization.OrganizationId">@providerOrganization.DisplayName()</a>
                                </td>
                                <td>
                                    @providerOrganization.Status
                                </td>
                                <td>
                                    <div class="float-end">
                                        @if (canUnlinkFromProvider)
                                        {
                                            <a href="#" class="text-danger float-end" onclick="return unlinkProvider('@Model.Provider.Id', '@providerOrganization.Id');">
                                                Unlink provider
                                            </a>
                                        }
                                        @if (providerOrganization.Status == OrganizationStatusType.Pending)
                                        {
                                            <a href="#" class="float-end me-3" onclick="return resendOwnerInvite('@providerOrganization.OrganizationId');">
                                                Resend invitation
                                            </a>
                                        }
                                    </div>
                                </td>
                            </tr>
                        }
                    }
                </tbody>
            </table>
        </div>
    </div>
</div>
