<!doctype html>
<html lang="en">

<head>
    <script src="jquery-3.4.1.min.js"></script>
    <meta charset="UTF-8">
    <meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
          name="viewport">
    <meta content="ie=edge" http-equiv="X-UA-Compatible">
    <script src="vue.js"></script>
    <link href="bootstrap/css/bootstrap.css" rel="stylesheet"></link>
    <title>Task Invoke</title>
    <style>
        table {
            table-layout: auto;
        }

        table,
        td,
        th,
        tr {
            border-color: black !important;
            text-overflow: ellipsis;
            overflow: hidden;
            white-space: nowrap;
            max-width: 400px;
            min-width: 150px;
        }

        .ID {
            width: 10%;
        }
    </style>
</head>

<body>
<div class="row" style="margin-top: 40px;">
    <div class="col-md-6" id="taskInfo" style="margin:0 auto" v-if="show">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb" style="padding-left:0;background-color: white">
                <li @click="gotoHome" class="breadcrumb-item"><a href="#">{{"Home~首页" | lang}}</a></li>
                <li @click="gotoInfo" aria-current="page" class="breadcrumb-item" style="color: black"><a href="#">{{"Task Information~任务信息" | lang}}</a></li>
                <li aria-current="page" class="breadcrumb-item active" style="color: black">{{"Task Invocation~任务调用"
                    | lang}}
                </li>
            </ol>
        </nav>
        <h4 style="text-align: center;">{{"Task Invocation~任务调用" | lang}}</h4>
        <p>{{"Task Name:~任务名称：" | lang}} {{task["name"]}}</p>
        <p style="word-wrap: break-word;word-break: break-all;overflow: hidden;max-height: 100px;">{{"Task Description:~任务描述：" | lang}} {{task["desc"]}}</p>
        <p style="word-wrap: break-word;word-break: break-all;overflow: hidden;max-height: 100px;">{{"URL:~URL：" |
            lang}} {{backEndAddressServiceWrapper}}/invokeTask?id={{task["id"]}}</p>
        <p>{{"Please Input Parameters:~请输入参数值：" | lang}} </p>
        <form id="form">
            <table class="table table-bordered">
                <tbody>
                <tr>
                    <th style="min-width: 50px;">ID</th>
                    <th>{{"Parameter Name~参数名称" | lang}}</th>
                    <th>{{"Invoke Name~调用名称" | lang}}</th>
                    <th>{{"Parameter Type~参数类型" | lang}}</th>
                    <th>{{"Parameter Value~参数值" | lang}}</th>
                </tr>

                <tr v-for="i in task.inputParameters.length" v-if="task.inputParameters.length>0">
                    <td style="min-width: 50px;">{{i}}</td>
                    <td style="max-width: 100px;">{{task.inputParameters[i-1]["nodeName"]}}</td>
                    <td>{{task.inputParameters[i-1]["name"]}}</td>
                    <td style="max-width: 100px;">{{task.inputParameters[i-1]["type"]}}</td>
                    <td><textarea class="form-control"
                                  style="min-height: 50px;min-width: 300px;" v-bind:name="task.inputParameters[i-1]['name']"
                                  v-model="task.inputParameters[i-1]['value']"></textarea></td>
                </tr>
                </tbody>
            </table>
            <div class="form-group" style="margin-top: 10px">
                <label>{{"User Data Folder (If you want to load the cookie data from your local browser, please set this folder path, and then cilck the 'Run with (Data Mode)' button to run the task):~用户本地浏览器数据目录（如果需要使用本地的登录信息和cookie，请设置此目录，并点击下方“执行（带用户信息模式）”按钮开始执行任务）：" | lang}}</label>
                <input type="text" class="form-control" v-model="user_data_folder"></input>
            </div>
        </form>

        <button class="btn btn-primary" v-on:click="localExecuteInstant(false)">{{"Directly Run Locally (Clean Mode)~本地直接执行（纯净模式）" |
            lang}}
        </button>
        <button class="btn btn-primary" v-on:click="localExecuteInstant(true)">{{"Directly Run Locally (Data Mode)~本地直接执行（带用户信息模式）" |
            lang}}
        </button>
        <!-- <button style="margin-left: 5px;" v-on:click="remoteExcuteInstant" class="btn btn-primary">Directly Run Remotely</button> -->
        <div style="margin-bottom: 10px;">
            <label style="margin-top: 10px;">{{"Execution ID (EID):~执行ID：" | lang}}</label>
            <input class="form-control" v-model="ID"></input>
            <p></p>
            <!-- <p>提示：点击下方按钮获得任务ID，然后根据此ID进行服务执行；也可自己POST调用接口得到ID，具体参照POST调用文档。</p> -->
            <p>{{"Hint: Click the following button to get the Execution ID (EID) of current running task.~提示：点击下方按钮获得任务执行ID，然后根据此执行ID运行任务。" | lang}}</p>
            <button class="btn btn-primary" href="javascript:void(0)" v-on:click="invokeTask">{{"Get Execution ID~获得任务执行ID" |
                lang}}</button>
            <button class="btn btn-primary" style="margin-left: 8px;" v-on:click="localExecute(false)">{{"Run Locally (Clean Mode)~本地执行（纯净模式）"
                | lang}}
            </button>
            <button class="btn btn-primary" style="margin-left: 8px;" v-on:click="localExecute(true)">{{"Run Locally (Data Mode)~本地执行（带用户信息模式）"
                | lang}}
            </button>
            <!-- <button v-on:click="remoteExcute" style="margin-left: 8px;" class="btn btn-primary">Run remotely</button></div> -->
        </div>

    </div>
