<!--

    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.

-->
<section class="tb-datasource-func" flex layout='column'
		 layout-align="center" layout-gt-sm='row' layout-align-gt-sm="start center">
	<md-input-container ng-if="widgetType != types.widgetType.alarm.value"
						class="tb-datasource-name" md-no-float style="min-width: 200px;">
		<input name="datasourceName"
			   placeholder="{{ 'datasource.name' | translate }}"
			   ng-model="datasourceName"
			   aria-label="{{ 'datasource.name' | translate }}">
	</md-input-container>
    <section flex layout='column' style="padding-left: 4px;">
	   <md-chips flex ng-if="widgetType != types.widgetType.alarm.value"
					 id="function_datakey_chips"
					 ng-required="true"
					 ng-model="funcDataKeys" md-autocomplete-snap
					 md-transform-chip="transformFuncDataKeyChip($chip)"
					 md-require-match="false">
				  <md-autocomplete
						md-no-cache="false"
						id="dataKey"
						md-selected-item="selectedDataKey"
						md-search-text="dataKeySearchText"
						md-items="item in dataKeysSearch(dataKeySearchText)"
						md-item-text="item.name"
						md-min-length="0"
						placeholder="{{ 'datakey.function-types' | translate }}"
						md-menu-class="tb-func-datakey-autocomplete">
						<span md-highlight-text="dataKeySearchText" md-highlight-flags="^i">{{item}}</span>
						<md-not-found>
							<div class="tb-not-found">
								<div class="tb-no-entries" ng-if="!textIsNotEmpty(dataKeySearchText)">
									<span translate>device.no-keys-found</span>
								</div>
								<div ng-if="textIsNotEmpty(dataKeySearchText)">
									<span translate translate-values='{ key: "{{dataKeySearchText | truncate:true:6:&apos;...&apos;}}" }'>device.no-key-matching</span>
									<span>
										<a translate ng-click="createKey($event, '#function_datakey_chips')">device.create-new-key</a>
									</span>
								</div>
							</div>
						</md-not-found>
				  </md-autocomplete>
				  <md-chip-template>
					  <div layout="row" layout-align="start center" class="tb-attribute-chip">
						  <div class="tb-color-preview" ng-click="showColorPicker($event, $chip, $index)" style="margin-right: 5px;">
							  <div class="tb-color-result" ng-style="{background: $chip.color}"></div>
						  </div>
						  <div layout="row" flex>
							  <div class="tb-chip-label">
								  {{$chip.label}}
							  </div>
							  <div class="tb-chip-separator">: </div>
							  <div class="tb-chip-label">
								  <strong>{{$chip.name}}</strong>
							  </div>
						  </div>
						  <md-button ng-click="editDataKey($event, $chip, $index)" class="md-icon-button tb-md-32">
							  <md-icon aria-label="edit" class="material-icons tb-md-20">edit</md-icon>
						  </md-button>
					  </div>
				  </md-chip-template>
		</md-chips>
		<md-chips flex ng-if="widgetType == types.widgetType.alarm.value"
				  id="alarm_datakey_chips"
				  ng-required="true"
				  ng-model="alarmDataKeys" md-autocomplete-snap
				  md-transform-chip="transformAlarmDataKeyChip($chip)"
				  md-require-match="true">
			<md-autocomplete
					md-no-cache="true"
					id="alarm_datakey"
					md-selected-item="selectedAlarmDataKey"
					md-search-text="alarmDataKeySearchText"
					md-items="item in dataKeysSearch(alarmDataKeySearchText, types.dataKeyType.alarm)"
					md-item-text="item.name"
					md-min-length="0"
					placeholder="{{'datakey.alarm' | translate }}"
					md-menu-class="tb-alarm-datakey-autocomplete">
				<span md-highlight-text="alarmDataKeySearchText" md-highlight-flags="^i">{{item}}</span>
				<md-not-found>
					<div class="tb-not-found">
						<div class="tb-no-entries" ng-if="!textIsNotEmpty(alarmDataKeySearchText)">
							<span translate>entity.no-keys-found</span>
						</div>
						<div ng-if="textIsNotEmpty(alarmDataKeySearchText)">
							<span translate translate-values='{ key: "{{alarmDataKeySearchText | truncate:true:6:&apos;...&apos;}}" }'>entity.no-key-matching</span>
						</div>
					</div>
				</md-not-found>
			</md-autocomplete>
			<md-chip-template>
				<div layout="row" layout-align="start center" class="tb-attribute-chip">
					<div class="tb-color-preview" ng-click="showColorPicker($event, $chip, $index)" style="margin-right: 5px;">
						<div class="tb-color-result" ng-style="{background: $chip.color}"></div>
					</div>
					<div layout="row" flex>
						<div class="tb-chip-label">
							{{$chip.label}}
						</div>
						<div class="tb-chip-separator">: </div>
						<div class="tb-chip-label">
							<strong ng-if="!$chip.postFuncBody">{{$chip.name}}</strong>
							<strong ng-if="$chip.postFuncBody">f({{$chip.name}})</strong>
						</div>
					</div>
					<md-button ng-click="editDataKey($event, $chip, $index)" class="md-icon-button tb-md-32">
						<md-icon aria-label="edit" class="material-icons tb-md-20">edit</md-icon>
					</md-button>
				</div>
			</md-chip-template>
		</md-chips>
		<div class="tb-error-messages" ng-messages="ngModelCtrl.$error" role="alert">
			<div translate ng-message="datasourceKeys" ng-if="widgetType !== types.widgetType.alarm.value" class="tb-error-message">datakey.function-types-required</div>
			<div translate ng-message="datasourceKeys" ng-if="widgetType === types.widgetType.alarm.value" class="tb-error-message">datakey.alarm-fields-required</div>
		</div>
   </section>
</section>
