{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="float-end">
        <button type="submit" form="form-api" data-bs-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa-solid fa-floppy-disk"></i></button>
        <a href="{{ back }}" data-bs-toggle="tooltip" title="{{ button_back }}" class="btn btn-light"><i class="fa-solid fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ol class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
          <li class="breadcrumb-item"><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ol>
    </div>
  </div>
  <div class="container-fluid">
    <div class="card">
      <div class="card-header"><i class="fa-solid fa-pencil"></i> {{ text_form }}</div>
      <div class="card-body">
        <form id="form-api" action="{{ save }}" method="post" data-oc-toggle="ajax">
          <ul class="nav nav-tabs">
            <li class="nav-item"><a href="#tab-general" data-bs-toggle="tab" class="nav-link active">{{ tab_general }}</a></li>
            <li class="nav-item"><a href="#tab-ip" data-bs-toggle="tab" class="nav-link">{{ tab_ip }}</a></li>
            <li class="nav-item"><a href="#tab-history" data-bs-toggle="tab" class="nav-link">{{ tab_history }}</a></li>
          </ul>
          <div class="tab-content">
            <div id="tab-general" class="tab-pane active">
              <div class="row mb-3 required">
                <label for="input-username" class="col-sm-2 col-form-label">{{ entry_username }}</label>
                <div class="col-sm-10">
                  <input type="text" name="username" value="{{ username }}" placeholder="{{ entry_username }}" id="input-username" class="form-control"/>
                  <div id="error-username" class="invalid-feedback"></div>
                </div>
              </div>
              <div class="row mb-3 required">
                <label for="input-key" class="col-sm-2 col-form-label">{{ entry_key }}</label>
                <div class="col-sm-10">
                  <textarea name="key" placeholder="{{ entry_key }}" rows="5" id="input-key" class="form-control mb-1">{{ key }}</textarea>
                  <button type="button" id="button-generate" class="btn btn-primary"><i class="fa-solid fa-rotate"></i> {{ button_generate }}</button>
                  <div id="error-key" class="invalid-feedback"></div>
                </div>
              </div>
              <div class="row mb-3">
                <label class="col-sm-2 col-form-label">{{ entry_status }}</label>
                <div class="col-sm-10">
                  <div class="form-check form-switch form-switch-lg">
                    <input type="hidden" name="status" value="0"/>
                    <input type="checkbox" name="status" value="1" id="input-status" class="form-check-input"{% if status %} checked{% endif %}/>
                  </div>
                </div>
              </div>
            </div>
            <div id="tab-ip" class="tab-pane">
              <div class="alert alert-info"><i class="fa-solid fa-info-circle"></i> {{ text_ip }}</div>
              <div class="table-responsive">
                <table id="ip" class="table table-bordered table-hover">
                  <thead>
                    <tr>
                      <th>{{ entry_ip }}</th>
                      <th class="text-end"></th>
                    </tr>
                  </thead>
                  <tbody>
                    {% set ip_row = 0 %}
                    {% for api_ip in api_ips %}
                      <tr id="ip-row-{{ ip_row }}">
                        <td><input type="text" name="api_ip[]" value="{{ api_ip }}" placeholder="{{ entry_ip }}" class="form-control"/></td>
                        <td class="text-end"><button type="button" onclick="$('#ip-row-{{ ip_row }}').remove()" data-bs-toggle="tooltip" title="{{ button_remove }}" class="btn btn-danger"><i class="fa-solid fa-minus-circle"></i></button></td>
                      </tr>
                      {% set ip_row = ip_row + 1 %}
                    {% endfor %}
                  </tbody>
                  <tfoot>
                    <tr>
                      <td></td>
                      <td class="text-end"><button type="button" id="button-ip" data-bs-toggle="tooltip" title="{{ button_ip_add }}" class="btn btn-primary"><i class="fa-solid fa-plus-circle"></i></button></td>
                    </tr>
                  </tfoot>
                </table>
              </div>
            </div>
            <div id="tab-history" class="tab-pane">
              <fieldset>
                <legend>{{ text_history }}</legend>
                <div id="history">{{ history }}</div>
              </fieldset>
            </div>
          </div>
          <input type="hidden" name="api_id" value="{{ api_id }}" id="input-api-id"/>
        </form>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript"><!--
$('#button-generate').on('click', function() {
    rand = '';

    string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

    for (i = 0; i < 256; i++) {
        rand += string[Math.floor(Math.random() * (string.length - 1))];
    }

    $('#input-key').val(rand);
});

var ip_row = {{ ip_row }};

$('#button-ip').on('click', function() {
    html = '<tr id="ip-row-' + ip_row + '">';
    html += '  <td class="text-end"><input type="text" name="api_ip[]" value="" placeholder="{{ entry_ip|escape('js') }}" class="form-control"/></td>';
    html += '  <td class="text-end"><button type="button" onclick="$(\'#ip-row-' + ip_row + '\').remove();" data-bs-toggle="tooltip" title="{{ button_remove|escape('js') }}" class="btn btn-danger"><i class="fa-solid fa-minus-circle"></i></button></td>';
    html += '</tr>';

    $('#ip tbody').append(html);

    ip_row++;
});

$('#history').on('click', '.pagination a', function(e) {
    e.preventDefault();

    $('#history').load(this.href);
});
//--></script>
{{ footer }}