$(function () {
    //配置树添加checkbox
    triggerCheckBox(1);

    //保存按钮方法
    $('#save').click(function () {
        var name = $('#name').val();//获取模板名称
        var describe = $('#describe').val();//获取模板描述
        var rdIndex = translateData(getTreeSelected());

        if (rdIndex.length === 0) {
            layer.msg('请在左边至少勾选一个模板配置项.');
            return;
        }

        saveInfo(name, describe, rdIndex);
    });

    //取消按钮
    $('#close').click(function () {
        close();
    });
});

function close() {
    //关闭当前iframe弹出层
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}

//格式化终端树选中信息
function translateData(nodes) {
    var arrayIndex = [];

    for (var i = 0; i < nodes.length; i++) { //获取rdIndex属性值
        arrayIndex.push(nodes[i].rdIndex);
    }

    return arrayIndex;
}

//保存模板信息
function saveInfo(name, describe, rdIndex) {
    var url = '';

    if (!isUpdate) {
        url = '/pqs9900/report/addexcelploy';
    } else {
        url = '/pqs9900/report/updateexcelploy';
    }

    $.post(url, {
        'name': filterXSS(name),
        'describe': filterXSS(describe),
        'rdIndex': JSON.stringify(rdIndex),
        'globalrpIndex': globalrpIndex
    }, function (data) {
        layer.msg(data.body);
        close();
        window.parent.location.reload(); //刷新父页面
    });
}

//编辑与新建复用一个界面
var isUpdate = false;

function setUpdate() {
    isUpdate = true;
}

var globalrpIndex = '';

function setValue(name, describe, rpIndex) {
    $('#name').val(name);
    $('#describe').val(describe);
    globalrpIndex = rpIndex;  //保存结果，更新模板内容使用到

    getTreeState(rpIndex);//根据rpIndex获取终端树被选中节点信息
}

//获取终端树节点选中信息
function getTreeState(rpIndex) {
    $.post('/pqs9900/report/getTreeState', {
        'rpIndex': rpIndex
    }, function (data) {
        setTreeSelect(data.body);
    });
}



