$(function () {
    triggerCheckBox(1);
    setTreeSelected(1, 10);

    $("#export").click(function () {
        getPercentData();
    });
});


function getTimeType() {
    var interval = $("#interval").val();
    if (interval === "年份") {
        return 1;
    } else if (interval === "季度") {
        return 2;
    } else if (interval === "月份") {
        return 3;
    } else /*if (interval === "周") {
        return 4;
    } else */{
        return 4;
    }
}

function getPercentData() {
    var startTime = $("#startTime").val();
    var endTime = $("#endTime").val();
    var timeType = getTimeType();
    var pointNode = getTreeSelected(1);

    if (0 === pointNode.length) {
        layer.msg('请选择一个监测点.');
        return;
    }

    var points = '';
    var pointName = '';
    var lineName = '';
    var factory = '';
    var factorys = '';
    factory = pointNode[0].getParentNode().getParentNode().getParentNode().getParentNode().name;

    for (var i = 0; i < pointNode.length; i++) {
        points += pointNode[i].nodeIndex + ",";
        pointName += pointNode[i].name + ",";
        lineName += pointNode[i].getParentNode().name + ",";
        factorys = pointNode[i].getParentNode().getParentNode().getParentNode().getParentNode().name;

        if (factory != factorys) {
            layer.msg('请不要跨省选择');
            return;
        }
    }

    $.post("/pqs9000/percent/createPercent", {
            "startTime": startTime,
            "endTime": endTime,
            "timeType": timeType,
            "list": points,
            "lineName": lineName,
            "listPointName": pointName,
            "factory": factory
        }, function (data) {
            if (data.code === 200) {
                window.location.href = "/pqs9000/percent/exportPercent";
            } else {
                layer.msg("报表生成失败");
            }
        }
    );
}