{% extends "layout.html" %}
{% block content %}
  <div class="jumbotron">
    <p>
    <strong>Kia ora {{ user.name}}</strong>,</p>

    <div class="body-message">
      <p>You are logged into the NZ ORCID Hub through <strong>{{ user.organisation.name }}</strong>.</p>

      <p>You have given the following permission(s) to your ORCID record using the Hub.</p>
      <div class="table-responsive">
        <table class="table permissions">
          <thead>
            <tr class="info">
              <th style="text-align: center;">email address</th>
              <th style="text-align: center;">ORCID permission(s)</th>
            </tr>
          </thead>
          <tbody>
            {% for d in users_orcid or [] %}
              {% set org_name = d.org.name %}
              <tr class="active">
                <td> {% if d.user.email %}{{ d.user.email}} {% endif %} </td>
                <td> {% if d.scopes %}
                    {% if d.scopes == "/read-limited,/activities/update" %}
                      <p>{{ org_name }} can read and write information to your ORCID record</p>
                      <ol>
                        <li>
                          <b>{{ org_name }}</b> can read your trusted-party information.
                        </li>
                        <li>
                          <b>{{ org_name }}</b> can create and update research activities.
                        </li>
                      </ol>
                    {% elif d.scopes == "/read-limited,/person/update" %}
                      <p>{{ org_name }} can read and write other information about you (country, researcher-urls, keywords, etc.) to your ORCID record</p>
                      <ol>
                        <li>
                          <b>{{ org_name }}</b> can read your trusted-party information.
                        </li>
                        <li>
                          <b>{{ org_name }}</b> can add/update other information about you (country, keywords, etc.)
                        </li>
                      </ol>
                    {% elif d.scopes == "/read-limited" %}
                      {{ org_name }} can read trusted-party information from your ORCID record.
                    {% elif d.scopes == "/authenticate" %}
                      {{ org_name }} knows your authenticated ORCID iD
                    {% else %}
                      {{d.scopes}}
                    {% endif %}
                  {% endif %} </td>
              </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>
      <p>No further action from you is needed; you're welcome to log out of the Hub.</p>
      {% if session['Should_not_logout_from_ORCID'] %}

        <p class="text-center">To review and manage all your ORCID permissions, log into the settings page of your ORCID record here:
        <a href= '{{ profile_url + "account"}}' ><span>{{ profile_url + 'account' }}</span></a> and review the "Trusted organisations" listed.</p>

        <a class="btn btn-default btn-lg" role="button" href="{{profile_url + 'my-orcid'}}" target="_blank">
          <img class="orcid-id-logo" src="https://orcid.org/sites/default/files/images/orcid_24x24.png" width="24" height="24" alt="ORCID logo" /> Take me to my record in ORCID
        </a>

      {% else %}
        <p class="text-center">To review and manage all your ORCID permissions, log into the settings page of your ORCID record here:
        <a onclick=logoutOrcidAccount()><span>{{ profile_url + 'account' }}</span></a> and review the "Trusted organisations" listed.</p>
        <button class="connect-orcid-button" onclick="logoutOrcid()" style="height: 50px;width: auto;">
          <img class="orcid-id-logo" src="https://orcid.org/sites/default/files/images/orcid_24x24.png" width="24" height="24" alt="ORCID logo" /> Manage my ORCID Record
        </button>
      {% endif %}

      <a class="btn btn-default btn-lg" role="button" href="{{url_for('faq')}}">
        <img class="orcidHub-id-logo" src="{{url_for('static', filename='images/logo.png')}}" width="24" height="24" alt="ORCID Hub logo" /> Hub FAQ
      </a>
      <a
        role="button"
        class="btn btn-primary btn-lg"
        href="{{ url_for('user_cv') }}"
        data-toggle="tooltip"
        title="Generated CV filled in with the data from your ORCID profile">Your CV</a>
    </div>
  </div>
  <script>
    function logoutOrcid() {
      $.ajax({
        url: "{{profile_url}}" + "userStatus.json?logUserOut=true",
        dataType: 'jsonp',
        success: function(result,status,xhr) {
          window.location='{{profile_url + "my-orcid"}}'
        },
        error: function (xhr, status, error) {
          alert(status);
        }
      });}
function logoutOrcidAccount() {
  $.ajax({
    url: "{{profile_url}}" + "userStatus.json?logUserOut=true",
    dataType: 'jsonp',
    success: function(result,status,xhr) {
      window.location='{{ profile_url + "account"}}'
    },
    error: function (xhr, status, error) {
      alert(status);
    }
  });}
  </script>
{% endblock %}
