<!--

    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-relation-table tb-data-table" layout="column">
    <div layout="column" class="md-whiteframe-z1">
        <md-toolbar class="md-table-toolbar md-default" ng-show="!vm.selectedRelations.length
                                                                 && vm.query.search === null">
            <div class="md-toolbar-tools">
                <span translate>relation.entity-relations</span>
                <span flex></span>
                <md-button class="md-icon-button" ng-click="vm.addRelation($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.reloadRelations()">
                    <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.selectedRelations.length
                                                                 && vm.query.search != null">
            <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="top">
                        {{ 'action.close' | translate }}
                    </md-tooltip>
                </md-button>
            </div>
        </md-toolbar>
        <md-toolbar class="md-table-toolbar alternate" ng-show="vm.selectedRelations.length">
            <div class="md-toolbar-tools">
                <span translate
                      translate-values="{count: selectedRelations.length}"
                      translate-interpolation="messageformat">relation.selected-relations</span>
                <span flex></span>
                <md-button class="md-icon-button" ng-click="vm.deleteRelations($event)">
                    <md-icon>delete</md-icon>
                    <md-tooltip md-direction="top">
                        {{ 'action.delete' | translate }}
                    </md-tooltip>
                </md-button>
            </div>
        </md-toolbar>
        <md-table-container>
            <table md-table md-row-select multiple="" ng-model="vm.selectedRelations" md-progress="vm.relationsDeferred.promise">
                <thead md-head md-order="vm.query.order" md-on-reorder="vm.onReorder">
                <tr md-row>
                    <th md-column md-order-by="typeName"><span translate>relation.type</span></th>
                    <th md-column md-order-by="toEntityTypeName"><span translate>relation.to-entity-type</span></th>
                    <th md-column md-order-by="toName"><span translate>relation.to-entity-name</span></th>
                    <th md-column><span>&nbsp</span></th>
                </tr>
                </thead>
                <tbody md-body>
                <tr md-row md-select="relation" md-select-id="relation" md-auto-select ng-repeat="relation in vm.relations">
                    <td md-cell>{{ relation.typeName }}</td>
                    <td md-cell>{{ relation.toEntityTypeName }}</td>
                    <td md-cell>{{ relation.toName }}</td>
                    <td md-cell class="tb-action-cell">
                        <md-button class="md-icon-button" aria-label="{{ 'action.edit' | translate }}"
                                   ng-click="vm.editRelation($event, relation)">
                            <md-icon aria-label="{{ 'action.edit' | translate }}" class="material-icons">edit</md-icon>
                            <md-tooltip md-direction="top">
                                {{ 'relation.edit' | translate }}
                            </md-tooltip>
                        </md-button>
                        <md-button class="md-icon-button" aria-label="{{ 'action.delete' | translate }}" ng-click="vm.deleteRelation($event, relation)">
                            <md-icon aria-label="{{ 'action.delete' | translate }}" class="material-icons">delete</md-icon>
                            <md-tooltip md-direction="top">
                                {{ 'relation.delete' | translate }}
                            </md-tooltip>
                        </md-button>
                    </td>
                </tr>
                </tbody>
            </table>
        </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.relationsCount}}"
                             md-on-paginate="onPaginate" md-page-select>
        </md-table-pagination>
    </div>
</md-content>
