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

    //头部查询
    $(".query_button").on("click", () => {
        phone = $("input[name=phone]").val(); //开始日期
        load(1, 7, phone);//查询报价单数据渲染
    });

    //头部重置
    $(".reset_button").on("click", () => {
        $("input[name=phone]").val(""); //开始日期
        phone = null;
        load(1, 7, phone);//查询报价单数据渲染
    });
    //新增子账号
    $(".add_button").on("click", function (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='phoneSend' 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/editorEmployees",
                type: "put",
                data: {
                    "phone": $("input[name=phoneSend]").val(),
                    "captcha": $("input[name=yzNum]").val()
                },
                headers: {"token": localStorage.getItem("token")},
                dataType: "json",
                success: (data) => {
                    if(data.msg == "200"){
                        layer.closeAll();
                        parent.layer.msg("修改成功", {icon: 1});
                        load(1, 7, null);//数据渲染
                    } 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=phoneSend]").val();//新手机号
        // let oldPhone = $("input[name=oldPhone]").val();//新手机号
        // if(!(/^1[3456789]\d{9}$/.test(oldPhone))){
        //     layer.alert("手机号格式不正确", {icon: 2});
        //     return false;
        // }
        console.log(phone)
        if(!(/^1[3456789]\d{9}$/.test(phone))){
            layer.alert("手机号格式不正确", {icon: 2});
            return false;
        }
        //发送验证码
        $.ajax({
            url: "https://www.tuyatrip.com/api/supplier/checkTele",
            type: "get",
            data: {
                "phone": phone
            },
            // contentType: "application/json",
            headers: {"token": localStorage.getItem("token")},
            dataType: "json",
            success: (data) => {
                if(data.msg == "500"){
                    parent.layer.alert(data.errMsg, {icon: 2});
                } else {
                    parent.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("网络错误 请联系管理员");
            }
        });
    });
    load(1, 7, phone);//数据渲染
    translationVal(parent.type);//翻译
})