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

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

        module.data.sheng = options;
        module.data.shi = options[0].children;
        var shengData = template('shengData', module.data);
        $("#sheng").html(shengData);
        var shiData = template('shiData', module.data);
        $("#shi").html(shiData);

        //账户类型
        tools.doGet(dictionaries+"/bank_account_type", {}, module.accountType, true);
        var cupload2 = new Cupload ({
            ele: '#cupload-3',
            num: 1,
            data:null
        });
        var cupload4 = new Cupload ({
            ele: '#cupload-4',
            num: 1,
            data:null
        });

        var cupload5 = new Cupload ({
            ele: '#cupload-5',
            num: 1,
            data:null
        });

        var cupload6 = new Cupload ({
            ele: '#cupload-6',
            num: 1,
            data:null
        });

        var cupload7 = new Cupload ({
            ele: '#cupload-7',
            num: 1,
            data:null
        });

    };

//底部友情链接
    module.bankType = function (data) {
        if (data.code == 200) {
            var content = data.data;
            module.data.bankTypeOption = content;
            var bankTypeData = template('bankTypeData', module.data);
            $("#bankType").html(bankTypeData);
        }
    }

    //账户类型
    module.accountType = function (data) {
        if (data.code == 200) {
            var content = data.data;
            module.data.accountTypeOption = content;
            var accountTypeData = template('accountTypeData', module.data);
            $("#accountType").html(accountTypeData);
        }
    }
    getShi = function () {
        console.log($('#bankTypeData').val())
        // if (!$('#bankTypeData').val()){
        //     return;
        // }
        module.data.shi = module.data.sheng.filter(function (e) { return e.label == $('#sheng').val(); })[0].children;
        var shiData = template('shiData', module.data);
        $("#shi").html(shiData);

        // tools.doGet(depositList, {sheng:$('#sheng').val(),shi:$('#shi').val()}, module.depositList, true);
    }
    getDeposit = function () {
        let data = {
            sheng:$('#sheng').val(),
            shi:$('#shi').val(),
            bankType:$('#bankType').val(),
            bankDeposit:$('#bankDeposit').val(),
        }
        console.log(data)
        tools.doGet(depositListYN, data,module.depositListYN);
    }
    changeBankAddress = function () {
        console.log($('#bankAddress').val())
        console.log(module.data.bankAddress.filter(function (e) { return e.bankDeposit == $('#bankAddress').val(); }))

        document.getElementById('payeePaymentLines').value =
            module.data.bankAddress.filter(function (e) { return e.bankDeposit == $('#bankAddress').val(); })[0].payeePaymentLines;
    }

    module.depositListYN = function (data) {
        var content = data.rows;
        if (content.length<1){
            let dataHtt = {
                sheng:$('#sheng').val(),
                shi:$('#shi').val(),
                bankType:$('#bankType').val(),
                bankDeposit:$('#bankDeposit').val(),
            }
            tools.doPost(depositListPub, dataHtt,module.depositList);
        }else{
            module.data.bankAddress = content;
            var bankAddressData = template('bankAddressData', module.data);
            $("#bankAddress").html(bankAddressData);
            if (content.length>0){
                changeBankAddress();
            }else{
                document.getElementById('payeePaymentLines').value = '';
            }
        }
    }

    module.depositList = function (data) {
        var content = data.data;
        module.data.bankAddress = content;
        var bankAddressData = template('bankAddressData', module.data);
        $("#bankAddress").html(bankAddressData);
        if (content.length>0){
            changeBankAddress();
        }else{
            document.getElementById('payeePaymentLines').value = '';
        }
    }

    //底部友情链接
    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.upLoadOverInfo1 = function(data){
        if (data.code == 200)
            module.data.sfz = data.fileName;
    }

    module.upLoadOverInfo2 = function(data){
        if (data.code == 200)
            module.data.yhk = data.fileName;
    }

    module.upLoadOverInfo5 = function(data){
        if (data.code == 200)
            module.data.khzm = data.fileName;
    }

    module.upLoadOverInfo6 = function(data){
        if (data.code == 200)
            module.data.frzjz = data.fileName;
    }

    module.upLoadOverInfo7 = function(data){
        if (data.code == 200)
            module.data.companyLicense = data.fileName;
    }

    module.submitInfo = function(type){
        document.getElementById('loading').style.display = 'flex';
        if(module.data.type == '1'){
            $('#cupload-3').find('input').each(function() {
                if($(this).val()!=''&&$(this).val().indexOf('http:') == -1){
                    module.data.upType1 = true;
                    tools.doPostImg(base64Upload2, {file:$(this).val()}, module.upLoadOverInfo1);
                }
            })
            $('#cupload-4').find('input').each(function() {
                if($(this).val()!=''&&$(this).val().indexOf('http:') == -1){
                    module.data.upType2 = true;
                    tools.doPostImg(base64Upload2, {file:$(this).val()}, module.upLoadOverInfo2);
                }
            })
            setTimeout(module.register,5000);

        }else{
            $('#cupload-5').find('input').each(function() {
                if($(this).val()!=''&&$(this).val().indexOf('http:') == -1){
                    module.data.upType5 = true;
                    tools.doPostImg(base64Upload2, {file:$(this).val()}, module.upLoadOverInfo5);
                }
            })
            $('#cupload-6').find('input').each(function() {
                if($(this).val()!=''&&$(this).val().indexOf('http:') == -1){
                    module.data.upType6 = true;
                    tools.doPostImg(base64Upload2, {file:$(this).val()}, module.upLoadOverInfo6);
                }
            })
            $('#cupload-7').find('input').each(function() {
                if($(this).val()!=''&&$(this).val().indexOf('http:') == -1){
                    module.data.upType67 = true;
                    tools.doPostImg(base64Upload2, {file:$(this).val()}, module.upLoadOverInfo7);
                }
            })
            setTimeout(module.register,5000);
        }
            //memberType 1个人 2单位
    }

    //用户注册
    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 idCardPic = module.data.sfz;
                var accountOpenCert = module.data.yhk;
                var memberType = 1 ;

                data['idCardPic'] = idCardPic;
                data['accountOpenCert'] = accountOpenCert;
                data['phone'] = phone;
                data['password'] = passwordVal;
                data['code'] = codeVal;
                data['realname'] = realName;
                data['idCardNum'] = idCardNum;
                data['address'] = address;
                data['economicType'] = 1;
                data['accountType'] = 2;
            }
            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 = module.data.companyLicense;
                var companyCode = $('#companyCode').val();
                var economicType = $('#economicType').val();
                var accountOpenCert = module.data.khzm;
                var idCardPic = module.data.frzjz;

                data['accountOpenCert'] = accountOpenCert;
                data['idCardPic'] = idCardPic;
                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['accountType'] = 1;
            }

            var bankCardName = $('#bankCardName').val();
            var bankCardNum = $('#bankCardNum').val();
            var bankAddress = $('#bankAddress').val();
            var bankType = $('#bankType').val();
            var accountType = $('#accountType').val();
            var payeePaymentLines = $('#payeePaymentLines').val();
            data['bankCardName'] = bankCardName;
            data['bankCardNum'] = bankCardNum;
            data['bankAddress'] = bankAddress;
            data['bankType'] = bankType;
            data['accountType'] = accountType;
            data['payeePaymentLines'] = payeePaymentLines;

            data['memberType'] = module.data.type;

            tools.doPost(userRegister, data, module.registerData, true)
        }else{
            document.getElementById('loading').style.display = 'none';
        }

    }

    //注册校验
    module.registerData = function (data) {
        console.log(data)

        document.getElementById('loading').style.display = 'none';
        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 companyRealName = $('#companyRealName').val();
        var companyIdCardNum = $('#companyIdCardNum').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.sfz == '') {
                $('#cupload-3')[0].focus()
                tools.initTips('请上传身份证', 'right', $('#cupload-3')[0], 2000)
                return false;
            }
            /* 银行卡 */
            if (module.data.yhk == '') {
                $('#cupload-4')[0].focus()
                tools.initTips('请上传银行卡', 'right', $('#cupload-4')[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 (module.data.frzjz == '') {
                $('#cupload-6')[0].focus()
                tools.initTips('请上传法人证件照', 'right', $('#cupload-6')[0], 2000)
                return false;
            }
            /* 上传附件 */
            if (module.data.companyLicense == '') {
                $('#cupload-7')[0].focus()
                tools.initTips('请上传营业执照', 'right', $('#cupload-7')[0], 2000)
                return false;
            }
            /* 上传附件 */
            if (module.data.khzm == '') {
                $('#cupload-5')[0].focus()
                tools.initTips('请上传开户证明', 'right', $('#cupload-5')[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;
        }
        var bankCardName = $('#bankCardName').val();
        var bankCardNum = $('#bankCardNum').val();
        var bankAddress = $('#bankAddress').val();
        var bankType = $('#bankType').val();
        var accountType = $('#accountType').val();
        var payeePaymentLines = $('#payeePaymentLines').val();

        if (bankCardName == ''||bankCardName == null) {
            $('#bankCardName')[0].focus()
            tools.initTips('请输入开户姓名', 'right', $('#bankCardName')[0], 2000)
            return false;
        }

        if (bankCardNum == ''||bankCardNum == null) {
            $('#bankCardNum')[0].focus()
            tools.initTips('请输入开户账号', 'right', $('#bankCardNum')[0], 2000)
            return false;
        }

        if (bankType == ''||bankCardNum == null) {
            $('#bankType')[0].focus()
            tools.initTips('请选择所属银行', 'right', $('#bankType')[0], 2000)
            return false;
        }

        if (bankAddress == ''||bankAddress == null) {
            $('#bankAddress')[0].focus()
            tools.initTips('请选择开户银行', 'right', $('#bankAddress')[0], 2000)
            return false;
        }

        if (payeePaymentLines == ''||payeePaymentLines == null) {
            $('#payeePaymentLines')[0].focus()
            tools.initTips('请输入联行号', 'right', $('#payeePaymentLines')[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;
});
