<div class="robots-manager-element">
  <div class="feedback-bar" feedback="feedback"></div>
  <div class="cor-loader" ng-show="loading"></div>

  <div ng-show="!loading">
    <div class="manager-header" header-title="Robot Accounts">
      <span class="popup-input-button" pattern="ROBOT_PATTERN"
            placeholder="'Robot Account Name'"
            submitted="createRobot(value)"
            ng-show="isEnabled">
        <i class="fa fa-plus"></i> Create Robot Account
      </span>
    </div>

    <div class="section-description-header">
      Robot Accounts are named tokens that can be granted permissions on multiple repositories
      under this <span ng-if="organization">organization</span><span ng-if="!organization">user namespace</span>. They are typically used in environments where credentials will
      be shared, such as deployment systems.
    </div>

    <div class="filter-box" collection="robots" filter-model="robotFilter" filter-name="Robot Accounts"></div>

    <div class="empty" ng-if="!robots.length">
      <div class="empty-primary-msg">No robot accounts defined.</div>
      <div class="empty-secondary-msg">
        Click the "Create Robot Account" button above to create a robot account.
      </div>
    </div>

    <div class="empty" ng-if="robots.length && !(robots | filter:robotFilter).length">
      <div class="empty-primary-msg">No robot accounts found matching filter.</div>
      <div class="empty-secondary-msg">
        Please change your filter to display robot accounts.
      </div>
    </div>

    <table class="cor-table" ng-if="(robots | filter:robotFilter).length">
      <thead>
        <td class="caret-col hidden-xs" ng-if="(user || organization.is_admin)"></td>
        <td>Robot Account Name</td>
        <td ng-if="organization">Teams</td>
        <td>Direct Repository Permissions</td>
        <td class="options-col"></td>
      </thead>

      <tbody ng-repeat="robotInfo in robots | filter:robotFilter | orderBy:getShortenedRobotName" bindonce>
        <tr ng-class="robotInfo.showing_permissions ? 'open' : 'closed'">
          <td class="caret-col hidden-xs" bo-if="(user || organization.is_admin)">
            <span bo-if="robotInfo.repositories.length > 0" ng-click="showPermissions(robotInfo)">
              <i class="fa"
                 ng-class="robotInfo.showing_permissions ? 'fa-caret-down' : 'fa-caret-right'"
                 data-title="View Permissions List" bs-tooltip></i>
            </span>
          </td>
          <td class="robot">
            <i class="fa ci-robot hidden-xs"></i>
            <a ng-click="showRobot(robotInfo)">
              <span class="prefix" bo-text="getPrefix(robotInfo.name) + '+'"></span><span bo-text="getShortenedName(robotInfo.name)"></span>
            </a>
          </td>
          <td bo-if="organization">
            <span class="empty" bo-if="robotInfo.teams.length == 0">
              (Not a member of any team)
            </span>
            <span class="empty" bo-if="robotInfo.teams.length > 0">
               <span ng-repeat="team in robotInfo.teams"
                     data-title="Team {{ team.name }}" bs-tooltip>
                 <span class="anchor" is-only-text="!organization.is_admin" href="/organization/{{ organization.name }}/teams/{{ team.name }}">
                   <span class="avatar" size="24" data="team.avatar"></span>
                 </span>
               </span>
            </span>
          </td>
          <td>
            <span class="empty" bo-if="robotInfo.repositories.length == 0">
              (No direct permissions on any repositories)
            </span>

            <span class="member-perm-summary" bo-if="robotInfo.repositories.length > 0">
              Direct Permissions on
              <span class="anchor hidden-xs" href="javascript:void(0)" is-only-text="!organization.is_admin"
                    ng-click="showPermissions(robotInfo)">
                <span bo-text="robotInfo.repositories.length"></span>
                <span bo-if="robotInfo.repositories.length == 1">repository</span>
                <span bo-if="robotInfo.repositories.length > 1">repositories</span>
              </span>
              <span class="visible-xs" href="javascript:void(0)">
                <span bo-text="robotInfo.repositories.length"></span>
                <span bo-if="robotInfo.repositories.length == 1">repository</span>
                <span bo-if="robotInfo.repositories.length > 1">repositories</span>
              </span>
            </span>
          </td>
          <td class="options-col">
            <span class="cor-options-menu">
              <span class="cor-option" option-click="showRobot(robotInfo)">
                <i class="fa fa-key"></i> View Credentials
              </span>
              <span class="cor-option" option-click="askDeleteRobot(robotInfo)">
                <i class="fa fa-times"></i> Delete Robot {{ robotInfo.name }}
              </span>
            </span>
          </td>
        </tr>
        <tr ng-if="robotInfo.showing_permissions">
          <td class="permissions-display-row" colspan="4">
            <span class="cor-loader" ng-if="robotInfo.loading_permissions"></span>
            <div class="permissions-table-wrapper">
              <table class="permissions-table" ng-if="!robotInfo.loading_permissions">
                <thead>
                  <td>Repository</td>
                  <td>Permission</td>
                </thead>

                <tr ng-repeat="permission in robotInfo.permissions">
                  <td>
                    <span class="repo-icon repo-circle no-background" repo="permission.repository"></span>
                    <a ng-href="/repository/{{ getPrefix(robotInfo.name) }}/{{ permission.repository.name }}?tab=settings">{{ getPrefix(robotInfo.name) }}/{{ permission.repository.name }}</a>
                  </td>
                  <td>
                    <div class="btn-group btn-group-sm">
                      <span class="role-group"
                            current-role="permission.role"
                            roles="repoRoles"
                            read-only="true"></span>
                    </div>
                  </td>
                </tr>
              </table>
            </div>
          </td>
        </tr>
      </tbody>
    </table>
  </div>

  <div class="docker-auth-dialog" username="shownRobot.name" token="shownRobot.token"
       shown="!!shownRobot" counter="showRobotCounter" supports-regenerate="true" regenerate="regenerateToken(username)">
    <i class="fa ci-robot"></i> {{ shownRobot.name }}
  </div>
</div>
