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

    module.init = function (page) {
        //底部友情链接
        tools.doGet(friendsLinks, {}, module.bottomFriendsLinks, true);

        //网站配置信息（网站名称 底部联系方式 公安备案号 网站备案号）
        tools.getWebConfig();

        module.data.loginNow = tools.getCookie('Admin-Token');

        //浏览次数
        tools.doGet(outProject, {id:getQueryVariable('id')}, module.projectNum,true);

        setTimeout(function(){//项目基本信息
            tools.doGet(outProject + '/id/'+getQueryVariable('id'), {}, module.outProjectInformation,true);
        },100)

        //焦点图
        setTimeout(function () {
            module.swiperBanner();
        },2000)

        setTimeout(function(){//项目基本信息
            tools.doGet(showImg + '/'+getQueryVariable('id'), {}, module.showImgInformation,true);
        },100)

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

    //获取地址栏参数
    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.webConfigInformation = function (data) {
        if (data.code == 200) {
            var content = data.data;
            console.log(content)
            //document.getElementById('time').innerHTML = '延时周期：'+content[8].configValue+content[8].remark+'<i class="wh">?</i>'
            module.data.timeConfig = content[8].configValue;
            module.data.titleConfig = content[9].configValue;
            module.data.importantTitle = content[7].configName+':'+ content[7].configValue.substr(3).substr(0,(content[7].configValue.substr(3).length-4));
            module.data.know = content[6].configValue;
            $("#know").html(content[6].configValue + '<span style="margin-left: 10px;"></span>' + content[6].configValue);
        }
    }
    //底部友情链接
    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);
        }
    }
    module.dataCompare = function(signupStartTime,signupStopTime,biddingStartTime,biddingStopTime){
        var signupStartTime = signupStartTime;
        var signupStopTime = signupStopTime;
        var biddingStartTime = biddingStartTime;
        var biddingStopTime = biddingStopTime;
        var nowTime = Date.parse(new Date());

        var beginTimes=signupStartTime.substring(0,10).split('-');
        var endTimes=signupStopTime.substring(0,10).split('-');

        var biddingBeginTimes=biddingStartTime.substring(0,10).split('-');
        var biddingEndTimes=biddingStopTime.substring(0,10).split('-');

        signupStartTime=beginTimes[1]+'/'+beginTimes[2]+'/'+beginTimes[0]+' '+signupStartTime.substring(10,19);
        signupStopTime=endTimes[1]+'/'+endTimes[2]+'/'+endTimes[0]+' '+signupStopTime.substring(10,19);

        biddingStartTime=biddingBeginTimes[1]+'/'+biddingBeginTimes[2]+'/'+biddingBeginTimes[0]+' '+biddingStartTime.substring(10,19);
        biddingStopTime=biddingEndTimes[1]+'/'+biddingEndTimes[2]+'/'+biddingEndTimes[0]+' '+biddingStopTime.substring(10,19);


        //时间差
        var leftTime = Date.parse(biddingStopTime)-nowTime;
        //报名开始时间差
        var signupLeftTime = Date.parse(signupStartTime)-nowTime;
        //报名结束时间差
        var signDownLeftTime = Date.parse(signupStopTime)-nowTime;
        //定义变量 d,h,m,s保存倒计时的时间
        var d,h,m,s;
        if(Date.parse(biddingStartTime)<nowTime&&nowTime<Date.parse(biddingStopTime)){
            //正在竞价
            module.data.biddingTimeType = 1;
            document.getElementById('biddingTitle').innerHTML = '竞价进行中...';
            document.getElementById('swiperType').style.display = 'block';
            d = Math.floor(leftTime/1000/60/60/24);
            h = Math.floor(leftTime/1000/60/60%24);
            m = Math.floor(leftTime/1000/60%60);
            s = Math.floor(leftTime/1000%60);
            $('#countType').html('竞价中')
            $('#countDown').html('<span>距竞价结束还有：'+d+'</span>天<span>'+h+'</span>时<span>'+m+'</span>分<span>'+s+'</span>秒')
        }else if (nowTime>Date.parse(biddingStopTime)){
            //竞价结束
            $('#countDown').html('竞价结束')
            module.data.biddingTimeType = 2;
        }else if (nowTime<Date.parse(biddingStartTime)){
            //竞价暂未开始
            document.getElementById('biddingTitle').innerHTML = '竞价暂未开始...';
            $('#countDown').html('暂未开始')
            module.data.biddingTimeType = 3;
        }

        if(Date.parse(signupStartTime)<nowTime&&nowTime<Date.parse(signupStopTime)){
            //正在报名
            module.data.signupTimeType = 1;
            document.getElementById('swiperType').style.display = 'block';
            d = Math.floor(signDownLeftTime/1000/60/60/24);
            h = Math.floor(signDownLeftTime/1000/60/60%24);
            m = Math.floor(signDownLeftTime/1000/60%60);
            s = Math.floor(signDownLeftTime/1000%60);
            $('#countType').html('报名中')
            $('#countDown').html('<span>距报名结束还有：'+d+'</span>天<span>'+h+'</span>时<span>'+m+'</span>分<span>'+s+'</span>秒')
        }else if (nowTime>Date.parse(signupStopTime)){
            //报名结束
            module.data.signupTimeType = 2;
        }else if (nowTime<Date.parse(signupStartTime)){
            //报名暂未开始
            module.data.signupTimeType = 3;
            d = Math.floor(signupLeftTime/1000/60/60/24);
            h = Math.floor(signupLeftTime/1000/60/60%24);
            m = Math.floor(signupLeftTime/1000/60%60);
            s = Math.floor(signupLeftTime/1000%60);
            $('#countType').html('未开始')
            $('#countDown').html('<span>距报名开始还有：'+d+'</span>天<span>'+h+'</span>时<span>'+m+'</span>分<span>'+s+'</span>秒')
        }
        if(module.data.outProjectInformationDetail.biddingType != '拍卖' && module.data.biddingTimeType != 2){
            document.getElementById('biddingTable').style.display = 'none';
            document.getElementById('biddingTitle').style.display = 'block';
        }
    }

    module.projectNum = function(data){
        console.log(data)
    }

    module.signup = function(data){
        console.log(data)
        module.data.signup = data.data;
        var outProjectInformationData = template('outProjectInformationData', module.data);
        $("#outProjectInformationContent").html(outProjectInformationData);
    }

    goBond = function(){
        tools.skip('bond.html?id='+getQueryVariable('id'))
    }

    //项目基本信息
    module.outProjectInformation = function (data) {
        if (data.code == 200) {
            console.log(serverApi)
            var content = data.data;
            //倒计时
            var timer = setInterval(function(){
                module.dataCompare(content.signupStartTime,content.signupStopTime,content.biddingStartTime,content.biddingStopTime);
            },1000)
            for(var i = 0 ; i < content.fileUrlList.length ; i++){
                content.fileUrlList[i].fileUrl = serverApi+content.fileUrlList[i].fileUrl;
            }
            module.data.outProjectInformationDetail = content;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);

            $("#know").html(module.data.know);
            //挂牌项目竞价记录
            tools.doGet(biddinglist, {projectId:getQueryVariable('id')}, module.biddinglistInformation,true);

            //挂牌价格字典
            tools.doGet(Dictionaries+'/price_unit', {}, module.unitDictionaries,true);

            //经济类型字典
            tools.doGet(Dictionaries+'/economic_type', {}, module.economicTypeDictionaries,true);

            //项目类型字典
            tools.doGet(Dictionaries+'/project_type', {}, module.projectNumberDictionaries,true);

            //农地性质字典
            tools.doGet(Dictionaries+'/sub_object_type', {}, module.projectTypeDictionaries,true);

            //前次转出方式
            tools.doGet(Dictionaries+'/rollout_type', {}, module.oneoutDictionaries,true);

            //面积单位字典
            tools.doGet(Dictionaries+'/area_unit', {}, module.areaDictionaries,true);

            //支付方式字典
            tools.doGet(Dictionaries+'/payment_type', {}, module.paymentTypeDictionaries,true);

            //缴纳形式字典
            tools.doGet(Dictionaries+'/deposit_payment_type', {}, module.depositPaymentTypeDictionaries,true);

            //交易方式字典
            tools.doGet(Dictionaries+'/deal_type', {}, module.transactionDictionaries,true);

            //竞价方式字典
            tools.doGet(Dictionaries+'/bidding_type', {}, module.biddingTypeDictionaries,true);

            //期满处理字典
            tools.doGet(Dictionaries+'/expire_type', {}, module.expireTypeDictionaries,true);


            if(module.data.loginNow){
                //判断是否报名
                tools.doGet(signup, {projectId:getQueryVariable('id'),depositStatus:'1',memberId:tools.getCookie('memberId')}, module.signup);
            }
        }
    }

    //标的物图片
    module.showImgInformation = function (data) {
        if (data.code == 200) {
            var content = data.data;
            debugger
            console.log(content)
            var contentList = [];
            for(var i = 0 ; i < content.length ; i++){
                contentList[i] = serverApi+content[i].fileUrl;
            }
            module.data.showImgInformationDetail = contentList;
            console.log(contentList)
            var showImgInformationData = template('showImgInformationData', module.data);
            $("#tabDetail2").html(showImgInformationData);
        }
    }

    //竞价记录
    module.biddinglistInformation = function (data) {
        if (data.code == 200) {
            console.log(data)
            var content = data.rows;
            for (var i = 0; i < content.length-1; i++) {
                for (var j = 0; j < content.length - 1 - i; j++) {
                    // 相邻元素两两对比，元素交换，大的元素交换到后面
                    if (content[j].money < content[j + 1].money) {
                        var temp = content[j];
                        content[j] = content[j+1];
                        content[j+1] = temp;
                    }
                }
            }
            console.log(content)
            module.data.biddinglistInformationDetail = content;
            var biddinglistInformationData = template('biddinglistInformationData', module.data);
            $("#tabDetail4").html(biddinglistInformationData);
            console.log(module.data.biddingTimeType)

        }
    }

    //焦点图滚动
    module.swiperBanner = function (type) {
        console.log('aaaa')
        var viewSwiper = new Swiper('.view .swiper-container', {
            onSlideChangeStart: function() {
                updateNavPosition()
            }
        })

        $('.view .arrow-left,.preview .arrow-left').on('click', function(e) {
            e.preventDefault()
            if (viewSwiper.activeIndex == 0) {
                viewSwiper.swipeTo(viewSwiper.slides.length - 1, 1000);
                return
            }
            viewSwiper.swipePrev()
        })
        $('.view .arrow-right,.preview .arrow-right').on('click', function(e) {
            e.preventDefault()
            if (viewSwiper.activeIndex == viewSwiper.slides.length - 1) {
                viewSwiper.swipeTo(0, 1000);
                return
            }
            viewSwiper.swipeNext()
        })

        var previewSwiper = new Swiper('.preview .swiper-container', {
            visibilityFullFit: true,
            slidesPerView: 'auto',
            onlyExternal: true,
            onSlideClick: function() {
                viewSwiper.swipeTo(previewSwiper.clickedSlideIndex)
            }
        })

        function updateNavPosition() {
            $('.preview .active-nav').removeClass('active-nav')
            var activeNav = $('.preview .swiper-slide').eq(viewSwiper.activeIndex).addClass('active-nav')
            if (!activeNav.hasClass('swiper-slide-visible')) {
                if (activeNav.index() > previewSwiper.activeIndex) {
                    var thumbsPerNav = Math.floor(previewSwiper.width / activeNav.width()) - 1
                    previewSwiper.swipeTo(activeNav.index() - thumbsPerNav)
                } else {
                    previewSwiper.swipeTo(activeNav.index())
                }
            }
        }
    }

    //切换
    tabClick = function(type){
        //标的物介绍
        for (var i = 0 ; i < 4 ; i++){
            document.getElementById('tabDetail'+(i+1)).style.display = 'none';
            document.getElementById('tab'+(i+1)).classList = '';
        }
        document.getElementById('tabDetail'+type).style.display = 'block';
        document.getElementById('tab'+type).classList = 'active';
    }

    //挂牌价格
    module.unitDictionaries = function (data) {
        if (data.code == 200) {
            var content = data.data;
            var unit = content[(module.data.outProjectInformationDetail.unit-1)].dictLabel;
            module.data.outProjectInformationDetail.unit = unit;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //出价
    biddingSubmitMethod = function (data) {
        var data = {};
        var projectId = getQueryVariable('id');
        var memberId = tools.getCookie('memberId');
        var userId = tools.getCookie('userId');
        var userAccount = tools.getCookie('phone');
        var userName = tools.getCookie('userName');
        var money = $('#money').val();
        var loginTime = tools.getNowFormatDate();
        data['projectId'] = projectId;
        data['memberId'] = memberId;
        data['userId'] = userId;
        data['userAccount'] = userAccount;
        data['userName'] = userName;
        data['money'] = money;
        data['loginTime'] = loginTime;
        tools.doPost(biddingSubmit, data, module.biddingSubmitOpen);
    }

    module.biddingSubmitOpen = function(){
        tools.initError('出价成功');
    }

    //经济类型
    module.economicTypeDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.economicType!='') {
            var content = data.data;
            if(content[(module.data.outProjectInformationDetail.economicType-1)] != undefined){
                var economicType = content[(module.data.outProjectInformationDetail.economicType-1)].dictLabel;
                module.data.outProjectInformationDetail.economicType = economicType;
                var outProjectInformationData = template('outProjectInformationData', module.data);
                $("#outProjectInformationContent").html(outProjectInformationData);
            }
        }
    }

    //项目类型
    module.projectNumberDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.projectNumber!='') {
            var content = data.data;
            var projectNumber = content[(module.data.outProjectInformationDetail.projectNumber-1)].dictLabel;
            module.data.outProjectInformationDetail.projectNumber = projectNumber;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //农地性质
    module.projectTypeDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.projectType!='') {
            var content = data.data;
            var projectType = content[(module.data.outProjectInformationDetail.projectType-1)].dictLabel;
            module.data.outProjectInformationDetail.projectType = projectType;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //前次转出方式
    module.oneoutDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.oneout!='') {
            var content = data.data;
            if(module.data.outProjectInformationDetail.oneout == 0){
                module.data.outProjectInformationDetail.oneout = '无';
            }else{
                var oneout = content[(module.data.outProjectInformationDetail.oneout-1)].dictLabel;
                module.data.outProjectInformationDetail.oneout = oneout;
            }
            var rollout = content[(module.data.outProjectInformationDetail.rollout-1)].dictLabel;
            module.data.outProjectInformationDetail.rollout = rollout;

            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //面积单位
    module.areaDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.allCountUnit!='') {
            var content = data.data;
            var allCountUnit = content[(module.data.outProjectInformationDetail.allCountUnit-1)].dictLabel;
            module.data.outProjectInformationDetail.allCountUnit = allCountUnit;
            if(module.data.outProjectInformationDetail.subjectList.length>0){
                for(var i = 0 ; i < module.data.outProjectInformationDetail.subjectList.length ; i++){
                    module.data.outProjectInformationDetail.subjectList[i].areaUnit = content[(module.data.outProjectInformationDetail.subjectList[i].areaUnit-1)].dictLabel;
                }
            }
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //支付方式
    module.paymentTypeDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.paymentType!='') {
            var content = data.data;
            var paymentType = content[(module.data.outProjectInformationDetail.paymentType-1)].dictLabel;
            module.data.outProjectInformationDetail.paymentType = paymentType;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //缴纳形式
    module.depositPaymentTypeDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.depositPaymentType!='') {
            var content = data.data;
            var depositPaymentType = content[(module.data.outProjectInformationDetail.depositPaymentType-1)].dictLabel;
            module.data.outProjectInformationDetail.depositPaymentType = depositPaymentType;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //交易方式
    module.transactionDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.dealType!='') {
            var content = data.data;
            var dealType = content[(module.data.outProjectInformationDetail.dealType-1)].dictLabel;
            module.data.outProjectInformationDetail.dealType = dealType;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //竞价方式
    module.biddingTypeDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.biddingType!='') {
            var content = data.data;
            var biddingType = content[(module.data.outProjectInformationDetail.biddingType-1)].dictLabel;
            module.data.outProjectInformationDetail.biddingType = biddingType;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //系统是否
    module.expireTypeDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.expire!='') {
            var content = data.data;
            var expire = content[(module.data.outProjectInformationDetail.expire-1)].dictLabel;
            module.data.outProjectInformationDetail.expire = expire;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
        }
    }

    //向上加价
    upMoney = function(){
        if(module.data.outProjectInformationDetail.money < module.data.outProjectInformationDetail.price){
            module.data.outProjectInformationDetail.money = module.data.outProjectInformationDetail.price;
        }
        module.data.outProjectInformationDetail.money += module.data.outProjectInformationDetail.ladderPrice ;
        document.getElementById('money').value = module.data.outProjectInformationDetail.money;

    }

    //向下降价
    downMoney = function(){
        if(module.data.outProjectInformationDetail.money <= (module.data.outProjectInformationDetail.price+module.data.outProjectInformationDetail.ladderPrice)){
            return;
        }
        module.data.outProjectInformationDetail.money -= module.data.outProjectInformationDetail.ladderPrice ;
        document.getElementById('money').value = module.data.outProjectInformationDetail.money;
    }

    //登陆跳转
    goLogin = function(){
        tools.initDialog('登陆提示', '登陆后可参与竞价，是否登录？', function () {
            tools.skip('../login/login.html')
        }, '登录', function () { }, "取消")
    }
    return module;
});