// 以下为官方示例

// 以下为官方示例
$().ready(function () {
    validateRule();
    var data = document.getElementById("firstCategory").value;
    var secondData = document.getElementById("secondCategory").value;
    var resultData = document.getElementById("resultCategory").value;
    refreshMultiSelect(JSON.parse(data), JSON.parse(secondData), JSON.parse(resultData));
    //  console.log(JSON.parse(data));
    //  $("#signupForm").validate()
    var ruleData = document.getElementById("ruleCategory").value;
    refreshSelect("rule", JSON.parse(ruleData), JSON.parse(resultData));
    var styleData = document.getElementById("styleCategory").value;
    refreshSelect("style", JSON.parse(styleData), JSON.parse(resultData));
    var themeData = document.getElementById("themeCategory").value;
    refreshSelect("theme", JSON.parse(themeData), JSON.parse(resultData));
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/result/save",
        data: $('#signupForm').serialize(), // 你的formid
        async: false,
        error: function (request) {
            alert("Connection error");
        },
        success: function (data) {
            if (data.code == 0) {
                parent.layer.msg(data.msg);
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.msg(data.msg);
            }

        }
    });
}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            packageName: {
                required: true
            }
        },
        messages: {
            packageName: {
                required: "必须输入"
            }
        }
    })
}


function refreshMultiSelect(json, secondJson, resultData) {
    //  $('#categoryCode').html("");
    var data = ""
    for (var i = 0; i < json.length; i++) {
        data += "<optgroup label='" + json[i].firstTag + "'>";
        for (var j = 0; j < secondJson.length; j++) {
            if (json[i].firstId == secondJson[j].firstId) {
                if (secondJson[j].secondId in resultData) {
                    data += "<option value=\"" + secondJson[j].secondId + "\" selected=\"true\">" + secondJson[j].secondTag + "</option>";
                } else {
                    data += "<option value=\"" + secondJson[j].secondId + "\">" + secondJson[j].secondTag + "</option>";
                }
            }
        }
        data += "</optgroup>";
    }
    console.log(data);
    $('#categoryCode').append(data);
    $('#categoryCode').multiselect("destroy").multiselect({
        nonSelectedText: '请选择标签！',
        maxHeight: 350,
        enableCollapsibleOptGroups: true,
        enableFiltering: true,
        numberDisplayed: 3
    });
}

function refreshSelect(name, json, resultData) {
    var data = ""
    for (var j = 0; j < json.length; j++) {
        if (json[j].firstId in resultData) {
            data += "<option value=\"" + json[j].firstId + "\" selected=\"true\">" + json[j].firstTag + "</option>";
        } else {
            data += "<option value=\"" + json[j].firstId + "\">" + json[j].firstTag + "</option>";
        }
    }
    console.log(data);
    $('#' + name + 'Code').append(data);
    $('#' + name + 'Code').multiselect("destroy").multiselect({
        nonSelectedText: '请选择标签！',
        maxHeight: 350,
        enableCollapsibleOptGroups: true,
        enableFiltering: true,
        numberDisplayed: 3
    });
}

/*
var openDept = function () {
    layer.open({
        type: 2,
        title: "选择部门",
        area: ['300px', '450px'],
        content: "/system/sysDept/treeView"
    })
}

function loadDept(deptId, deptName) {
    $("#deptId").val(deptId);
    $("#deptName").val(deptName);
}
*/