<div class="col-sm-9 col-md-10 col-sm-push-3 col-md-push-2">

    <ol class="breadcrumb">
        <li><a href="#/realms/{{realm.realm}}/clients">{{:: 'clients' | translate}}</a></li>
        <li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}">{{client.clientId}}</a></li>
        <li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server">Authorization</a></li>
        <li><a href="#/realms/{{realm.realm}}/clients/{{client.id}}/authz/resource-server/evaluate">Policy Evaluation</a></li>
    </ol>

    <kc-tabs-resource-server></kc-tabs-resource-server>

    <div data-ng-show="showResult">
        <br>
        <a href="" data-ng-click="showRequestTab()">New Evaluation</a>
    </div>

    <div data-ng-show="evaluationResult && !showResult">
        <br>
        <a href="" data-ng-click="showResultTab()">Previous Result</a>
    </div>

    <div data-ng-hide="showResult">
        <form class="form-horizontal" name="clientForm" novalidate>
            <fieldset>
                <fieldset class="border-top">
                    <legend><span class="text">Identity Information</span>
                        <kc-tooltip>The available options to configure the identity information that will be used when evaluating policies.</kc-tooltip>
                    </legend>
                    <div class="form-group">
                        <label class="col-md-2 control-label" for="client">Client</label>

                        <div class="col-sm-2">
                            <div>
                                <select class="form-control" id="client"
                                        ng-model="authzRequest.clientId"
                                        ng-options="client.id as client.clientId for client in clients track by client.id">
                                    <option value="">Select a client...</option>
                                </select>
                                </select>
                            </div>
                        </div>
                        <kc-tooltip>A resource server is an already existing client application. In this case, the
                            client application will also act as a resource server in order to have its resources managed
                            and protected.
                        </kc-tooltip>
                    </div>
                    <div class="form-group clearfix">
                        <label class="col-md-2 control-label" for="users">User <span class="required"
                                                                                     data-ng-show="!authzRequest.roleIds || authzRequest.roleIds.length == 0">*</span></label>

                        <div class="col-md-6">
                            <select ui-select2="{ minimumInputLength: 1, allowClear:true}" id="users"
                                    data-ng-model="authzRequest.userId" data-placeholder="Select an user..."
                                    ng-options="user.id as user.username for user in users track by user.id"
                                    data-ng-required="!authzRequest.roleIds || authzRequest.roleIds.length == 0">
                                <option value=""></option>
                            </select>
                        </div>

                        <kc-tooltip>Specifies which user(s) are allowed by this policy.</kc-tooltip>
                    </div>

                    <div class="form-group">
                        <div class="col-md-10 col-md-offset-2">
                            <button class="btn btn-primary" data-ng-click="entitlements()" data-ng-disabled="authzRequest.userId == null || authzRequest.clientId == null">Entitlements</button>
                        </div>
                    </div>

                    <div class="form-group clearfix">
                        <label class="col-md-2 control-label" for="reqActions">Roles <span class="required"
                                                                                           data-ng-show="!authzRequest.userId || authzRequest.userId == null">*</span></label>

                        <div class="col-md-6">
                            <select ui-select2="{ minimumInputLength: 1}"
                                    data-ng-model="authzRequest.roleIds"
                                    data-placeholder="Any role..." multiple
                                    data-ng-required="!authzRequest.userId || authzRequest.userId == null">
                                <option ng-repeat="role in roles track by role.id" value="{{role.name}}">{{role.name}}
                                </option>
                            </select>
                        </div>

                        <kc-tooltip>Specifies that this policy must be applied to one or more scopes.</kc-tooltip>
                    </div>
                </fieldset>
                <fieldset>
                    <legend collapsed><span class="text">Contextual Information</span>
                        <kc-tooltip>The available options to configure any contextual information that will be used when evaluating policies.</kc-tooltip>
                    </legend>
                    <div class="form-group clearfix block">
                        <label class="col-md-2 control-label" for="newRedirectUri">Contextual Attributes</label>

                        <div class="col-sm-6">
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>Key</th>
                                        <th>Value</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ng-repeat="(key, value) in (authzRequest.context.attributes)">
                                        <td>{{getContextAttributeName(key)}}</td>
                                        <td>
                                            <select class="form-control" id="attribute-{{key}}"
                                                    data-ng-model="authzRequest.context.attributes[key]"
                                                    data-ng-show="getContextAttribute(key).values"
                                                    ng-options="value1.key as value1.name for value1 in getContextAttribute(key).values">
                                            </select>
                                            <input ng-model="authzRequest.context.attributes[key]" class="form-control"
                                                   type="text" name="{{key}}" id="attribute-{{key}}"
                                                   data-ng-hide="getContextAttribute(key).values"/>
                                        </td>
                                        <td class="kc-action-cell">
                                            <button class="btn btn-default btn-block btn-sm"
                                                    data-ng-click="removeContextAttribute(key)">Delete
                                            </button>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <select class="form-control" id="newContextAttribute.key"
                                                    data-ng-model="newContextAttribute"
                                                    ng-change="selectDefaultContextAttribute()"
                                                    data-ng-hide="!isDefaultContextAttribute()"
                                                    ng-options="attribute as attribute.name for attribute in defaultContextAttributes track by attribute.key">
                                            </select>
                                            <input ng-model="newContextAttribute.key" class="form-control" type="text"
                                                   id="newAttributeKey" data-ng-hide="isDefaultContextAttribute()"/>
                                        </td>
                                        <td>
                                            <select class="form-control" id="newContextAttribute.value"
                                                    data-ng-model="newContextAttribute.value"
                                                    data-ng-show="newContextAttribute.values"
                                                    ng-options="value.key as value.name for value in newContextAttribute.values track by value.key">
                                            </select>
                                            <input ng-model="newContextAttribute.value" class="form-control" type="text"
                                                   id="newAttributeValue" data-ng-show="!newContextAttribute.values"/>
                                        </td>
                                        <td class="kc-action-cell">
                                            <button class="btn btn-default btn-block btn-sm"
                                                    data-ng-click="addContextAttribute()"
                                                    data-ng-disabled="!newContextAttribute.key || newContextAttribute.key == ''">
                                                Add
                                            </button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>

                        <kc-tooltip>Any attribute provided by a running environment or execution context.</kc-tooltip>
                    </div>
                </fieldset>
                <fieldset>
                    <legend><span class="text">Permission</span>
                        <kc-tooltip>The available options to configure the permissions to which policies will be applied.</kc-tooltip>
                    </legend>
                    <div class="form-group">
                        <label class="col-md-2 control-label" for="applyResourceType">Apply to Resource Type</label>

                        <div class="col-md-6">
                            <input ng-model="applyResourceType" id="applyResourceType" onoffswitch
                                   data-ng-click="setApplyToResourceType()"/>
                        </div>
                        <kc-tooltip>Specifies if this policy must be applied to all resources with a given type. In this
                            case, this policy will be evaluated for all instances
                            of a given resource type.
                        </kc-tooltip>
                    </div>
                    <div class="form-group clearfix" data-ng-hide="applyResourceType">
                        <label class="col-md-2 control-label" for="reqActions">Resources <span class="required">*</span></label>

                        <div class="col-md-6">
                            <select ui-select2="{ minimumInputLength: 1, allowClear:true }"
                                    ng-model="newResource._id"
                                    data-placeholder="Select a resource..."
                                    data-ng-required="!applyResourceType && authzRequest.resources.length == 0 && !authzRequest.entitlements"
                                    data-ng-click="resolveScopes()"
                                    ng-options="resource._id as resource.name for resource in resources track by resource._id">
                                <option value=""></option>
                            </select>
                        </div>
                        <kc-tooltip>Specifies that this policy must be applied to a specific resource instance.
                        </kc-tooltip>
                    </div>
                    <div class="form-group clearfix" data-ng-show="applyResourceType">
                        <label class="col-md-2 control-label" for="newResource.type">Resource Type <span
                                class="required">*</span></label>

                        <div class="col-md-6">
                            <input class="form-control" type="text" id="newResource.type" name="newResource.type"
                                   data-ng-model="authzRequest.resources[0].type"
                                   data-ng-required="applyResourceType && !authzRequest.resources[0].type && !authzRequest.entitlements">
                        </div>

                        <kc-tooltip>Specifies that this policy must be applied to all resources instances of a given
                            type.
                        </kc-tooltip>
                    </div>
                    <div class="form-group clearfix" data-ng-show="applyResourceType || newResource._id == null">
                        <label class="col-md-2 control-label" for="newResource.scopes">Scopes</label>

                        <div class="col-md-6">
                            <select ui-select2="{ minimumInputLength: 1}"
                                    id="newResource.scopes"
                                    multiple
                                    data-ng-model="newResource.scopes"
                                    data-placeholder="Select a scope..."
                                    data-ng-options="scope.name as scope.name for scope in scopes track by scope.name"/>
                        </div>

                        <kc-tooltip>Specifies that this policy must be applied to one or more scopes.</kc-tooltip>
                    </div>
                    <div class="form-group clearfix" data-ng-show="newResource._id != null">
                        <label class="col-md-2 control-label" for="newResource.scopes">Scopes</label>

                        <div class="col-md-6">
                            <select ui-select2
                                    id="newResource.scopes"
                                    data-ng-model="newResource.scopes"
                                    data-placeholder="Any scope..." multiple>
                                <option ng-repeat="scope in scopes" value="{{scope.name}}">{{scope.name}}</option>
                            </select>
                        </div>

                        <kc-tooltip>Specifies that this policy must be applied to one or more scopes.</kc-tooltip>
                    </div>
                    <div class="form-group clearfix block" data-ng-show="!applyResourceType">
                        <label class="col-md-2 control-label" for="newRedirectUri"></label>

                        <div class="col-sm-6">
                            <button data-ng-click="addResource()" class="btn btn-primary">Add</button>
                            <table class="table table-striped table-bordered">
                                <thead>
                                    <tr>
                                        <th>Resource</th>
                                        <th>Scopes</th>
                                        <th>Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr data-ng-show="!authzRequest.resources || authzRequest.resources.length == 0">
                                        <td colspan="3">
                                            No resources.
                                        </td>
                                    </tr>
                                    <tr ng-repeat="resource in authzRequest.resources">
                                        <td>{{resource.name ? resource.name : 'Any resource with scope(s)'}}</td>
                                        <td>
                                            <span data-ng-show="!resource.scopes.length">Any scope.</span>
                                        <span data-ng-show="resource.scopes.length > 0">
                                            <span ng-repeat="scope in resource.scopes">
                                                {{scope}} {{$last ? '' : ', '}}
                                            </span>
                                        </span>
                                        </td>
                                        <td class="kc-action-cell">
                                            <button class="btn btn-default btn-block btn-sm"
                                                    data-ng-click="removeResource($index)">Delete
                                            </button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </fieldset>

                <div class="form-group">
                    <div class="col-md-10 col-md-offset-2">
                        <button kc-save data-ng-click="evaluate()">Evaluate</button>
                        <button kc-reset data-ng-disabled="!changed">Reset</button>
                    </div>
                </div>
            </fieldset>
        </form>
    </div>
    <div data-ng-include="resultUrl" data-ng-show="showResult"/>
</div>

<kc-menu></kc-menu>