
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(){

    var OnFlag = "-1";


    //开始
    $("#FlowShape_Start").click(function () {
        let loading = layer.load(2, {time: 10000});
        //设置电池恒流放电电流值
        interface.setSerialSend(0, "BATT:CURR " + $("#TextField_DischargeCurrent_input").val(), "batt_curr", obj.delay1, obj.delay2, obj.overDelay);
        window.batt_curr = function () {
            //设置电池恒流放电截止电压
            interface.setSerialSend(0, "BATT:CCV " + $("#TextField_CutoffVoltage_input").val(), "batt_ccv", obj.delay1, obj.delay2, obj.overDelay);
            window.batt_ccv = function () {
                //设置电池恒阻放电电阻值
                // this.set_bat_RES($("#").val());
                //设置电池恒阻放电截止电压值
                // this.set_bat_CRV($("#").val());
                //设置电池恒功率放电功率值
                // this.set_bat_POW($("#").val());
                //设置电池恒功率放电截止电压
                // this.set_bat_CPV($("#").val());
                interface.setSerialSend(0, "MODE CCBattery", "mode_ccbattery", obj.delay1, obj.delay2, obj.overDelay);
                window.mode_ccbattery = function () {
                    interface.setSerialSend(0, "INPut on", "input_on", obj.delay1, obj.delay2, obj.overDelay);
                    window.input_on = function(){
                        layer.close(loading);
                        layer.msg("<h1>完成设置</h1>");
                    }
                }
            }
        }
    });
    //停止
    $("#FlowShape_Stop").click(function () {
        interface.setSerialSend(0, "INPut off", "", obj.delay1, obj.delay2, obj.overDelay);
        // layer.msg("<h1>完成设置</h1>");
    });

    //表数据的导入与导出
    {
        //导出表中所有输入数据
        $("#FlowShape_Export").click(function () {
            var str = JSON.stringify(chart_bat.option_data[0]);
            interface.saveFile(str, "BatTest");
        });

        //导入数据到表中
        $("#FlowShape_Import").click(function () {
            var json = JSON.parse(interface.readFile());
            if(json != false){
                chart_bat.option_data[0] = json;
            }
        });
    }

    //通道选择
    function ch_select(){
        interface.getSerialComm(0, "CHAN?", "get_now_chan", TaktTimeDelay, OverTimeDelay);
        window.get_now_chan = function (i, dat) {
            var select_ch = $("#Droplist_Channel_input").val();
            if(dat == "1" && select_ch == "CH2"){
                interface.setSerialSend(0, "CHAN2", "", obj.delay1, obj.delay2, obj.overDelay);
            }
            else if(dat == "2" && select_ch == "CH1"){
                interface.setSerialSend(0, "CHAN1", "", obj.delay1, obj.delay2, obj.overDelay);
            }
        }
        return true;
    }



    {
        window.t1 = function () {
            if(interface.getFreeTime(0) < 100){
            }
            else if(obj.cnt == 0){//查询控制方式
                interface.getSerialComm(0, "SYSTem:LOCal?", "system_local", TaktTimeDelay, OverTimeDelay);
                window.system_local = function (i, dat) {
                    if(dat == "1"){
                        obj.cnt++;
                    }
                }
            }
            else if(obj.cnt == 1) {//通道选择
                ch_select();
                obj.cnt++;
            }
            else if(obj.cnt == 2) {//查询电流
                interface.getSerialComm(0, "MEASure:CURRent?", "measure_current", TaktTimeDelay, OverTimeDelay);
                window.measure_current = function (i, dat) {
                    chart_data_add(0, dat);obj.cnt++;
                    $('#TextField_Current_input').val(dat);
                }
            }
            else if(obj.cnt == 3) {//查询电压
                interface.getSerialComm(0, "MEASure:VOLTage?", "measure_voltage", TaktTimeDelay, OverTimeDelay);
                window.measure_voltage = function (i, dat) {
                    chart_data_add(1, dat);obj.cnt++;
                    $('#TextField_Voltage_input').val(dat);
                }
            }
            else if(obj.cnt == 4) {//查询电池容量
                interface.getSerialComm(0, "MEAS:CAPacity?", "get_meas_capacity", TaktTimeDelay, OverTimeDelay);
                window.get_meas_capacity = function (i, dat) {
                    obj.cnt++;
                    $('#TextField_Capacity_input').val(dat);
                }
            }
            else if(obj.cnt == 5) {//查询开关状态
                interface.getSerialComm(0, "INPut?", "get_input", TaktTimeDelay, OverTimeDelay);
                window.get_input = function (i, dat) {
                    obj.cnt++;
                    if(OnFlag == "1" && dat == "0"){
                        layer.msg("<h1>测试结束</h1>");
                    }
                    OnFlag = dat;
                }
            }
            else if(obj.cnt == 6){//查询版本
                interface.getSerialComm(0, "*IDN?", "get_idn", TaktTimeDelay, OverTimeDelay);
                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_bat = new eEhart("#FlowShape_ChartBat", "", [{name: '电流'},{name: '电压'}], 2);

        function chart_data_add(num, data) {
            var select_ch = $("#Droplist_Channel_input").val();
            if(select_ch == "CH2"){
                chart_bat.add_data(1, num, data);   chart_bat.chart_select = 1;
            }
            else{
                chart_bat.add_data(0, num, data);   chart_bat.chart_select = 0;
            }
        }
        $("#Droplist_Channel_input").change(function () {
            chart_bat.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_bat.option_data[chart_bat.chart_select].dataZoom[0].end = end;
                chart_bat.option_data[chart_bat.chart_select].dataZoom[0].start = start;
                chart_bat.echart.setOption(chart_bat.option_data[chart_bat.chart_select]);
            }
        }
        chart_bat.echart.on('dataZoom', function (params) {
            dataZoom(params);
        });

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

    }


    //显示时间
    var TimeCnt = 0;
    window.t3 = 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
        }
        if(OnFlag == "1"){
            TimeCnt++;
        }else{
            TimeCnt = 0;
        }
        $('#TextField_Time_input').val(formatSeconds(TimeCnt));
    }



    {//input值恢复与定时备份
        var userAgent = myBrowser().indexOf("WOW64");
        if(userAgent >= 0){
            $("#Droplist_Channel_input").val(interface.getIni("BatTest", "Droplist_Channel_input",                        "CH1"));
            $("#TextField_CutoffVoltage_input").val(interface.getIni("BatTest", "TextField_CutoffVoltage_input",        "5.000"));  $("#TextField_CutoffVoltage_input").attr("maxlength", 7);
            $("#TextField_DischargeCurrent_input").val(interface.getIni("BatTest", "TextField_DischargeCurrent_input", "5.000"));  $("#TextField_DischargeCurrent_input").attr("maxlength", 7);

            chart_bat.option_data[0].legend.selected = JSON.parse(interface.getIni("BatTest", "FlowShape_chart_bat_legend_selected",  "{}"));

            for(var i=0; i<chart_bat.option_data[0].series.length; i++){
                chart_bat.option_data[0].series[i].color = interface.getIni("BatTest", "FlowShape_chart_bat_series" + i,  "#0000ff");
            }
            chart_bat.echart.setOption(chart_bat.option_data[0]);
        }
        // var tini = window.setInterval(function() {
        window.tini = function () {
            if(userAgent >= 0){
                interface.setIni("BatTest", "Droplist_Channel_input",               $("#Droplist_Channel_input").val());
                interface.setIni("BatTest", "TextField_CutoffVoltage_input",       $("#TextField_CutoffVoltage_input").val());
                interface.setIni("BatTest", "TextField_DischargeCurrent_input",   $("#TextField_DischargeCurrent_input").val());

                interface.setIni("BatTest", "FlowShape_chart_bat_legend_selected",  JSON.stringify(chart_bat.echart.getOption().legend[0].selected));

                var opt = chart_bat.echart.getOption().series;
                for(var i=0; i<opt.length; i++){
                    interface.setIni("BatTest", "FlowShape_chart_bat_series" + i,  opt[i].color);
                }
            }
        }
    }
    interface.setTime([["t1", 30], ["t3", 1000], ["tini", 10000]]);

});




