<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>玉龙科技</title>
  <link rel="shortcut icon" href="/web/static/src/img/favicon.ico" type="image/x-icon">
  <link rel="stylesheet" href="/web/static/lib/fontawesome/css/font-awesome.css">
  <link rel="stylesheet" href="/web/static/lib/bootstrap/css/bootstrap.css">
  <script src="/web/static/lib/jquery/jquery.js" type="text/javascript"></script>
  <script src="/web/static/lib/bootstrap/js/modal.js"></script>
  <script src="/web/static/lib/bootstrap/js/tooltip.js"></script>
  <script src="/web/static/lib/bootstrap/js/dropdown.js"></script>
  <script type="text/javascript">
  $(function() {
      // Little eye
      $('body').on('mousedown','.o_little_eye',function(ev) {
          $(ev.target).siblings('input').prop('type','text');
      });
      $('body').on('mouseup','.o_little_eye',function(ev) {
          $(ev.target).siblings('input').prop('type','password');
      });
      // db modal
      $('body').on('click','.o_database_action', function(ev) {
          ev.preventDefault();
          var db = $(ev.currentTarget).data('db');
          var target = $(ev.currentTarget).data('target');
          $(target).find('input[name=name]').val(db);
          $(target).modal();
      });
      // close modal on submit
      $('.modal').on('click','input[type="submit"]', function(ev) {
          var modal  = $(this).parentsUntil('body', '.modal');
          if (modal.hasClass('o_database_backup')) {
            $(modal).modal('hide');
            if (!$('.alert-backup-long').length) {
              $('.list-group').before("<div class='alert alert-info alert-backup-long'>备份下载可能花费一些时间，请耐心等候</div>");
            }
          }
      });
  });
  </script>
</head>

{% macro master_input() -%}
  <div class="form-group">
    {% if insecure %}
      <input type="hidden" name="master_pwd" class="form-control" value="admin"/>
    {% else %}
      <label for="master_pwd" class="control-label">主控密码</label>
      <input type="password" name="master_pwd" class="form-control" required="required" autofocus="autofocus"/>
    {% endif %}
  </div>
{%- endmacro %}

{% macro create_form() -%}
  <p>
    填写数据库信息
  </p>
  {{ master_input() }}
  <div class="form-group">
    <div class="row">
      <div class="col-md-12">
        <label for="name" class="control-label">数据库名字</label>
        <input type="text" name="name" class="form-control" required="required" autocomplete="off" pattern="{{ pattern }}" title="只可以使用字母、数字、下划线、点和横杠"/>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col-md-12">
        <label for="login" class="control-label">用户名/邮箱</label>
        <input id="login" type="text" name="login" class="form-control" required="required" autocomplete="off"/>
      </div>
    </div>
  </div>
  <div class="form-group has-feedback">
    <label for="password" class="control-label">密码</label>
    <input id="password" type="password" name="password" class="form-control" required="required" autocomplete="off"/>
    <span class="fa fa-eye o_little_eye form-control-feedback" aria-hidden="true" style="cursor: pointer; pointer-events: auto"></span>
  </div>
  <div class="form-group">
    <div class="row">
      <div class="col-md-6">
        <label for="lang" class="control-label">语言</label>
        <select id="lang" name="lang" class="form-control" required="required" autocomplete="off">
          {% for lang in langs %}
              <option {% if lang[0] == "zh_CN" %}selected="selected" {% endif %}value="{{ lang[0] }}">{{ lang[1] }}</option>
          {% endfor %}
        </select>
      </div>
      <div class="col-md-6">
          <label for="country" class="control-label">国家</label>
          <select id="country" name="country_code" class="form-control" autocomplete="off">
              <option value=""></option>
            {% for country in countries %}
              <option {% if country[0] == "cn" %}selected="selected" {% endif %}value="{{ country[0] }}">{{ country[1] }}</option>
            {% endfor %}
            </select>
        </div>
    </div>
  </div>
{%- endmacro %}

