

document.write("<script type='text/javascript' src='../static/bootstrap/bootstrap/js/bootstrap.js'></script>");
document.write("<link rel='stylesheet' href='../static/bootstrap/bootstrap/css/bootstrap.css'  media='all'>");
document.write("<script type='text/javascript' src='../static/echarts/echarts.min.js'></script>");
document.write("<script type='text/javascript' src='../public/interface.js'></script>");
document.write("<script type='text/javascript' src='../public/public.js'></script>");

document.write("<script type='text/javascript' src='../static/layui/layui.all.js'></script>");
document.write("<link rel='stylesheet' href='../static/layui/css/layui.css'  media='all'>");


$(function(){
    var interface = new Interface(16);
    var obj = {cnt:new Array(16), delay:0, set_delay:1, delay1:500, delay2:500, overDelay:1200, get_delay:100, TaktTimeDelay:50, OverTimeDelay:200};
    window.t1 = function() {


        let PortState = JSON.parse(interface.getSerialInfo());
        function get_idn(i,dat){
            if(dat.length > 0){
                $("#Text_model_" + (i+1) + "_text").text(dat.substring(0,7));
                $("#Text_Port_" + (i+1) + "_text").text(PortState.data[i]["PortName"]);
            }else{
                $("#Text_model_" + (i+1) + "_text").text("未应答");
                set_Button_OnOff("#Button_OnOff_" + (i+1), false);
            }
            obj.cnt[i]++;
        }
        window.get_idn_0 = function(i, dat){get_idn(i,dat);}
        window.get_idn_1 = function(i, dat){get_idn(i,dat);}
        window.get_idn_2 = function(i, dat){get_idn(i,dat);}
        window.get_idn_3 = function(i, dat){get_idn(i,dat);}
        window.get_idn_4 = function(i, dat){get_idn(i,dat);}
        window.get_idn_5 = function(i, dat){get_idn(i,dat);}
        window.get_idn_6 = function(i, dat){get_idn(i,dat);}
        window.get_idn_7 = function(i, dat){get_idn(i,dat);}
        window.get_idn_8 = function(i, dat){get_idn(i,dat);}
        window.get_idn_9 = function(i, dat){get_idn(i,dat);}
        window.get_idn_10 = function(i, dat){get_idn(i,dat);}
        window.get_idn_11 = function(i, dat){get_idn(i,dat);}
        window.get_idn_12 = function(i, dat){get_idn(i,dat);}
        window.get_idn_13 = function(i, dat){get_idn(i,dat);}
        window.get_idn_14 = function(i, dat){get_idn(i,dat);}
        window.get_idn_15 = function(i, dat){get_idn(i,dat);}


        function get_vcm(i,dat){
            if(dat.length > 0){
                let volt = dat.substring(0, dat.indexOf(","));
                let curr = dat.substring(dat.indexOf(",")+1, dat.length-1);
                $("#Text_Volt_" + (i+1) + "_text").text(volt);
                $("#Text_Curr_" + (i+1) + "_text").text(curr);
                chart.add_data(i, 1, volt);
                chart.add_data(i, 0, curr);
            }
            obj.cnt[i]++;
        }
        window.get_vcm_0 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_1 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_2 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_3 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_4 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_5 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_6 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_7 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_8 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_9 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_10 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_11 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_12 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_13 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_14 = function(i, dat){get_vcm(i,dat);}
        window.get_vcm_15 = function(i, dat){get_vcm(i,dat);}


        function get_onoff(i,dat){
            if(dat == "0"){
                set_Button_OnOff("#Button_OnOff_" + (i+1), false);
            }
            else if(dat == "1"){
                set_Button_OnOff("#Button_OnOff_" + (i+1), true);
            }
            obj.cnt[i] = 0;
        }
        window.get_onoff_0 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_1 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_2 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_3 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_4 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_5 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_6 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_7 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_8 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_9 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_10 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_11 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_12 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_13 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_14 = function(i, dat){get_onoff(i,dat);}
        window.get_onoff_15 = function(i, dat){get_onoff(i,dat);}



        for(let a=0; a<16; a++){
            if(interface.getSerialIsOpen(a) != "True"){
                $("#Text_Volt_" + (a+1) + "_text").text("");
                $("#Text_Curr_" + (a+1) + "_text").text("");
                $("#Text_Port_" + (a+1) + "_text").text("");
                set_Button_OnOff("#Button_OnOff_" + (a+1), false);
                $("#Text_model_" + (a+1) + "_text").text("未连接");
            }
            else if(interface.getFreeTime(a) < 100){

            }
            else if(obj.cnt[a] == 0){
                interface.getSerialComm(a, "*idn?", "get_idn_" + a, obj.TaktTimeDelay, obj.OverTimeDelay);
            }
            else if(obj.cnt[a] == 1){
                interface.getSerialComm(a, "MEAS:VCM?", "get_vcm_" + a, obj.TaktTimeDelay, obj.OverTimeDelay);
            }
            else if(obj.cnt[a] == 2){
                interface.getSerialComm(a, "OUTP?", "get_onoff_" + a, obj.TaktTimeDelay, obj.OverTimeDelay);
            }else{
                obj.cnt[a] = 0;
            }
        }


        // if(obj.cnt == 0){//获得版本
        //     obj.cnt++;
        //     var PortState = JSON.parse(interface.getSerialInfo());
        //     for(var i=0; i<16; i++){
        //
        //     }
        // }
        // else if(obj.cnt == 1){//获得电压
        //     obj.cnt++ ;
        //     for(var i=0; i<16; i++){
        //
        //     }
        // }
        // else if(obj.cnt == 2){//获得电流
        //     obj.cnt++;
        //     for(var i=0; i<16; i++){
        //
        //     }
        // }
        // else if(obj.cnt == 3){//获得开关状态
        //     obj.cnt = 0;
        //     for(var i=0; i<16; i++){
        //
        //     }
        // }


        $("#Line_Volt").css("background-color", chart.echart.getOption().series[1].color);    $("#Line_Volt_img").css("display", "none");
        $("#Line_Current").css("background-color", chart.echart.getOption().series[0].color); $("#Line_Current_img").css("display", "none");
    }


    //开关按钮
    {
        function set_Button_OnOff(id, flag) {
            var img = $(id).find("img")[0];
            set_img_select("#" + img.id, flag);
        }
        for(var num = 1; num <= 16; num++) {
            $("#Button_OnOff_" + num).bind("click", {
                index: num
            }, clickOnoffHandler);
        }
        function clickOnoffHandler(event) {
            interface.getSerialComm(event.data.index-1, "OUTP?", "get_OnState2", obj.TaktTimeDelay, obj.OverTimeDelay);
            window.get_OnState2 = function (i,dat) {
                if(dat == "0"){
                    interface.setSerialSend(i, "OUTP 1", "", obj.delay1, obj.delay2, obj.overDelay);
                }
                else if(dat == "1"){
                    interface.setSerialSend(i, "OUTP 0", "", obj.delay1, obj.delay2, obj.overDelay);
                }
            }
        }
    }

    //设置电流电压按钮
    {
        for(var num = 1; num <= 16; num++) {
            $("#Button_SetCurrVolt_" + num).bind("click", {
                index: num
            }, clickSetCurrVoltHandler);
        }
        function clickSetCurrVoltHandler(event) {
            var curr = $("#Input_SetCurr_" + event.data.index + "_input").val();
            var volt = $("#Input_SetVolt_" + event.data.index + "_input").val();
            interface.setSerialSend(event.data.index-1, "CURR " + curr, "set_curr_bt", obj.delay1, obj.delay2, obj.overDelay);
            window.set_curr_bt = function(i){
                interface.setSerialSend(i, "VOLT " + volt, "", obj.delay1, obj.delay2, obj.overDelay);
                layer.msg("<h1>完成设置</h1>")
            }
        }
    }

    //同步输出
    $("#Button_SynchronousOutput").click(function () {
        var img = $("#Button_SynchronousOutput").find("img")[0];
        for(var i=0; i<16; i++){
            if(interface.getSerialIsOpen(i) == "True" && $("#Text_EnState_" + (i + 1).toString()).attr("style") == "visibility: inherit;"){
                if($("#" + img.id).attr("class") == "img"){
                    interface.setSerialSend(i, "OUTP 0", "", obj.delay1, obj.delay2, obj.overDelay);
                }else{
                    interface.setSerialSend(i, "OUTP 1", "", obj.delay1, obj.delay2, obj.overDelay);
                }
            }
        }
    });
    //所有设备输出
    $("#Button_AllDeviceOutput").click(function () {
        var img = $("#Button_AllDeviceOutput").find("img")[0];
        for(var i=0; i<16; i++){
            if(interface.getSerialIsOpen(i) == "True"){
                if($("#" + img.id).attr("class") == "img"){
                    interface.setSerialSend(i, "OUTP 0", "", obj.delay1, obj.delay2, obj.overDelay);
                }else{
                    interface.setSerialSend(i, "OUTP 1", "", obj.delay1, obj.delay2, obj.overDelay);
                }
            }
        }
    });

    //显示时间
    {
        var TimeCnt = 0;
        window.t2 = function() {
            function formatSeconds(value) {
                let result = parseInt(value);
                let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
                let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
                let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
                result = `${h}:${m}:${s}`;
                return result
            }
            TimeCnt++;
            $('#Text_Time_text').html(formatSeconds(TimeCnt));
        }
        $("#Button_ResetTime").click(function () {
            TimeCnt = 0;
        });
    }



    //如下为图表显示
    {
        var chart = new eEhart("#Chart", "", [{name: '电流'},{name: '电压'}], 16);

        //水平滚动条联动
        function dataZoom(params){
            if(params.dataZoomId.indexOf("1") < 0){
                var end = 100,start = 0;
                if(params.start == undefined){
                    end = params.batch[0].end;
                    start = params.batch[0].start;
                }else{
                    end = params.end;
                    start = params.start;
                }
            }
            chart.option_data[chart.chart_select].dataZoom[0].end = end;
            chart.option_data[chart.chart_select].dataZoom[0].start = start;
            chart.echart.setOption(chart.option_data[chart.chart_select]);
        }
        chart.echart.on('dataZoom', function (params) {
            dataZoom(params);
        });
        chart.echart.on('legendselectchanged', function (params) {

        });
        $("#Select_Device_input").change(function () {
            chart.chart_select = this.selectedIndex;
            chart.echart.setOption(chart.option_data[chart.chart_select]);
        });

        var seriesIndex = 0;
        $("#Chart").append('<div id="div_colpick_show1" style="padding-left: 0px; padding-top: 0px"></div>');
        chart.echart.on('click', function (params) {
            seriesIndex = params.seriesIndex;
            const box = document.querySelector("#div_colpick_show1");
            $(box).colpick({
                onSubmit: (hsb, hex, rgb, el, bySetColor) => {
                    // layer.msg(hex);
                    chart.option_data[chart.chart_select].series[seriesIndex].color = "#" + hex;
                    chart.echart.setOption(chart.option_data[chart.chart_select]);
                    $(box).colpickHide();
                }
            }); // 初始化
            $(box).colpickShow(); // 显示
        });

    }

    $("#button_exit").click(function () {
        interface.exit_windows();
    });

    //input值恢复与定时备份
    {
        var userAgent = myBrowser().indexOf("WOW64");
        function stringToBool(str) {
            if(str == "True" || str == "true" || str == "TRUE"){
                return true;
            }
            return false;
        }

        var id_list = [
            "Input_SetVolt_1_input",                   //
            "Input_SetVolt_2_input",                   //
            "Input_SetVolt_3_input",                   //
            "Input_SetVolt_4_input",                   //
            "Input_SetVolt_5_input",                   //
            "Input_SetVolt_6_input",                   //
            "Input_SetVolt_7_input",                   //
            "Input_SetVolt_8_input",                   //
            "Input_SetVolt_9_input",                   //
            "Input_SetVolt_10_input",                  //
            "Input_SetVolt_11_input",                  //
            "Input_SetVolt_12_input",                  //
            "Input_SetVolt_13_input",                  //
            "Input_SetVolt_14_input",                  //
            "Input_SetVolt_15_input",                  //
            "Input_SetVolt_16_input",                  //

            "Input_SetCurr_1_input",                   //
            "Input_SetCurr_2_input",                   //
            "Input_SetCurr_3_input",                   //
            "Input_SetCurr_4_input",                   //
            "Input_SetCurr_5_input",                   //
            "Input_SetCurr_6_input",                   //
            "Input_SetCurr_7_input",                   //
            "Input_SetCurr_8_input",                   //
            "Input_SetCurr_9_input",                   //
            "Input_SetCurr_10_input",                  //
            "Input_SetCurr_11_input",                  //
            "Input_SetCurr_12_input",                  //
            "Input_SetCurr_13_input",                  //
            "Input_SetCurr_14_input",                  //
            "Input_SetCurr_15_input",                  //
            "Input_SetCurr_16_input",                  //
        ];

        var id_en_list = [
            "Text_EnState_1",                        //
            "Text_EnState_2",                        //
            "Text_EnState_3",                        //
            "Text_EnState_4",                        //
            "Text_EnState_5",                        //
            "Text_EnState_6",                        //
            "Text_EnState_7",                        //
            "Text_EnState_8",                        //
            "Text_EnState_9",                        //
            "Text_EnState_10",                        //
            "Text_EnState_11",                        //
            "Text_EnState_12",                        //
            "Text_EnState_13",                        //
            "Text_EnState_14",                        //
            "Text_EnState_15",                        //
            "Text_EnState_16",                        //

            "Button_Enable1_ON",                     //
            "Button_Enable2_ON",                     //
            "Button_Enable3_ON",                     //
            "Button_Enable4_ON",                     //
            "Button_Enable5_ON",                     //
            "Button_Enable6_ON",                     //
            "Button_Enable7_ON",                     //
            "Button_Enable8_ON",                     //
            "Button_Enable9_ON",                     //
            "Button_Enable10_ON",                     //
            "Button_Enable11_ON",                     //
            "Button_Enable12_ON",                     //
            "Button_Enable13_ON",                     //
            "Button_Enable14_ON",                     //
            "Button_Enable15_ON",                     //
            "Button_Enable16_ON",                     //

            "Button_Enable1_OFF",                     //
            "Button_Enable2_OFF",                     //
            "Button_Enable3_OFF",                     //
            "Button_Enable4_OFF",                     //
            "Button_Enable5_OFF",                     //
            "Button_Enable6_OFF",                     //
            "Button_Enable7_OFF",                     //
            "Button_Enable8_OFF",                     //
            "Button_Enable9_OFF",                     //
            "Button_Enable10_OFF",                     //
            "Button_Enable11_OFF",                     //
            "Button_Enable12_OFF",                     //
            "Button_Enable13_OFF",                     //
            "Button_Enable14_OFF",                     //
            "Button_Enable15_OFF",                     //
            "Button_Enable16_OFF",                     //
        ];
        // var id_tb_list = [
        //     "Text_EnState_1",                   //
        //     "Text_EnState_2",                   //
        //     "Text_EnState_3",                   //
        //     "Text_EnState_4",                   //
        //     "Text_EnState_5",                   //
        //     "Text_EnState_6",                   //
        //     "Text_EnState_7",                   //
        //     "Text_EnState_8",                   //
        //     "Text_EnState_9",                   //
        //     "Text_EnState_10",                   //
        //     "Text_EnState_11",                   //
        //     "Text_EnState_12",                   //
        //     "Text_EnState_13",                   //
        //     "Text_EnState_14",                   //
        //     "Text_EnState_15",                   //
        //     "Text_EnState_16",                   //
        // ];

        function EnShowSet(id1, id2, id3, flag) {
            if(flag){
                $("#" + id1).attr("style", "visibility: inherit;");
                $("#" + id2).attr("style", "visibility: inherit; cursor: pointer;");
                $("#" + id3).attr("style", "cursor: pointer; visibility: hidden; display: none;");
            }else{
                $("#" + id1).attr("style", "visibility: hidden; display: none;");
                $("#" + id3).attr("style", "visibility: inherit; cursor: pointer;");
                $("#" + id2).attr("style", "cursor: pointer; visibility: hidden; display: none;");
            }
        }

        if(userAgent >= 0){
            for(var i=0; i<id_list.length; i++){
                $("#" + id_list[i]).val(interface.getIni("Dc_PowerBasicmode", id_list[i],  "1.000"));
            }
            for(var i=0; i<16; i++){
                if(interface.getIni("Dc_PowerBasicmode", "En_" + id_en_list[i],  "false") == "false"){
                    EnShowSet(id_en_list[i+0], id_en_list[i+16], id_en_list[i+32], false);
                }else{
                    EnShowSet(id_en_list[i+0], id_en_list[i+16], id_en_list[i+32], true);
                }
            }

            $("#Select_Device_input").val(interface.getIni("Dc_PowerBasicmode", "Select_Device_input",  "设备A"));
            chart.chart_select = $("#Select_Device_input").get(0).selectedIndex;

            for(var a=0; a<chart.option_data.length; a++){
                chart.option_data[a].series[0].color = interface.getIni("Dc_PowerBasicmode", a + "_FlowShape_chart_series_0",  "#0000ff");
                chart.option_data[a].series[1].color = interface.getIni("Dc_PowerBasicmode", a + "_FlowShape_chart_series_1",  "#ff0000");
                chart.option_data[a].legend.selected = JSON.parse(interface.getIni("Dc_PowerBasicmode", a + "_FlowShape_chart_legend_selected",  "{}"));   chart.echart.setOption(chart.option_data[a]);
            }
            chart.echart.setOption(chart.option_data[chart.chart_select]);

            window.t_save = function() {
                for(var i=0; i<id_list.length; i++){
                    interface.setIni("Dc_PowerBasicmode", id_list[i],     $("#" + id_list[i]).val());
                }
                for(var i=0; i<16; i++){
                    if($("#" + id_en_list[i]).attr("style") == "visibility: inherit;"){
                        interface.setIni("Dc_PowerBasicmode", "En_" + id_en_list[i],     "true");
                    }else{
                        interface.setIni("Dc_PowerBasicmode", "En_" + id_en_list[i],     "false");
                    }
                }

                interface.setIni("Dc_PowerBasicmode", "Select_Device_input",     $("#Select_Device_input").val());

                var opt = chart.echart.getOption().series;
                for(var i=0; i<opt.length; i++){
                    interface.setIni("Dc_PowerBasicmode", chart.chart_select + "_FlowShape_chart_series_" + i,  opt[i].color);
                }
                interface.setIni("Dc_PowerBasicmode", chart.chart_select + "_FlowShape_chart_legend_selected",  JSON.stringify(chart.echart.getOption().legend[0].selected));
            }
        }
    }

    interface.setTime([["t1", 100], ["t2", 1000], ["t_save", 10000]]);








});





