// 以下为官方示例

// 以下为官方示例
$().ready(function () {
    validateRule();
    var data = document.getElementById("firstECTag").value;
    var secondData = document.getElementById("secondECTag").value;
    var resultData = document.getElementById("resultECTag").value;
    refreshMultiSelect(JSON.parse(data), JSON.parse(secondData), JSON.parse(resultData));
});

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

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "/ec_storage/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: {
            previewUrl: {
                required: false
            }
        }
    })
}


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);
    $('#ecCode').append(data);
    $('#ecCode').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);
}
*/