<body class="container">
  <!-- Database List -->
  <div class="row">
    <div class="col-md-6 col-md-offset-3 o_database_list">
      <div class="text-center">
        <img src="/jd_init/static/img/logo2.png?v={{ random_number }}" class="img-responsive center-block"/>
      </div>
      {% if insecure and databases %}
      <div class="alert alert-warning">
      您的数据库未被保护，建议<a href="#" data-toggle="modal" data-target=".o_database_master">设置一个主控密码</a>
      </div>
      {% endif %}
      {% if error %}
      <div class="alert alert-danger">{{ error }}</div>
      {% endif %}
      {% if databases %}
        <div class="list-group">
          {% for db in databases %}
          <a href="/web?db={{ db }}" class="list-group-item">
            {{ db }}
            {% if manage %}
            <div class="text-right pull-right">
            <span data-db="{{ db }}" data-target=".o_database_backup" class="o_database_action btn-link"><i class="fa fa-floppy-o fa-fw"></i> 备份</span>
            <span data-db="{{ db }}" data-target=".o_database_duplicate" class="o_database_action btn-link"><i class="fa fa-files-o fa-fw"></i> 复制</span>
            <span data-db="{{ db }}" data-target=".o_database_delete" class="o_database_action btn-link"><i class="fa fa-trash-o fa-fw"></i> 删除</span>
            </div>
            {% endif %}
          </a>
          {% endfor %}
        </div>
        {% if manage %}
        <div class="text-left">
          <button type="button" data-toggle="modal" data-target=".o_database_create" class="btn btn-sm btn-primary">
            创建数据库
          </button>
          <button type="button" data-toggle="modal" data-target=".o_database_restore" class="btn btn-sm btn-primary">
            恢复数据库
          </button>
          <button type="button" data-toggle="modal" data-target=".o_database_master" class="btn btn-sm btn-primary">
            设置主控密码
          </button>
        </div>
        {% else %}
        <div class="text-center">
          <a href="/web/database/manager">管理数据库</a>
        </div>
        {% endif %}
      {% else %}
        <form role="form" action="/web/database/create" method="post">
          {{ create_form() }}
          <input type="submit" value="创建数据库" class="btn btn-primary pull-left"/>
        </form>
        <a role="button" data-toggle="modal" data-target=".o_database_restore" class="btn btn-link">
          或恢复数据库
        </a>
      {% endif %}
    </div>
  </div>

  <!-- Create -->
  <div class="modal fade o_database_create" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <form role="form" action="/web/database/create" method="post">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">创建数据库</h4>
          </div>
          <div class="modal-body">
            {{ create_form() }}
          </div>
          <div class="modal-footer">
            <input type="submit" value="确定" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Restore -->
  <div class="modal fade o_database_restore" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">恢复数据库</h4>
        </div>
        <form id="form_restore_db" role="form" action="/web/database/restore" method="post" enctype="multipart/form-data">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="backup_file" class="control-label">文件</label>
              <input id="backup_file" type="file" name="backup_file" class="required"/>
            </div>
            <div class="form-group">
              <label for="name" class="control-label">数据库名字</label>
              <input type="text" name="name" class="form-control" required="required" pattern="{{ pattern }}" placeholder="字母、数字、点、横杠和下划线,不能以下划线和横杠开头"/>
            </div>
            <div class="form-group">
                <label for="copy">请选择恢复类型</label>
              <div class="radio">
                <label>
                  <input name="copy" type="radio" class="pull-right" value="true" checked="1">
                  备份恢复
                </label>
              </div>
              <div class="radio">
                <label>
                  <input name="copy" type="radio" class="pull-right" value="false">
                  移动恢复
                </label>
              </div>
            </div>
          </div>
          <div class="modal-footer">
            <input type="submit" value="Continue" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>


  <!-- Master password -->
  <div class="modal fade o_database_master" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">设置主控密码</h4>
        </div>
        <form id="form_change_pwd" role="form" action="/web/database/change_password" method="post">
          <div class="modal-body">
            <p>主控密码用于创建、删除、导出或导入数据库</p>
            {{ master_input() }}
            <div class="form-group has-feedback">
              <label for="master_pwd_new" class="control-label">新的主控密码</label>
              <input id="master_pwd_new" type="password" name="master_pwd_new" class="form-control" required="required" autocomplete="off"/>
              <span class="fa fa-eye o_little_eye form-control-feedback" aria-hidden="true" style="cursor: pointer; pointer-events: auto"></span>
            </div>
          </div>
          <div class="modal-footer">
            <input type="submit" value="完成" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>


  <!-- Duplicate DB -->
  <div class="modal fade o_database_duplicate" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">复制数据库</h4>
        </div>
        <form id="form-duplicate-db" role="form" action="/web/database/duplicate" method="post">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="name" class="control-label">数据库名字</label>
              <input type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
            <div class="form-group">
              <label for="new_name" class="control-label">新名字</label>
              <input id="new_name" type="text" name="new_name" class="form-control" required="required" pattern="{{ pattern }}" title="只可以使用字母、数字、下划线、点和横杠"/>
            </div>
          </div>
          <div class="modal-footer">
          <input type="submit" value="确认" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Drop DB -->
  <div class="modal fade o_database_delete" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">删除数据库</h4>
        </div>
        <form id="form_drop_db" role="form" action="/web/database/drop" method="post">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="name" class="control-label">数据库</label>
              <input type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
          </div>
          <div class="modal-footer">
            <input type="submit" value="删除" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

  <!-- Backup DB -->
  <div class="modal fade o_database_backup" role="dialog">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">备份数据库</h4>
        </div>
        <form id="form_backup_db" role="form" action="/web/database/backup" method="post">
          <div class="modal-body">
            {{ master_input() }}
            <div class="form-group">
              <label for="name" class="control-label">数据库名字</label>
              <input type="text" name="name" class="form-control" required="required" readonly="readonly"/>
            </div>
            <div class="form-group">
              <label for="backup_format" class="control-label">备份格式</label>
              <select id="backup_format" name="backup_format" id="backup_format" class="form-control" required="required">
                <option value="zip">zip (includes filestore)</option>
                <option value="dump">pg_dump custom format (without filestore)</option>
              </select>
            </div>
          </div>
          <div class="modal-footer">
          <input type="submit" value="备份" class="btn btn-primary pull-right"/>
          </div>
        </form>
      </div>
    </div>
  </div>

</body>
</html>
