@using Bit.Admin.Enums;
@using Bit.Core
@using Bit.Core.Billing.Extensions
@inject Bit.Admin.Services.IAccessControlService AccessControlService
@inject Bit.Core.Services.IFeatureService FeatureService

@model ProviderEditModel
@{
    ViewData["Title"] = "Provider: " + Model.Provider.DisplayName();
    var canEdit = AccessControlService.UserHasPermission(Permission.Provider_Edit);
}

<h1>Provider <small>@Model.Provider.DisplayName()</small></h1>

<h2>Provider Information</h2>
@await Html.PartialAsync("_ViewInformation", Model)
@await Html.PartialAsync("Admins", Model)
<form method="post" id="edit-form">
    <h2>General</h2>
    <dl class="row">
        <dt class="col-sm-4 col-lg-3">Name</dt>
        <dd class="col-sm-8 col-lg-9">@Model.Provider.DisplayName()</dd>
    </dl>
    <h2>Business Information</h2>
    <dl class="row">
        <dt class="col-sm-4 col-lg-3">Business Name</dt>
        <dd class="col-sm-8 col-lg-9">@Model.Provider.DisplayBusinessName()</dd>
    </dl>
    <h2>Billing</h2>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="BillingEmail"></label>
                <input type="email" class="form-control" asp-for="BillingEmail" readonly='@(!canEdit)'>
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm">
            <div class="form-group">
                <label asp-for="BillingPhone"></label>
                <input type="tel" class="form-control" asp-for="BillingPhone">
            </div>
        </div>
    </div>
    @if (FeatureService.IsEnabled(FeatureFlagKeys.EnableConsolidatedBilling) && Model.Provider.IsBillable())
    {
        <div class="row">
            <div class="col-sm">
                <div class="form-group">
                    <label asp-for="TeamsMonthlySeatMinimum"></label>
                    <input type="number" class="form-control" asp-for="TeamsMonthlySeatMinimum">
                </div>
            </div>
            <div class="col-sm">
                <div class="form-group">
                    <label asp-for="EnterpriseMonthlySeatMinimum"></label>
                    <input type="number" class="form-control" asp-for="EnterpriseMonthlySeatMinimum">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm">
                <div class="form-group">
                    <div class="form-group">
                        <label asp-for="Gateway"></label>
                        <select class="form-control" asp-for="Gateway" asp-items="Html.GetEnumSelectList<Bit.Core.Enums.GatewayType>()">
                            <option value="">--</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-sm">
                <div class="form-group">
                    <label asp-for="GatewayCustomerId"></label>
                    <div class="input-group">
                        <input type="text" class="form-control" asp-for="GatewayCustomerId">
                        <div class="input-group-append">
                            <a href="@Model.GatewayCustomerUrl" class="btn btn-secondary" target="_blank">
                                <i class="fa fa-external-link"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm">
                <div class="form-group">
                    <label asp-for="GatewaySubscriptionId"></label>
                    <div class="input-group">
                        <input type="text" class="form-control" asp-for="GatewaySubscriptionId">
                        <div class="input-group-append">
                            <a href="@Model.GatewaySubscriptionUrl" class="btn btn-secondary" target="_blank">
                                <i class="fa fa-external-link"></i>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    }
</form>
@await Html.PartialAsync("Organizations", Model)
@if (canEdit)
{
    <!-- Modals -->
    <div class="modal fade rounded" id="requestDeletionModal" tabindex="-1" aria-labelledby="requestDeletionModal" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content rounded">
                <div class="p-3">
                    <h4 class="font-weight-bolder" id="exampleModalLabel">Request provider deletion</h4>
                </div>
                <div class="modal-body">
                    <span class="font-weight-light">
                        Enter the email of the provider admin that will receive the request to delete the provider portal.
                    </span>
                    <form>
                        <div class="form-group">
                            <label for="provider-email" class="col-form-label">Provider email</label>
                            <input type="email" class="form-control" id="provider-email">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-primary btn-pill" data-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-danger btn-pill" onclick="initiateDeleteProvider('@Model.Provider.Id')">Send email request</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="DeleteModal" tabindex="-1" aria-labelledby="DeleteModal" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content rounded">
                <div class="p-3">
                    <h4 class="font-weight-bolder" id="exampleModalLabel">Delete provider</h4>
                </div>
                <div class="modal-body">
                    <span class="font-weight-light">
                        This action is permanent and irreversible. Enter the provider name to complete deletion of the provider and associated data.
                    </span>
                    <form>
                        <div class="form-group">
                            <label for="provider-name" class="col-form-label">Provider name</label>
                            <input type="text" class="form-control" id="provider-name">
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-primary btn-pill" data-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-danger btn-pill" onclick="deleteProvider('@Model.Provider.Id');">Delete provider</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="linkedWarningModal" tabindex="-1" role="dialog" aria-labelledby="linkedWarningModal" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content rounded">
                <div class="modal-body">
                    <h4 class="font-weight-bolder">Cannot Delete @Model.Name</h4>
                    <p class="font-weight-lighter">You must unlink all clients before you can delete @Model.Name.</p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-outline-primary btn-pill" data-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary btn-pill" data-dismiss="modal">Ok</button>
                </div>
            </div>
        </div>
    </div>

    <!-- End of Modal Section -->

    <div class="d-flex mt-4">
        <button type="submit" class="btn btn-primary" form="edit-form">Save</button>
        @if (FeatureService.IsEnabled(FeatureFlagKeys.EnableDeleteProvider))
        {
            <div class="ml-auto d-flex">
                <button class="btn btn-danger" onclick="openRequestDeleteModal(@Model.ProviderOrganizations.Count())">Request Delete</button>
                <button id="requestDeletionBtn" hidden="hidden" data-toggle="modal" data-target="#requestDeletionModal"></button>

                <button class="btn btn-outline-danger ml-2" onclick="openDeleteModal(@Model.ProviderOrganizations.Count())">Delete</button>
                <button id="deleteBtn" hidden="hidden" data-toggle="modal" data-target="#DeleteModal"></button>

                <button id="linkAccWarningBtn" hidden="hidden" data-toggle="modal" data-target="#linkedWarningModal"></button>

            </div>
        }
    </div>
}
