<!--

    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">
    <section layout="row">
        <md-input-container class="md-block" style="width: 200px;">
            <label translate>relation.direction</label>
            <md-select ng-model="vm.direction" ng-disabled="loading">
                <md-option ng-repeat="direction in vm.types.entitySearchDirection" ng-value="direction">
                    {{ ('relation.search-direction.' + direction) | translate}}
                </md-option>
            </md-select>
        </md-input-container>
    </section>
    <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>{{(vm.direction == vm.types.entitySearchDirection.from ?
                    'relation.from-relations' : 'relation.to-relations') | translate}}</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: vm.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="type"><span translate>relation.type</span></th>
                    <th md-column ng-if="vm.direction == vm.types.entitySearchDirection.from"
                        md-order-by="toEntityTypeName"><span translate>relation.to-entity-type</span></th>
                    <th md-column ng-if="vm.direction == vm.types.entitySearchDirection.to"
                        md-order-by="fromEntityTypeName"><span translate>relation.from-entity-type</span></th>
                    <th md-column ng-if="vm.direction == vm.types.entitySearchDirection.from"
                        md-order-by="toName"><span translate>relation.to-entity-name</span></th>
                    <th md-column ng-if="vm.direction == vm.types.entitySearchDirection.to"
                        md-order-by="fromName"><span translate>relation.from-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.type }}</td>
                    <td md-cell ng-if="vm.direction == vm.types.entitySearchDirection.from">{{ relation.toEntityTypeName  }}</td>
                    <td md-cell ng-if="vm.direction == vm.types.entitySearchDirection.to">{{ relation.fromEntityTypeName  }}</td>
                    <td md-cell ng-if="vm.direction == vm.types.entitySearchDirection.from">{{ relation.toName }}</td>
                    <td md-cell ng-if="vm.direction == vm.types.entitySearchDirection.to">{{ relation.fromName }}</td>
                    <td md-cell class="tb-action-cell">
                        <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="vm.onPaginate" md-page-select>
        </md-table-pagination>
    </div>
</md-content>
