<!--

    Copyright © 2016-2017 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->

<md-content flex class="md-padding tb-absolute-fill tb-data-table extension-table" layout="column">
    <div layout="column" class="md-whiteframe-z1" ng-class="{'tb-absolute-fill' : vm.inWidget}">
        <md-toolbar ng-if="!vm.inWidget" class="md-table-toolbar md-default" ng-show="!vm.selectedExtensions.length
                                                                                      && vm.query.search === null">
            <div class="md-toolbar-tools">
                <span translate>{{ 'extension.extensions' }}</span>
                <span flex></span>
                <md-button class="md-icon-button" ng-click="vm.addExtension($event)">
                    <md-icon>add</md-icon>
                    <md-tooltip md-direction="top">
                        {{ 'action.add' | translate }}
                    </md-tooltip>
                </md-button>
                <md-button class="md-icon-button" ng-click="vm.enterFilterMode()">
                    <md-icon>search</md-icon>
                    <md-tooltip md-direction="top">
                        {{ 'action.search' | translate }}
                    </md-tooltip>
                </md-button>
                <md-button class="md-icon-button" ng-click="vm.reloadExtensions()">
                    <md-icon>refresh</md-icon>
                    <md-tooltip md-direction="top">
                        {{ 'action.refresh' | translate }}
                    </md-tooltip>
                </md-button>
            </div>
        </md-toolbar>
        <md-toolbar class="md-table-toolbar md-default" ng-show="!vm.selectedExtensions.length
                                                                 && vm.query.search != null" ng-class="{'toolbar-widget' : vm.inWidget}">
            <div class="md-toolbar-tools">
                <md-button class="md-icon-button" aria-label="{{ 'action.search' | translate }}">
                    <md-icon aria-label="{{ 'action.search' | translate }}" class="material-icons">search</md-icon>
                    <md-tooltip md-direction="top">
                        {{ 'action.search' | translate }}
                    </md-tooltip>
                </md-button>
                <md-input-container flex>
                    <label>&nbsp;</label>
                    <input ng-model="vm.query.search" placeholder="{{ 'common.enter-search' | translate }}"/>
                </md-input-container>
                <md-button class="md-icon-button" aria-label="{{ 'action.back' | translate }}" ng-click="vm.exitFilterMode()">
                    <md-icon aria-label="{{ 'action.close' | translate }}" class="material-icons">close</md-icon>
                    <md-tooltip md-direction="{{vm.ctx.dashboard.isWidgetExpanded ? 'bottom' : 'top'}}">
                        {{ 'action.close' | translate }}
                    </md-tooltip>
                </md-button>
            </div>
        </md-toolbar>
        <md-toolbar class="md-table-toolbar alternate" ng-show="vm.selectedExtensions.length" ng-class="{'toolbar-widget' : vm.inWidget}">
            <div class="md-toolbar-tools">
                <span translate
                      translate-values="{count: vm.selectedExtensions.length}"
                      translate-interpolation="messageformat">extension.selected-extensions</span>
                <span flex></span>
                <md-button class="md-icon-button" ng-click="vm.deleteExtensions($event)">
                    <md-icon>delete</md-icon>
                    <md-tooltip md-direction="{{vm.ctx.dashboard.isWidgetExpanded ? 'bottom' : 'top'}}">
                        {{ 'action.delete' | translate }}
                    </md-tooltip>
                </md-button>
            </div>
        </md-toolbar>

        <div class="md-padding" flex layout="row" ng-class="{'sync-widget' : vm.inWidget}">
            <md-input-container flex="50" class="md-block">
                <label translate>extension.sync.status</label>
                <input ng-model="vm.syncStatus"
                       ng-class="{'extension__syncStatus--green':isSync, 'extension__syncStatus--red':!isSync}"
                       disabled
                >
            </md-input-container>

            <md-input-container flex="50" class="md-block">
                <label translate>extension.sync.last-sync-time</label>
                <input ng-model="vm.syncLastTime"
                       class="extension__syncStatus--black"
                       disabled
                >
            </md-input-container>
        </div>

        <md-table-container flex>
            <table md-table md-row-select multiple="" ng-model="vm.selectedExtensions" md-progress="vm.extensionsDeferred.promise">
                <thead md-head md-order="vm.query.order" md-on-reorder="vm.onReorder">
                    <tr md-row>
                        <th md-column md-order-by="id"><span translate>extension.id</span></th>
                        <th md-column md-order-by="type"><span translate>extension.type</span></th>
                        <th md-column><span>&nbsp</span></th>
                    </tr>
                </thead>
                <tbody md-body>
                <tr md-row md-select="extension" md-select-id="extension" md-auto-select ng-repeat="extension in vm.extensions">
                    <td md-cell>{{ extension.id }}</td>
                    <td md-cell>{{ extension.type }}</td>
                    <td md-cell class="tb-action-cell">
                        <md-button class="md-icon-button" aria-label="{{ 'action.edit' | translate }}" ng-click="vm.editExtension($event, extension)">
                            <md-icon aria-label="{{ 'action.edit' | translate }}" class="material-icons">edit</md-icon>
                            <md-tooltip md-direction="top">
                                {{ 'extension.edit' | translate }}
                            </md-tooltip>
                        </md-button>
                        <md-button class="md-icon-button" aria-label="{{ 'action.delete' | translate }}" ng-click="vm.deleteExtension($event, extension)">
                            <md-icon aria-label="{{ 'action.delete' | translate }}" class="material-icons">delete</md-icon>
                            <md-tooltip md-direction="top">
                                {{ 'extension.delete' | translate }}
                            </md-tooltip>
                        </md-button>
                    </td>
                </tr>
                </tbody>
            </table>
            <md-divider ng-if="vm.inWidget"></md-divider>
        </md-table-container>
        <md-table-pagination md-limit="vm.query.limit" md-limit-options="[5, 10, 15]"
                             md-page="vm.query.page" md-total="{{vm.extensionsCount}}"
                             md-on-paginate="vm.onPaginate" md-page-select>
        </md-table-pagination>
    </div>

</md-content>