{% extends theme('base.html') %}

{% block title %}pyLoad - {{_('Settings')}}{% endblock %}

{% block head %}
<meta id="plugins-list" data-plugin='{{conf.plugin|tojson}}'/>
<link rel="stylesheet" type="text/css" href="{{theme_static('css/settings.css')}}" />
{% endblock %}

{% block footer %}
<script type="text/javascript" src="{{theme_template('js/settings.js')}}"></script>
{% endblock %}

{% block pageactions %}
{% if user.is_admin %}
  <div class="btn-group btn-group-sm" role="group" aria-label="...">
    <button id="quit-pyload" class="btn btn-danger" data-toggle="modal" data-target="#quit_box"><span class="glyphicon glyphicon-off"></span> {{_('Quit pyLoad')}}</button>
    <button id="restart-pyload" class="btn btn-primary" data-toggle="modal" data-target="#restart_box"><span class="glyphicon glyphicon-repeat"></span> {{_('Restart pyLoad')}}</button>
  </div>
{% endif %}
{% endblock %}

{% block content %}
<ul id="toptabs" class="nav nav-tabs left col-xs-12 nav-settings">
  <li class="active" ondragstart="return false;"><a class="noselect" data-toggle="tab" href="#core">{{_('General')}}</a></li>
  <li ondragstart="return false;"><a class="noselect" data-toggle="tab" href="#plugins">{{_('Plugins')}}</a></li>
  <li ondragstart="return false;"><a class="noselect" data-toggle="tab" href="#accounts">{{_('Accounts')}}</a></li>
  {% if user.is_admin %}
    <li ondragstart="return false;"><a class="noselect" data-toggle="tab" href="#users">{{_('Users')}}</a></li>
  {% endif %}
</ul>

