@model MemberCenter.Web.Areas.Admin.Models.AccountsIndexViewModel
@{
    ViewData["Title"] = L["Member Management"];
    var status = Model.StatusFilter ?? "all";
    var firstRowNumber = ((Model.Page - 1) * Model.PageSize) + 1;
}

@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>
}

<nav class="admin-tabs" aria-label="@L["Member status"]">
  <a class="admin-tab @(status == "all" ? "is-active" : null)" asp-action="Index" asp-route-status="all" asp-route-pageSize="@Model.PageSize">@L["Members"]</a>
  @if (Model.OperatorIsSuperuser)
  {
    <a class="admin-tab @(status == "admins" ? "is-active" : null)" asp-action="Index" asp-route-status="admins" asp-route-pageSize="@Model.PageSize">@L["Admins"]</a>
  }
  <a class="admin-tab @(status == "disabled" ? "is-active" : null)" asp-action="Index" asp-route-status="disabled" asp-route-pageSize="@Model.PageSize">@L["Disabled"]</a>
  <a class="admin-tab @(status == "unverified" ? "is-active" : null)" asp-action="Index" asp-route-status="unverified" asp-route-pageSize="@Model.PageSize">@L["Pending verification"]</a>
  <a class="admin-tab @(status == "blacklisted" ? "is-active" : null)" asp-action="Index" asp-route-status="blacklisted" asp-route-pageSize="@Model.PageSize">@L["Blacklist"]</a>
</nav>

<form method="get" class="admin-table-toolbar">
  <input type="hidden" name="status" value="@status" />
  <input type="hidden" name="pageSize" value="@Model.PageSize" />
  <input type="search" name="search" value="@Model.Search" placeholder="@L["Search"]" aria-label="@L["Search"]" />
  <button type="submit" class="data-table-icon-button" aria-label="@L["Search"]">⌕</button>
</form>

