% extends 'settings', title => 'KorAP: '.loc('Auth_oauthSettings'), page => 'oauth';

%= page_title

<div class="form-table">
  <fieldset>
    <legend><%= loc 'Auth_clientCredentials' %></legend>
    <ul class="client-list">
      <li class="client">
        <span class="client-name"><%= stash 'client_name' %></span>
        % if (stash('client_desc')) {
        <span class="client-desc"><%= stash 'client_desc' %></span>
        % };
        % if (stash('client_url')) {
        <span class="client-url"><a href="<%= stash('client_url') %>"><%= stash('client_url') %></a></span>
        % };

        <p><%= loc 'Auth_clientType' %>: <tt><%= stash 'client_type' %></tt></p>
        %= label_for 'client_id' => loc('Auth_clientID')
        %= text_field 'client_id', stash('client_id'), readonly => 'readonly', class => 'copy-to-clipboard'
        % if (stash('client_type') && stash('client_type') ne 'PUBLIC') {
        <div>
          %= label_for 'client_secret' => loc('Auth_clientSecret')
          %= password_field 'client_secret', value => stash('client_secret'), readonly => 'readonly'
        </div>
        % };

        <br />

        <span class="button-group button-panel">
          %= link_to loc('Auth_oauthUnregister_short') => url_for('oauth-unregister', client_id => stash('client_id'))->query('name' => stash('client_name')) => {} => ( class => 'client-unregister' )
          %= link_to loc('Auth_oauthIssueToken_short') => url_for('oauth-issue-token', client_id => stash('client_id'))->query('name' => stash('client_name')) => {} => ( class => 'client-issue-token' )
        </span>        
      </li>
    </ul>    

    % if ($c->stash('tokens') && @{$c->stash('tokens')}) {
    <legend>Tokens</legend>
    <ul class="token-list">
      % foreach (@{$c->stash('tokens')}) {
      <li class="token">
      %= label_for 'token' => loc('Auth_accessToken')
      %= password_field '', value => $_->{token}, readonly => 'readonly', class => 'show-pwd copy-to-clipboard'
      <p name="created"><%== loc('Auth_createdAt', date => $_->{created_date}) %></p>
      <p name="expires"><%== loc('Auth_expiresIn', seconds => $_->{expires_in}) %></p>
      % if ($_->{scope}) {
      <p name="scope">Scope: <tt><%= join ',', @{$_->{scope}} %></tt></p>
      % };
      %= form_for 'oauth-revoke-token' => {} => ( class => 'token-revoke' ), begin
        %= csrf_field
        <input type="hidden" name="name" value="<%= stash('client_name') %>" />
        <input type="hidden" name="token" value="<%= $_->{token} %>" />
        <span class="button-group button-panel">
          <input type="submit" value="<%= loc 'Auth_oauthRevokeToken_short' %>" />
        </span>
      % end
      </li>
      % }
    </ul>
    % };

  </fieldset>
</div>