<div class="tab-content">
  <div id="core" class="tab-pane fade in active">
    <div class="panel panel-default col-xs-6 col-sm-4 col-md-3" style="float:left;">
      <div class="panel-body">
        <ul id="core-menu" style="float: left; height: 300px; overflow: auto; overflow-x: hidden; width: 100%; padding-left: 0;">
          {% for entry,name in conf.general %}
          <nobr>
            <li style="list-style-type: none; cursor: pointer; margin-top: 10px;" id="core|{{entry}}">{{_(name)}}</li>
          </nobr>
          {% endfor %}
        </ul>
      </div>
    </div>
    <form style="overflow: auto;" class="col-lg-8 col-xs-6 col-md-5" id="core_form" action="" method="POST" autocomplete="off">
      <span id="core_form_content">
        <br>
        <h3>{{_('Choose a section from the menu')}}</h3>
        <br>
      </span>
      <input class="btn btn-primary" style="float:right; margin-right: 10px;" id="core_submit" type="submit" value="{{_('Save')}}"/>
    </form>
  </div>
  <div id="plugins" class="tab-pane fade">
    <div class="panel panel-default col-xs-6 col-sm-4 col-md-3" style="float:left;">
      <div class="panel-body">
        <div class="form-group has-feedback has-clear">
          <span style="left: 0;" class="glyphicon glyphicon-search form-control-feedback"></span>
          <input type="text" id="query-text" disabled style="padding-left: 30px" class="form-control" autocomplete="off" placeholder="{{_(' Loading data...')}}">
          <button type="button" class="close form-control-feedback hidden" style="pointer-events: auto; z-index: 10;">&times;</button>
        </div>
        <ul id="plugin-menu" style="float: left; height: 600px; overflow: auto; overflow-x: hidden; width: 100%; padding-left: 0;">
          <div id="result-template" style="display: none;">
            <nobr>
              <li class="plugin-row" style="list-style-type: none; cursor: pointer; margin-top: 10px;"></li>
            </nobr>
          </div>
          <div id="noresult-template" style="display: none;">
            <nobr>
              <span id="no-result" class="noselect" style="white-space: nowrap;">No results found</span>
            </nobr>
          </div>
        </ul>
      </div>
    </div>
    <form class="col-lg-8 col-xs-6 col-md-5" id="plugin_form" action="" method="POST" autocomplete="off">
       <span id="plugin_form_content" style="position: static;">
            <br>
            <h3>{{_('Choose a section from the menu')}}</h3>
            <br>
       </span>
      <input class="btn btn-primary" style="float:right; margin-right: 10px;" id="plugin_submit" type="submit" value="{{_('Save')}}"/>
    </form>
  </div>
  <div id="accounts" class="tab-pane fade">
    <form id="account_form" action="{{url_for('json.update_accounts')}}" method="POST">
      <table class="settable wide table">
        <thead>
        <tr>
          <th>{{_('Plugin')}}</th>
          <th>{{_('Name')}}</th>
          <th>{{_('Password')}}</th>
          <th>{{_('Status')}}</th>
          <th>{{_('Premium')}}</th>
          <th>{{_('Valid until')}}</th>
          <th>{{_('Traffic left')}}</th>
          <th>{{_('Time')}}</th>
          <th>{{_('Max Parallel')}}</th>
          <th>{{_('Delete?')}}</th>
        </tr>
        </thead>

        {% for account in conf.accs %}
        {% set plugin = account.type %}
        <tr>
          <td>
            <span style="padding:5px">{{plugin}}</span>
          </td>

          <td>
            <label for="{{plugin}}|password;{{account.login}}" style="font-size: 14px; font-weight: 400;">{{account.login}}</label>
          </td>
          <td>
            <input id="{{plugin}}|password;{{account.login}}" name="{{plugin}}|password;{{account.login}}" type="password" value="{{account.password}}" size="12"/>
          </td>
          <td>
            {% if account.valid %}<span style="font-weight: bold; color: #333;">{{_('valid')}}{% else %}<span style="font-weight: bold; color: #D9534F;">{{_('not valid')}}{% endif %}</span>
          </td>
          <td>
            {% if account.premium %}
              <span style="font-weight: bold; color: #333;">{{_('yes')}}{% else %}<span style="font-weight: bold; color: #D9534F;">{{_('no')}}{% endif %}</span>
          </td>
          <td>
               <span style="font-weight: bold;">{{account.validuntil}}</span>
          </td>
          <td>
               <span style="font-weight: bold;">{{account.trafficleft}}</span>
          </td>
          <td>
            <input id="{{plugin}}|time;{{account.login}}" name="{{plugin}}|time;{{account.login}}" type="text" size="7" value="{{account.time}}"/>
          </td>
          <td>
            <input id="{{plugin}}|limitdl;{{account.login}}" name="{{plugin}}|limitdl;{{account.login}}" type="text" size="2" value="{{account.limitdl}}"/>
          </td>
          <td>
            <input id="{{plugin}}|delete;{{account.login}}" name="{{plugin}}|delete;{{account.login}}" type="checkbox" value="True"/>
          </td>
        </tr>
        {% endfor %}
      </table>
      <button id="account_add" type="button" data-toggle="modal" data-target="#account_box" style="margin-left: 5px;" class="btn btn-default">&nbsp;<span class="glyphicon glyphicon-plus"></span></button>
      <button id="account_submit" type="submit" class="btn btn-primary">{{_('Submit')}}</button>
    </form>
  </div>
  {% if user.is_admin %}
    <div id="users" class="tab-pane fade">
      <div class="row">
        <div class="col-sm-4 col-sm-offset-3 mt-sm-10" style="margin-top: 10px">
          <div class="alert alert-danger" style="padding:8px;">
            <p class="align-middle">{{ _("To reset default admin login run:") }} <b>pyload -r</b></p>
            <br style="line-height: 2px;">
            <p class="align-middle"><b>{{_('Important:')}}</b> {{_('Admin user have always all permissions!')}}</p>
          </div>
        </div>
      </div>
      <form id="user_form" action="" method="POST">
        <table class="settable table" style="width:50%;">
          <thead>
          <tr>
            <th>{{_('Name')}}</th>
            <th>{{_('Change Password')}}</th>
            <th>{{_('Admin')}}</th>
            <th>{{_('Permissions')}}</th>
            {% if user.is_admin %}
              <th>{{_('Delete?')}}</th>
            {% endif %}
          </tr>
          </thead>

          {% for name, data in conf.admin.users.items() %}
            <tr>
              <td>{{name}}</td>
              <td><div class="change_password btn btn-success btn-xs" data-toggle="modal" data-target="#password_box" id="change_pw|{{name}}"><span class="glyphicon glyphicon-pencil"></span> {{_('change')}}</div></td>
              <td>
                {% if user.name == name %}
                  <input class="is_admin" name="{{name}}|admin" type="checkbox" {% if data.perms.admin %} checked="checked" {% endif %} disabled="disabled">
                  <input name="{{name}}|admin" type="checkbox" {% if data.perms.admin %} checked="checked" {% endif %} hidden="hidden">
                {% else %}
                  <input class="is_admin" name="{{name}}|admin" type="checkbox" {% if data.perms.admin %} checked="checked" {% endif %}>
                {% endif %}
              </td>
              <td>
                <select id="{{name}}|perms" class="permlist" multiple="multiple" size="{{conf.admin.permlist|length}}" name="{{name}}|perms" {% if data.perms.admin %} disabled="disabled" {% endif %}>
                  {% for perm in conf.admin.permlist %}
                    {% if data.perms[perm] %}
                      <option selected="selected">{{perm}}</option>
                    {% else %}
                      <option>{{perm}}</option>
                    {% endif %}
                  {% endfor %}
                </select>
              </td>
              {% if user.is_admin %}
                <td>
                  {% if user.name != name %}
                    <input id="{{name}}|delete" name="{{name}}|delete" type="checkbox" value="True"/>
                  {% endif %}
                </td>
              {% endif %}

            </tr>
          {% endfor %}

        </table>
        <button id="user_add" type="button" data-toggle="modal" data-target="#user_box" style="margin-left: 5px;" class="btn btn-default">&nbsp;<span class="glyphicon glyphicon-plus"></span></button>
        <button id="user_submit" class="btn btn-primary" type="submit">{{_('Submit')}}</button>
      </form>
    </div>
  {% endif %}
