/**
 * Created by Administrator on 2021/4/5.
 */
define(['jquery', "template", "Tools", "echarts", 'register', 'swiper'], function ($, template, Tools, echarts, swiper) {
    //数据存储
    var module = {
        data: {
            type: 1
        },
    };
    var tools = new Tools();

    module.init = function (page) {
        //点击注册
        $('#register-submit').on('click', module.register)
        //点击图形验证码
        $('#graphicImgBtn').on('click', module.verificationCode)
        //图形验证码加载
        module.verificationCode()
        //网站配置信息（网站名称 底部联系方式 公安备案号 网站备案号）
        tools.getWebConfig();
        module.getType();
        tools.doGet(Dictionaries+'/economic_type', {}, module.economicTypeDictionaries,true);
    };


    //底部友情链接
    module.bottomFriendsLinks = function (data) {
        if (data.code == 200) {
            var content = data.data;
            console.log(content)
            module.data.friendsLinksList = content;
            var friendsLinksData = template('friendsLinksData', module.data);
            $("#friendsLinksContent").html(friendsLinksData);
        }
    }
    //获取地址栏参数
    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    //经济类型
    module.economicTypeDictionaries = function (data) {
        if (data.code == 200) {
            var content = data.data;
            console.log(content)
            module.data.outProjectInformationDetail = content;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#economicType").html(outProjectInformationData);
        }
    }

    module.getType = function(){
        var type = getQueryVariable("type");
        if (type == 1){
            document.getElementById("userBtn").className = "active";
            document.getElementById("companyBtn").className = "";
            tabCheck('user');
            module.data.type = type;
        }else if(type == 2){
            document.getElementById("userBtn").className = "";
            document.getElementById("companyBtn").className = "active";
            tabCheck('company');
            module.data.type = type;
        }
    }

    tabCheck = function(type,typeNum){
        document.getElementById("userBtn").className = "";
        document.getElementById("companyBtn").className = "";
        document.getElementById("userTable").style.display = "none";
        document.getElementById("companyTable").style.display = "none";

        module.data.type = typeNum;
        document.getElementById(type+'Btn').className = 'active';
        document.getElementById(type+'Table').style.display = 'block';
    }


    //图形验证码
    module.verificationCode = function () {
        tools.doGet(captchaImage_get, {}, module.verificationAjax, true)
    }
    module.verificationAjax = function (data) {
        if (data.code == 200) {
            $('#graphicImg').attr('src', 'data:image/gif;base64,' + data.img)
            module.uuid = data.uuid;
        }
    }

    //用户注册
    module.register = function (data) {
        var data = {};
        if(module.check()){
            if (module.data.type == 1){
                var phone = $('#phone').val();
                var passwordVal = $('#password').val();
                var codeVal = $('#code').val();
                var realName = $('#realName').val();
                var idCardNum = $('#idCardNum').val();
                var address = $('#address').val();
                var memberType = 1 ;
                data['phone'] = phone;
                data['password'] = passwordVal;
                data['code'] = codeVal;
                data['realname'] = realName;
                data['idCardNum'] = idCardNum;
                data['address'] = address;
                data['economicType'] = 1;
            }
            if (module.data.type == 2){
                var phone = $('#phone').val();
                var passwordVal = $('#password').val();
                var codeVal = $('#code').val();

                var companyName = $('#companyName').val();
                var realName = $('#companyRealName').val();
                var idCardNum = $('#companyIdCardNum').val();
                var address = $('#companyAddress').val();
                var companyLicense = $('#companyLicense').val();
                var companyCode = $('#companyCode').val();
                var economicType = $('#economicType').val();

                data['phone'] = phone;
                data['password'] = passwordVal;
                data['code'] = codeVal;
                data['realname'] = realName;
                data['idCardNum'] = idCardNum;
                data['address'] = address;
                data['companyName'] = companyName;
                data['companyLicense'] = companyLicense;
                data['companyCode'] = companyCode;
                data['economicType'] = economicType;
            }
            data['memberType'] = module.data.type;
            tools.doPost(userRegister, data, module.registerData, true)
        }

    }

    //注册校验
    module.registerData = function (data) {
        console.log(data)
        if (data.code == 500) {
            module.verificationCode()
        }
        if (data.code == 200){
            tools.initError("注册成功");
            setTimeout(function(){
                tools.skip('login.html')
            },2000)
        }
    }

    //手动验证表单
    module.check = function () {
        var phone = $('#phone').val();
        var passwordVal = $('#password').val();
        var codeVal = $('#code').val();
        var realName = $('#realName').val();
        var idCardNum = $('#idCardNum').val();
        var address = $('#address').val();

        var companyRealName = $('#companyRealName').val();
        var companyIdCardNum = $('#companyIdCardNum').val();
        var companyAddress = $('#companyAddress').val();
        /* 手机号 */
        if (phone == '') {
            $('#phone')[0].focus()
            tools.initTips('请输入手机号', 'right', $('#phone')[0], 2000)
            return false;
        }
        if (module.data.type == 1) {
            /* 姓名 */
            if (realName == '') {
                $('#realName')[0].focus()
                tools.initTips('请输入姓名', 'right', $('#realName')[0], 2000)
                return false;
            }
            /* 身份证号 */
            if (idCardNum == '') {
                $('#idCardNum')[0].focus()
                tools.initTips('请输入身份证号', 'right', $('#idCardNum')[0], 2000)
                return false;
            }
            /* 地址 */
            if (address == '') {
                $('#address')[0].focus()
                tools.initTips('请输入地址', 'right', $('#address')[0], 2000)
                return false;
            }
        }
        if (module.data.type == 2){
            /* 姓名 */
            if (companyRealName == '') {
                $('#companyRealName')[0].focus()
                tools.initTips('请输入姓名', 'right', $('#companyRealName')[0], 2000)
                return false;
            }
            /* 身份证号 */
            if (companyIdCardNum == '') {
                $('#companyIdCardNum')[0].focus()
                tools.initTips('请输入身份证号', 'right', $('#companyIdCardNum')[0], 2000)
                return false;
            }
            /* 地址 */
            if (companyAddress == '') {
                $('#companyAddress')[0].focus()
                tools.initTips('请输入地址', 'right', $('#companyAddress')[0], 2000)
                return false;
            }
        }
        /* 密码 */
        if (passwordVal == '') {
            $('#password')[0].focus()
            tools.initTips('请输入密码', 'right', $('#password')[0], 2000)
            return false;
        } else if (parseInt(passwordVal.length) < 6 || parseInt(passwordVal.length) > 18) {
            $('#password')[0].focus()
            tools.initTips('请输入正确格式密码', 'right', $('#password')[0], 2000)
            return false;
        }else if($('#password').val() != $('#passwordAgain').val()){
            $('#password')[0].focus()
            tools.initTips('两次密码输入不一致', 'right', $('#password')[0], 2000)
            return false;
        }
        /*图形验证码*/
        if (module.uuid == '' || codeVal == '') {
            $('#code')[0].focus()
            tools.initTips('请输入图形验证码', 'right', $('#code')[0], 2000)
            return false;
        }
        console.log($('#iAgree:checked').val())
        if ($('#iAgree:checked').val() == undefined){
            tools.initTips('请选择我已同意', 'right', $('#iAgree')[0], 2000)
            return false;
        }
        return true;
    }
    return module;
});