{% extends 'base.html' %}

{% block content %}
    <section>

      <h1>Domains</h1>

      {% for domain in domains %}
        <h2>
          {{ domain.name }}
          <form method="post"
                action="{{ url_for('.domain_details', domain_id=domain.id) }}"
                class="form-inline">
            <input type="hidden" name="_method" value="DELETE">
            <input type="submit"
                   value="Delete domain"
                   class="btn btn-danger btn-xs">
          </form>
        </h2>

        <form method="post"
              action="{{ url_for('.domain_details', domain_id=domain.id) }}">
              {{ domain.form.csrf_token }}
              {{ domain.form.mname.label }}{{ domain.form.mname }}<br>
              {{ domain.form.rname.label }}{{ domain.form.rname }}<br>

              <input type="submit" value="Update" class="btn btn-primary btn-xs">
        </form>

        <table class="table">
          <tr><th>Domain</th><th>Type</th><th>Value</th><th>DynDNS</th><th>Actions</th></tr>
          {% for record in domain.records %}
            {% if record.type and record.type != 'SOA' %}
              <tr>
                <td>
                  <input type="text"
                         name="name"
                         form="record-form-{{ loop.index }}"
                         value="{{ record.name }}">
                </td>
                <td>{{ record.type }}</td>
                <td title="{{ record.content }}">
                  <input type="text"
                         name="content"
                         value="{{ record.content }}"
                         form="record-form-{{ loop.index }}">
                </td>
                <td>
                  {% if record.dyndns_client %}
                    <input type="text"
                           class="input-copyable-key"
                           value="{{ record.dyndns_client.printable_key }}">
                    <form method="post"
                          action="{{ url_for('.rekey_dyndns_record', record_id=record.id) }}"
                          class="form-inline">
                      <input type="submit"
                             value="Rekey"
                             class="btn btn-primary btn-xs">
                    </form>
                  {% elif record.type in ('A', 'AAAA') %}
                    <form action="{{ url_for('.new_dyndns_client', record_id=record.id) }}" method="post" class="form-inline">
                      <input type="submit" value="Generate DynDNS secret" class="btn btn-primary btn-xs">
                    </form>
                  {% endif %}
                </td>
                <td>
                  {# Submit update #}
                  <form method="post"
                        id="record-form-{{ loop.index }}"
                        action="{{ url_for('.record_details', record_id=record.id) }}"
                        class="form-inline">
                    {{ recordform.csrf_token }}
                    <input type="hidden"
                           value="{{ record.type }}"
                           name="type">
                    <input type="submit"
                           value="Update record"
                           class="btn btn-primary btn-xs">
                  </form>

                  {# Delete record form #}
                  <form method="post"
                        class="form-inline"
                        action="{{ url_for('.record_details', record_id=record.id) }}">
                    <input type="hidden"
                           value="DELETE"
                           name="_method">
                    <input type="submit"
                           value="Delete record"
                           class="btn btn-danger btn-xs">
                  </form>
                </td>
              </tr>
            {% endif %}
          {% endfor %}
        </table>

        <form action="{{ url_for('.new_record', domain_id=domain.id) }}" method="post">
          {{ recordform.render() }}
          <input type="submit" value="New record" class="btn btn-primary">
        </form>

        <h4>TSIG keys</h4>
        {% for tsigkey in domain.tsigkeys %}
        <label>
          {{ tsigkey.name }}:
          <input value="{{ tsigkey.secret }}" class="input-copyable-tsig">
        </label> ({{ tsigkey.algorithm }})

        <form action="{{ url_for('.tsigkey_details', domain_id=domain.id, tsig_name=tsigkey.name) }}"
              method="post"
              class="form-inline">
            <input type="hidden" name="_method" value="DELETE">
            <input type="submit" name="Delete" class="btn btn-danger btn-xs" value="Delete key">
        </form>

        <br>
        {% endfor %}

        <form action="{{ url_for('.domain_tsigkeys', domain_id=domain.id) }}" method="post">
          {{ tsigkeyform.render() }}
          <input type="submit" value="New DynDNS key" class="btn btn-primary">
        </form>
      {% endfor %}

      <h3>New domain</h3>
      <form action="/domains" method="post">
        {{ domainform.render() }}
        <input type="submit" value="New domain">
      </form>

    </section>

{% endblock %}
