
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='../ElectronicLoadTester/files/public.js'></script>");
document.write("<script type='text/javascript' src='../static/layui/layui.all.js'></script>");


$(function(){



    // $("#TextField_OCP_StartCurrent_input").val("1.000");
    // $("#TextField_OCP_StepCurrent_input").val("0.500");
    // $("#TextField_OCP_CutoffCurrent_input").val("5.000");
    // $("#TextField_OCP_StepTime_input").val("1000.0");
    // $("#TextField_OCP_CutoffVoltage_input").val("2.000");


    $("#FlowShape_OCP_Start").click(function () {
        //设置OCP起始电流
        var val = $("#TextField_OCP_StartCurrent_input").val();
        interface.setSerialSend(0, "OCP:IST " + val, "ocp_ist", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_ocp_IST(val);
        if(get_InstrumentModel("UTL85") == false){
            return layer.msg("<h1>该功能仅支持UTL85系列</h1>");
        }

        window.ocp_ist = function () {
            //设置OCP截止电流
            let loading = layer.load(2, {time: 10000});
            var val = $("#TextField_OCP_CutoffCurrent_input").val();
            interface.setSerialSend(0, "OCP:IEND " + val, "ocp_iend", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_ocp_IEND(val);
            window.ocp_iend = function () {
                //设置OCP步进电流值
                var val = $("#TextField_OCP_StepCurrent_input").val();
                interface.setSerialSend(0, "OCP:CSTep " + val, "ocp_cstep", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_ocp_CSTep(val);
                window.ocp_cstep = function () {
                    //设置OCP单步驻留时间
                    var val = $("#TextField_OCP_StepTime_input").val();
                    interface.setSerialSend(0, "OCP:DWEL " + val, "ocp_dwel", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_ocp_DWEL(val + "ms");
                    window.ocp_dwel = function () {
                        //设置OCP触发电平
                        var val = $("#TextField_OCP_CutoffVoltage_input").val();
                        interface.setSerialSend(0, "OCP:VTR " + val, "ocp_vtr", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_ocp_VTR(val);
                        window.ocp_vtr = function () {
                            //运行模式
                            interface.setSerialSend(0, "MODE OCP", "mode_ocp", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_LimitMode("OCP");
                            window.mode_ocp = function () {
                                interface.setSerialSend(0, "OCP ON", "ocp_on", obj.delay1, obj.delay2, obj.overDelay); //var dat = comm.set_ocp_ON("ON");
                                window.ocp_on = function () {
                                    layer.close(loading);
                                    layer.msg("<h1>完成设置</h1>");
                                }
                            }
                        }
                    }
                }
            }
        }
    });

    $("#FlowShape_OCP_Stop").click(function () {
        interface.setSerialSend(0, "OCP OFF", "ocp_off", obj.delay1, obj.delay2, obj.overDelay);  // var dat = comm.set_ocp_ON("OFF");
        window.ocp_off = function () {
            layer.msg("<h1>完成设置</h1>");
        }
    });




    //如下为动态测试
    {
        window.t1 = function () {
            if(interface.getFreeTime(0) < 100){
                // layer.msg(interface.getFreeTime(0))
            }
            else if(obj.cnt == 0){//查询控制方式
                get_LOCal();
                if(select_is("#RadioButton_Remote_img") == true){
                    obj.cnt++;
                }else{
                    $('#Heading_ConnectionStatus_text').html("连接状态:未连接");
                }
            }
            else if(obj.cnt == 1) {//通道选择
                if(ch_select() == "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 2) {//查询电流
                interface.getSerialComm(0, "MEASure:CURRent?", "measure_current", TaktTimeDelay, OverTimeDelay); //var dat = comm.get_Curr();
                window.measure_current = function (i, dat) {
                    chart_data_add(0, dat);obj.cnt++;
                    $('#Heading_GetCurrent_text').html(dat + " A");
                }
            }
            else if(obj.cnt == 3) {//查询电压
                interface.getSerialComm(0, "MEASure:VOLTage?", "measure_voltage", TaktTimeDelay, OverTimeDelay); //var dat = comm.get_Volt();
                window.measure_voltage = function (i, dat) {
                    chart_data_add(1, dat);obj.cnt++;
                    $('#Heading_GetVoltage_text').html(dat + " V");
                }
            }
            else if(obj.cnt == 4) {//查询功率
                interface.getSerialComm(0, "MEAS:POWer?", "measure_Power", TaktTimeDelay, OverTimeDelay); //var dat = comm.get_Power();
                window.measure_Power = function (i, dat) {
                    chart_data_add(2, dat);obj.cnt++;
                    $('#Heading_GetPower_text').html(dat + " W");
                }
            }
            else if(obj.cnt == 5) {//查询开关状态
                if(get_OnOff() == "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 6){//查询触发方式
                if(get_Trig_Mode()== "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 7){//查询触发状态
                if(get_Trigger()== "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 8){//查询暂停状态
                if(get_PauseState()== "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 9){//查询短路状态
                if(get_ShortState()== "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 10){//查询补偿功能状态
                if(get_SENSe()== "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 11){//查询补偿功能状态
                if(get_ModeState()== "ok"){
                    obj.cnt++;
                }
            }
            else if(obj.cnt == 12){//查询版本
                interface.getSerialComm(0, "*IDN?", "get_idn", TaktTimeDelay, OverTimeDelay); //var dat = comm.get_IDN();
                window.get_idn = function (i, dat) {
                    obj.cnt = 0;
                    InstrumentModel = dat;
                    $('#Heading_DeviceInformation_text').html("设备信息:" + dat);
                    $('#Heading_ConnectionStatus_text').html("连接状态:已连接");
                }
            }
            else{
                obj.cnt = 0;
            }
        }
    }



    //如下为图表显示
    {
        var chart_curr = new eEhart("#FlowShape_ChartCurr",        "", [{name: '电流'},{name: '电压'}, {name: '功率'}], 2);
        var chart_power = new eEhart("#FlowShape_ChartVoltPower", "", [{name: '电流'},{name: '电压'}, {name: '功率'}], 2);

        function chart_data_add(num, data) {
            var select_ch = $("#Droplist_Channel_input").val();
            if(select_ch == "CH2"){
                chart_curr.add_data(1, num, data);   chart_curr.chart_select = 1;
                chart_power.add_data(1, num, data);  chart_power.chart_select = 1;
            }
            else{
                chart_curr.add_data(0, num, data);   chart_curr.chart_select = 0;
                chart_power.add_data(0, num, data);   chart_power.chart_select = 0;
            }
        }
        $("#Droplist_Channel_input").change(function () {
            chart_curr.chart_select = this.selectedIndex;
            chart_power.chart_select = this.selectedIndex;
        });

        //水平滚动条联动
        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_curr.option_data[chart_curr.chart_select].dataZoom[0].end = end;
                chart_curr.option_data[chart_curr.chart_select].dataZoom[0].start = start;
                chart_power.option_data[chart_curr.chart_select].dataZoom[0].end = end;
                chart_power.option_data[chart_curr.chart_select].dataZoom[0].start = start;
                chart_curr.echart.setOption(chart_curr.option_data[chart_curr.chart_select]);
                chart_power.echart.setOption(chart_power.option_data[chart_curr.chart_select]);
            }
        }
        chart_curr.echart.on('dataZoom', function (params) {
            dataZoom(params);
        });
        chart_power.echart.on('dataZoom', function (params) {
            dataZoom(params);
        });

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

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

    }

    {//input值恢复与定时备份
        var userAgent = myBrowser().indexOf("WOW64");
        if(userAgent >= 0){
            $("#TextField_SetVoltage_input").val(interface.getIni("ocp_Test", "TextField_SetVoltage_input",            "5.00"));      $("#TextField_SetVoltage_input").attr("maxlength",5);
            $("#TextField_SetCurrent_input").val(interface.getIni("ocp_Test", "TextField_SetCurrent_input",            "1.20"));      $("#TextField_SetCurrent_input").attr("maxlength",5);
            $("#TextField_SetPower_input").val(interface.getIni("ocp_Test", "TextField_SetPower_input",                 "2.00"));      $("#TextField_SetPower_input").attr("maxlength",5);
            $("#TextField_SetResistance_input").val(interface.getIni("ocp_Test", "TextField_SetResistance_input",      "2.00"));      $("#TextField_SetResistance_input").attr("maxlength",5);
            $("#Droplist_ResistanceUnit_input").val(interface.getIni("ocp_Test", "Droplist_ResistanceUnit_input",      "Ohm"));
            $("#Droplist_Channel_input").val(interface.getIni("ocp_Test", "Droplist_Channel_input",                       "CH1"));

            $("#TextField_OCP_StartCurrent_input").val(interface.getIni("ocp_Test", "TextField_OCP_StartCurrent_input",    "1.000"));      $("#TextField_OCP_StartCurrent_input").attr("maxlength",7);
            $("#TextField_OCP_StepCurrent_input").val(interface.getIni("ocp_Test", "TextField_OCP_StepCurrent_input",       "0.500"));      $("#TextField_OCP_StepCurrent_input").attr("maxlength",7);
            $("#TextField_OCP_CutoffCurrent_input").val(interface.getIni("ocp_Test", "TextField_OCP_CutoffCurrent_input",  "5.000"));      $("#TextField_OCP_CutoffCurrent_input").attr("maxlength",7);
            $("#TextField_OCP_StepTime_input").val(interface.getIni("ocp_Test", "TextField_OCP_StepTime_input",              "1000.0"));      $("#TextField_OCP_StepTime_input").attr("maxlength",7);
            $("#TextField_OCP_CutoffVoltage_input").val(interface.getIni("ocp_Test", "TextField_OCP_CutoffVoltage_input",  "2.000"));      $("#TextField_OCP_CutoffVoltage_input").attr("maxlength",7);

            chart_curr.option_data[0].legend.selected = JSON.parse(interface.getIni("ocp_Test", "FlowShape_OCP_ChartCurr_legend_selected0",  "{}"));   chart_curr.echart.setOption(chart_curr.option_data[0]);
            chart_curr.option_data[1].legend.selected = JSON.parse(interface.getIni("ocp_Test", "FlowShape_OCP_ChartCurr_legend_selected1",  "{}"));   chart_curr.echart.setOption(chart_curr.option_data[1]);
            chart_power.option_data[0].legend.selected = JSON.parse(interface.getIni("ocp_Test", "FlowShape_OCP_ChartPower_legend_selected0",  "{}")); chart_power.echart.setOption(chart_power.option_data[0]);
            chart_power.option_data[1].legend.selected = JSON.parse(interface.getIni("ocp_Test", "FlowShape_OCP_ChartPower_legend_selected1",  "{}")); chart_power.echart.setOption(chart_power.option_data[1]);

            for(var i=0; i<chart_curr.option_data[0].series.length; i++){
                chart_curr.option_data[0].series[i].color = interface.getIni("ocp_Test", "FlowShape_chart_curr_series" + i,  "#0000ff");
            }
            chart_curr.echart.setOption(chart_curr.option_data[0]);

            for(var i=0; i<chart_power.option_data[0].series.length; i++){
                chart_power.option_data[0].series[i].color = interface.getIni("ocp_Test", "FlowShape_chart_power_series" + i,  "#0000ff");
            }
            chart_power.echart.setOption(chart_power.option_data[0]);
        }
        window.tini = function () {
            if(userAgent >= 0){
                interface.setIni("ocp_Test", "TextField_SetVoltage_input",                $("#TextField_SetVoltage_input").val());
                interface.setIni("ocp_Test", "TextField_SetCurrent_input",                $("#TextField_SetCurrent_input").val());
                interface.setIni("ocp_Test", "TextField_SetPower_input",                   $("#TextField_SetPower_input").val());
                interface.setIni("ocp_Test", "TextField_SetResistance_input",             $("#TextField_SetResistance_input").val());
                interface.setIni("ocp_Test", "Droplist_ResistanceUnit_input",             $("#Droplist_ResistanceUnit_input").val());
                interface.setIni("ocp_Test", "Droplist_Channel_input",                     $("#Droplist_Channel_input").val());

                interface.setIni("ocp_Test", "TextField_OCP_StartCurrent_input",          $("#TextField_OCP_StartCurrent_input").val());
                interface.setIni("ocp_Test", "TextField_OCP_StepCurrent_input",           $("#TextField_OCP_StepCurrent_input").val());
                interface.setIni("ocp_Test", "TextField_OCP_CutoffCurrent_input",         $("#TextField_OCP_CutoffCurrent_input").val());
                interface.setIni("ocp_Test", "TextField_OCP_StepTime_input",               $("#TextField_OCP_StepTime_input").val());
                interface.setIni("ocp_Test", "TextField_OCP_CutoffVoltage_input",         $("#TextField_OCP_CutoffVoltage_input").val());

                interface.setIni("ocp_Test", "FlowShape_OCP_ChartCurr_legend_selected0",  JSON.stringify(chart_curr.echart.getOption().legend[0].selected));
                interface.setIni("ocp_Test", "FlowShape_OCP_ChartCurr_legend_selected1",  JSON.stringify(chart_curr.echart.getOption().legend[0].selected));
                interface.setIni("ocp_Test", "FlowShape_OCP_ChartPower_legend_selected0", JSON.stringify(chart_power.echart.getOption().legend[0].selected));
                interface.setIni("ocp_Test", "FlowShape_OCP_ChartPower_legend_selected1", JSON.stringify(chart_power.echart.getOption().legend[0].selected));

                var opt = chart_curr.echart.getOption().series;
                for(var i=0; i<opt.length; i++){
                    interface.setIni("ocp_Test", "FlowShape_chart_curr_series" + i,  opt[i].color);
                }
                var opt = chart_power.echart.getOption().series;
                for(var i=0; i<opt.length; i++){
                    interface.setIni("ocp_Test", "FlowShape_chart_power_series" + i,  opt[i].color);
                }
            }
        }
    }
    interface.setTime([["t1", 30], ["tini", 10000]]);

});






