

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);
            }
            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++){
                if(interface.getSerialIsOpen(i) == "True"){
                    var dat = serial_comm(i, "*idn?", 100, 500, 1);
                    if(dat.sta == "ok"){
                        $("#Text_model_" + (i+1) + "_text").text(dat.val.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);
                    }
                }else{
                    $("#Text_model_" + (i+1) + "_text").text("未连接");
                    set_Button_OnOff("#Button_OnOff_" + (i+1), false);
                }
            }
        }
        else if(obj.cnt == 1){//获得电压
            obj.cnt++ ;
            for(var i=0; i<16; i++){
                if(interface.getSerialIsOpen(i) == "True"){
                    var dat = serial_comm(i, "MEAS:VOLT?", 100, 500, 1);
                    if(dat.sta == "ok"){
                        $("#Text_Volt_" + (i+1) + "_text").text(dat.val);
                    }
                }else{
                    $("#Text_Volt_" + (i+1) + "_text").text("");
                }
            }
        }
        else if(obj.cnt == 2){//获得电流
            obj.cnt++;
            for(var i=0; i<16; i++){
                if(interface.getSerialIsOpen(i) == "True"){
                    var dat = serial_comm(i, "MEAS:CURR?", 100, 500, 1);
                    if(dat.sta == "ok"){
                        $("#Text_Curr_" + (i+1) + "_text").text(dat.val);
                    }
                }else{
                    $("#Text_Curr_" + (i+1) + "_text").text("");
                }
            }
        }
        else if(obj.cnt == 3){//获得开关状态
            obj.cnt = 0;
            for(var i=0; i<16; i++){
                if(interface.getSerialIsOpen(i) == "True"){
                    var dat = serial_comm(i, "OUTP?", 100, 500, 1);
                    if(dat.sta == "ok"){
                        if(dat.val == "0"){
                            set_Button_OnOff("#Button_OnOff_" + (i+1), false);
                        }
                        else if(dat.val == "1"){
                            set_Button_OnOff("#Button_OnOff_" + (i+1), true);
                        }
                    }
                }
            }
        }*/
    }

    //开关按钮
    {
        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);
                }
            }

        }
    }

    //同步输出
    $("#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);
                }
                TestResultsData.celerTestResultsData(i);
            }
        }
    });

    //列表测试
    {
        //初始化表头
        {
            var str_num = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"];
            var str_html1 = "<table border='1' id='TableCell_List_ListTestTable' class='table table-bordered table-condensed' style='background: white; word-break:keep-all '>";
            str_html1 += "<tr>";
            str_html1 += "<th style='text-align: center; width: 50px'>序号</th>";
            for(var i=0; i<str_num.length; i++){
                str_html1 += "<th style='width: 200px '>设备" + str_num[i] + "电压(V)</th>";
                str_html1 += "<th style='width: 200px '>设备" + str_num[i] + "电流(A)</th>";
                str_html1 += "<th style='width: 300px '>设备" + str_num[i] + "时间(S)</th>";
            }
            str_html1 += "</tr></table>"

            $("#ListTab").html(str_html1);
            $("#ListTab").css("overflow", "auto");
            $("#ListTab").css("margin-top",  $("#ListTab").data("top"));
            $("#ListTab").css("left",         $("#ListTab").data("left"));
            $("#ListTab").css("width",        $("#ListTab").data("width"));
            $("#ListTab").css("height",       $("#ListTab").data("height"));
            // $("th").css({'padding':'5px', 'vertical-align': 'middle', 'margin': '0 0 0 0'});
            $("td").css({'padding':'5px', 'vertical-align': 'middle', 'margin': '0 0 0 0'});
        }

        //行的增删操作
        {
            function select(str1, str2) {
                if(str1 == str2){return "selected='selected'";}else{return "";}
            }
            function add_row(row, data){
                var newRow = "\
                <tr style='background: white'>\
                    <td style=''></td>";
                    for(var i=0; i<str_num.length; i++){
                        newRow += "<td style=''><input placeholder='电流' class='form-control' style='' value='" + data[i*3 + 0] + "'></td>"
                        newRow += "<td style=''><input placeholder='电压' class='form-control' style='' value='" + data[i*3 + 1] + "'></td>"
                        newRow += "<td style=''><input placeholder='时间' class='form-control' style='' value='" + data[i*3 + 2] + "'></td>"
                    }
                 newRow += "</tr>";

                if(row < 0){
                    $("#TableCell_List_ListTestTable tr:last").after(newRow);//追加
                }else{
                    $("#TableCell_List_ListTestTable tr:gt(0):eq(" + row + ")").before(newRow);//指定位置前插入
                }
                tabSelectSetBackground(row + 1);
                // $("th").css({'padding':'0px', 'vertical-align': 'middle', 'margin': '0 0 0 0'});
                $("td").css({'padding':'0px', 'vertical-align': 'middle', 'margin': '0 0 0 0'});
            };
            //删除行
            $("#Button_Del").click(function () {//删除行
                var state = get_NowRowSelect("TableCell_List_ListTestTable");
                if(state.SelectRow > 0){
                    $("#TableCell_List_ListTestTable tr").eq(state.SelectRow).remove();
                    if(state.SelectRow > 1){tabSelectSetBackground(state.SelectRow);}else{tabSelectSetBackground(1);}
                }else{layer.msg("<h1>请选择删除位置</h1>");}
            });

            function define_dat(){
                var data = [];
                for(var i=0; i<16; i++){
                    data.push("2.00", "0.5", "1");
                }
                return data;
            }
            //添加行
            $("#Button_Add").click(function () {
                var state = get_NowRowSelect("TableCell_List_ListTestTable");
                if(state.RowSum > 16){
                    layer.msg("<h1>不能超过16行</h1>");
                }
                else {
                    add_row(-1, define_dat());
                }
            });
            //插入行
            $("#Button_Insert").click(function () {
                var state = get_NowRowSelect("TableCell_List_ListTestTable");
                if(state.RowSum > 16){
                    layer.msg("<h1>不能超过16行</h1>");
                }
                else if(state.SelectRow > 0){
                    add_row(state.SelectRow - 1, define_dat());
                }else{layer.msg("<h1>请选择插入位置</h1>")}
            });
        }


        //行状态获取及选中颜色设置
        {
            //获得当前选择的行
            function get_NowRowSelect(id){
                var ob  = {RowSum:-1, SelectRow: -1}
                var tbl = document.getElementById(id);
                var trs = tbl.getElementsByTagName("tr");
                ob.RowSum = trs.length;
                for (var i = 0; i < trs.length; i++) {
                    if(trs[i].style.background != "" && trs[i].style.background != "white"){
                        ob.SelectRow = i;
                        break;
                    }
                }
                return ob;
            }
            //选中行颜色
            $(document).on('click', '#TableCell_List_ListTestTable tr', function () {
                tabSelectSetBackground(this);
            });
            function tabSelectSetBackground(a){
                var tbl = document.getElementById("TableCell_List_ListTestTable");
                var trs = tbl.getElementsByTagName("tr");
                for (var i = 1; i < trs.length; i++) {
                    if (trs[i] == a || a == i) {  //判断是不是当前选择的行
                        trs[i].style.background = "#02f5ff";
                    }
                    else {
                        trs[i].style.background = "white";
                    }
                    trs[i].cells[0].innerText = i;
                }
            }
        }


        //表数据的导入与导出
        {
            //导出表中所有输入数据
            $("#Button_ListExport").click(function () {
                var RepeatNumber = $('#Input_RepeatNumber_input').val();
                var StartNumber = $('#Input_StartNumber_input').val();
                var str = "{";
                str += "\"RepeatNumber\":\"" + RepeatNumber + "\", \"StartNumber\":\"" + StartNumber + "\", \"data\":[";
                var dat = get_list_data();
                for(var row=1; row<dat.length; row++){
                    str += "{";
                    for(var col=0; col<dat[row].length; col++){
                        str += "\"" + dat[row][col][0] + "\":";
                        str += "\"" + dat[row][col][1] + "\",";
                    }
                    str = str.substr(0, str.length - 1);//去除多余逗号
                    str += "},";
                }
                str = str.substr(0, str.length - 1);//去除多余逗号
                str += "]}";
                interface.saveFile(str, "DcPowerBatTest");
            });
            function get_list_data() {
                var dat = [];
                $("#TableCell_List_ListTestTable tr").each(function(row){
                    var row_dat = []
                    $(this).find("input").each(function(col){
                        row_dat.push(["input" + col, $(this).val()]);
                    })
                    dat.push(row_dat);
                })
                return dat;
            }
            //导入数据到表中
            $("#Button_Import").click(function () {
                var json = JSON.parse(interface.readFile());
                if(json != false){
                    var state = get_NowRowSelect("TableCell_List_ListTestTable");
                    $('#Input_RepeatNumber_input').val(json["RepeatNumber"]);
                    $('#Input_StartNumber_input').val(json["StartNumber"]);
                    for(var i=1; i<state.RowSum; i++){
                        $("#TableCell_List_ListTestTable tr").eq(1).remove();
                    }
                    for(var row=0; row<json["data"].length; row++){
                        var data =  Object.values(json["data"][row]);
                        add_row(-1, data);
                    }
                }
            });
        }

        //定时设置电流电压
        {
            window.t3 = function() {
                let dat = get_list_data();
                let StartNumber = parseInt($("#Input_StartNumber_input").val())-1;
                let RepeatNumber = parseInt($("#Input_RepeatNumber_input").val());
                for(let ch=0; ch<16; ch++){
                    if(interface.getSerialIsOpen(ch) == "True"){
                        let img = $("#Button_OnOff_" + (ch+1)).find("img")[0];
                        let Curr = parseFloat($("#Text_Curr_" + (ch+1) + "_text").text());
                        let Volt = parseFloat($("#Text_Volt_" + (ch+1) + "_text").text());
                        let volt_max = parseFloat($("#input_Volt_" + (ch+1) + "_input").val());
                        let curr_max = parseFloat($("#input_Curr_" + (ch+1) + "_input").val());

                        if(select_is("#" + img.id) == false){

                        }
                        else if(Curr > curr_max || Volt > volt_max){
                            // serial_comm(ch, "OUTP 0", 100, 500, 0);
                            interface.setSerialSend(ch, "OUTP 0", "", obj.delay1, obj.delay2, obj.overDelay);
                        }
                        else if(RepeatNumber >  TestResultsData.tests[ch]){
                            if(TestResultsData.rows[ch] < StartNumber){
                                TestResultsData.rows[ch] = StartNumber;
                            }
                            if(dat.length > 1 && TestResultsData.rows[ch] < dat.length-1){
                                if($("#Text_model_" + (ch+1) + "_text").text().indexOf("UDP") >= 0){//查询是否有通信
                                    let SetTime = dat[TestResultsData.rows[ch]+1][ch*3+2][1];
                                    if(TestResultsData.times[ch] == 0){
                                        let temp = [];
                                        temp["NowCurr"] = Curr;
                                        temp["NowVolt"] = Volt;
                                        temp["SetTime"] = SetTime;
                                        temp["volt_max"] = volt_max;
                                        temp["curr_max"] = curr_max;
                                        temp["NowTime"] = new Date().getHours() + ":" + new Date().getMinutes() + ":"  + new Date().getSeconds();

                                        let volt_set = parseFloat(dat[TestResultsData.rows[ch]+1][ch*3+0][1]);
                                        let curr_set = parseFloat(dat[TestResultsData.rows[ch]+1][ch*3+1][1]);
                                        // serial_comm(ch, "CURR " + curr_set, 100, 500, 0);
                                        // serial_comm(ch, "VOLT " + volt_set, 100, 500, 0);
                                        interface.setSerialSend(ch, "CURR " + curr_set, "set_curr_bt", obj.delay1, obj.delay2, obj.overDelay);
                                        window.set_curr_bt = function(i){
                                            interface.setSerialSend(i, "VOLT " + volt_set, "", obj.delay1, obj.delay2, obj.overDelay);
                                        }

                                        if(temp["NowCurr"] <= temp["curr_max"] && temp["NowVolt"] <= temp["volt_max"]){
                                            temp["Yield"] = "PASS";
                                            TestResultsData.all[ch]["PassSum"]++;
                                        }else{temp["Yield"] = "FAIL";}
                                        var Percent = parseFloat(TestResultsData.all[ch]["PassSum"]) / parseFloat(TestResultsData.all[ch]["data"].length) * 100;
                                        Percent = parseInt(Percent * 1000)/1000;//保留3位小数
                                        temp["YieldPercent"] = Percent;
                                        TestResultsData.all[ch]["data"].push(temp);
                                    }
                                    if(TestResultsData.times[ch] < SetTime){
                                        TestResultsData.times[ch]++;
                                    }else{
                                        TestResultsData.rows[ch]++;
                                        TestResultsData.times[ch] = 0;
                                    }
                                }
                            }else{
                                TestResultsData.tests[ch]++;
                                TestResultsData.rows[ch] = 0;
                                if(RepeatNumber <=  TestResultsData.tests[ch]){
                                    layer.msg("<h1>设备" + str_num[ch] + "测试结束</h1>");
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    //测试结果记录
    {
        var TestResultsData = new defObj(16);
        function defObj(sum){
            var temp = {
                PassSum: 0,
                // YieldPercent: 0,
                data: [],
            };
            this.all = [];
            this.rows =  [];
            this.times = [];
            this.tests = [];
            this.num = parseInt($("#Input_RepeatNumber_input").val()) + 1;
            for(var i=0; i<sum; i++){
                this.rows.push(0);
                this.times.push(0);
                this.tests.push(this.num);
                this.all.push(JSON.parse(JSON.stringify(temp)));
            }

            this.celerTestResultsData = function (ch){
                this.rows[ch] = 0;
                this.times[ch] = 0;
                this.tests[ch] = 0;
                this.all[ch]["data"] = [];
                this.all[ch]["PassSum"] = 0;
                // this.all[ch]["YieldPercent"] = 0;
            }
        }

        //结果导出
        {
            function SaveSelect(sel){
                if(sel == 0){
                    $("#CheckBox_SaveAll_img").css("visibility", "hidden");
                    $("#CheckBox_SavePass_img").css("visibility", "visible");
                    $("#CheckBox_SaveFail_img").css("visibility", "hidden");
                }else if(sel == 1){
                    $("#CheckBox_SaveAll_img").css("visibility", "hidden");
                    $("#CheckBox_SavePass_img").css("visibility", "hidden");
                    $("#CheckBox_SaveFail_img").css("visibility", "visible");
                }else if(sel == 2){
                    $("#CheckBox_SaveAll_img").css("visibility", "visible");
                    $("#CheckBox_SavePass_img").css("visibility", "hidden");
                    $("#CheckBox_SaveFail_img").css("visibility", "hidden");
                }
            }

            $("#CheckBox_SaveAll").click(function () {
                SaveSelect(2);
            });
            $("#CheckBox_SavePass").click(function () {
                SaveSelect(0);
            });
            $("#CheckBox_SaveFail").click(function () {
                SaveSelect(1);
            });
            $("#Button_ResultExport").click(function () {
                let  str = "通道, 时间, 间隔时间, 电压, 电流, 电压上限, 电流上限, 测试结果, 良率(%)\r\n\r\n";
                for(let ch=0; ch<TestResultsData.all.length; ch++){
                    for(let row=0; row<TestResultsData.all[ch]["data"].length; row++){
                        if(($("#CheckBox_SavePass_img").css("visibility") == "visible" && TestResultsData.all[ch]["data"][row].Yield == "PASS") ||
                            ($("#CheckBox_SaveFail_img").css("visibility") == "visible" && TestResultsData.all[ch]["data"][row].Yield == "FAIL") ||
                            ($("#CheckBox_SaveAll_img").css("visibility") == "visible")
                        ){
                            str +=  str_num[ch] + ", " +
                                TestResultsData.all[ch]["data"][row].NowTime + ", " +
                                TestResultsData.all[ch]["data"][row].SetTime + ", " +
                                TestResultsData.all[ch]["data"][row].NowVolt + ", " +
                                TestResultsData.all[ch]["data"][row].NowCurr + ", " +
                                TestResultsData.all[ch]["data"][row].volt_max + ", " +
                                TestResultsData.all[ch]["data"][row].curr_max + ", " +
                                TestResultsData.all[ch]["data"][row].Yield + ", " +
                                TestResultsData.all[ch]["data"][row].YieldPercent + " " +
                                "\r\n";
                        }
                    }
                }
                interface.saveFile(str, "DcPower_batmodeYield");
            });
        }
    }

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

    //input值恢复与定时备份
    {
        var userAgent = myBrowser().indexOf("WOW64");

        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",                     //
        ];


        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<16; i++){
                if(interface.getIni("Dc_PowerBatMode", "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);
                }
                $("#input_Volt_" + (i+1) + "_input").val(interface.getIni("Dc_PowerBatMode", "MaxVolt_" + (i+1),  "2.00"));
                $("#input_Curr_" + (i+1) + "_input").val(interface.getIni("Dc_PowerBatMode", "MaxCurr_" + (i+1),  "0.50"));
            }

            {//取列表测试数据
                $("#Input_StartNumber_input").val(interface.getIni("Dc_PowerBatMode", "StartNumber",  "1"));
                $("#Input_RepeatNumber_input").val(interface.getIni("Dc_PowerBatMode", "RepeatNumber",  "1"));
                var rows = parseInt(interface.getIni("Dc_PowerBatMode", "ListTableRows",  "1"));
                for(let a=0; a<rows; a++){
                    let temp = [];
                    for(let b=0; b<16; b++){
                        temp.push(interface.getIni("Dc_PowerBatMode", "ListTableRow_" + (a+1) + "_Col" + (b*3+0),  "0.50"));
                        temp.push(interface.getIni("Dc_PowerBatMode", "ListTableRow_" + (a+1) + "_Col" + (b*3+1),  "2.00"));
                        temp.push(interface.getIni("Dc_PowerBatMode", "ListTableRow_" + (a+1) + "_Col" + (b*3+2),  "1"));
                    }
                    add_row(-1, temp);
                }
                $("#CheckBox_SaveAll_img").css("visibility", interface.getIni("Dc_PowerBatMode", "SaveAll_img",  "visible"));
                $("#CheckBox_SavePass_img").css("visibility", interface.getIni("Dc_PowerBatMode", "SavePass_img",  "hidden"));
                $("#CheckBox_SaveFail_img").css("visibility", interface.getIni("Dc_PowerBatMode", "SaveFail_img",  "hidden"));
            }

            window.t_save = function() {
                for(var i=0; i<16; i++){
                    if($("#" + id_en_list[i]).attr("style") == "visibility: inherit;"){
                        interface.setIni("Dc_PowerBatMode", "En_" + id_en_list[i],     "true");
                    }else{
                        interface.setIni("Dc_PowerBatMode", "En_" + id_en_list[i],     "false");
                    }
                    interface.setIni("Dc_PowerBatMode", "MaxVolt_" + (i+1),  $("#input_Volt_" + (i+1) + "_input").val());
                    interface.setIni("Dc_PowerBatMode", "MaxCurr_" + (i+1),  $("#input_Curr_" + (i+1) + "_input").val());
                }

                {//存列表测试数据
                    let Listdat = get_list_data();
                    interface.setIni("Dc_PowerBatMode", "ListTableRows",  Listdat.length - 1);
                    interface.setIni("Dc_PowerBatMode", "StartNumber",  $("#Input_StartNumber_input").val());
                    interface.setIni("Dc_PowerBatMode", "RepeatNumber",  $("#Input_RepeatNumber_input").val());
                    for(let a=1; a<Listdat.length; a++){
                        for(let b=0; b<Listdat[a].length; b++){
                            interface.setIni("Dc_PowerBatMode", "ListTableRow_" + a + "_Col" + b,  JSON.stringify(Listdat[a][b][1]));
                        }
                    }
                    interface.setIni("Dc_PowerBatMode", "SaveAll_img",  $("#CheckBox_SaveAll_img").css("visibility"));
                    interface.setIni("Dc_PowerBatMode", "SavePass_img",  $("#CheckBox_SavePass_img").css("visibility"));
                    interface.setIni("Dc_PowerBatMode", "SaveFail_img",  $("#CheckBox_SaveFail_img").css("visibility"));
                }
            }
        }
    }

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





