
<!-- Modal add client -->
<div class="modal fade" id="clientAddModal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Add OAuth Client</h4>
            </div>
            <div class="modal-body">
                <form name="clientAddForm" role="form" novalidate ng-submit="clientAdd.submit()" autocomplete="off">
                    <div class="form-group" ng-class="{ 'has-error': (clientAddForm.name.$dirty && clientAddForm.name.$invalid) || (!clientAddForm.name.$dirty && clientAdd.error.name) }">
                        <label class="control-label">Application name</label>
                        <div class="control-label" ng-show="(!clientAddForm.name.$dirty && clientAdd.error.name) || (clientAddForm.name.$dirty && clientAddForm.name.$invalid)">
                            <small ng-show="clientAddForm.name.$error.required">A name is required</small>
                            <small ng-show="!clientAddForm.name.$dirty && clientAdd.error.name">{{ clientAdd.error.name }}</small>
                        </div>
                        <input type="text" class="form-control" ng-model="clientAdd.name" name="name" id="clientAddName" required autofocus>
                    </div>
                    <div class="form-group" ng-class="{ 'has-error': (clientAddForm.redirectURI.$dirty && clientAddForm.redirectURI.$invalid) || (!clientAddForm.redirectURI.$dirty && clientAdd.error.redirectURI) }">
                        <label class="control-label">Authorization callback URL</label>
                        <div class="control-label" ng-show="!clientAddForm.redirectURI.$dirty && clientAdd.error.redirectURI">
                            <small ng-show="!clientAddForm.redirectURI.$dirty && clientAdd.error.redirectURI">{{ clientAdd.error.redirectURI }}</small>
                        </div>
                        <input type="text" class="form-control" ng-model="clientAdd.redirectURI" name="redirectURI" id="clientAddRedirectURI" required>
                    </div>
                    <div class="form-group" ng-class="{ 'has-error': (clientAddForm.scope.$dirty && clientAddForm.scope.$invalid) || (!clientAddForm.scope.$dirty && clientAdd.error.scope) }">
                        <label class="control-label">Scope</label>
                        <div class="control-label" ng-show="(!clientAddForm.scope.$dirty && clientAdd.error.scope) || (clientAddForm.scope.$dirty && clientAddForm.scope.$invalid)">
                            <small ng-show="clientAddForm.scope.$error.required">A scope is required</small>
                            <small ng-show="!clientAddForm.scope.$dirty && clientAdd.error.scope">{{ clientAdd.error.scope }}</small>
                        </div>
                        <input type="text" class="form-control" ng-model="clientAdd.scope" name="scope" id="clientAddScope" placeholder="Specify any number of scope separated by a comma ','" required>
                    </div>
                    <input class="hide" type="submit" ng-disabled="clientAddForm.$invalid || clientAdd.busy"/>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-success" ng-click="clientAdd.submit()" ng-disabled="clientAddForm.$invalid || clientAdd.busy"><i class="fa fa-circle-o-notch fa-spin" ng-show="clientAdd.busy"></i> Add OAuth Client</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal remove client -->
<div class="modal fade" id="clientRemoveModal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Remove OAuth Client</h4>
            </div>
            <div class="modal-body">
                <p>
                    Removing client <b>{{ clientRemove.client.appId }}</b> will also remove all access from scripts and apps using those credentials.
                    You may want to consult the other Cloudron admins first.
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger" ng-click="clientRemove.submit()" ng-disabled="clientRemove.busy"><i class="fa fa-circle-o-notch fa-spin" ng-show="clientRemove.busy"></i> Remove OAuth Client</button>
            </div>
        </div>
    </div>
</div>

<!-- Modal add token -->
<div class="modal fade" id="tokenAddModal" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">New token created</h4>
            </div>
            <div class="modal-body"><b ng-click-select>{{ tokenAdd.token.accessToken }}</b></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Done</button>
            </div>
        </div>
    </div>
</div>

