var getUrl = function () {
    if(global.target == undefined || global.target == ""){
        var ss = window.location.href;
        var s = ss.split(":");
        console.log(s);
        var p = s[2].split("/")[0];
        var port = parseInt(p);
        port = port + 2;
        var url = s[0] + ":" + s[1] + ":" + port;
        global.target = url;
    }
    return global.target;
};

var changeBehaviourTime = function (data) {
    var time = data.split("天")[0];
    console.log(time);
    global.behaviorTime = time;

    console.log("changeBehaviourTime");
    var str = getUrl() + "/view?action=statistics&day=" + time;
    console.log(str);

    $.ajax({
        url : str,
        type: "GET",
        dataType : "json"
    }).done(function(result) {
        showBehaviour(result);
    });
};

var showBehaviour = function(data){
    console.log(data);

    $("#behaviourGraphDiv")
        .html(
            "<div class='col-12' id='behaviourGraph' style='min-height: 250px; padding: 0px'></div>");
    if(global.behaviorTime == undefined || global.behaviorTime == ""){
        console.log("draw behaviour picture error!");
        return;
    }

    var resolveAna = (data.resolve);
    var deleteAna = data.delete;
    var registerAna = data.register;

    var lastTime = Date.parse(new Date() ).toString();//获取到毫秒的时间戳，精确到毫秒
    //lastTime = lastTime.substr(0,10);//精确到秒
    var startTime = lastTime - 1000*24*60*60*global.behaviorTime;
    var interval = 24*60*60*1000;  //一天

    var xAxisData = [];
    for (var i=0;i<global.behaviorTime;i++){
        var tmp = new Date(startTime).toISOString().substring(0, 10);
        xAxisData.push(tmp);
        startTime+=interval;
    }

    var myChart = echarts.init($("#behaviourGraph")[0], 'walden');
    var option = {
        title : {
            subtext : '次数'
        },
        tooltip : {
            trigger : 'axis'
        },
        legend : {
            data : [ '解析', '注册', '删除' ],
            y : 'bottom',
        },
        toolbox : {
            show : true,
            feature : {
                dataZoom : {
                    yAxisIndex : 'none'
                },
                dataView : {
                    readOnly : false
                },
                magicType : {
                    type : [ 'line', 'bar' ]
                },
                restore : {},
                saveAsImage : {}
            }
        },
        xAxis : {
            type : 'category',
            boundaryGap : false,
            data : xAxisData
        },
        yAxis : {
            type : 'value',
            axisLabel : {
                formatter : '{value}次'
            }
        },
        series : [ {
            name : '解析',
            type : 'line',
            data : resolveAna,
            markPoint : {
                data : [ {
                    type : 'max',
                    name : '最大值'
                }, {
                    type : 'min',
                    name : '最小值'
                } ]
            },

        }, {
            name : '注册',
            type : 'line',
            data : registerAna,
            markPoint : {
                data : [ {
                    type : 'max',
                    name : '最大值'
                }, {
                    type : 'min',
                    name : '最小值'
                } ]
            }
        }, {
            name : '删除',
            type : 'line',
            data : deleteAna,
            markPoint : {
                data : [ {
                    type : 'max',
                    name : '最大值'
                }, {
                    type : 'min',
                    name : '最小值'
                } ]
            }
        } ]
    };
    myChart.setOption(option);

    console.log(resolveAna);
    console.log(deleteAna);
    console.log(registerAna);
};