layui.use(["layer", "form", "laydate", 'laypage'], function() {
    $ = layui.$;
    form = layui.form;
    layer = layui.layer;

    // load(); //信息初始化

    //昵称修改
    $(".name_div_val").on("click", (e) => {
        let str = "<div class='phoneBox'>"
            +"<div class='phoneSmBox'>请输入新昵称 <input type='text' name='nameVal' autocomplete='off' class='layui-input'></div>"
            +"</div>";
        layer.confirm(str,{
            btn: ['取消', '确定'],
            title: "昵称修改",
            skin: 'layclass'
        }, function () {
            layer.closeAll()
        }, function(){
            layer.load(3);
            //修改
            $.ajax({
                url: "/personal/updateName",
                type: "put",
                data: {
                    "name": $("input[name=nameVal]").val()
                },
                // contentType: "application/json",
                headers: {"token": token},
                dataType: "json",
                success: (data) => {
                    if(data.msg == "200"){
                        layer.closeAll();
                        parent.layer.msg("修改成功", {icon: 1});
                        load();
                    } else if(data.msg == "300"){
                        layer.closeAll("loading");
                        layer.msg(data.errMsg, {icon: 2});
                    }
                },
                error: () => {
                    layer.closeAll("loading");
                    layer.alert("网络错误 请联系管理员");
                }
            });
        });
    });

    //手机号修改
    $(".phone_update").on("click", (e) => {
        let str = "<div class='phoneBox'>"
            +"<div class='phoneSmBox'>原手机号码 <input type='text' name='oldPhone' lay-verify='oldPhone' autocomplete='off' class='layui-input'></div>"
            +"<div class='phoneSmBox'>新手机号码 <input type='text' name='phone' lay-verify='newPhone' autocomplete='off' class='layui-input'></div>"
            +"<div class='phoneSmBox2'><input type='text' name='yzNum' lay-verify='yzNum' autocomplete='off' class='layui-input'>"
            +"<div class='getCode'>获取验证码</div></div>"
            +"</div>";
        layer.confirm(str,{
            btn: ['取消', '确定'],
            title: "手机号修改",
            skin: 'layclass'
        }, function () {
            layer.closeAll()
        }, function(){
            layer.load(3);
            //修改
            $.ajax({
                url: "/personal/updatePhone",
                type: "put",
                data: {
                    "phone": $("input[name=phone]").val(),
                    "captcha": $("input[name=yzNum]").val()
                },
                // contentType: "application/json",
                headers: {"token": token},
                dataType: "json",
                success: (data) => {
                    if(data.msg == "200"){
                        layer.closeAll();
                        parent.layer.msg("修改成功", {icon: 1});
                        load();
                    } else if(data.msg == "300"){
                        layer.closeAll("loading");
                        layer.msg(data.errMsg, {icon: 2});
                    }

                },
                error: () => {
                    layer.closeAll("loading");
                    layer.alert("网络错误 请联系管理员");
                }
            });
        });
    });
    //发送验证码
    $(document).on("click", ".getCode", (e) => {
        let phone = $("input[name=phone]").val();//新手机号
        let oldPhone = $("input[name=oldPhone]").val();//新手机号
        if(!(/^1[3456789]\d{9}$/.test(oldPhone))){
            layer.alert("手机号格式不正确", {icon: 2});
            return false;
        }
        if(!(/^1[3456789]\d{9}$/.test(phone))){
            layer.alert("手机号格式不正确", {icon: 2});
            return false;
        }
        //发送验证码
        $.ajax({
            url: "http://116.62.173.33:3033/api/supplier/checkTele",
            type: "get",
            data: {
                "phone": phone
            },
            // contentType: "application/json",
            headers: {"token": token},
            dataType: "json",
            success: (data) => {
               if(data.msg == "500"){
                    layer.alert(data.errMsg, {icon: 1});
               }
            },
            error: () => {
                layer.closeAll("loading");
                layer.alert("网络错误 请联系管理员");
            }
        });
    });
});