{% extends "base.html" %}

{% block title %}{{ bot.name }} - irc.now{% endblock %}

{% block banner %}{% include "partials/announcement.html" %}{% endblock %}
{% block flash %}{% include "partials/flash.html" %}{% endblock %}
{% block content %}
<div class="content">
  <h1>{{ bot.name }}</h1>

  <div class="card">
    <h2>status</h2>
    {% if bot.running %}
    <p style="color: var(--g);">running</p>
    <form method="post" action="/bots/{{ bot.id }}/stop"
          hx-post="/bots/{{ bot.id }}/stop" hx-swap="none">
      <button type="submit" class="btn-outline">
        <span class="htmx-hide-on-request">stop</span>
        <span class="htmx-indicator">stopping...</span>
      </button>
    </form>
    {% elif bot.status == "error" %}
    <p style="color: #E08080;">error</p>
    {% if let Some(msg) = bot.status_message %}
    <p style="font-size: 12px; font-family: var(--mono); color: var(--text-3);">{{ msg }}</p>
    {% endif %}
    <form method="post" action="/bots/{{ bot.id }}/start"
          hx-post="/bots/{{ bot.id }}/start" hx-swap="none">
      <button type="submit" class="btn-fill">
        <span class="htmx-hide-on-request">start</span>
        <span class="htmx-indicator">starting...</span>
      </button>
    </form>
    {% else %}
    <p style="color: var(--text-3);">stopped</p>
    <form method="post" action="/bots/{{ bot.id }}/start"
          hx-post="/bots/{{ bot.id }}/start" hx-swap="none">
      <button type="submit" class="btn-fill">
        <span class="htmx-hide-on-request">start</span>
        <span class="htmx-indicator">starting...</span>
      </button>
    </form>
    {% endif %}
  </div>

  <form method="post" action="/bots/{{ bot.id }}/update"
        hx-post="/bots/{{ bot.id }}/update" hx-swap="none">
    <div class="card">
      <h2>configuration</h2>
      <div class="form-group" style="margin-bottom: 12px;">
        <label for="name">name</label>
        <input class="input" type="text" id="name" name="name" value="{{ bot.name }}" required>
      </div>
      <div class="form-group" style="margin-bottom: 12px;">
        <label for="nick">nick</label>
        <input class="input" type="text" id="nick" name="nick" value="{{ bot.nick }}" required>
      </div>
      <div class="form-group" style="margin-bottom: 12px;">
        <label for="network_addr">network address</label>
        <input class="input" type="text" id="network_addr" name="network_addr" value="{{ bot.network_addr }}">
      </div>
      <div class="form-group" style="margin-bottom: 12px;">
        <label for="sasl_username">sasl username</label>
        <input class="input" type="text" id="sasl_username" name="sasl_username" value="{{ bot.sasl_username.as_deref().unwrap_or_default() }}">
      </div>
      <div class="form-group" style="margin-bottom: 12px;">
        <label for="sasl_password">sasl password</label>
        <input class="input" type="password" id="sasl_password" name="sasl_password" placeholder="leave blank to keep current">
      </div>
      <div class="form-group" style="margin-bottom: 14px;">
        <label for="channels">channels (comma-separated)</label>
        <input class="input" type="text" id="channels" name="channels" value="{{ channels_str }}">
      </div>
      <button type="submit" class="btn-fill">
        <span class="htmx-hide-on-request">save</span>
        <span class="htmx-indicator">saving...</span>
      </button>
    </div>
  </form>

  <div class="card">
    <h2>scripts</h2>
    {% if scripts.is_empty() %}
    <p>no scripts yet</p>
    {% else %}
    {% for script in scripts %}
    <a href="/bots/{{ bot.id }}/scripts/{{ script.id }}" style="text-decoration: none; color: inherit;">
      <div class="profile-field">
        <span class="profile-label">{{ script.name }}</span>
        <span>{% if script.enabled %}<span style="color: var(--g);">enabled</span>{% else %}<span style="color: var(--text-3);">disabled</span>{% endif %}</span>
      </div>
    </a>
    {% endfor %}
    {% endif %}
    <div style="margin-top: 12px;">
      <a href="/bots/{{ bot.id }}/scripts/create" class="btn-outline">new script</a>
    </div>
  </div>

  <div class="card">
    <h2>tools</h2>
    <div class="card-actions">
      <a href="/bots/{{ bot.id }}/logs" class="btn-outline">logs</a>
      <a href="/bots/{{ bot.id }}/kv" class="btn-outline">kv store</a>
    </div>
  </div>

  <div class="card">
    <h2>danger zone</h2>
    <p>deleting a bot removes all its scripts, logs, and kv data.</p>
    <form method="post" action="/bots/{{ bot.id }}/delete"
          hx-post="/bots/{{ bot.id }}/delete" hx-confirm="delete bot {{ bot.name }}? this cannot be undone." hx-swap="none">
      <button type="submit" class="btn-fill" style="background: #AA5555;">
        <span class="htmx-hide-on-request">delete bot</span>
        <span class="htmx-indicator">deleting...</span>
      </button>
    </form>
  </div>
</div>
{% endblock %}
