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.load(3);
            //修改
            $.ajax({
                url: "/personal/updateName",
                type: "put",
                data: {
                    "name": $("input[name=nameVal]").val()
                },
                // contentType: "application/json",
                headers: {"token": localStorage.getItem("token")},
                dataType: "json",
                success: (data) => {
                    if(data.msg == "200"){
                        layer.closeAll();
                        parent.layer.msg("修改成功", {icon: 1});
                        // parent.conn.updateCurrentUserNick($("input[name=nameVal]").val())
                        parent.conn.updateOwnUserInfo({nickname: $("input[name=nameVal]").val()});
                        parent.$(".user_font").html("Hi，" + $("input[name=nameVal]").val());
                        load();
                    } else if(data.msg == "300"){
                        layer.closeAll("loading");
                        layer.msg(data.errMsg, {icon: 2});
                    }
                },
                error: () => {
                    layer.closeAll("loading");
                    layer.alert("网络错误 请联系管理员");
                }
            });
        }, function(){
            layer.closeAll()
        });
    });

    //手机号修改
    $(".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 getCaptcha'>获取验证码</div></div>"
            +"</div>";
        layer.confirm(str,{
            btn: ['确定', '取消'],
            title: "手机号修改",
            skin: 'layclass'
        }, 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": localStorage.getItem("token")},
                dataType: "json",
                success: (data) => {
                    if(data.msg == "200"){
                        layer.closeAll();
                        parent.layer.msg("修改成功", {icon: 1});
                        localStorage.setItem("token", data.data);
                        load();
                    } else if(data.msg == "300"){
                        layer.closeAll("loading");
                        layer.msg(data.errMsg, {icon: 2});
                    }

                },
                error: () => {
                    layer.closeAll("loading");
                    layer.alert("网络错误 请联系管理员");
                }
            });
        }, function(){
            layer.closeAll()
        });
    });
    //发送验证码
    $(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://www.tuyatrip.com/api/supplier/updatePhoneSupplier",
            type: "post",
            data: {
                "phone": phone
            },
            // contentType: "application/json",
            headers: {"token": localStorage.getItem("token")},
            dataType: "json",
            success: (data) => {
               if(data.msg == "500"){
                    layer.alert(data.errMsg, {icon: 2});
               } else {
                   layer.alert("发送成功", {icon: 1});
                   let time = 60;
                   let timer = "";
                   clearInterval(timer);
                   timer = setInterval(function () {
                       if (time > 0) {
                           $('.getCaptcha').html(time + "S");
                           $(".getCaptcha").removeClass("getCode");
                           time--;
                       } else {
                           $('.getCaptcha').html("获取验证码");
                           time = 60;
                           $(".getCaptcha").addClass("getCode");
                           clearInterval(timer);
                       }
                   }, 1000);
               }
            },
            error: () => {
                layer.closeAll("loading");
                layer.alert("网络错误 请联系管理员");
            }
        });
    });
    //报价单次数减一
    $(".minus_div").on("click", (e) => {
        var offerNum =  $("input[name=offerNum]").val();
        if(offerNum == 1){
            return false;
        } else {
            $("input[name=offerNum]").val(offerNum - 1)
            $(".price_calculate").html((Number($(".price_calculate").html()) - unitPrice).toFixed(2));
        }
    });

    //报价单次数加一
    $(".add_div").on("click", (e) => {
        var offerNum =  $("input[name=offerNum]").val();
        $("input[name=offerNum]").val(Number(offerNum) + 1)
        $(".price_calculate").html((Number($(".price_calculate").html()) + unitPrice).toFixed(2));
    });
    //点击头像触发上传
    $(".headpir_img").on("click", (e) => {
        $("#file").click();
    });
    //截取取消
    $(".cancel-btn").click(function (){
        layer.closeAll();
    });

    //图片截取
    var clipAreaName = null;
    var clipArea = new bjj.PhotoClip("#clipArea", {
        size: [260, 260],
        outputSize: [640, 640],
        file: "#file",
        // view: "#view",
        ok: "#clipBtn",
        loadStart: function(obj) {
            clipAreaName= obj.name;
        },
        loadComplete: function(obj) {
            layer.open({
                type: 1,
                title: false,
                offset: ["1366px","auto"],
                closeBtn: 2,
                anim: 2,
                shadeClose: true,
                content: $(".van-popup"),
                end: () => {

                }
            })
        },
        clipFinish: function(dataURL) {
            var file = blobToFile(dataURLtoBlob(dataURL), clipAreaName);
            var formData = new FormData();
            formData.append("file", file);
            cutOos(formData);
        }
    });

    translationVal(parent.type);//翻译
});