<!--

    Copyright © 2016-2019 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.

-->
<form name="theForm" ng-submit="vm.update()">
	<fieldset ng-disabled="$root.loading">
		<md-content style="height: 100%" flex layout="column">
			<section layout="column">
				<md-tabs ng-class="{'tb-headless': vm.historyOnly}" md-dynamic-height md-selected="vm.timewindow.selectedTab" md-border-bottom>
					<md-tab label="{{ 'timewindow.realtime' | translate }}">
						<md-content class="md-padding" layout="column">
							<tb-timeinterval predefined-name="'timewindow.last'"
								ng-required="vm.timewindow.selectedTab === 0"
								ng-model="vm.timewindow.realtime.timewindowMs" style="padding-top: 8px;"></tb-timeinterval>
						</md-content>
					</md-tab>
					<md-tab label="{{ 'timewindow.history' | translate }}">
						<md-content class="md-padding" layout="column" style="padding-top: 8px;">
							<md-radio-group ng-model="vm.timewindow.history.historyType" class="md-primary">
								<md-radio-button ng-value=0 aria-label="{{ 'timewindow.last' | translate }}" class="md-primary md-align-top-left md-radio-interactive">
									<section layout="column">
										<tb-timeinterval predefined-name="'timewindow.last'"
											ng-required="vm.timewindow.selectedTab === 1 && vm.timewindow.history.historyType === 0"
											ng-show="vm.timewindow.history.historyType === 0"
											ng-model="vm.timewindow.history.timewindowMs" style="padding-top: 8px;"></tb-timeinterval>
									</section>
								</md-radio-button>
								<md-radio-button ng-value=1 aria-label="{{ 'timewindow.time-period' | translate }}" class="md-primary md-align-top-left md-radio-interactive">
									<section layout="column">
										<span translate>timewindow.time-period</span>
										<tb-datetime-period
												ng-required="vm.timewindow.selectedTab === 1 && vm.timewindow.history.historyType === 1"
												ng-show="vm.timewindow.history.historyType === 1"
												ng-model="vm.timewindow.history.fixedTimewindow" style="padding-top: 8px;"></tb-datetime-period>
									</section>
								</md-radio-button>
							</md-radio-group>
						</md-content>
					</md-tab>
				</md-tabs>
				<md-content ng-if="vm.aggregation" class="md-padding" layout="column">
					<md-input-container>
						<label translate>aggregation.function</label>
						<md-select ng-model="vm.timewindow.aggregation.type" style="min-width: 150px;">
							<md-option ng-repeat="type in vm.aggregationTypes" ng-value="type.value">
								{{type.name | translate}}
							</md-option>
						</md-select>
					</md-input-container>
					<md-slider-container ng-if="vm.showLimit()">
						<span translate>aggregation.limit</span>
						<md-slider flex min="{{vm.minDatapointsLimit()}}" max="{{vm.maxDatapointsLimit()}}" step="1"
                                   ng-model="vm.timewindow.aggregation.limit" aria-label="limit" id="limit-slider">
						</md-slider>
						<md-input-container style="max-width: 80px;">
							<input flex type="number" step="1"
                                   min="{{vm.minDatapointsLimit()}}" max="{{vm.maxDatapointsLimit()}}"
                                   ng-model="vm.timewindow.aggregation.limit" aria-label="limit" aria-controls="limit-slider">
						</md-input-container>
					</md-slider-container>
					<tb-timeinterval ng-if="vm.showRealtimeAggInterval()" min="vm.minRealtimeAggInterval()" max="vm.maxRealtimeAggInterval()"
									 predefined-name="'aggregation.group-interval'"
									 ng-model="vm.timewindow.realtime.interval">
					</tb-timeinterval>
					<tb-timeinterval ng-if="vm.showHistoryAggInterval()" min="vm.minHistoryAggInterval()" max="vm.maxHistoryAggInterval()"
									 predefined-name="'aggregation.group-interval'"
									 ng-model="vm.timewindow.history.interval">
					</tb-timeinterval>
				</md-content>
			</section>
			<span flex></span>
			<section layout="row" layout-alignment="start center">
				  <span flex></span>
				  <md-button ng-disabled="$root.loading || theForm.$invalid || !theForm.$dirty" type="submit" class="md-raised md-primary">
				  		{{ 'action.update' | translate }}
				  </md-button>
			      <md-button ng-disabled="$root.loading" ng-click="vm.cancel()" style="margin-right:20px;">
					  {{ 'action.cancel' | translate }}
			      </md-button>
			</section> 
		</md-content>
	</fieldset>
</form>