<!--

    Copyright © 2016 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 style="width: 700px;" aria-label="{{ 'device.aliases' | translate }}">
	<form name="theForm" ng-submit="vm.save()">
	    <md-toolbar>
	      <div class="md-toolbar-tools">
	        <h2>{{ vm.isSingleDevice ? ('device.select-device-for-alias' | translate:vm.singleDeviceAlias ) : ('device.aliases' | 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-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.isSingleDevice">
					  <md-autocomplete					  			
					    		ng-required="vm.isSingleDevice"
					    		md-input-name="device_id"
					    		ng-model="vm.singleDevice"
								md-selected-item="vm.singleDevice"
								md-search-text="vm.singleDeviceSearchText"
								md-search-text-change="vm.deviceSearchTextChanged(vm.singleDevice)"
								md-items="item in vm.fetchDevices(vm.singleDeviceSearchText)"
								md-item-text="item.name"
								md-min-length="0"
								placeholder="{{ 'device.device' | translate }}">
								<md-item-template>
					         	    <span md-highlight-text="vm.singleDeviceSearchText" md-highlight-flags="^i">{{item.name}}</span>
					       		</md-item-template>
						        <md-not-found>
									<span translate translate-values='{ device: vm.singleDeviceSearchText }'>device.no-devices-matching</span>
						        </md-not-found>
						        <div ng-messages="theForm.device_id.$error">
				      				<div translate ng-message="required">device.device-required</div>
				    			</div>	
					  </md-autocomplete>							
				</div> 
				<div ng-show="!vm.isSingleDevice" 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="40" style="min-width: 100px;">device.alias</span>
						   <span translate flex="60" style="min-width: 190px; padding-left: 10px;">device.device</span>
						   <span style="min-width: 40px;"></span>
				    </div>
				</div>
				<div ng-show="!vm.isSingleDevice" style="max-height: 300px; overflow: auto; padding-bottom: 20px;">
					<div ng-form name="aliasForm" flex layout="row" layout-align="start center" ng-repeat="deviceAlias in vm.deviceAliases 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="40" style="min-width: 100px;" md-no-float class="md-block">
								<input required name="alias" placeholder="{{ 'device.alias' | translate }}" ng-model="deviceAlias.alias">
								  <div ng-messages="aliasForm.alias.$error">
									  <div translate ng-message="required">device.alias-required</div>
								  </div>
							  </md-input-container>
							  <section flex="60" layout="column">
								  <md-autocomplete flex
											ng-required="!vm.isSingleDevice"
											md-input-name="device_id"
											ng-model="deviceAlias.device"
											md-selected-item="deviceAlias.device"
											md-search-text="deviceAlias.searchText"
											md-search-text-change="vm.deviceSearchTextChanged(deviceAlias)"
											md-selected-item-change="vm.deviceChanged(deviceAlias)"
											md-items="item in vm.fetchDevices(deviceAlias.searchText)"
											md-item-text="item.name"
											md-min-length="0"
											placeholder="{{ 'device.device' | translate }}">
											<md-item-template>
												<span md-highlight-text="deviceAlias.searchText" md-highlight-flags="^i">{{item.name}}</span>
											</md-item-template>
											<md-not-found>
												<span translate translate-values='{ device: deviceAlias.searchText }'>device.no-devices-matching</span>
											</md-not-found>
								  </md-autocomplete>
								  <div class="tb-error-messages" ng-messages="aliasForm.device_id.$error">
									<div translate ng-message="required" class="tb-error-message">device.device-required</div>
								  </div>
							  </section>
							<md-button ng-disabled="loading" class="md-icon-button md-primary" style="min-width: 40px;"
					                ng-click="vm.removeAlias($event, deviceAlias)" aria-label="{{ 'action.remove' | translate }}">
					          		<md-tooltip md-direction="top">
				        						{{ 'device.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.isSingleDevice" 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">
			      						{{ 'device.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>