/**
 * 修改部门
 */
var nodeId;
var area = null;
var parentNode = document.getElementById("parentNode");
var customDept = 0;
var parentName = null;
$(document).ready(function () {
        var deptsIndex = parent.$("#deptsIndex").val();
        $.ajax({
            type: "POST",
            url: "/pqs9900/depts/getAll",
            data: {"deptsIndex": deptsIndex},
            success: function (data) {
                $("#deptsName").val(data.deptsName);
                $("#deptsDescription").val(data.deptsDescription);
                customDept = data.customDept;
                if (customDept == 1 || customDept == 2) {
                    $("#areaid").hide();
                    $("#parentid").hide();
                    $("#customDepts").show();
                    $("#customDept").val(customDept);
                } else if (customDept == 0) {
                    $("#areaid").show();
                    $("#parentid").show();
                    $("#customDepts").hide();
                }
                $.ajax({
                    type: "POST",
                    url: "/pqs9900/depts/selectName",
                    data: {"parentNode": data.parentNodeId},
                    success: function (d) {
                        if (data.parentNodeId == 0) {
                            parentNode.options.add(new Option("根节点", 0));
                            // $.ajax({
                            // 	type : "POST",
                            // 	url : "/pqs9900/depts/selName",
                            // 	data:{
                            // 		deptsIndex:deptsIndex
                            // 	},
                            // 	success : function(data) {
                            // 		// $.each(data,function (i,item) {
                            //      // var option=$("<option value='"+item.node+"'>"+item.deptsName+"</option>");
                            //      // $("#parentNode").append(option);
                            // 		// })
                            // 	}
                            // })
                            $.each(data.areas, function (i, item) {
                                var option = $("<option value='" + item.dicIndex + "'>" + item.dicName + "</option>");
                                $("#area").append(option);
                            })

                            area = data.area;
                            if (area === "暂无区域") {
                                $("#area").prepend("<option value='暂无区域'>-------请选择------</option>");
                                $("#area").val("暂无区域");
                            } else {
                                $("#area").val(area);
                            }
                        } else {
                            //parentNode.options.add(new Option(d.deptsName, d.node));
                            $.ajax({
                                type: "POST",
                                url: "/pqs9900/depts/selName",
                                data: {
                                    deptsIndex: deptsIndex
                                },
                                success: function (data) {
                                    $.each(data, function (i, item) {
                                        var option = $("<option value='" + item.deptsIndex + "'>" + item.deptsName + "</option>");
                                        $("#parentNode").append(option);
                                    })

                                    parentNode = d.deptsIndex;
                                    $("#parentNode").val(parentNode);
                                }
                            })
                            $.each(data.areas, function (i, item) {
                                var option = $("<option value='" + item.dicIndex + "'>" + item.dicName + "</option>");
                                $("#area").append(option);
                            })

                            area = data.area;
                            if (area === "暂无区域") {
                                $("#area").prepend("<option value='暂无区域'>-------请选择------</option>");
                                $("#area").val("暂无区域");
                            } else {
                                $("#area").val(area);
                            }
                        }

                    }
                })

            }
        })
        $("#close").click(function () {
            parent.layer.closeAll();
        });
        $("#updateDepts").click(function () {
            var deptsIndex = parent.$("#deptsIndex").val();
            deptsIndex = filterXSS(deptsIndex);
            var customDeptType = $("#customDept").val();
            customDeptType = filterXSS(customDeptType);
            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 {
                if (customDept == 0) {
                    parentNode = $("#parentNode").val();
                    area = $("#area").val();
                }
                var deptsDescription = $("#deptsDescription").val();
                deptsDescription = filterXSS(deptsDescription);
                deptsDescription = deptsDescription.replace(/(^\s*)|(\s*$)/g, '');
                $.ajax({
                    type: "POST",
                    url: "/pqs9900/depts/selectAreaDept",
                    data: {
                        deptsIndex: parentNode
                    },
                    async: false,
                    success: function (data) {
                        parentName = data.deptsName;
                    }
                })
                $.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/updateDepts',
                                                type: 'POST',
                                               // dataType: "text",
                                                data: {
                                                    deptsIndex: deptsIndex,
                                                    deptsName: deptsName,
                                                    deptsDescription: deptsDescription,
                                                    customDeptType: customDeptType,
                                                    parentNode: parentNode,
                                                    area: area
                                                },
                                                success: function (data) {
                                                    //没有权限的请求
                                                    if (data == 300) {
                                                        location.href = '/pqs9900/auth/unauthorized';
                                                        //没有登陆的请求
                                                    } else if (data == 301) {
                                                        location.href = '/pqs9900';
                                                    } else {
                                                        if (data == 1) {
                                                            layer.msg("修改部门成功", {
                                                                icon: 6,
                                                                time: 1000
                                                            }, function () {
                                                               // parent.getId(nodeId)
                                                                parent.getTreeDepts();
                                                                parent.refresh();
                                                                parent.layer.closeAll()
                                                            });
                                                        } else if (data == 0) {
                                                            layer.msg("该部门已存在", {
                                                                icon: 5,
                                                                time: 1000
                                                            })
                                                        } else {
                                                            layer.msg("修改部门失败", {
                                                                icon: 5,
                                                                time: 1000
                                                            })
                                                        }
                                                    }
                                                }
                                            });
                                        } else {
                                            layer.msg(
                                                '解绑失败！', {
                                                    icon: 6, time: 1000
                                                });
                                        }
                                    }
                                })
                                layer.close(index);
                            })
                        } else {
                            $.ajax({
                                url: '/pqs9900/depts/updateDepts',
                                type: 'POST',
                                dataType: "text",
                                data: {
                                    deptsIndex: deptsIndex,
                                    deptsName: deptsName,
                                    deptsDescription: deptsDescription,
                                    parentNode: parentNode,
                                    customDeptType: customDeptType,
                                    area: area
                                },
                                success: function (data) {
                                    //没有权限的请求
                                    if (data == 300) {
                                        location.href = '/pqs9900/auth/unauthorized';
                                        //没有登陆的请求
                                    } else if (data == 301) {
                                        location.href = '/pqs9900';
                                    } else {
                                        if (data == 1) {
                                            layer.msg("修改部门成功", {
                                                icon: 6,
                                                time: 1000
                                            }, function () {
                                                //parent.getId(nodeId)
                                                parent.getTreeDepts();
                                                parent.refresh();
                                                parent.layer.closeAll()
                                            });
                                        } else if (data == 0) {
                                            layer.msg("该部门已存在", {
                                                icon: 5,
                                                time: 1000
                                            })
                                        } else {
                                            layer.msg("修改部门失败", {
                                                icon: 5,
                                                time: 1000
                                            })
                                        }
                                    }
                                }
                            });
                        }
                    }
                })
            }
        })
    }
)