@model MemberCenter.Web.Areas.Admin.Models.TenantDetailViewModel
@{
    ViewData["Title"] = Model.Tenant.Name;
    var avatarText = string.IsNullOrWhiteSpace(Model.Tenant.Name) ? "T" : Model.Tenant.Name[..1].ToUpperInvariant();
}

@if (TempData["Result"] is string result)
{
  <div class="alert alert-success">@result</div>
}

@if (TempData["Error"] is string error)
{
  <div class="alert alert-danger">@error</div>
}

<div class="account-detail-title">
  <a class="account-detail-back" asp-action="Index" aria-label="@L["Back to tenant list"]">&lt;</a>
  <h1>@Model.Tenant.Name</h1>
  <div class="account-detail-actions">
    @if (await AdminPermissionChecker.HasPermissionAsync(User, AdminPermissions.TenantsEdit))
    {
      <a class="account-action-button" href="/admin/tenants/edit/@Model.Tenant.Id">@L["Edit information"]</a>
    }
  </div>
</div>

<div class="tenant-detail-grid">
  <section class="tenant-summary-panel">
    <div class="profile-summary-header">
      <div class="profile-avatar" aria-hidden="true">@avatarText</div>
      <div>
        <p>@L["Tenant ID"]</p>
        <p>@Model.Tenant.Id</p>
      </div>
    </div>

    <dl class="profile-detail-list">
      <div>
        <dt>@L["Tenant ID"]</dt>
        <dd>@Model.Tenant.Id</dd>
      </div>
      <div>
        <dt>@L["Webhook Client ID"]</dt>
        <dd>@(Model.Tenant.SendEngineWebhookClientId?.ToString() ?? "-")</dd>
      </div>
      <div>
        <dt>@L["Domains"]</dt>
        <dd>@(Model.Tenant.Domains.Any() ? string.Join(", ", Model.Tenant.Domains) : "-")</dd>
      </div>
      <div>
        <dt>@L["Status"]</dt>
        <dd>@Model.Tenant.Status</dd>
      </div>
    </dl>
  </section>

  <section class="tenant-subscription-summary">
    <div class="account-detail-subscription-count">
      <span>@L["Newsletter subscription count:"]</span>
      <strong>@Model.ActiveSubscriptionCount</strong>
    </div>

    @if (!Model.NewsletterLists.Any())
    {
      <p class="profile-empty">@L["No newsletters yet."]</p>
    }
    else
    {
      <ul class="account-detail-subscription-list">
      @foreach (var newsletter in Model.NewsletterLists)
      {
        <li>
          <span>@newsletter.Name</span>
          <span>@newsletter.ActiveSubscriptionCount</span>
        </li>
      }
      </ul>
    }
  </section>

  <section class="tenant-manager-panel">
    <div class="tenant-manager-panel-header">
      <h2>@L["Tenant Managers"]</h2>
    </div>

    @if (Model.CanManageManagers)
    {
      <form method="post" asp-action="AssignManager" asp-route-id="@Model.Tenant.Id" class="tenant-manager-form">
        @Html.AntiForgeryToken()
        <input type="email" name="email" placeholder="@L["Member email"]" aria-label="@L["Member email"]" required />
        <button type="submit" class="account-action-button">@L["Assign"]</button>
      </form>
    }

    @if (!Model.Managers.Any())
    {
      <p class="profile-empty">@L["No tenant managers yet."]</p>
    }
    else
    {
      <ul class="tenant-manager-list">
      @foreach (var manager in Model.Managers)
      {
        <li>
          <div>
            <strong>@manager.DisplayName</strong>
            <span>@manager.Email</span>
          </div>
          @if (Model.CanManageManagers)
          {
            <form method="post" asp-action="RemoveManager" asp-route-id="@Model.Tenant.Id" asp-route-userId="@manager.UserId">
              @Html.AntiForgeryToken()
              <button type="submit" class="account-action-button is-danger">@L["Remove"]</button>
            </form>
          }
        </li>
      }
      </ul>
    }
  </section>

  <section class="tenant-newsletter-grid" aria-label="@L["Tenant newsletters"]">
    @if (!Model.NewsletterLists.Any())
    {
      <article class="tenant-newsletter-card">
        <h2>@L["No newsletters yet"]</h2>
        <p>@L["Subscriptions"]</p>
        <strong>0</strong>
      </article>
    }
    else
    {
      @foreach (var newsletter in Model.NewsletterLists)
      {
        <a class="tenant-newsletter-card @(newsletter.Status.Equals("active", StringComparison.OrdinalIgnoreCase) ? null : "is-off")" asp-controller="NewsletterLists" asp-action="Details" asp-route-id="@newsletter.Id">
          <div class="tenant-newsletter-card-header">
            <h2>@newsletter.Name</h2>
            @if (!newsletter.Status.Equals("active", StringComparison.OrdinalIgnoreCase))
            {
              <span>@newsletter.Status</span>
            }
          </div>
          <p>@L["URL"]</p>
          <div class="tenant-newsletter-count">
            <span>@L["Subscriptions"]</span>
            <strong>@newsletter.ActiveSubscriptionCount</strong>
          </div>
        </a>
      }
    }
  </section>
</div>