<div class="content">
  <div class="text-left">
    <h3>Access Tokens <button class="btn btn-xs btn-primary btn-outline pull-right" ng-click="tokenAdd.show(apiClient)"><i class="fa fa-plus"></i> New Token</button> </h3>
  </div>

  <!-- we will always at least have the webadmin token here, so activeClients always will have one entry with at least one token -->
  <div class="card">
      <div class="grid-item-top">
          <div class="row">
              <div class="col-xs-12">
                  <p>These tokens can be used to access the <a ng-href="{{ config.webServerOrigin + '/documentation/developer/api/' }}" target="_blank">Cloudron API</a>.</p>
                  <br/>
                  <h4 class="text-muted">Active Tokens</h4>
                  <hr/>
                  <p ng-repeat="token in apiClient.activeTokens">
                      <span ng-click-select>{{ token.accessToken }}</span> <button class="btn btn-xs btn-danger pull-right" ng-click="removeToken(apiClient, token)" title="Revoke Token"><i class="fa fa-trash-o"></i></button>
                  </p>
              </div>
          </div>
      </div>
  </div>

  <br/>

  <div class="text-left">
    <h3>OAuth Apps<button class="btn btn-xs btn-primary btn-outline pull-right" ng-click="clientAdd.show()"><i class="fa fa-plus"></i> New OAuth Client</button></h3>
  </div>

  <!-- we will always at least have the webadmin token here, so activeClients always will have one entry with at least one token -->
  <div class="card" ng-repeat="client in activeClients | activeOAuthClients:user">
      <div class="grid-item-top">
          <div class="row">
              <div class="col-xs-12">
                  <h4 class="text-muted">
                      {{client.name}} <span ng-show="client.type !== 'external' && client.type !== 'built-in'">on {{client.domain}}</span>
                  </h4>
              </div>
          </div>
          <div class="row">
              <div class="col-xs-12">
                  <div class="row">
                      <div class="col-xs-12">
                          <b>{{ client.activeTokens.length }}</b> active token(s).
                          <br/>
                          <a href="" data-toggle="collapse" data-parent="#accordion" data-target="#collapse{{client.id}}">Advanced</a>
                          <div id="collapse{{client.id}}" class="panel-collapse collapse">
                              <div class="panel-body">
                                  <h4 class="text-muted">Credentials <button class="btn btn-xs btn-danger pull-right" ng-click="clientRemove.show(client)" title="Remove OAuth Client" ng-show="client.type === 'external'">Remove OAuth Client</button></h4>
                                  <hr/>
                                  <p>Scope: <b ng-click-select>{{ client.scope }}</b></p>
                                  <p>RedirectURI: <b ng-click-select>{{ client.redirectURI }}</b></p>
                                  <p>Client ID: <b ng-click-select>{{ client.id }}</b></p>
                                  <p ng-show="client.clientSecret" style="overflow: auto; white-space: nowrap;">Client Secret: <b ng-click-select>{{ client.clientSecret }}</b></p>

                                  <br/>

                                  <h4 class="text-muted">Tokens
                                      <div class="pull-right">
                                          <button class="btn btn-xs btn-default" ng-click="removeAccessTokens(client)" ng-disabled="!client.activeTokens.length || client.busy"><i class="fa fa-circle-o-notch fa-spin" ng-show="client.busy"></i> Revoke All</button>
                                          <button class="btn btn-xs btn-primary btn-outline" ng-click="tokenAdd.show(client)"><i class="fa fa-plus"></i> New Token</button>
                                      </div>
                                  </h4>

                                  <hr/>

                                  <p ng-repeat="token in client.activeTokens">
                                      <b ng-click-select>{{ token.accessToken }}</b> <button class="btn btn-xs btn-danger pull-right" ng-click="removeToken(client, token)" title="Revoke Token"><i class="fa fa-trash-o"></i></button>
                                  </p>
                              </div>
                          </div>
                      </div>
                  </div>
              </div>
          </div>
      </div>
  </div>
</div>
