<!--

    Copyright © 2016-2018 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-content class="md-padding" layout="column">
    <md-autocomplete ng-if="model.type === types.dataKeyType.timeseries ||
    						model.type === types.dataKeyType.attribute ||
    						model.type === types.dataKeyType.alarm"
    	   style="padding-bottom: 8px;"
           ng-required="true"
   		   md-no-cache="true"
   		   id="key"
   		   ng-model="model.name"
		   md-selected-item="model.name"
		   md-search-text="keySearchText"
		   md-items="item in keysSearch(keySearchText)"
	 	   md-item-text="item"
		   md-min-length="0"
           placeholder="{{ 'entity.key-name' | translate }}"
           md-floating-label="{{ 'entity.key' | translate }}">
           <span md-highlight-text="keySearchText" md-highlight-flags="^i">{{item}}</span>
    </md-autocomplete>
	<div layout="row" layout-align="start center">     
		<md-input-container flex class="md-block">
			<label translate>datakey.label</label>
			<input ng-required="true" name="label" ng-model="model.label">	
		</md-input-container>
		<div flex md-color-picker
		    ng-required="true"
		    ng-model="model.color"
		    label="{{ 'datakey.color' | translate }}"
		    icon="format_color_fill"
		    default="#999"
		    md-color-clear-button="false"
		    open-on-input="true"
		    md-color-generic-palette="false"
		    md-color-history="false">
		</div>    
	</div>
	<div layout="row" layout-align="start center" ng-if="model.type !== types.dataKeyType.alarm">
		<md-input-container flex>
			<label translate>datakey.units</label>
			<input name="units" ng-model="model.units">
		</md-input-container>
		<md-input-container flex>
			<label translate>datakey.decimals</label>
			<input name="decimals" ng-model="model.decimals" type="number" min="0" max="15" step="1" ng-pattern="/^\d*$/">
		</md-input-container>
	</div>
	<section layout="column" ng-if="model.type === types.dataKeyType.function">
		<span translate>datakey.data-generation-func</span>
		<br/>
		<tb-js-func ng-model="model.funcBody"
				 function-args="{{ ['time', 'prevValue'] }}"
				 validation-args="{{ [[1, 1],[1, '1']] }}"
				 result-type="any">
		</tb-js-func>
	</section>	
	<section layout="column" ng-if="model.type === types.dataKeyType.timeseries || model.type === types.dataKeyType.attribute">
        <md-checkbox ng-model="model.usePostProcessing" aria-label="{{ 'datakey.use-data-post-processing-func' | translate }}">
			{{ 'datakey.use-data-post-processing-func' | translate }}
        </md-checkbox>		
		<tb-js-func ng-if="model.usePostProcessing"
		         ng-model="model.postFuncBody"
				 function-args="{{ ['time', 'value', 'prevValue'] }}"
				 validation-args="{{ [[1, 1, 1],[1, '1', '1']] }}"
				 result-type="any">
		</tb-js-func>
	</section>
</md-content>