</div>
{% endblock %}

{% block dialog %}
<div id="account_box" class="modal" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header bg-info text-center" style="padding: 5px"> {{_('Add Account')}}</div>
      <div class="modal-body" style="margin-bottom: 30px;">
        <form id="add_account_form" action="{{url_for('json.add_account')}}" method="POST" enctype="multipart/form-data" style="margin-bottom: 40px;">
          <p>{{_('Enter your account data to use premium features')}}</p>
          <div class="form-group">
            <label for="account_login">{{_('Login')}}</label>
            <input class="form-control" id="account_login" name="account_login" type="text"/>
            <p>{{_('Your username')}}</p>
          </div>
          <div class="form-group has-feedback">
            <label for="account_password">{{_('Password')}}</label>
            <input class="form-control reveal-pass" id="account_password" name="account_password" type="password" size="20"/>
            <p>{{_('The password for this account')}}</p>
          </div>
          <div class="form-group">
            <label for="account_type">{{_('Type')}}</label>
            <p>{{_('Choose the hoster for your account')}}</p>
          </div>
          <div class="form-group">
            <select name=account_type id="account_type">
              {% for type in types|sort %}<option value="{{type}}">{{type}}</option>{% endfor %}
            </select>
          </div>
            <button class="btn btn-primary" style="float: right; margin-left: 5px;" id="account_add_button" type="submit">{{_('Add')}}</button>
            <button class="btn btn-warning" style="float: right;" data-dismiss="modal" id="account_reset" type="reset">{{_('Cancel')}}</button>
        </form>
      </div>
    </div>
  </div>
</div>

<div id="path_chooser" class="modal" role="dialog">
  <div class="modal-dialog" style="width: 680px">
    <div class="modal-content">
      <div class="modal-header bg-info text-center" id="chooser_title" style="padding: 5px">{{_('Select')}}</div>
      <div class="modal-body text-left" style="padding-top: 3px; padding-bottom: 3px;">
        <div class="form-group" style="margin-bottom: 0; margin-left: 6px;">
          <label for="path_type1">{{_('Path')}}: </label>
          <div class="radio noselect" style="display: inline-block; margin-bottom: 0;">
            <label><input type="radio" name="path_type" id="path_type1" value="1" checked="checked" style="padding: 10px 10px;"/>{{_('absolute')}}</label>
          </div>
          <div class="radio" style="display: inline-block;  margin-bottom: 0;">
            <label><input type="radio" name="path_type" id="path_type0" value="0" checked="" style="padding: 10px 10px;"/>{{_('relative')}}</label>
          </div>
        </div>
        <iframe id="chooser_ifrm" src="#" width="650" height="420" frameborder="0" allowtransparency="true" scrolling="no"></iframe><br>
        <p id="path_p" class="text-ellipsis noselect" style="height: 18px; padding-right: 6px; padding-left: 6px; font-size: 12px;" title=""></p>
        <div class="modal-footer">
          <button class="btn btn-primary" id="chooser_confirm_button" style="float: right; margin-left: 5px;" type="submit">{{_('Select')}}</button>
          <button class="btn btn-warning" data-dismiss="modal" style="float: right;" type="reset">{{_('Cancel')}}</button>
        </div>
    </div>
    </div>
  </div>
</div>

<div id="restart_msg" class="hidden" style="position:fixed; z-index: 1; width: 100%;">
  <div align="center" class="alert alert-success">
    <p style="font-weight: bold;">{{_('pyLoad is restarting...')}}</p>
  </div>
</div>

<div id="shutdown_msg" class="hidden" style="position:fixed; z-index: 1; width: 100%;">
  <div align="center" class="alert alert-success">
    <p style="font-weight: bold;">{{_('pyLoad was shut down, goodbye!')}}</p>
  </div>
</div>

<div id="restart_box" class="modal" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header bg-info navbar-default text-center" style="padding: 5px" >{{_('Are you sure you want to restart pyLoad?')}}</div>
      <div class="modal-body" style="margin-bottom: 30px;">
        <button class="btn btn-primary" id="restart_button" type="submit" style="float: right">{{_('Ok')}}</button>
        <button class="btn btn-warning" data-dismiss="modal" id="restart_reset" style="margin-right: 5px; float: right" type="reset">{{_('Cancel')}}</button>
      </div>
    </div>
  </div>