<div class="data-table-shell">
  @if (status == "blacklisted")
  {
    <table class="data-table">
      <thead>
        <tr>
          <th class="data-table-index">#</th>
          <th>@L["Email"]</th>
          <th>@L["Reason"]</th>
          <th>@L["Blacklisted By"]</th>
          <th>@L["Blacklisted At"]</th>
          <th>@L["Action"]</th>
        </tr>
      </thead>
      <tbody>
      @if (!Model.BlacklistItems.Any())
      {
        <tr>
          <td colspan="6" class="data-table-empty">@L["No blacklist records."]</td>
        </tr>
      }
      else
      {
        var rowNumber = firstRowNumber;
        foreach (var item in Model.BlacklistItems)
        {
          <tr>
            <td class="data-table-index">@rowNumber.ToString("000")</td>
            <td>@item.Email</td>
            <td>@item.Reason</td>
            <td>@item.BlacklistedBy</td>
            <td>@item.BlacklistedAt.ToString("yyyy/MM/dd HH:mm")</td>
            <td><span class="data-table-muted">@L["Detail"]</span></td>
          </tr>
          rowNumber++;
        }
      }
      </tbody>
    </table>
  }
  else
  {
    <table class="data-table">
      <thead>
        <tr>
          <th class="data-table-index">#</th>
          <th>@L["Email"]</th>
          <th>@L["Name"]</th>
          <th>@L["Status"]</th>
          <th>@L["Verified"]</th>
          <th>@L["Created"]</th>
          <th>@L["Action"]</th>
        </tr>
      </thead>
      <tbody>
      @if (!Model.Items.Any())
      {
        <tr>
          <td colspan="7" class="data-table-empty">@L["No member records."]</td>
        </tr>
      }
      else
      {
        var rowNumber = firstRowNumber;
        foreach (var item in Model.Items)
        {
          var fullName = string.Join(" ", new[] { item.LastName, item.FirstName }.Where(value => !string.IsNullOrWhiteSpace(value)));
          var statusSummary = item.IsDisabled ? L["Frozen"].Value : item.IsBlacklisted ? L["Blacklist"].Value : L["Normal"].Value;
          var canDisable = !item.IsAdmin && !item.IsSuperuser;
          var canResetPassword = !item.IsSuperuser && (Model.OperatorIsSuperuser || !item.IsAdmin);
          var canAssignAdmin = Model.OperatorIsSuperuser && !item.IsSuperuser && !item.IsDisabled;
          var canContact = !item.IsSuperuser;
          var isAdminsTab = status == "admins";
          <tr>
            <td class="data-table-index">@rowNumber.ToString("000")</td>
            <td>@item.Email</td>
            <td>@(string.IsNullOrWhiteSpace(fullName) ? item.NickName ?? "-" : fullName)</td>
            <td>@statusSummary</td>
            <td>@(item.EmailConfirmed ? L["Verified"] : L["Unverified"])</td>
            <td>@item.CreatedAt.ToString("yyyy/MM/dd HH:mm")</td>
            <td>
              <div class="data-table-actions">
                <a class="data-table-pill-button" asp-action="Details" asp-route-id="@item.UserId">@L["Detail"]</a>

              @if (isAdminsTab)
              {
                @if (canAssignAdmin && item.IsAdmin)
                {
                  <form method="post" asp-action="SetAdmin" asp-route-id="@item.UserId" class="data-table-action-form">
                    @Html.AntiForgeryToken()
                    <input type="hidden" name="enabled" value="false" />
                    <input type="hidden" name="search" value="@Model.Search" />
                    <input type="hidden" name="status" value="@status" />
                    <input type="hidden" name="page" value="@Model.Page" />
                    <input type="hidden" name="pageSize" value="@Model.PageSize" />
                    <button type="submit" class="data-table-pill-button">@L["Remove admin role"]</button>
                  </form>
                }
              }
              else
              {
              @if (canDisable)
              {
                <form method="post" asp-action="SetDisabled" asp-route-id="@item.UserId" class="data-table-action-form">
                  @Html.AntiForgeryToken()
                  <input type="hidden" name="disabled" value="@(item.IsDisabled ? "false" : "true")" />
                  <input type="hidden" name="search" value="@Model.Search" />
                  <input type="hidden" name="status" value="@status" />
                  <input type="hidden" name="page" value="@Model.Page" />
                  <input type="hidden" name="pageSize" value="@Model.PageSize" />
                  <button type="submit" class="data-table-pill-button">@(item.IsDisabled ? L["Unfreeze account"] : L["Freeze account"])</button>
                </form>
              }

              @if (canResetPassword)
              {
                <a class="data-table-pill-button" href="#reset-password-@item.UserId">@L["Change Password"]</a>
              }

              @if (canContact)
              {
                <button type="button" class="data-table-pill-button" disabled>@L["Contact"]</button>
              }

              @if (canAssignAdmin)
              {
                <form method="post" asp-action="SetAdmin" asp-route-id="@item.UserId" class="data-table-action-form">
                  @Html.AntiForgeryToken()
                  <input type="hidden" name="enabled" value="@(item.IsAdmin ? "false" : "true")" />
                  <input type="hidden" name="search" value="@Model.Search" />
                  <input type="hidden" name="status" value="@status" />
                  <input type="hidden" name="page" value="@Model.Page" />
                  <input type="hidden" name="pageSize" value="@Model.PageSize" />
                  <button type="submit" class="data-table-pill-button">@(item.IsAdmin ? L["Remove admin role"] : L["Assign admin role"])</button>
                </form>
              }
              }
              </div>
              @if (!isAdminsTab && canResetPassword)
              {
                <div id="reset-password-@item.UserId" class="admin-modal" role="dialog" aria-modal="true" aria-labelledby="reset-password-title-@item.UserId">
                  <a class="admin-modal-backdrop" href="#" aria-label="@L["Close password reset dialog"]"></a>
                  <div class="admin-modal-panel">
                    <a class="admin-modal-close" href="#" aria-label="@L["Close password reset dialog"]">×</a>
                    <h2 id="reset-password-title-@item.UserId">@L["Change Password"]</h2>
                    <p>@item.Email</p>
                    <form method="post" asp-action="ResetPassword" asp-route-id="@item.UserId" class="admin-modal-form">
                      @Html.AntiForgeryToken()
                      <input type="password" name="newPassword" placeholder="@L["Enter new password"]" minlength="8" required />
                      <input type="hidden" name="search" value="@Model.Search" />
                      <input type="hidden" name="status" value="@status" />
                      <input type="hidden" name="page" value="@Model.Page" />
                      <input type="hidden" name="pageSize" value="@Model.PageSize" />
                      <button type="submit" class="account-action-button">@L["Confirm change"]</button>
                    </form>
                  </div>
                </div>
              }
            </td>
          </tr>
          rowNumber++;
        }
      }
      </tbody>
    </table>
  }
</div>

<div class="admin-table-pager">
  <span>@Model.Page / @Model.TotalPages</span>
  <a class="@(Model.HasPreviousPage ? null : "is-disabled")" asp-action="Index" asp-route-status="@status" asp-route-search="@Model.Search" asp-route-page="@(Model.Page - 1)" asp-route-pageSize="@Model.PageSize">@L["Prev"]</a>
  <a class="@(Model.HasNextPage ? null : "is-disabled")" asp-action="Index" asp-route-status="@status" asp-route-search="@Model.Search" asp-route-page="@(Model.Page + 1)" asp-route-pageSize="@Model.PageSize">@L["Next"]</a>
</div>
