<!--

    Copyright © 2016-2019 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-dialog aria-label="{{ 'dashboard.manage-states' | translate }}" style="min-width: 600px;">
    <form name="theForm" ng-submit="vm.save()">
        <md-toolbar>
            <div class="md-toolbar-tools">
                <h2 translate>{{ 'dashboard.manage-states' }}</h2>
                <span flex></span>
                <md-button class="md-icon-button" ng-click="vm.cancel()">
                    <ng-md-icon icon="close" aria-label="{{ 'dialog.close' | translate }}"></ng-md-icon>
                </md-button>
            </div>
        </md-toolbar>
        <md-progress-linear class="md-warn" md-mode="indeterminate" ng-disabled="!$root.loading" ng-show="$root.loading"></md-progress-linear>
        <span style="min-height: 5px;" flex="" ng-show="!$root.loading"></span>
        <md-dialog-content>
            <div class="md-dialog-content">
                <fieldset ng-disabled="$root.loading">
                    <div class="manage-dashboard-states" layout="column">
                        <md-toolbar class="md-table-toolbar md-default" ng-show="vm.query.search === null">
                            <div class="md-toolbar-tools">
                                <span translate>dashboard.states</span>
                                <span flex></span>
                                <md-button class="md-icon-button" ng-click="vm.addState($event)">
                                    <md-icon>add</md-icon>
                                    <md-tooltip md-direction="top">
                                        {{ 'dashboard.add-state' | 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>
                            </div>
                        </md-toolbar>
                        <md-toolbar class="md-table-toolbar md-default" ng-show="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">
                                        {{ 'dashboard.search-states' | translate }}
                                    </md-tooltip>
                                </md-button>
                                <md-input-container flex>
                                    <label>&nbsp;</label>
                                    <input ng-model="vm.query.search" placeholder="{{ 'dashboard.search-states' | translate }}"/>
                                </md-input-container>
                                <md-button class="md-icon-button" aria-label="Close" ng-click="vm.exitFilterMode()">
                                    <md-icon aria-label="Close" class="material-icons">close</md-icon>
                                    <md-tooltip md-direction="top">
                                        {{ 'action.close' | translate }}
                                    </md-tooltip>
                                </md-button>
                            </div>
                        </md-toolbar>
                        <md-table-container>
                            <table md-table>
                                <thead fix-head md-head md-order="vm.query.order" md-on-reorder="vm.onReorder">
                                    <tr md-row>
                                        <th md-column md-order-by="name"><span translate>dashboard.state-name</span></th>
                                        <th md-column md-order-by="id"><span translate>dashboard.state-id</span></th>
                                        <th md-column md-order-by="root"><span translate>dashboard.is-root-state</span></th>
                                        <th md-column><span>&nbsp</span></th>
                                    </tr>
                                </thead>
                                <tbody md-body>
                                    <tr md-row md-select="state" ng-disabled="state.root" md-select-id="id" md-auto-select ng-repeat="state in vm.states">
                                        <td md-cell>{{state.name}}</td>
                                        <td md-cell>{{state.id}}</td>
                                        <td md-cell>
                                            <md-checkbox aria-label="{{'dashboard.is-root-state' | translate }}"
                                                         disabled ng-model="state.root">
                                            </md-checkbox>
                                        </td>
                                        <td md-cell class="tb-action-cell">
                                            <md-button class="md-icon-button" aria-label="{{ 'action.edit' | translate }}"
                                                       ng-click="vm.editState($event, state)">
                                                <md-icon aria-label="{{ 'action.edit' | translate }}" class="material-icons">edit</md-icon>
                                                <md-tooltip md-direction="top">
                                                    {{ 'dashboard.edit-state' | translate }}
                                                </md-tooltip>
                                            </md-button>
                                            <md-button ng-show="!state.root" class="md-icon-button" aria-label="Delete" ng-click="vm.deleteState($event, state)">
                                                <md-icon aria-label="Delete" class="material-icons">delete</md-icon>
                                                <md-tooltip md-direction="top">
                                                    {{ 'dashboard.delete-state' | 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.statesCount}}"
                                             md-on-paginate="vm.onPaginate" md-page-select>
                        </md-table-pagination>
                    </div>
                </fieldset>
            </div>
        </md-dialog-content>
        <md-dialog-actions layout="row">
            <span flex></span>
            <md-button ng-disabled="$root.loading || !theForm.$dirty || !theForm.$valid" type="submit" class="md-raised md-primary">
                {{ 'action.save' | translate }}
            </md-button>
            <md-button ng-disabled="$root.loading" ng-click="vm.cancel()" style="margin-right:20px;">{{ 'action.cancel' | translate }}</md-button>
        </md-dialog-actions>
    </form>
</md-dialog>