</div>

<div id="quit_box" class="modal" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header bg-info text-center" style="padding: 5px;" >{{_('You are really sure you want to quit pyLoad?')}}</div>
      <div class="modal-body" style="margin-bottom: 30px;">
        <button class="btn btn-primary" id="quit_button" type="submit" style="float: right">{{_('Ok')}}</button>
        <button class="btn btn-warning" data-dismiss="modal" id="quit_reset" style="margin-right: 5px; float: right" type="reset">{{_('Cancel')}}</button>
      </div>
    </div>
  </div>
</div>

<div id="password_box" class="modal" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header bg-info text-center" style="padding: 5px" >{{_('Change Password')}}</div>
      <div class="modal-body" style="margin-bottom: 30px;">
        <form id="password_form" class="from-group" action="{{url_for("json.change_password")}}" method="POST" enctype="multipart/form-data" style="margin-bottom: 40px;">
          <p>{{_('Enter your current and desired Password.')}}</p>
          <div class="form-group">
            <label for="user_login">{{_('User')}}</label>
            <input class="form-control" id="user_login" name="user_login" type="text" readonly/>
            <p class="help-block">{{_('Your username.')}}</p>
          </div>
          <div class="form-group">
            <label for="login_current_password">{{_('Current password')}}</label>
            <input class="form-control reveal-pass" id="login_current_password" name="login_current_password" type="password"/>
            <p class="help-block">{{_('The password for this account.')}}</p>
          </div>
          <div class="form-group">
            <label for="login_new_password">{{_('New password')}}</label>
            <input class="form-control reveal-pass" id="login_new_password" name="login_new_password" type="password"/>
            <p class="help-block">{{_('The new password.')}}</p>
          </div>
          <div class="form-group">
            <label for="login_new_password2">{{_('New password (repeat)')}}</label>
            <input class="form-control reveal-pass" id="login_new_password2" name="login_new_password2" type="password" />
            <p class="help-block">{{_('Please repeat the new password.')}}</p>
          </div>
          <button class="btn btn-success" id="login_password_button" type="submit" style="float: right">{{_('Ok')}}</button>
          <button class="btn btn-warning" id="login_password_reset" data-dismiss="modal" style="margin-right: 5px; float: right" type="reset">{{_('Cancel')}}</button>
        </form>
      </div>
    </div>
  </div>
</div>

<div id="user_box" class="modal" role="dialog">
  <div class="modal-dialog modal-sm">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header bg-info text-center" style="padding: 5px;" >{{_('Create new pyLoad login')}}</div>
      <div class="modal-body" style="margin-bottom: 30px;">
        <form id="user_add_form" class="from-group" action="{{url_for('json.add_user')}}" method="POST" enctype="multipart/form-data" style="margin-bottom: 40px;">
          <p>{{_('Enter new user name and password.')}}</p>
          <div class="form-group">
            <label for="new_user">{{_('Login')}}</label>
            <input class="form-control" id="new_user" name="new_user" type="text" autocomplete="off"/>
            <p class="help-block">{{_("New username.")}}</p>
          </div>
          <div class="form-group">
            <label for="new_password">{{ _("Password") }}</label>
            <input class="form-control reveal-pass" id="new_password" name="new_password" type="password"/>
            <p class="help-block">{{ _("The password for the new login.") }}</p>
          </div>
          <div class="form-group">
            <label for="new_password2">{{_('Password (repeat)')}}</label>
            <input class="form-control reveal-pass" id="new_password2" name="new_password2" type="password"/>
            <p class="help-block">{{_('Please repeat the password.')}}</p>
          </div>
          <div class="form-group">
            <label for="new_role">{{ _("Admin") }}</label>
            <input class="form-control" id="new_role" name="new_role" type="checkbox" style="width: auto; height: auto; display: inline;" />
            <p class="help-block">{{ _("Shall the user be an admin?") }}</p>
          </div>
          <div class="form-group">
            <label for="new_perms">{{ _("Permissions") }}</label>
            <select class="permlist" multiple="multiple" id="new_perms" size="{{conf.admin.permlist|length}}" name="new_perms">
              {% for perm in conf.admin.permlist %}
                <option>{{perm}}</option>
              {% endfor %}
            </select>
            <p class="help-block">{{ _("Permmisions for the new user.") }}</p>
          </div>
          <button class="btn btn-success" id="new_user_button" type="submit" style="float: right">{{ _("Add") }}</button>
          <button class="btn btn-warning" id="new_user_reset" data-dismiss="modal" style="margin-right: 5px; float: right" type="reset">{{ _("Cancel") }}</button>
        </form>
      </div>
    </div>
  </div>
</div>
{% endblock %}