</div>

</body>

</html>

<style>
    button{
        margin-top: 5px;
    }
</style>
<script src="global.js"></script>
<script>
    var sId = getUrlParam('id');
    var app = new Vue({
        el: '#taskInfo',
        data: {
            task: {},
            show: false, //是否渲染
            ID: "",
            user_data_folder:"",
            with_user_data: true,
            backEndAddressServiceWrapper: getUrlParam("backEndAddressServiceWrapper"),
        }, mounted() {
        $.get(this.backEndAddressServiceWrapper + "/getConfig", function (result) {
            app.$data.user_data_folder = result.user_data_folder;
        });
    },
        methods: {
            gotoHome: function () {
                let url = "";
                if (getUrlParam("lang") == "zh") {
                    url = "taskList.html?lang=zh&type="+getUrlParam("type")+"&wsport="+getUrlParam("wsport")+"&backEndAddressServiceWrapper=" + app.$data.backEndAddressServiceWrapper
                } else {
                    url = "taskList.html?lang=en&type="+getUrlParam("type")+"&wsport="+getUrlParam("wsport")+"&backEndAddressServiceWrapper=" + app.$data.backEndAddressServiceWrapper
                }
                window.location.href = url;
            }, gotoInfo: function () {
                let url = "";
                if (getUrlParam("lang") == "zh") {
                    url = "taskInfo.html?id="+getUrlParam("id")+"&lang=zh&type="+getUrlParam("type")+"&wsport="+getUrlParam("wsport")+"&backEndAddressServiceWrapper=" + app.$data.backEndAddressServiceWrapper
                } else {
                    url = "taskInfo.html?id="+getUrlParam("id")+"&lang=en&type="+getUrlParam("type")+"&wsport="+getUrlParam("wsport")+"&backEndAddressServiceWrapper=" + app.$data.backEndAddressServiceWrapper
                }
                window.location.href = url;
            }, invokeTask: function () {
                let text = "";
                if (getUrlParam("lang") == "en" || getUrlParam("lang") == "") {
                    text = "Are you sure to get the Execution ID (EID) of current running task?";
                } else {
                    text = "确定要获得当前运行任务的执行号吗？";
                }
                if (confirm(text)) {
                    var para = {};
                    var t = $('#form').serializeArray();
                    t.forEach(function (item, index) {
                        para[item.name] = item.value;
                    });
                    $.post(app.$data.backEndAddressServiceWrapper + "/invokeTask?id=" + sId, {
                        id: this.task.id,
                        paras: JSON.stringify(para)
                    }, function (result) {
                        app.$data.ID = result; //得到返回的ID
                    });
                }
            },
            localExecute: function (with_user_data=false) {
                if (this.ID == "") {
                    if (getUrlParam("lang") == "en" || getUrlParam("lang") == "") {
                        alert("Please get EID first!");
                    } else {
                        alert("请先获得执行号！");
                    }
                    return;
                }
                this.with_user_data = with_user_data;
                let text = "";
                if (getUrlParam("lang") == "en" || getUrlParam("lang") == "") {
                    text = "Are you sure to run this task locally?";
                } else {
                    text = "确定要在本地运行此任务吗？";
                }
                if (confirm(text)) {
                    let message = { //显示flowchart
                        type: 5, //消息类型，调用执行程序
                        message: {
                            "id": app.$data.ID,
                            "user_data_folder": app.$data.with_user_data ? app.$data.user_data_folder : "",
                        }
                    };
                    ws.send(JSON.stringify(message));
                }
            },
            remoteExecute: function () {

            },
            localExecuteInstant: function (with_user_data=false) {
                let text = "";
                if (getUrlParam("lang") == "en" || getUrlParam("lang") == "") {
                    text = "Are you sure to run this task locally now?";
                } else {
                    text = "确定要立即在本地运行此任务吗？";
                }
                this.with_user_data = with_user_data;
                if (confirm(text)) {
                    var para = {};
                    var t = $('#form').serializeArray();
                    t.forEach(function (item, index) {
                        para[item.name] = item.value;
                    });
                    $.post(app.$data.backEndAddressServiceWrapper + "/invokeTask?id=" + sId, {
                        id: this.task.id,
                        paras: JSON.stringify(para)
                    }, function (result) {
                        let message = { //显示flowchart
                            type: 5, //消息类型，调用执行程序
                            message: {
                                "id": result,
                                "user_data_folder": app.$data.with_user_data ? app.$data.user_data_folder : "",
                            }
                        };
                        ws.send(JSON.stringify(message));
                    });
                }
            },
            remoteExcuteInstant: function () {
            },
        }
    });
    $.get(app.$data.backEndAddressServiceWrapper + "/queryTask?id=" + sId, function (result) {
        app.$data.task = result;
        app.$data.show = true;
    });
    ws = new WebSocket("ws://localhost:"+getUrlParam("wsport"));
    ws.onopen = function () {
        // Web Socket 已连接上，使用 send() 方法发送数据
        console.log("Connected");
        message = {
            type: 0, //消息类型，0代表链接操作
            message: {
                id: 1, //socket id
            }
        };
        this.send(JSON.stringify(message));
    };
    ws.onclose = function () {
        // 关闭 websocket
        console.log("连接已关闭...");
    };
</script>