<!--

    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.

-->
<md-dialog class="tb-node-script-test-dialog"
           aria-label="{{ 'rulenode.test-script-function' | translate }}" style="width: 800px;">
    <form flex name="theForm" ng-submit="vm.save()">
        <md-toolbar>
            <div class="md-toolbar-tools">
                <h2>{{ 'rulenode.test-script-function' | 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-dialog-content flex style="position: relative;">
            <div class="tb-absolute-fill">
                <div id="top_panel" class="tb-split tb-split-vertical">
                    <div id="top_left_panel" class="tb-split tb-content">
                        <div class="tb-resize-container">
                            <div class="tb-editor-area-title-panel">
                                <label translate>rulenode.message</label>
                            </div>
                            <ng-form name="payloadForm">
                                <div layout="column" style="height: 100%;">
                                    <div layout="row">
                                        <tb-message-type-autocomplete
                                                style="margin-bottom: 0px; min-width: 300px;"
                                                ng-required="true"
                                                ng-model="vm.inputParams.msgType"
                                                the-form="payloadForm">
                                        </tb-message-type-autocomplete>
                                    </div>
                                    <tb-json-content flex
                                                     ng-model="vm.inputParams.msg"
                                                     label="{{ 'rulenode.message' | translate }}"
                                                     content-type="vm.types.contentType.JSON.value"
                                                     validate-content="true"
                                                     validation-trigger-arg="validatePayload"
                                                     fill-height="true">
                                    </tb-json-content>
                                </div>
                            </ng-form>
                        </div>
                    </div>
                    <div id="top_right_panel" class="tb-split tb-content">
                        <div class="tb-resize-container" id="metadata-panel">
                            <div class="tb-editor-area-title-panel">
                                <label translate>rulenode.metadata</label>
                            </div>
                            <ng-form name="metadataForm">
                                <tb-key-val-map title-text="rulenode.metadata" ng-disabled="$root.loading"
                                                key-val-map="vm.inputParams.metadata"></tb-key-val-map>
                            </ng-form>
                        </div>
                    </div>
                </div>
                <div id="bottom_panel" class="tb-split tb-split-vertical">
                    <div id="bottom_left_panel" class="tb-split tb-content">
                        <div class="tb-resize-container">
                            <div class="tb-editor-area-title-panel tb-js-function">
                                <label>{{ vm.functionTitle }}</label>
                            </div>
                            <ng-form name="funcBodyForm">
                                <tb-js-func id="funcBodyInput" ng-model="vm.inputParams.script"
                                            function-name="{{vm.inputParams.functionName}}"
                                            function-args="{{ vm.inputParams.argNames }}"
                                            validation-args="{{ [[vm.inputParams.msg, vm.inputParams.metadata, vm.inputParams.msgType]] }}"
                                            validation-trigger-arg="validateFuncBody"
                                            result-type="object"
                                            fill-height="true">
                                </tb-js-func>
                            </ng-form>
                        </div>
                    </div>
                    <div id="bottom_right_panel" class="tb-split tb-content">
                        <div class="tb-resize-container">
                            <div class="tb-editor-area-title-panel">
                                <label translate>rulenode.output</label>
                            </div>
                            <tb-json-content ng-model="vm.output"
                                             label="{{ 'rulenode.output' | translate }}"
                                             content-type="vm.types.contentType.JSON.value"
                                             validate-content="false"
                                             ng-readonly="true"
                                             fill-height="true">
                            </tb-json-content>
                        </div>
                    </div>
                </div>
            </div>
        </md-dialog-content>
        <md-dialog-actions layout="row">
            <md-button ng-disabled="$root.loading || theForm.$invalid" ng-click="vm.test()" class="md-raised md-primary">
                {{ 'rulenode.test' | translate }}
            </md-button>
            <span flex></span>
            <md-button ng-disabled="$root.loading || theForm.funcBodyForm.$invalid || !theForm.funcBodyForm.$dirty" type="submit" class="md-raised md-primary">
                {{ 'action.save' | translate }}
            </md-button>
            <md-button ng-disabled="$root.loading" ng-click="vm.cancel()" style="margin-right:20px;">{{ 'action.cancel' | translate }}</md-button>
        </md-dialog-actions>
    </form>
</md-dialog>
