<!--

    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-dialog class="tb-entity-filter-dialog" style="width: 600px;" aria-label="{{ 'alias.entity-filter' | translate }}">
    <form name="theForm" ng-submit="vm.save()">
        <md-toolbar>
            <div class="md-toolbar-tools">
                <h2>{{ (vm.isAdd ? 'alias.create-entity-filter' : 'alias.edit-entity-filter') | translate  }}</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="!loading" ng-show="loading"></md-progress-linear>
        <span style="min-height: 5px;" flex="" ng-show="!loading"></span>
        <md-dialog-content>
            <div class="md-dialog-content">
                <fieldset ng-disabled="loading">
                    <div flex layout="column">
                        <md-input-container>
                            <label>{{ 'alias.filter-type' | translate }}</label>
                            <md-select required name="filterType"
                                       ng-model="vm.filter.type" aria-label="{{ 'alias.filter-type' | translate }}">
                                <md-option ng-repeat="type in vm.types.aliasFilterType" ng-value="type.value">
                                    {{type.name | translate}}
                                </md-option>
                            </md-select>
                            <div ng-messages="theForm.filterType.$error">
                                <div ng-message="required" translate>alias.filter-type-required</div>
                            </div>
                        </md-input-container>
                        <section layout="column" ng-if="vm.filter.type == vm.types.aliasFilterType.entityList.value" id="entityListFilter">
                            <md-checkbox flex aria-label="{{ 'alias.use-state-entity' | translate }}"
                                         ng-model="vm.filter.stateEntity">{{ 'alias.use-state-entity' | translate }}
                            </md-checkbox>
                            <tb-entity-type-select
                                    ng-if="!vm.filter.stateEntity"
                                    ng-model="vm.filter.entityType"
                                    the-form="theForm"
                                    ng-disabled="vm.filter.stateEntity"
                                    tb-required="!vm.filter.stateEntity"
                                    allowed-entity-types="vm.allowedEntityTypes">
                            </tb-entity-type-select>
                            <tb-entity-list
                                    ng-if="!vm.filter.stateEntity"
                                    ng-model="vm.filter.entityList"
                                    ng-disabled="vm.filter.stateEntity"
                                    tb-required="!vm.filter.stateEntity"
                                    entity-type="vm.filter.entityType">
                            </tb-entity-list>
                        </section>
                        <section flex layout="column" ng-if="vm.filter.type == vm.types.aliasFilterType.entityName.value" id="entityNameFilter">
                            <tb-entity-type-select
                                    ng-model="vm.filter.entityType"
                                    the-form="theForm"
                                    tb-required="true"
                                    allowed-entity-types="vm.allowedEntityTypes">
                            </tb-entity-type-select>
                            <md-input-container flex>
                                <label translate>entity.name-starts-with</label>
                                <input required name="entityNameFilter"
                                       ng-model="vm.filter.entityNameFilter"
                                       aria-label="{{ 'entity.name-starts-with' | translate }}">
                                <div ng-messages="theForm.entityNameFilter.$error">
                                    <div ng-message="required" translate>entity.entity-name-filter-required</div>
                                </div>

                            </md-input-container>
                        </section>
                        <div class="tb-error-messages" ng-messages="theForm.$error" role="alert">
                            <div translate ng-message="entityFilter" class="tb-error-message">alias.entity-filter-no-entity-matched</div>
                        </div>
                    </div>
                </fieldset>
            </div>
        </md-dialog-content>
        <md-dialog-actions layout="row">
            <span flex></span>
            <md-button ng-disabled="loading || theForm.$invalid || !theForm.$dirty" type="submit" class="md-raised md-primary">
                {{ 'action.save' | translate }}
            </md-button>
            <md-button ng-disabled="loading" ng-click="vm.cancel()" style="margin-right:20px;">{{ 'action.cancel' | translate }}</md-button>
        </md-dialog-actions>
    </form>
</md-dialog>