<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script>

    /* *************************** 模块初始化 ***************************** */
    var shell = {
        grid: null,
        dialog: null,
        init: function () {
            this.bindEvents();
            this.initForm();
        },
        initForm: function () {
            var me = this;
            $('.m-retrieve-area').show();

        },
        bindEvents: function () {
            var me = this;
            $(".l-text").css("display","inline-block");
            $(".l-text-wrapper").css("display","inline-block");

            $("#main_text").keyup(function (e) {
                if (e.keyCode == 13) {
                    var command = $("#main_text").val();
                    var arr=command.split("\r\n");
                    var currentCommand = arr[0].split("]#");
                   me.sendShell(currentCommand[1]);
                }
            });

        },
        anthorize: function (id) {
        },
        sendShell:function(command){
            var disCon;
            var statuaValue = $("#disCon").ischecked;
            if("checked" == statuaValue){
                disCon = true;
            }else{
                disCon = false;
            }
            $.ajax({ //ajax处理
                type: "GET",
                url : "${contextRoot}/shell/sendShell",
                dataType : "json",
                data:{command:command,disCon:disCon},
                cache:false,
                success :function(data){
                    if(data.successFlg) {
                        var flg = data.successFlg;
                        if(flg){
                            shell.getShellBack();
                        }else{
                            alert("shell请求命令失败！");
                        }
                    }
                    else{
                        $.ligerDialog.error(data.message);
                    }
                },
                error :function(data){
                    $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                }
            });

        },
        getShellBack:function(){
            $.ajax({ //ajax 获取shell执行结果
                type: "GET",
                url : "${contextRoot}/shell/getShellBack",
                dataType : "json",
                data:{},
                cache:false,
                success :function(data){
                    if(data.successFlg) {
                        var flg = data.successFlg;
                        if(flg){
                            debugger
                            //TODO 字体颜色textare无法解析，所以暂时去除，后面看看有没有其他控件可以比较完美的支持
                            var content=data.message.myReplace("\\u001B\\[0m\\u001B\\[01;31m","").myReplace("\\u001B\\[0m","").myReplace("\\u001B\\[01;31m","").myReplace("\\u001B\\[01;34m","");
                            if(content.endsWith("]# ")){
                                $("#main_text").keydown(function(){
                                    PingBi(8);
                                });
                                $("#main_text").val(content);
                            }else{
                                $("#main_text").val(content+"\n ~]# ");
                                $("#main_text").keydown(function(){
                                    PingBi(8);
                                });
                            }
                        }else{
                            alert("shell请求命令失败！");
                        }
                    }
                    else{
                        $.ligerDialog.error(data.message);
                    }
                },
                error :function(data){
                    $.ligerDialog.error("Status:"+data.status +"(" +data.statusText+")");
                }
            });

        },
        //弹窗返回
        dialogSuccess: function (message) {
            $.ligerDialog.success(message);
            shell.reloadGrid();
            shell.dialog.close();
        }

    };

    function PingBi(id){
        var command = $("#main_text").val();
        var arr=command.split("\r\n");
        var currentCommand = arr[0];
        var k=window.event.keyCode;
        /* 以]# 结束的一行，使之无法删除*/
        if(k==id && currentCommand.endsWith("]# ")){
            window.event.keyCode=0;
            window.event.returnValue=false;
            return false;
        }
    }

    String.prototype.myReplace=function(f,e){//吧f替换成e
        var reg=new RegExp(f,"g"); //创建正则RegExp对象
        return this.replace(reg,e);
    }


    $(function () {
        shell.init();
        shell.sendShell("\n");
    });
</script>