/**
 * Created by Administrator on 2021/4/5.
 */
define(['jquery', "template", "Tools", 'swiper', 'itemsApi',"cupload"], function ($, template, Tools, swiper) {
    //数据存储
    var module = {
        data: {
            //焦点图数据
            focusListTop: [],
            showImgInformationDetail:''
        },
    };
    var tools = new Tools();

    // var wp = new WritingPad();


    module.init = function (page) {//底部友情链接
        // var wp = new WritingPad();
        //获取焦点图信息
        tools.doGet(websitePicture, {picType:1,status:0,orderByColumn:'picSort',isAsc:'desc'}, module.focusNewsTop , true);
        tools.doGet(friendsLinks, {}, module.bottomFriendsLinks, true);
        document.getElementById('realName').value = tools.getCookie('userName');
        document.getElementById('userNum').value = tools.getCookie('phone');
        document.getElementById('userBank').value = tools.getCookie('bankAddress');
        document.getElementById('bankId').value = tools.getCookie('bankCardNum');
        document.getElementById('bankCardName').value = tools.getCookie('bankCardName');
        document.getElementById('idCard').value = tools.getCookie('idCardNum');
        document.getElementById('payeePaymentLines').value = tools.getCookie('payeePaymentLines');
        document.getElementById('accountType').value = tools.getCookie('accountType');
        document.getElementById('bankType').value = tools.getCookie('bankType');
        tools.doGet(webDeptType+"/bank_account_type", {}, module.accountType, true);
        tools.doGet(webDeptType+"/bank_type_all", {}, module.bankType, true);
        //项目基本信息
        tools.doGet(outProject + '/id/'+getQueryVariable('id'), {}, module.outProjectInformation,true);
        //网站配置信息（网站名称 底部联系方式 公安备案号 网站备案号）
        tools.getWebConfig();

        tools.doGet(webConfig, {}, module.webConfig, true)


    };

    //底部友情链接
    module.webConfig = function (data) {
        if (data.code == 200) {
            $("#xzConfig").html(data.data[6].configValue);
            $("#fwxyConfig").html(data.data[15].configValue);
            $("#cnsConfig").html(data.data[16].configValue);
        }
    }

    //底部友情链接
    module.bankType = function (data) {
        if (data.code == 200) {
            var content = data.data;
            var bankType = ''
            if(tools.getCookie('bankType') != undefined && tools.getCookie('bankType') != 'null' && tools.getCookie('bankType') != ''){
                bankType = content.filter(function (e) { return e.dictValue == tools.getCookie('bankType'); })[0].dictLabel;
                document.getElementById('bankTypeText').value = bankType;
            }
        }
    }

    //底部友情链接
    module.accountType = function (data) {
        if (data.code == 200) {
            var content = data.data;
            if(tools.getCookie('accountType') != undefined){
                var accountType = content.filter(function (e) { return e.dictValue == tools.getCookie('accountType'); })[0].dictLabel;
                document.getElementById('accountTypeText').value = accountType;
            }
        }
    }

    //焦点图数据
    module.focusNewsTop = function (data) {
        if (data.code == 200) {

            let focusArray = [];

            if(data.data.length<1){
                focusArray.push({
                    picUrl:'../../../static/images/banner1.png'
                },{
                    picUrl:'../../../static/images/banner2.png'
                },{
                    picUrl:'../../../static/images/banner3.png'
                })
            }else{
                data.data.forEach(res=>{
                    focusArray.push({
                        picUrl:'/api'+res.picUrl
                    })
                })
            }

            module.data.focusListTop = focusArray;
            var bannerFocusDataTop = template('bannerFocusDataTop', module.data);
            $("#bannerFocusTopContent").html(bannerFocusDataTop);

            new Swiper('#bannerFocusWrapTop', {
                paginationClickable: true,
                autoplay : 4000,
                loop: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);
    }

    //获取上传图片list
    module.upLoadAction = function(data){
        console.log(data)
        if (data.msg != '操作成功'){
            tools.initError(data.msg);
            return;
        }else{
            var upLoadList = [];
            var dataList = {};
            $('#cupload-3').find('input').each(function() {
                if($(this).val()!=''){
                    upLoadList.push($(this).val())
                }
            });
            dataList["files"] = upLoadList;
            dataList["tableId"] = data.data;
            dataList["fileType"] = '';
            dataList["bizPath"] = 'transaction';
            dataList["tableName"] = 't_transaction_signup';
            console.log(dataList)
            tools.doPostImg(base64Upload, dataList, module.upLoadOver);
        }
    }

    module.upLoadOver = function(data){
        tools.initError('提交申请，等待审核')
        setTimeout(function () {
            window.history.back();
        },2000)
    }

    module.outProjectInformation = function (data) {
        if (data.code == 200) {
            console.log(data)
            var content = data.data;
            $("#projectTitle").html('项目名称：'+content.projectName);
            $("#projectNum").html('需缴纳保证新：'+content.deposit+'元');
            $("#payAccount").html('保证金缴纳账号：'+content.payAccount);
            $("#accountName").html('账户名称：'+content.accountName);
            $("#bankName").html('开户银行：'+content.bankName);
            var cupload2 = new Cupload ({
                ele: '#cupload-3',
                num: 5,
            });
            var cupload4 = new Cupload ({
                ele: '#cupload-4',
                num: 1,
            });
        }
    }

    module.upLoadOver2 = function(data){
        if (data.code == 200) {
            submit(data.fileUrl)
        }
    }

    submitFirst = function(){
        $('#cupload-4').find('input').each(function() {
            if($(this).val()!=''){
                let wj  =  dataURLtoBlob($(this).val());
                let param = new FormData()  // 创建form对象
                param.append('file', wj)  // 通过append向form对象添加数据
                param.append('isAngle', 'false')  // 通过append向form对象添加数据
                console.log(param)
                tools.doPostSign(sign, param, module.upLoadOver2);
            }
        });
    }
    dataURLtoBlob = function (dataurl, filename = 'file') {
        let arr = dataurl.split(',')
        let mime = arr[0].match(/:(.*?);/)[1]
        let suffix = mime.split('/')[1]
        let bstr = atob(arr[1])
        let n = bstr.length
        let u8arr = new Uint8Array(n)
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], `${filename}.${suffix}`, {
            type: mime
        })
    }
    submit  = function(signUrl){
        if(module.onCheck()){
        var data = {};
        var realName = $('#realName').val();
        var userNum = $('#userNum').val();
        var userBank = $('#userBank').val();
        var bankId = $('#bankId').val();
        var idCard = $('#idCard').val();
        var bankCardName = $('#bankCardName').val();
        var payeePaymentLines = $('#payeePaymentLines').val();
        var bankType = $('#bankType').val();
        var accountType = $('#accountType').val();

        data['projectId'] = getQueryVariable('id');
        data['memberId'] = tools.getCookie('memberId');
        data['realname'] = realName;
        data['idCardNum'] = idCard;
        data['phone'] = userNum;
        data['address'] = tools.getCookie('address');
        data['bankAddress'] = userBank;
        data['bankCardName'] = bankCardName;
        data['bankCardNum'] = bankId;
        data['payeePaymentLines'] = payeePaymentLines;
        data['bankType'] = bankType;
        data['accountType'] = accountType;
        data['signPic'] = signUrl;
        data['status'] = 'N';

        tools.doPost(signupSubmit, data, module.upLoadAction);
        }
    }
    //验证表单
    module.onCheck = function () {
        var userNum = $('#userNum').val();
        var realName = $('#realName').val();
        var userBank = $('#userBank').val();
        var bankCardName = $('#bankCardName').val();
        var bankId = $('#bankId').val();
        var idCard = $('#idCard').val();
        var payeePaymentLines = $('#payeePaymentLines').val();
        var bankType = $('#bankType').val();
        var accountType = $('#accountType').val();

        if ($('#checkAgree:checked').val() == undefined ) {
            $('#checkAgree')[0].focus()
            tools.initTips('请选择我已同意', 'right', $('#checkAgree')[0], 2000)
            return false;
        }
        if (realName == '') {
            $('#realName')[0].focus()
            tools.initTips('姓名不可为空', 'right', $('#realName')[0], 2000)
            return false;
        }
        if (userNum == '') {
            $('#userNum')[0].focus()
            tools.initTips('会员账号不可为空', 'right', $('#userNum')[0], 2000)
            return false;
        }
        if (accountType == '') {
            $('#accountType')[0].focus()
            tools.initTips('账户类型不可为空', 'right', $('#accountType')[0], 2000)
            return false;
        }
        if (bankType == '') {
            $('#bankType')[0].focus()
            tools.initTips('所属银行不可为空', 'right', $('#bankType')[0], 2000)
            return false;
        }
        if (bankCardName == '') {
            $('#bankCardName')[0].focus()
            tools.initTips('开户行姓名不可为空', 'right', $('#bankCardName')[0], 2000)
            return false;
        }
        if (userBank == '') {
            $('#userBank')[0].focus()
            tools.initTips('开户银行不可为空', 'right', $('#userBank')[0], 2000)
            return false;
        }
        if (bankId == '') {
            $('#bankId')[0].focus()
            tools.initTips('银行卡号不可为空', 'right', $('#bankId')[0], 2000)
            return false;
        }
        if (payeePaymentLines == '') {
            $('#payeePaymentLines')[0].focus()
            tools.initTips('联行号不可为空', 'right', $('#payeePaymentLines')[0], 2000)
            return false;
        }
        if (idCard == '') {
            $('#idCard')[0].focus()
            tools.initTips('身份证号不可为空', 'right', $('#idCard')[0], 2000)
            return false;
        }
        if (idCard == '') {
            $('#idCard')[0].focus()
            tools.initTips('身份证号不可为空', 'right', $('#idCard')[0], 2000)
            return false;
        }
        else{
            return true;
        }
        // let array = []
        // $('#cupload-3').find('input').each(function() {
        //     if($(this).val()!=''){
        //         array.push($(this).val())
        //     }
        // });
        // if(array.length<1){
        //     $('#cupload-3')[0].focus()
        //     tools.initTips('请上传相关凭证', 'right', $('#cupload-3')[0], 2000)
        //     return false;
        // }

    }
    module.toTips = function(data){
        console.log(data)
        if(data.msg != '操作成功'){
            tools.initError(data.msg)
        }else{
            tools.initError('提交申请，等待审核')
        }
    }
    return module;
});
