{#
## This file is part of Zenodo.
## Copyright (C) 2015, 2016 CERN.
##
## Zenodo is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Zenodo is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Zenodo. If not, see <http://www.gnu.org/licenses/>.
##
## In applying this licence, CERN does not waive the privileges and immunities
## granted to it by virtue of its status as an Intergovernmental Organization
## or submit itself to any jurisdiction.
#}

{%- extends config.ACCESSREQUESTS_SETTINGS_TEMPLATE %}
{%- from "zenodo_accessrequests/_pagination.html" import render_pagination with context %}

{%- block settings_content %}
{%- set pending_num = requests.count() %}
<div class="panel-group panel-bot-margin">
  <div class="panel panel-default">
    <div class="panel-heading">
      <i class="fa fa-edit fa-fw"></i>
      <strong>{{ _('Pending access requests') }}</strong>
      {%- if pending_num > 0 %}
      <span class="badge">{{pending_num}}</span>
      {%- endif %}
    </div>
      <ul class="list-group">
        {%- for r in requests %}
          {%- set record = get_record(r.recid)[1]  %}
          {%- set url = url_for('zenodo_accessrequests_settings.accessrequest', request_id=r.id) %}
          <li class="list-group-item">
            <div class="pull-right">
              <a href="{{url}}" class="btn btn-default btn-xs"><i class="fa fa-eye"></i> {{_('View')}}</a>
            </div>
            <a href="{{ url_for('zenodo_accessrequests_settings.accessrequest', request_id=r.id) }}">{{ record.get('title') }}</a><br/><small class="text-muted">{{_('Full name')}}: {{r.sender_full_name}}, {{_('Email')}}: {{r.sender_email}}, {{_('Justification')}}: {{r.justification|truncate(150)}}</small>
            </li>
          {%- else %}
            <li class="list-group-item">
            <p align="center"><em>{{ _('You have no pending access requests to accept/reject.') }}</em></p>
            <div class="row">
                <div class="col-md-12">
                    <p><strong>{{_('What\'s this?')}}</strong>
                    {%- trans sitename=config.THEME_SITENAME %}
                    There can be several reasons why a dataset cannot be shared openly (e.g. some clinical trial data). With {{sitename}}, you can now during upload set the access right to <span class="label label-danger">Restricted access</span>. This allows other users to request access to your dataset via {{sitename}}. Naturally, you are in full control of who gets granted/denied access.
                    {%- endtrans %}
                    </p>
                </div>

            </div>
            </li>
          {%- endfor %}
      </ul>
  </div>
</div>
<div class="panel-group panel-bot-margin">
  <div class="panel panel-default">
    <div class="panel-heading">
      <i class="fa fa-share fa-fw"></i>
      <strong>{{ _('Shared links') }}</strong>
    </div>
    {%- if links_pagination.total > 0 or query %}
      <div class="panel-body">
        <div class="row">
          <div class="col-md-8">
            Shared links gives anyone with the link access to restricted files in embargoed/restricted/closed access uploads.
          </div>
          <div class="col-md-4">
            <form role="form" method="GET">
              <input name="sort" type="hidden" value="{{order.selected()}}">
              <div class="input-group">
                 <input autocomplete="off" name="query" class="form-control input-sm" type="text" tabindex="1" value="{{query}}">
                 <span class="input-group-btn">
                   <button type="submit" class="btn btn-primary btn-sm btn-inline-icon-hide-sm">
                     <i class="glyphicon glyphicon-search"></i> <span>Search</span>
                   </button>
                 </span>
              </div>
            </form>
          </div>
        </div>
      </div>


        <table class="table">
        <thead>
          <tr class="active">
            {% for t, f in [('Link','title'), ('Created','created'), ('Expires','expires_at'),] %}
            <td>
              <a href="{{ url_for('.index', query=query, sort=order.reverse(f)) }}"><strong>{{t}}</strong></a>
                {% if order.is_selected(f) %} <i class="fa fa-caret-{{order.dir(f, asc='up', desc='down')}}"></i>{% endif %}
            </td>
            {% endfor %}
            <td></td>
          </tr>
        </thead>
        <tbody>
          <tr>
          </tr>
          {%- for l in links_pagination.items %}
          {%- set url = l.get_absolute_url('invenio_records_ui.recid') %}
            <tr>
              <td class="col-md-6"><a href="{{url}}">{{l.title}}</a> <button data-clipboard-text="{{url}}" class="btn btn-default btn-xs clip-button"><i class="fa fa-copy" title="Copy link"></i> Copy</button> <br/><small class="text-muted">{{l.description|truncate(150)}}</small></td>
              <td class="col-md-2">{{l.created|tousertimezone|datetimeformat}}</td>
              <td class="col-md-2">{% if l.expires_at %}{{l.expires_at|dateformat}}{% endif %}</td>
              <td class="col-md-2">
                <form action="." method="POST">
                  {{form.csrf_token}}
                  {{form.link(value=l.id)}}
                  {{form.delete(class_="btn btn-danger btn-xs")}}
                </form>
              </td>
            </tr>
          {% else %}
          <tr>
            <td colspan="4"><div align="center"><em>Empty search result</em></div></td>
          </tr>
          {%- endfor %}
        </tbody>
        </table>

        {%- if links_pagination.pages > 1 %}
          <hr />
          <div align="center">
            {{ render_pagination(links_pagination, '.index') }}
          </div>
        {%- endif %}
        </div>

      {%- else %}
        <ul class="list-group">
          <li class="list-group-item">{{ _('Shared links gives anyone with the link access to restricted files in embargoed/restricted/closed access uploads.') }}</li>
        </ul>
      {%- endif %}
    </div>
  </div>
</div>
{%- endblock %}
