<?php
/** @var array $repos @var array $users @var string $currentUsername
 *  @var ?string $error @var ?string $success @var string $csrf */
$warnIcon = '<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">'
    . '<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Z"></path>'
    . '<rect x="7.25" y="4" width="1.5" height="5.5" rx="0.75"></rect><circle cx="8" cy="11.75" r="0.9"></circle></svg>';
$okIcon = '<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">'
    . '<path d="M8 0a8 8 0 1 1 0 16A8 8 0 0 1 8 0ZM8 1.5a6.5 6.5 0 1 0 0 13 6.5 6.5 0 0 0 0-13Z"></path>'
    . '<path d="M4.5 8.2 7 10.7l4.5-5" style="fill:none;stroke:currentColor;stroke-width:1.6;stroke-linecap:round;stroke-linejoin:round"></path></svg>';
$plusIcon = '<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">'
    . '<rect x="7" y="2" width="2" height="12" rx="1"></rect><rect x="2" y="7" width="12" height="2" rx="1"></rect></svg>';
$trashIcon = '<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">'
    . '<rect x="6" y="1.5" width="4" height="2" rx="0.5"></rect><rect x="2.5" y="4" width="11" height="1.5" rx="0.5"></rect>'
    . '<rect x="3.5" y="6.2" width="9" height="8.3" rx="1"></rect></svg>';
// exact same path as the repo icon in partials/head.php / repo-index.php, reused here
$repoIcon = '<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true"><path d="M2 2.5A2.5 2.5 0 0 1 4.5 0h8.75a.75.75 0 0 1 .75.75v12.5a.75.75 0 0 1-.75.75h-2.5a.75.75 0 0 1 0-1.5h1.75v-2h-8a1 1 0 0 0-.714 1.7.75.75 0 1 1-1.072 1.05A2.495 2.495 0 0 1 2 11.5Zm10.5-1h-8a1 1 0 0 0-1 1v6.708A2.486 2.486 0 0 1 4.5 9h8ZM5 12.25a.25.25 0 0 1 .25-.25h3.5a.25.25 0 0 1 .25.25v3.25a.25.25 0 0 1-.4.2l-1.45-1.087a.249.249 0 0 0-.3 0L5.4 15.7a.25.25 0 0 1-.4-.2Z"></path></svg>';
$personIcon = '<svg class="icon" width="16" height="16" viewBox="0 0 16 16" aria-hidden="true">'
    . '<circle cx="8" cy="5" r="3"></circle><path d="M2 14.5c0-3.5 2.7-6 6-6s6 2.5 6 6" style="fill:none;stroke:currentColor;stroke-width:2;stroke-linecap:round"></path></svg>';
?>
<h1>Admin</h1>

<?php if ($error !== null): ?>
<div class="msg-error"><?= $warnIcon ?><div><?= h($error) ?></div></div>
<?php endif; ?>
<?php if ($success !== null): ?>
<div class="msg-ok"><?= $okIcon ?><div><?= h($success) ?></div></div>
<?php endif; ?>

<div class="box">
  <div class="box-header"><?= $repoIcon ?> Repositories</div>
  <?php if (!$repos): ?>
  <table><tr><td class="desc">No repositories.</td></tr></table>
  <?php else: ?>
  <table>
    <thead><tr><th>Name</th><th>Description</th><th></th></tr></thead>
    <?php foreach ($repos as $name => $meta): ?>
    <?php $bareName = repo_display_name($name); // create_bare_repo()/delete_bare_repo() take the name WITHOUT .git ?>
    <tr>
      <td><a href="?r=<?= h($name) ?>"><?= h($bareName) ?></a></td>
      <td class="desc"><?= h($meta['desc']) ?></td>
      <td class="num">
        <details class="row-actions">
          <summary title="Delete <?= h($bareName) ?>" aria-label="Delete <?= h($bareName) ?>"><?= $trashIcon ?></summary>
          <form class="confirm-delete" method="post" action="?a=admin">
            <input type="hidden" name="csrf" value="<?= h($csrf) ?>">
            <input type="hidden" name="sub" value="delete-repo">
            <input type="hidden" name="name" value="<?= h($bareName) ?>">
            <input type="text" name="confirm" placeholder="type &quot;<?= h($bareName) ?>&quot; to confirm" required>
            <button type="submit" class="danger">Delete</button>
          </form>
        </details>
      </td>
    </tr>
    <?php endforeach; ?>
  </table>
  <?php endif; ?>
</div>

<div class="box">
  <div class="box-header"><?= $plusIcon ?> New repository</div>
  <form class="form" method="post" action="?a=admin">
    <input type="hidden" name="csrf" value="<?= h($csrf) ?>">
    <input type="hidden" name="sub" value="create-repo">
    <div class="field-row">
      <input type="text" id="repo-name" name="name" placeholder="repository name" pattern="[A-Za-z0-9._-]+" required
             aria-label="New repository name">
      <button type="submit" class="primary">Create repo</button>
    </div>
    <p class="field-hint desc">Letters, digits, dot, dash and underscore. Created bare, and push-ready over SSH and HTTPS.</p>
  </form>
</div>

<div class="box">
  <div class="box-header"><?= $personIcon ?> Accounts</div>
  <table>
    <thead><tr><th>Username</th><th>Role</th><th>Created</th><th></th></tr></thead>
    <?php foreach ($users as $name => $user): ?>
    <tr>
      <td><?= h($name) ?></td>
      <td class="desc"><?= $user['is_admin'] ? 'admin' : 'user' ?></td>
      <td class="desc"><?= h(format_relative_time($user['created'])) ?></td>
      <td class="num">
        <?php if ($name !== $currentUsername): ?>
        <details class="row-actions">
          <summary title="Delete <?= h($name) ?>" aria-label="Delete <?= h($name) ?>"><?= $trashIcon ?></summary>
          <form class="confirm-delete" method="post" action="?a=admin">
            <input type="hidden" name="csrf" value="<?= h($csrf) ?>">
            <input type="hidden" name="sub" value="delete-user">
            <input type="hidden" name="username" value="<?= h($name) ?>">
            <button type="submit" class="danger">Delete account &amp; revoke its keys</button>
          </form>
        </details>
        <?php endif; ?>
      </td>
    </tr>
    <?php endforeach; ?>
  </table>
</div>

<div class="box">
  <div class="box-header"><?= $plusIcon ?> New account</div>
  <form class="form" method="post" action="?a=admin">
    <input type="hidden" name="csrf" value="<?= h($csrf) ?>">
    <input type="hidden" name="sub" value="add-user">
    <div class="form-row">
      <label for="new-username">Username</label>
      <input type="text" id="new-username" name="username" placeholder="username" autocomplete="off" required>
    </div>
    <div class="form-row">
      <label for="new-password">Password</label>
      <input type="password" id="new-password" name="password" placeholder="at least 8 characters" autocomplete="new-password" required minlength="8">
    </div>
    <div class="form-row check-row">
      <label for="new-is-admin"><input type="checkbox" id="new-is-admin" name="is_admin"> Can manage repos and accounts</label>
    </div>
    <button type="submit" class="primary">Create account</button>
  </form>
</div>
