<!--

    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-aliases-dialog" style="width: 700px;" aria-label="{{ vm.title | translate }}">
	<form name="theForm" ng-submit="vm.save()">
		<md-toolbar>
			<div class="md-toolbar-tools">
				<h2>{{ vm.isSingleEntityAlias ? ('entity.configure-alias' | translate:vm.singleEntityAlias ) : (vm.title | 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 ng-show="vm.isSingleEntityAlias" layout="row">
						<tb-entity-type-select style="min-width: 100px;"
											   ng-model="vm.singleEntityAlias.entityType"
											   allowed-entity-types="vm.allowedEntityTypes">
						</tb-entity-type-select>
						<tb-entity-filter flex entity-type="vm.singleEntityAlias.entityType" ng-model="vm.singleEntityAlias.entityFilter">
						</tb-entity-filter>
					</div>
					<div ng-show="!vm.isSingleEntityAlias" flex layout="row" layout-align="start center">
						<span flex="5"></span>
						<div flex layout="row" layout-align="start center"
							 style="padding: 0 0 0 10px; margin: 5px;">
							<span translate flex="20" style="min-width: 100px;">entity.alias</span>
							<span translate flex="20" style="min-width: 100px;">entity.type</span>
							<span translate flex="60" style="min-width: 190px; padding-left: 10px;">entity.entities</span>
							<span style="min-width: 40px;"></span>
						</div>
					</div>
					<div ng-show="!vm.isSingleEntityAlias" style="max-height: 500px; overflow: auto; padding-bottom: 20px;">
						<div ng-form name="aliasForm" flex layout="row" layout-align="start center" ng-repeat="entityAlias in vm.entityAliases track by $index">
							<span flex="5">{{$index + 1}}.</span>
							<div class="md-whiteframe-4dp tb-alias" flex layout="row" layout-align="start center">
								<md-input-container flex="20" style="min-width: 100px;" md-no-float class="md-block">
									<input required ng-change="entityAlias.changed=true" name="alias" placeholder="{{ 'entity.alias' | translate }}" ng-model="entityAlias.alias">
									<div ng-messages="aliasForm.alias.$error">
										<div translate ng-message="required">entity.alias-required</div>
									</div>
								</md-input-container>
								<section flex="20" layout="column" style="min-width: 100px;" >
									<tb-entity-type-select hide-label style="padding-left: 10px;"
													  ng-model="entityAlias.entityType"
													  allowed-entity-types="vm.allowedEntityTypes">
									</tb-entity-type-select>
								</section>
								<section flex="60" layout="column">
									<tb-entity-filter style="padding-left: 10px;"
													  entity-type="entityAlias.entityType"
													  ng-model="entityAlias.entityFilter"
													  on-matching-entity-change="vm.onFilterEntityChanged(entity, entityAlias)">
									</tb-entity-filter>
								</section>
								<md-button ng-disabled="loading" class="md-icon-button md-primary" style="min-width: 40px;"
										   ng-click="vm.removeAlias($event, entityAlias)" aria-label="{{ 'action.remove' | translate }}">
									<md-tooltip md-direction="top">
										{{ 'entity.remove-alias' | translate }}
									</md-tooltip>
									<md-icon aria-label="{{ 'action.delete' | translate }}" class="material-icons">
										close
									</md-icon>
								</md-button>
							</div>
						</div>
					</div>
					<div ng-show="!vm.isSingleEntityAlias && !vm.disableAdd" style="padding-bottom: 10px;">
						<md-button ng-disabled="loading" class="md-primary md-raised" ng-click="vm.addAlias($event)" aria-label="{{ 'action.add' | translate }}">
							<md-tooltip md-direction="top">
								{{ 'entity.add-alias' | translate }}
							</md-tooltip>
							<span translate>action.add</span>
						</md-button>
					</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>