<div class="datatable">
  <rd-widget>
    <rd-widget-body classes="no-padding" ng-if="$ctrl.createEnabled">
      <div class="toolBar vertical-center !gap-x-5 !gap-y-1 flex-wrap">
        <div class="toolBarTitle vertical-center">
          <div class="widget-icon space-right">
            <pr-icon icon="'layers'" feather="true"></pr-icon>
          </div>
          {{ $ctrl.titleText }}
        </div>
        <div class="searchBar vertical-center !mr-0">
          <pr-icon icon="'search'" feather="true" class-name="'searchIcon'"></pr-icon>
          <input
            type="text"
            class="searchInput"
            ng-model="$ctrl.state.textFilter"
            ng-change="$ctrl.onTextFilterChange()"
            placeholder="Search for a stack..."
            auto-focus
            ng-model-options="{ debounce: 300 }"
            data-cy="stack-searchInput"
          />
        </div>
        <div class="actionBar !gap-3" ng-if="!$ctrl.offlineMode" authorization="PortainerStackCreate, PortainerStackDelete">
          <button
            type="button"
            class="btn btn-sm btn-dangerlight h-fit vertical-center !ml-0"
            authorization="PortainerStackDelete"
            ng-disabled="!$ctrl.createEnabled || $ctrl.state.selectedItemCount === 0"
            ng-click="$ctrl.removeAction($ctrl.state.selectedItems)"
            data-cy="stack-removeStackButton"
          >
            <pr-icon icon="'trash-2'" feather="true"></pr-icon>Remove
          </button>
          <button
            ng-disabled="!$ctrl.createEnabled"
            type="button"
            class="btn btn-sm btn-primary h-fit vertical-center !ml-0"
            ui-sref="docker.stacks.newstack"
            authorization="PortainerStackCreate"
            data-cy="stack-addStackButton"
          >
            <pr-icon icon="'plus'" feather="true"></pr-icon>Add stack
          </button>
        </div>
        <div class="settings">
          <datatable-columns-visibility columns="$ctrl.columnVisibility.columns" on-change="($ctrl.onColumnVisibilityChange)"></datatable-columns-visibility>
          <span class="setting ml-2" ng-class="{ 'setting-active': $ctrl.settings.open }" uib-dropdown dropdown-append-to-body auto-close="disabled" is-open="$ctrl.settings.open">
            <span uib-dropdown-toggle aria-label="Settings">
              <pr-icon icon="'more-vertical'" feather="true"></pr-icon>
            </span>
            <div class="dropdown-menu dropdown-menu-right" uib-dropdown-menu>
              <div class="tableMenu">
                <div class="menuHeader"> Table settings </div>
                <div class="menuContent">
                  <div>
                    <div class="md-checkbox" ng-if="$ctrl.isAdmin">
                      <input id="setting_all_orphaned_stacks" type="checkbox" ng-model="$ctrl.settings.allOrphanedStacks" ng-change="$ctrl.onSettingsAllOrphanedStacksChange()" />
                      <label for="setting_all_orphaned_stacks">Show all orphaned stacks</label>
                    </div>
                    <div class="md-checkbox">
                      <input id="setting_auto_refresh" type="checkbox" ng-model="$ctrl.settings.repeater.autoRefresh" ng-change="$ctrl.onSettingsRepeaterChange()" />
                      <label for="setting_auto_refresh">Auto refresh</label>
                    </div>
                    <div ng-if="$ctrl.settings.repeater.autoRefresh">
                      <label for="settings_refresh_rate"> Refresh rate </label>
                      <select id="settings_refresh_rate" ng-model="$ctrl.settings.repeater.refreshRate" ng-change="$ctrl.onSettingsRepeaterChange()" class="small-select">
                        <option value="10">10s</option>
                        <option value="30">30s</option>
                        <option value="60">1min</option>
                        <option value="120">2min</option>
                        <option value="300">5min</option>
                      </select>
                      <span>
                        <pr-icon id="refreshRateChange" icon="'check'" feather="true" mode="'success'" size="'sm'"></pr-icon>
                      </span>
                    </div>
                  </div>
                </div>
                <div>
                  <a type="button" class="btn btn-default btn-sm" ng-click="$ctrl.settings.open = false;">Close</a>
                </div>
              </div>
            </div>
          </span>
        </div>
      </div>

      <div class="table-responsive">
        <table class="table table-hover nowrap-cells" data-cy="stack-stackTable">
          <thead>
            <tr>
              <th uib-dropdown dropdown-append-to-body auto-close="disabled" is-open="$ctrl.filters.state.open">
                <div class="flex flex-row flex-no-wrap gap-1">
                  <span class="md-checkbox" ng-if="!$ctrl.offlineMode" authorization="PortainerStackCreate, PortainerStackDelete">
                    <input id="select_all" type="checkbox" ng-model="$ctrl.state.selectAll" ng-change="$ctrl.selectAll()" />
                    <label for="select_all"></label>
                  </span>
                  <table-column-header
                    col-title="'Name'"
                    can-sort="true"
                    is-sorted="$ctrl.state.orderBy === 'Name'"
                    is-sorted-desc="$ctrl.state.orderBy === 'Name' && $ctrl.state.reverseOrder"
                    ng-click="$ctrl.changeOrderBy('Name')"
                  ></table-column-header>
                  <div>
                    <span uib-dropdown-toggle ng-class="['table-filter vertical-center !ml-1', { 'filter-active': $ctrl.filters.state.enabled }]">
                      Filter
                      <pr-icon ng-if="$ctrl.filters.state.enabled" icon="'check'" feather="true" size="'sm'"></pr-icon>
                      <pr-icon ng-if="!$ctrl.filters.state.enabled" icon="'filter'" feather="true" size="'sm'"></pr-icon>
                    </span>
                  </div>
                  <div class="dropdown-menu" uib-dropdown-menu>
                    <div class="tableMenu">
                      <div class="menuHeader"> Filter by activity </div>
                      <div class="menuContent">
                        <div class="md-checkbox">
                          <input id="filter_usage_activeStacks" type="checkbox" ng-model="$ctrl.filters.state.showActiveStacks" ng-change="$ctrl.onFilterChange()" />
                          <label for="filter_usage_activeStacks">Active stacks</label>
                        </div>
                        <div class="md-checkbox">
                          <input id="filter_usage_unactiveStacks" type="checkbox" ng-model="$ctrl.filters.state.showUnactiveStacks" ng-change="$ctrl.onFilterChange()" />
                          <label for="filter_usage_unactiveStacks">Inactive stacks</label>
                        </div>
                      </div>
                      <div>
                        <a type="button" class="btn btn-default btn-sm" ng-click="$ctrl.filters.state.open = false;">Close</a>
                      </div>
                    </div>
                  </div>
                </div>
              </th>
              <th>
                <table-column-header
                  col-title="'Type'"
                  can-sort="true"
                  is-sorted="$ctrl.state.orderBy === 'Type'"
                  is-sorted-desc="$ctrl.state.orderBy === 'Type' && $ctrl.state.reverseOrder"
                  ng-click="$ctrl.changeOrderBy('Type')"
                ></table-column-header>
              </th>
              <th><table-column-header col-title="'Control'" can-sort="false"></table-column-header></th>
              <th>
                <table-column-header
                  col-title="'Created'"
                  can-sort="true"
                  is-sorted="$ctrl.state.orderBy === 'ResourceControl.CreationDate'"
                  is-sorted-desc="$ctrl.state.orderBy === 'ResourceControl.CreationDate' && $ctrl.state.reverseOrder"
                  ng-click="$ctrl.changeOrderBy('ResourceControl.CreationDate')"
                ></table-column-header>
              </th>
              <th ng-if="$ctrl.columnVisibility.columns.updated.display">
                <table-column-header
                  col-title="'Updated'"
                  can-sort="true"
                  is-sorted="$ctrl.state.orderBy === 'ResourceControl.UpdateDate'"
                  is-sorted-desc="$ctrl.state.orderBy === 'ResourceControl.UpdateDate' && $ctrl.state.reverseOrder"
                  ng-click="$ctrl.changeOrderBy('ResourceControl.UpdateDate')"
                ></table-column-header>
              </th>
              <th>
                <table-column-header
                  col-title="'Ownership'"
                  can-sort="true"
                  is-sorted="$ctrl.state.orderBy === 'ResourceControl.Ownership'"
                  is-sorted-desc="$ctrl.state.orderBy === 'ResourceControl.Ownership' && $ctrl.state.reverseOrder"
                  ng-click="$ctrl.changeOrderBy('ResourceControl.Ownership')"
                ></table-column-header>
              </th>
            </tr>
          </thead>
          <tbody>
            <tr
              dir-paginate="item in ($ctrl.state.filteredDataSet = ($ctrl.dataset | filter: $ctrl.applyFilters | filter:$ctrl.state.textFilter | orderBy:$ctrl.state.orderBy:$ctrl.state.reverseOrder | itemsPerPage: $ctrl.state.paginatedItemLimit))"
              ng-class="{ active: item.Checked }"
            >
              <td>
                <span class="md-checkbox" ng-if="!$ctrl.offlineMode" authorization="PortainerStackCreate, PortainerStackDelete">
                  <input id="select_{{ $index }}" type="checkbox" ng-model="item.Checked" ng-click="$ctrl.selectItem(item, $event)" ng-disabled="!$ctrl.allowSelection(item)" />
                  <label for="select_{{ $index }}"></label>
                </span>
                <a
                  ng-if="!$ctrl.offlineMode && $ctrl.createEnabled"
                  ui-sref="docker.stacks.stack({ name: item.Name, id: item.Id, type: item.Type, regular: item.Regular, external: item.External, orphaned: item.Orphaned, orphanedRunning: item.OrphanedRunning })"
                  >{{ item.Name }}</a
                >
                <span ng-if="$ctrl.offlineMode">{{ item.Name }}</span>
                <span ng-if="item.Regular && item.Status == 2" class="label label-warning image-tag ml-2">Inactive</span>
              </td>
              <td>{{ item.Type === 1 ? 'Swarm' : 'Compose' }}</td>
              <td>
                <span
                  ng-if="item.Orphaned"
                  class="interactive vertical-center"
                  tooltip-append-to-body="true"
                  tooltip-placement="bottom"
                  tooltip-class="portainer-tooltip"
                  uib-tooltip="This stack was created inside an environment that is no longer registered inside Portainer."
                >
                  Orphaned
                  <pr-icon icon="'alert-circle'" feather="true" class-name="'ml-0.5'" mode="'warning'"></pr-icon>
                </span>
                <span
                  ng-if="item.External"
                  class="interactive vertical-center"
                  tooltip-append-to-body="true"
                  tooltip-placement="bottom"
                  tooltip-class="portainer-tooltip"
                  uib-tooltip="This stack was created outside of Portainer. Control over this stack is limited."
                >
                  Limited
                  <pr-icon icon="'alert-circle'" feather="true" class-name="'ml-0.5'" mode="'warning'"></pr-icon>
                </span>
                <span ng-if="item.Regular">Total</span>
              </td>
              <td>
                <span ng-if="item.CreationDate">{{ item.CreationDate | getisodatefromtimestamp }} {{ item.CreatedBy ? 'by ' + item.CreatedBy : '' }}</span>
                <span ng-if="!item.CreationDate"> - </span>
              </td>
              <td ng-if="$ctrl.columnVisibility.columns.updated.display">
                <span ng-if="item.UpdateDate">{{ item.UpdateDate | getisodatefromtimestamp }} {{ item.UpdatedBy ? 'by ' + item.UpdatedBy : '' }}</span>
                <span ng-if="!item.UpdateDate"> - </span>
              </td>
              <td>
                <span class="vertical-center">
                  <pr-icon ng-attr-icon="item.ResourceControl.Ownership | ownershipicon" feather="true" class-name="'icon ml-0.5'"></pr-icon>
                  {{ item.ResourceControl.Ownership ? item.ResourceControl.Ownership : item.ResourceControl.Ownership = $ctrl.RCO.ADMINISTRATORS }}
                </span>
              </td>
            </tr>
            <tr ng-if="!$ctrl.dataset" data-cy="stacks-loadingTableRow">
              <td colspan="6" class="text-center text-muted">Loading...</td>
            </tr>
            <tr ng-if="$ctrl.state.filteredDataSet.length === 0" data-cy="stacks-noStackTableRow">
              <td colspan="6" class="text-center text-muted">No stack available.</td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="footer pl-5" ng-if="$ctrl.dataset">
        <div class="infoBar !ml-0" ng-if="$ctrl.state.selectedItemCount !== 0">
          {{ $ctrl.state.selectedItemCount }}
          item(s) selected
        </div>
        <div class="paginationControls">
          <form class="form-inline">
            <span class="limitSelector">
              <span class="mr-1"> Items per page </span>
              <select class="form-control" ng-model="$ctrl.state.paginatedItemLimit" ng-change="$ctrl.changePaginationLimit()" data-cy="component-paginationSelect">
                <option value="0">All</option>
                <option value="10">10</option>
                <option value="25">25</option>
                <option value="50">50</option>
                <option value="100">100</option>
              </select>
            </span>
            <dir-pagination-controls max-size="5"></dir-pagination-controls>
          </form>
        </div>
      </div>
    </rd-widget-body>
  </rd-widget>
</div>
