@model IReadOnlyList<object>

<div class="admin-page-title">
  <h1>@L["OAuth Clients"]</h1>
@if (await AdminPermissionChecker.HasPermissionAsync(User, AdminPermissions.OAuthClientsCreate))
{
  <a class="account-action-button" href="/admin/oauth-clients/create">@L["Add Client"]</a>
}
</div>
@if (TempData["CreatedClientId"] is string createdId)
{
  <div class="admin-notice">
    <strong>@L["Client Created"]</strong><br />
    <div>@L["Client ID:"] <code>@createdId</code></div>
    @if (TempData["CreatedClientSecret"] is string createdSecret)
    {
      <div>@L["Client Secret (show once):"] <code>@createdSecret</code></div>
    }
  </div>
}
@if (TempData["RotatedClientId"] is string rotatedId)
{
  <div class="admin-notice">
    <strong>@L["Client Secret Rotated"]</strong><br />
    <div>@L["Client ID:"] <code>@rotatedId</code></div>
    @if (TempData["RotatedClientSecret"] is string rotatedSecret)
    {
      <div>@L["New Client Secret (show once):"] <code>@rotatedSecret</code></div>
    }
  </div>
}
@if (TempData["GeneratedClientId"] is string generatedId)
{
  <div class="admin-notice">
    <strong>@L["Client Secret Generated"]</strong><br />
    <div>@L["Client ID:"] <code>@generatedId</code></div>
    @if (TempData["GeneratedClientSecret"] is string generatedSecret)
    {
      <div>@L["New Client Secret (show once):"] <code>@generatedSecret</code></div>
    }
  </div>
}
<div class="data-table-shell">
  <table class="data-table">
    <thead>
      <tr>
        <th class="data-table-index">#</th>
        <th>@L["Name"]</th>
        <th>@L["Client ID"]</th>
        <th>@L["Type"]</th>
        <th>@L["Usage"]</th>
        <th>@L["Action"]</th>
      </tr>
    </thead>
    <tbody>
    @if (!Model.Any())
    {
      <tr>
        <td colspan="6" class="data-table-empty">@L["No OAuth clients."]</td>
      </tr>
    }
    else
    {
      var rowNumber = 1;
      foreach (var item in Model)
      {
        var name = (string)item.GetType().GetProperty("name")!.GetValue(item)!;
        var clientId = (string)item.GetType().GetProperty("client_id")!.GetValue(item)!;
        var clientType = (string)item.GetType().GetProperty("client_type")!.GetValue(item)!;
        var usage = (string)item.GetType().GetProperty("usage")!.GetValue(item)!;
        var id = (string)item.GetType().GetProperty("id")!.GetValue(item)!;
        <tr>
          <td class="data-table-index">@rowNumber.ToString("000")</td>
          <td>@name</td>
          <td>@clientId</td>
          <td>@clientType</td>
          <td>@usage</td>
          <td>
            <div class="data-table-actions">
              @if (await AdminPermissionChecker.HasPermissionAsync(User, AdminPermissions.OAuthClientsEdit))
              {
                <a class="data-table-pill-button" href="/admin/oauth-clients/edit/@id">@L["Edit"]</a>
              }
              @if (string.Equals(clientType, "confidential", StringComparison.OrdinalIgnoreCase)
                  && await AdminPermissionChecker.HasPermissionAsync(User, AdminPermissions.OAuthClientsRotateSecret))
              {
                <form method="post" action="/admin/oauth-clients/rotate-secret/@id" class="data-table-action-form">
                  @Html.AntiForgeryToken()
                  <button type="submit" class="data-table-pill-button">@L["Rotate Secret"]</button>
                </form>
              }
              @if (await AdminPermissionChecker.HasPermissionAsync(User, AdminPermissions.OAuthClientsDelete))
              {
                <form method="post" action="/admin/oauth-clients/delete/@id" class="data-table-action-form">
                  @Html.AntiForgeryToken()
                  <button type="submit" class="data-table-pill-button">@L["Delete"]</button>
                </form>
              }
            </div>
          </td>
        </tr>
        rowNumber++;
      }
    }
    </tbody>
  </table>
</div>
