// 以下为官方示例

// 以下为官方示例
$().ready(function () {
    validateRule();
    var data = document.getElementById("firstCategory").value;
    var secondData = document.getElementById("secondCategory").value;
    //  console.log(JSON.parse(data));
    //  $("#signupForm").validate()
    refreshMultiSelect(JSON.parse(data), JSON.parse(secondData));
});

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

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/tag/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) {
    //  $('#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) {
                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
    });
}

/*
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);
}
*/