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

    //点击发送验证码
    $(document).on("click", ".getCode", (e) => {
        let tel = $("input[name=tel]").val();
        if(tel == null || tel == ""){
            layer.msg("请输入手机号", {icon: 2});
            return false;
        }
        if(!(/^1[3456789]\d{9}$/.test(tel))){
            layer.msg("手机号格式不正确", {icon: 2});
            return false;
        }
        //发送验证码
        $.ajax({
            url: "https://www.tuyatrip.com/api/supplier/checkTele",
            type: "get",
            data: {
                "phone": tel
            },
            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) {
                            $('.code-btn').html(time + "S");
                            $(".code-btn").removeClass("getCode");
                            time--;
                        } else {
                            $('.code-btn').html("获取验证码");
                            time = 60;
                            $(".code-btn").addClass("getCode");
                            clearInterval(timer);
                        }
                    }, 1000);
                }
            },
            error: () => {
                layer.closeAll("loading");
                layer.alert("网络错误 请联系管理员");
            }
        });
    });
    //注册登录
    $(".login-btn").on("click", (e) =>{
        let captcha = $("input[name=captcha]").val();
        let tel = $("input[name=tel]").val();
        if(captcha == null || captcha == ""){
            layer.msg("验证码不能为空", {icon: 2});
            return false;
        }
        if(tel == null || tel == ""){
            layer.msg("请输入手机号", {icon: 2});
            return false;
        }
        if(!(/^1[3456789]\d{9}$/.test(tel))){
            layer.msg("手机号格式不正确", {icon: 2});
            return false;
        }
        if($("input[name=termsSelect]").prop("checked") == false){
            layer.msg("请点击同意协议", {icon: 2});
            return false;
        }
        //登录
        $.ajax({
            url: "https://www.tuyatrip.com/api/supplier/updateUser",
            type: "get",
            data: {
                "phone": tel,
                "captcha": captcha
            },
            dataType: "json",
            success: (data) => {
                if(data.msg == "500"){
                    layer.msg(data.errMsg, {icon: 2});
                    return false;
                } else if(data.msg == "100"){
                    window.location.href="/page/registered/" + tel;
                }
                localStorage.setItem("token", data.data.token);
                window.location.href="/page/index"
            },
            error: () => {
                layer.closeAll("loading");
                layer.alert("网络错误 请联系管理员");
            }
        });
    });

    $.ajax({
        url: "/json/wave.json",
        type: "get",
        data: {},
        dataType: "json",
        success: (data) => {
            var anim = lottie.loadAnimation({
                container: document.querySelector(".lottie"), // 容器
                renderer: "svg",
                loop: true,
                autoplay: true,
                animationData: data, //如果使用的是JSON
            });
        },
        error: () => {
            layer.closeAll("loading");
            layer.alert("网络错误 请联系管理员");
        }
    });
})