/**
 * 修改用户
 */
$(document).ready(function () {
    var userIndex = parent.$("#userIndex").val();
    //查询用户的信息
    $.ajax({
        type: "POST",
        url: "/pqs9900/user/selectId",
        data: {
            userIndex: userIndex,
            flag: 1
        },
        success: function (data) {
            if (data == "") {
                layer.msg("当前用户为管理员，不可修改", {
                    icon: 5, time: 2000
                }, function () {
                    parent.layer.closeAll()
                })
            } else {
                $("#name").val(data.name);
                $("#loginName").val(data.loginName);
                $("#phone").val(data.phone);
                $("#email").val(data.email);
                var ipStart = data.limitIpStart;
                ipStart = ipStart.split(".");
                $("#ipStart1").val(ipStart[0]);
                $("#ipStart2").val(ipStart[1]);
                $("#ipStart3").val(ipStart[2]);
                $("#ipStart4").val(ipStart[3]);
                var ipEnd = data.limitIpEnd;
                ipEnd = ipEnd.split(".");
                $("#ipEnd1").val(ipEnd[0]);
                $("#ipEnd2").val(ipEnd[1]);
                $("#ipEnd3").val(ipEnd[2]);
                $("#ipEnd4").val(ipEnd[3]);

                var time = data.limitTime;
                time = time.split("-");
                for (var i = 0; i <= 23; i++) {
                    var option = $("<option value='"
                        + i + "'>"
                        + i + "</option>");
                    $("#times1").append(option);
                }
                $("#times1").val(time[0]);
                for (var i = 24; i >= 1; i--) {
                    var option = $("<option value='"
                        + i + "'>"
                        + i + "</option>");
                    $("#times2").append(option);
                }
                $("#times2").val(time[1]);
                $.each(data.deptsList, function (i, item) {
                    var option = $("<option value='" + item.deptsIndex + "'>" + item.deptsName + "</option>");
                    $("#deptsIndex").append(option);
                })
                var deptsName = data.deptsName;
                if (deptsName === "暂无部门") {
                    $("#deptsIndex").prepend("<option value='暂无部门'>=======请选择=======</option>");
                    $("#deptsIndex").val("暂无部门");
                } else {
                    $("#deptsIndex").val(deptsName);
                }


                $("#usertype").append($("<option value='0'>长期账号</option>"));
                $("#usertype").append($("<option value='1'>临时账号</option>"));
                $("#usertype").val(data.casualUser);

                $("#isnotice").append($("<option value='0'>是</option>"));
                $("#isnotice").append($("<option value='1'>否</option>"));
                $("#isnotice").val(data.isnotice);
            }
        }
    })
    //app用户
    $.ajax({
        type: "POST",
        url: "/pqs9900/user/selectAppId",
        data: {
            userIndex: userIndex,
            flag: 1
        },
        success: function (data) {
            if (data != null) {
                $("#referralCode").val(data.referralCode);
                $("#semName").val(data.semName);
                $("#semPhone").val(data.semPhone);
            }
        }
    })
    $("#close").click(function () {
        parent.layer.closeAll();
    })
    //修改
    $("#updatetUser").click(function () {
        var name = $("#name").val();
        name = name.replace(/(^\s*)|(\s*$)/g, '');
        name = filterXSS(name);
        var re = chinesez;//只能输入中文
        if (!re.test(name)) {
            layer.tips('名称由中文组成',
                '#name', {
                    tips: [1, 'red'],
                    time: 2000
                    //还可配置颜色
                });
            $("#name").val("");
        } else {
            var loginName = $("#loginName").val();
            loginName = loginName.replace(/(^\s*)|(\s*$)/g, '');
            loginName = filterXSS(loginName);
            var re = englishz;//只能输入英文字母
            if (!re.test(loginName)) {
                layer.tips('登录名由3-16的字母和数字以及_组成',
                    '#loginName', {
                        tips: [1, 'red'],
                        time: 2000
                        //还可配置颜色
                    });
                $("#loginName").val("");
            } else {

                var tel = $('#phone').val();
                tel = tel.replace(/(^\s*)|(\s*$)/g, '');
                tel = filterXSS(tel);
                var re = phonez;
                if (!re.test(tel)) {
                    layer.tips('手机号必须有11位，并且为数字，是正常的手机号码开头',
                        '#phone', {
                            tips: [2, 'red'],
                            time: 2000
                            //还可配置颜色
                        });
                    $("#phone").val("");
                } else {
                    var email = $('#email').val();
                    email = email.replace(/(^\s*)|(\s*$)/g, '');
                    email = filterXSS(email);
                    var re = emailz;
                    if (!re.test(email)) {
                        layer.tips('邮箱不能以数字下划线开头，含有@ .在@后，以.com、.con结尾（qq,163等）',
                            '#email', {
                                tips: [2, 'red'],
                                time: 2000
                                //还可配置颜色
                            });
                        $("#email").val("");
                    } else {
                        var ipStart1 = $("#ipStart1").val();
                        var ipStart2 = $("#ipStart2").val();
                        var ipStart3 = $("#ipStart3").val();
                        var ipStart4 = $("#ipStart4").val();
                        var ipGo = ipStart1 + "." + ipStart2 + "." + ipStart3 + "." + ipStart4;
                        ipGo = ipGo.replace(/(^\s*)|(\s*$)/g, '');
                        ipGo = filterXSS(ipGo);
                        var re = ipz;
                        if (!re.test(ipGo)) {
                            layer.tips(
                                'ip地址输入错误',
                                "#ipStart4",
                                {
                                    tips: [
                                        2,
                                        'red'],
                                    time: 2000
                                    // 还可配置颜色
                                });
                            $("#ipStart1").val("");
                            $("#ipStart2").val("");
                            $("#ipStart3").val("");
                            $("#ipStart4").val("");
                        } else {
                            var ipEnd1 = $("#ipEnd1").val();
                            var ipEnd2 = $("#ipEnd2").val();
                            var ipEnd3 = $("#ipEnd3").val();
                            var ipEnd4 = $("#ipEnd4").val();
                            var ipE = ipEnd1 + "." + ipEnd2 + "." + ipEnd3 + "." + ipEnd4;
                            ipEnd1 = ipEnd1.replace(/(^\s*)|(\s*$)/g, '');
                            ipEnd2 = ipEnd2.replace(/(^\s*)|(\s*$)/g, '');
                            ipEnd3 = ipEnd3.replace(/(^\s*)|(\s*$)/g, '');
                            ipEnd4 = ipEnd4.replace(/(^\s*)|(\s*$)/g, '');
                            ipE = filterXSS(ipE);
                            var re = ipz;
                            if (!re.test(ipE)) {
                                layer.tips('ip地址输入错误', "#ipEnd4",
                                    {
                                        tips: [2, 'red'],
                                        time: 2000
                                    });
                                $("#ipEnd1").val("");
                                $("#ipEnd2").val("");
                                $("#ipEnd3").val("");
                                $("#ipEnd4").val("");
                            } else {
                                if (ipGo == ipE) {
                                    layer.msg("起始ip和结束ip相同", {
                                            icon: 5
                                        },
                                        function () {
                                            layer.close();
                                        })
                                } else {
                                    var times1 = $("#times1").val();
                                    var times2 = $("#times2").val();
                                    if (times1 == times2) {
                                        layer.msg("时间段选择相同", {
                                                icon: 5
                                            },
                                            function () {
                                                layer.close();
                                            })
                                    } else {
                                        if (parseInt(times1) > parseInt(times2)) {
                                            layer.msg("时间段选择错误，起止时间不对", {
                                                    icon: 5
                                                },
                                                function () {
                                                    layer.close();
                                                })
                                        } else {
                                            var deptsIndex = $("#deptsIndex").val();
                                            deptsIndex = filterXSS(deptsIndex);
                                            if (deptsIndex == "暂无部门") {
                                                layer.tips('请选择具体部门', '#deptsIndex', {
                                                    tips: [2, 'red']
                                                });
                                            } else {
                                                var time = times1 + "-" + times2;
                                                time = filterXSS(time);
                                                var token = $("#token").val();
                                                var username = $("#username").val();
                                                var usertype = $("#usertype").val();
                                                usertype = filterXSS(usertype);
                                                var isnotice = $("#isnotice").val();
                                                isnotice = filterXSS(isnotice);
                                                var referralCode = $("#referralCode").val();

                                                var semName = $("#semName").val();
                                                semName = semName.replace(/(^\s*)|(\s*$)/g, '');
                                                semName = filterXSS(semName);
                                                var re = chinesez;// 只能输入中文
                                                if (!re.test(semName)) {
                                                    layer.tips('名称只由中文组成', '#semName', {
                                                        tips: [1, 'red'],
                                                        time: 2000
                                                        // 还可配置颜色
                                                    });
                                                    $("#semName").val("");
                                                } else {
                                                    var semPhone = $('#semPhone').val();
                                                    semPhone = semPhone.replace(/(^\s*)|(\s*$)/g, '');
                                                    semPhone = filterXSS(semPhone);
                                                    var re = phonez;
                                                    if (!re.test(semPhone)) {
                                                        layer.tips('手机号必须有11位，并且为数字，是正常的手机号码开头', '#semPhone',
                                                            {
                                                                tips: [2, 'red'],
                                                                time: 2000
                                                                // 还可配置颜色
                                                            });
                                                        $("#semPhone").val("");
                                                    } else {
                                                        $.ajax({
                                                            url: '/pqs9900/user/updateAppUser',
                                                            type: 'POST',
                                                            dataType: "json",
                                                            data: {
                                                                userIndex: userIndex,
                                                                name: name,
                                                                loginName: loginName,
                                                                deptsIndex: deptsIndex,
                                                                phone: tel,
                                                                email: email,
                                                                ipGo: ipGo,
                                                                ipE: ipE,
                                                                time: time,
                                                                mark: 1,
                                                                usertype: usertype,
                                                                isnotice:isnotice,
                                                                semName: semName,
                                                                semPhone: semPhone,
                                                                token: token
                                                            },
                                                            success: function (data) {
                                                                if (data === 1) {
                                                                    layer.msg("修改用户成功", {
                                                                        icon: 6,
                                                                        time: 1000
                                                                    }, function () {
                                                                        parent.load(parent.userState, parent.careUser);
                                                                        parent.layer.closeAll()
                                                                    });
                                                                } else if (data === 0) {
                                                                    layer.msg("名称已存在", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data === 3) {
                                                                    layer.msg("该手机号已被注册", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 4) {
                                                                    layer.msg("结束ip地址格式不正确", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 5) {
                                                                    layer.msg("起始ip地址格式不正确", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 6) {
                                                                    layer.msg("手机号格式不正确，必须有11位，并且为数字，是正常的手机号码开头", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 7) {
                                                                    layer.msg("邮箱格式不正确，应含有@ .在@后，以.com、.con结尾（qq,163等）", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 8) {
                                                                    layer.msg("登录名只能由3-16的字母和数字组成", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 9) {
                                                                    layer.msg("名称只由中文组成，长度为1-20", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 10) {
                                                                    layer.msg("结束ip不可为空", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 11) {
                                                                    layer.msg("起始ip不可为空", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 12) {
                                                                    layer.msg("邮箱不可为空", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 13) {
                                                                    layer.msg("手机号不可为空", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 14) {
                                                                    layer.msg("登录名不可为空", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else if (data == 15) {
                                                                    layer.msg("名称不可为空", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                } else {
                                                                    layer.msg("修改用户失败", {
                                                                        icon: 5, time: 1000
                                                                    })
                                                                }
                                                            }
                                                        });
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }
    })
});