<!--

    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 aria-label="{{ 'attribute.add' | translate }}" style="min-width: 400px;">
    <form name="theForm" ng-submit="vm.add()">
        <md-toolbar>
            <div class="md-toolbar-tools">
                <h2 translate>attribute.add</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">
                <md-content class="md-padding" layout="column">
                    <fieldset ng-disabled="loading">
                        <md-input-container class="md-block">
                            <label translate>attribute.key</label>
                            <input required name="key" ng-model="vm.attribute.key">
                            <div ng-messages="theForm.key.$error">
                                <div translate ng-message="required">attribute.key-required</div>
                            </div>
                        </md-input-container>
                        <section layout="row">
                            <md-input-container flex="40" class="md-block" style="width: 200px;">
                                <label translate>value.type</label>
                                <md-select ng-model="vm.valueType" ng-disabled="loading()">
                                    <md-option ng-repeat="type in vm.valueTypes" ng-value="type">
                                        <md-icon md-svg-icon="{{ type.icon }}"></md-icon>
                                        <span>{{type.name | translate}}</span>
                                    </md-option>
                                </md-select>
                            </md-input-container>
                            <md-input-container ng-if="vm.valueType===vm.valueTypes.string" flex="60" class="md-block">
                                <label translate>value.string-value</label>
                                <input required name="value" ng-model="vm.attribute.value">
                                <div ng-messages="theForm.value.$error">
                                    <div translate ng-message="required">attribute.value-required</div>
                                </div>
                            </md-input-container>
                            <md-input-container ng-if="vm.valueType===vm.valueTypes.integer" flex="60" class="md-block">
                                <label translate>value.integer-value</label>
                                <input required name="value" type="number" step="1" ng-pattern="/^-?[0-9]+$/" ng-model="vm.attribute.value">
                                <div ng-messages="theForm.value.$error">
                                    <div translate ng-message="required">attribute.value-required</div>
                                    <div translate ng-message="pattern">value.invalid-integer-value</div>
                                </div>
                            </md-input-container>
                            <md-input-container ng-if="vm.valueType===vm.valueTypes.double" flex="60" class="md-block">
                                <label translate>value.double-value</label>
                                <input required name="value" type="number" step="any" ng-model="vm.attribute.value">
                                <div ng-messages="theForm.value.$error">
                                    <div translate ng-message="required">attribute.value-required</div>
                                </div>
                            </md-input-container>
                            <div layout="column" layout-align="center" flex="60" ng-if="vm.valueType===vm.valueTypes.boolean">
                                <md-checkbox ng-model="vm.attribute.value" style="margin-bottom: 0px;">
                                    {{ (vm.attribute.value ? 'value.true' : 'value.false') | translate }}
                                </md-checkbox>
                            </div>
                        </section>
                    </fieldset>
                </md-content>
            </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.add' | 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>
