/**
 * 添加部门
 */
var isFalse = false;
var node = parent.node;
$(document).ready(function () {
    $.ajax({
        type: "POST",
        url: "/pqs9900/depts/selectAllDept",
        data: "",
        success: function (data) {
            if (data.body == null) {
                var option = $("<option value='" + 0
                    + "'>" + '根节点' + "</option>");
                $("#parentNode").append(option);
            } else {
                $.each(data.body, function (i, item) {

                    var option = $("<option value='" + item.deptsIndex
                        + "'>" + item.deptsName + "</option>");
                    $("#parentNode").append(option);
                })
                $("#parentNode").val(node);
            }
        }
    });
    $.ajax({
        type: "POST",
        url: "/pqs9900/dic/setArea",
        data: "",
        success: function (data) {
            if (data.length == 0) {
                document.getElementById('areano').style.display = "block";
                document.getElementById('area').style.display = "none";
            } else {
                document.getElementById('areano').style.display = "none";
                document.getElementById('area').style.display = "block";
                $.each(data, function (i, item) {
                    var option = $("<option value='" + item.dicIndex
                        + "'>" + item.dicName + "</option>");
                    $("#area").append(option);
                })
            }
        }
    })
    $("#customDept").on("change", function () {
        if ($("option:selected", this).val() == '0') {
            $("#areaid").show();
            isFalse = false;
        } else if ($("option:selected", this).val() == '1') {
            $("#areaid").hide();
            isFalse = true;
        }
    })
    $("#insertAddDept")
        .click(
            function () {
                var deptsName = $("#deptsName").val();
                deptsName = deptsName.replace(
                    /(^\s*)|(\s*$)/g, '');
                deptsName = filterXSS(deptsName);
                var re = chinesez;// 只能输入汉字
                if (!re.test(deptsName)) {
                    layer.tips('部门名称非法', '#deptsName',
                        {
                            tips: [1, 'red']
                            // 还可配置颜色
                        });
                    $("#deptsName").val("");
                    $("#deptsName").prop("placeholder",
                        "部门名称由汉字组成");
                } else {
                    var parentNode = $("#parentNode").val();
                    parentNode = filterXSS(parentNode);
                    var customDept = $("#customDept").val();
                    customDept = filterXSS(customDept);
                    var area = null;
                    var parentName = "";

                    //通过父节点获取区域
                    $.ajax({
                        type: "POST",
                        url: "/pqs9900/depts/selectAreaDept",
                        data: {
                            deptsIndex: parentNode
                        },
                        async: false,
                        success: function (data) {
                            parentName = data.deptsName;
                            if (isFalse == false) {
                                area = $("#area").val();
                                area = filterXSS(area);
                            } else if (isFalse == true) {
                                area = data.area;
                            }
                        }
                    })
                    var deptsDescription = $("#deptsDescription").val();
                    deptsDescription = deptsDescription.replace(/(^\s*)|(\s*$)/g, '');
                    deptsDescription = filterXSS(deptsDescription);
                    //用于判断父节点是否绑定了监测点
                    $.ajax({
                        url: '/pqs9900/deptsline/selectLine',
                        type: 'POST',
                        dataType: "text",
                        data: {
                            parentNode: parentNode
                        },
                        success: function (data) {
                            if (data > 0) {
                                layer.confirm(parentName + "已绑定监测点。新增子部门，是否全部解除绑定？", function (index) {
                                    //全部解除父节点所绑定的监测点
                                    $.ajax({
                                        url: '/pqs9900/deptsline/removeBind',
                                        type: 'POST',
                                        dataType: "text",
                                        data: {
                                            parentNode: parentNode
                                        },
                                        success: function (data) {
                                            if (data > 0) {
                                                $.ajax({
                                                    url: '/pqs9900/depts/saveDepts',
                                                    type: 'POST',
                                                    dataType: "text",
                                                    data: {
                                                        deptsName: deptsName,
                                                        deptsDescription: deptsDescription,
                                                        parentNode: parentNode,
                                                        customDept: customDept,
                                                        area: area
                                                    },
                                                    success: function (data) {
                                                        // 没有权限的请求
                                                        if (data == 300) {
                                                            location.href = '/pqs9900/auth/unauthorized';
                                                            // 没有登陆的请求
                                                        } else if (data == 301) {
                                                            location.href = '/pqs9900';
                                                        } else {
                                                            var info = $.parseJSON(data);
                                                            if (info.code != 200) {
                                                                layer.msg(
                                                                    info.message, {
                                                                        icon: 5, time: 1000
                                                                    });
                                                            } else {
                                                                layer.msg(
                                                                    info.message, {
                                                                        icon: 6, time: 1000
                                                                    }, function () {
                                                                        parent.refresh();
                                                                        parent.getTreeDepts();
                                                                        parent.layer.closeAll();
                                                                    });
                                                            }
                                                        }
                                                    }
                                                });
                                            } else {
                                                layer.msg(
                                                    '解绑失败！', {
                                                        icon: 6, time: 1000
                                                    });
                                            }
                                        }
                                    })
                                    layer.close(index);
                                })
                            } else {
                                $.ajax({
                                    url: '/pqs9900/depts/saveDepts',
                                    type: 'POST',
                                    dataType: "text",
                                    data: {
                                        deptsName: deptsName,
                                        deptsDescription: deptsDescription,
                                        parentNode: parentNode,
                                        customDept: customDept,
                                        area: area
                                    },
                                    success: function (data) {
                                        // 没有权限的请求
                                        if (data == 300) {
                                            location.href = '/pqs9900/auth/unauthorized';
                                            // 没有登陆的请求
                                        } else if (data == 301) {
                                            location.href = '/pqs9900';
                                        } else {
                                            var info = $.parseJSON(data);
                                            if (info.code != 200) {
                                                layer.msg(
                                                    info.message, {
                                                        icon: 5, time: 1000
                                                    });
                                            } else {
                                                layer.msg(
                                                    info.message, {
                                                        icon: 6, time: 1000
                                                    }, function () {
                                                        parent.refresh();
                                                        parent.getTreeDepts();
                                                        parent.layer.closeAll();
                                                    });
                                            }
                                        }
                                    }
                                });
                            }

                        }
                    })
                }
            });
    $("#close").click(function () {
        parent.layer.closeAll();
    });
})