{% extends ../../../templates/base.html %}

{% block title %}[{{instance.hostname}}:{{instance.pg_port}}] - Configuration / {% if file_type == 'hba' %}Host Base Authentication{% end %}{%end%}

{% block content %}
<!-- HBA row edit modal -->
<div class="modal fade" id="HBAModal" tabindex="-1" role="dialog" aria-labelledby="HBAModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title" id="HBAModalLabel"></h4>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      </div>
      <div class="modal-body" id="HBAModalInfo">
      </div>
      <div class="modal-body" id="HBAModalBody">
      </div>
      <div class="modal-footer" id="HBAModalFooter">
      </div>
    </div>
  </div>
</div>

<div class="limiter">

{% include menu.html %}

{% try %}
{% if ret_post %}
<div class="row">
  <div class="col-12">
    <div id="ok-configuration" class="alert alert-success alert-dismissible" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <h2><i class="fa fa-check-circle fa-fw"></i> OK </h2>
      <p><strong>{{file_content['filepath']}}</strong> file successfully updated and PostgreSQL configuration reloaded.</p>
    </div>
  </div>
</div>
{% end %}
{% except %}
{% end %}

{% try %}
{% if error_code > 0 %}
<div class="row">
  <div class="col-12">
    <div id="error-configuration" class="alert alert-danger" role="alert">
      <h2><i class="fa fa-ban fa-fw"></i> Error <small></small></h2>
      <p>{{error_code}}: {{error_message}}</p>
    </div>
  </div>
</div>
{% end %}
{% except %}
{% end %}
<div class="row mb-2">
  <div class="col d-flex">
    <div class="dropdown mr-auto">
      <button class="btn btn-sm btn-outline-secondary dropdown-toggle"
              data-toggle="dropdown" role="button" id="selectHBAVersion">Version: {{ conf_file['version'] if conf_file['version'] else 'Current'}}
      </button>
      <div class="dropdown-menu" aria-labelledby="selectHBAVersion">
        <a class="dropdown-item {% if not conf_file['version'] %}active{% end %}"
           href="/server/{{instance.agent_address}}/{{instance.agent_port}}/pgconf/hba">CURRENT</a>
        {% for version in file_versions['versions'] %}
        <a class="dropdown-item {% if version == conf_file['version'] %}active{% end %}"
           href="/server/{{instance.agent_address}}/{{instance.agent_port}}/pgconf/hba?version={{version}}">{{version}}</a>
        {% end %}
      </div>
    </div>
    <button class="btn btn-sm btn-success add">+ Add a new row</button>
  </div>
</div>
<div class="row">
  <div class="col-12">
    <div id="divtopHBA">
      <table id="HBATable" class="table table-bordered table-sm table-hover">
      <tbody>
      <tr data-header="1">
        <th width="58"></th>
        <th width="20">#</th>
        <th>Connection</th>
        <th>Database</th>
        <th>User</th>
        <th>Address</th>
        <th width="50">Method</th>
        <th>Options</th>
        <th width="58"></th>
      </tr>
      {% set no = 0 %}
      {% for record in conf_file['entries'] %}
        {% set no += 1 %}
      <tr data-line-number="{{no}}">
        <td>
          <button type="button" class="btn btn-outline-secondary btn-sm up" data-toggle="tooltip" data-placement="bottom" title="Move up"><i class="fa fa-arrow-up"></i></button>
          <button type="button" class="btn btn-outline-secondary btn-sm down" data-toggle="tooltip" data-placement="bottom" title="Move down"><i class="fa fa-arrow-down"></i></button>
        </td>
        <td class="text-center"><span class="no">{{no}}</span></td>
        {% if 'comment' in record %}
        <td colspan="6" class="comment"># {{record['comment']}}</td>
        {% else %}
        <td class="connection">{{record['connection']}}</td>
        <td class="database">{{record['database']}}</td>
        <td class="user">{{record['user']}}</td>
        <td class="address">{{record['address']}}</td>
        <td class="text-center auth_method"><span class="badge badge-{% if record['auth_method'] == 'trust' %}danger{% elif record['auth_method'] == 'password' %}warning{% else %}success{% end %}">{{record['auth_method']}}</span></td>
        <td class="auth_options">{{record['auth_options']}}</td>
        {% end %}
        <td>
          <button type="button" class="btn btn-outline-secondary btn-sm edit" data-toggle="tooltip" data-placement="bottom" title="Edit this row"><i class="fa fa-edit"></i></button>
          <button type="button" class="btn btn-outline-secondary btn-sm remove" data-toggle="tooltip" data-placement="bottom" title="Remove this row"><i class="fa fa-trash-o"></i></button>
        </td>
      </tr>
      {% end %}
      </tbody>
      </table>
    </div>
  </div>
</div>
<div class="row">
  <div class="col-12 text-center">
    {% if conf_file['version'] %}
    <button type="submit" class="btn btn-sm btn-danger remove-version">Remove this version</button>
    {% end %}
    <a href="/server/{{instance.agent_address}}/{{instance.agent_port}}/pgconf/hba" class="btn btn-sm btn-warning cancel">Cancel</a>
    <button type="submit" class="btn btn-sm btn-success save">Save and reload configuration</button>
  </div>
</div>
</div> <!-- end limiter -->
<script src="/js/bootstrap-multiselect.js"></script>
<script src="/js/bootstrap-tagsinput.min.js"></script>
<script src="/js/typeahead.bundle.js"></script>
<script src="/js/pgconf/temboard.pgconf.js"></script>
<script type="text/javascript">
var xsession = "{{xsession}}";
var agent_address = "{{instance.agent_address}}";
var agent_port = "{{instance.agent_port}}";
$(document).ready(function() {
  var tableid = "HBATable";
  var modalid = "HBAModal";

  function init()
  {
    $(".up,.down").click(function(){
      var row = $(this).parents("tr:first");
      $(this).tooltip("hide");
      if ($(this).is(".up")) {
        tr_move("up", row, tableid);
      } else {
        tr_move("down", row, tableid);
      }
    });
    $(".remove").click(function(){
      var row = $(this).parents("tr:first");
      $(this).tooltip("hide");
      tr_remove(row, tableid);
    });
    $(".edit").click(function () {
      $('#HBAModal').modal('show');
      $('[data-toggle=popover]').popover('hide');
      var row = $(this).parents("tr:first");
      row_edit(row, tableid, modalid, agent_address, agent_port, xsession, null);
    });
  }
  init();
  $(".add").click(function (){
    $('#HBAModal').modal('show');
    $('[data-toggle=popover]').popover('hide');
    row_edit(null, tableid, modalid, agent_address, agent_port, xsession, 'record');
  });

  $(".save").click(function(){
    save_hba_table(tableid, modalid, agent_address, agent_port, xsession);
  });

  {% if conf_file['version'] %}

  $(".remove-version").click(function(){
    try {
      var version = "{{conf_file['version']}}";
      delete_hba(modalid, agent_address, agent_port, xsession, version);
    } catch(e) {
      return;
    }
  });

  {% end %}
});
</script>
<link href="/css/pgconf/temboard.pgconf.css" rel="stylesheet">
{% end %}
