layui.use(["layer", "form", "laydate", "upload"], function(){
    $ = layui.$;
    form = layui.form;
    layer = layui.layer;
    laydate = layui.laydate;
    upload = layui.upload;
    //手机号
    $("input[name=phone]").val(phone);
    //日期
    laydate.render({
        elem: '#date'
    });

    //营业执照
    upload.render({
        elem: '#test1'
        , url: 'https://www.tuyatrip.com/api/upload/uploadFile' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,method: 'post'  //可选项。HTTP类型，默认post
        ,accept: 'images'
        ,acceptMime: 'image/*'
        ,headers: {token: token}
        , before: function (obj) {
            //预读本地文件示例，不支持ie8
            // obj.preview(function (index, file, result) {
            //     $('#demo1').attr('src', result); //图片链接（base64）
            // });
            layer.load(3);
        }
        , done: function (res) {
            console.log(res)
            $('#demo1').attr('src', res.data); //图片链接（base64）
            $("input[name=businessLicense]").val(res.data);
            layer.closeAll("loading");
        }
        , error: function () {
        }
        //进度条
        , progress: function (n, elem, e) {
        }
    });
    //身份证正面
    upload.render({
        elem: '#test2'
        , url: 'https://www.tuyatrip.com/api/upload/uploadFile' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,method: 'post'  //可选项。HTTP类型，默认post
        ,accept: 'images'
        ,acceptMime: 'image/*'
        ,headers: {token: token}
        , before: function (obj) {
            layer.load(3);
        }
        , done: function (res) {
            console.log(res)
            $('#demo2').attr('src', res.data); //图片链接（base64）
            $("input[name=idCardJust]").val(res.data);
            layer.closeAll("loading");
        }
        , error: function () {
        }
        //进度条
        , progress: function (n, elem, e) {
        }
    });
    //身份证反面
    upload.render({
        elem: '#test3'
        , url: 'https://www.tuyatrip.com/api/upload/uploadFile' //此处用的是第三方的 http 请求演示，实际使用时改成您自己的上传接口即可。
        ,method: 'post'  //可选项。HTTP类型，默认post
        ,accept: 'images'
        ,acceptMime: 'image/*'
        ,headers: {token: token}
        , before: function (obj) {
            layer.load(3);
        }
        , done: function (res) {
            console.log(res)
            $('#demo3').attr('src', res.data); //图片链接（base64）
            $("input[name=idCardReverse]").val(res.data);
            layer.closeAll("loading");
        }
        , error: function () {
        }
        //进度条
        , progress: function (n, elem, e) {
        }
    });

    //注册
    $(".registered_button").on("click", () => {
        if($("input[name=company]").val() == null || $("input[name=company]").val() == ""){
            layer.msg("请输入公司名称", {icon: 2});
            return false;
        }
        if($("input[name=representative]").val() == null || $("input[name=representative]").val() == ""){
            layer.msg("请输入法人", {icon: 2});
            return false;
        }
        if($("select[name=documentType]").val() == null || $("select[name=documentType]").val() == ""){
            layer.msg("请选择证件类型", {icon: 2});
            return false;
        }
        if($("input[name=idNumber]").val() == null || $("input[name=idNumber]").val() == ""){
            layer.msg("请输入证件号码", {icon: 2});
            return false;
        }
        if($("input[name=registeredCapital]").val() == null || $("input[name=registeredCapital]").val() == ""){
            layer.msg("请输入资本", {icon: 2});
            return false;
        }
        if($("input[name=setUpTheDate]").val() == null || $("input[name=setUpTheDate]").val() == ""){
            layer.msg("请选择成立日期", {icon: 2});
            return false;
        }
        if($("input[name=residence]").val() == null || $("input[name=residence]").val() == ""){
            layer.msg("请输入地址", {icon: 2});
            return false;
        }
        if($("input[name=businessLicense]").val() == null || $("input[name=businessLicense]").val() == ""){
            layer.msg("请上传营业执照", {icon: 2});
            return false;
        }
        if($("input[name=idCardJust]").val() == null || $("input[name=idCardJust]").val() == ""){
            layer.msg("请上传身份证正面", {icon: 2});
            return false;
        }
        if($("input[name=idCardReverse]").val() == null || $("input[name=idCardReverse]").val() == ""){
            layer.msg("请上传身份证反面", {icon: 2});
            return false;
        }
        $.ajax({
            url: "https://www.tuyatrip.com/api/supplier/registeredRelease",
            // url: "http://localhost:9600/supplier/registeredRelease",
            type: "post",
            data: $("#form").serialize(),
            dataType: "json",
            headers: {token: localStorage.getItem("token")},
            success: (data) => {
                if(data.msg == "200"){
                    // if(data.data != null){
                    //     localStorage.setItem("token", data.data.token);
                    //     window.location.href="/page/index"
                    // }
                    window.location.href="/page/index"
                }
                layer.closeAll("loading");
            },
            error: () => {
                layer.alert("服务器异常，请联系管理员", {icon: 2});
                layer.closeAll("loading");
            }
        });
    });
})