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

    module.init = function (page) {

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

        //焦点图
        module.swiperBanner();

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

        //挂牌项目竞价记录
        tools.doGet(biddinglist, {projectId:getQueryVariable('id')}, module.biddinglistInformation,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.outProjectInformation = function (data) {
        if (data.code == 200) {
            console.log(data)
            var content = data.data;
            module.data.outProjectInformationDetail = content;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);


            //挂牌价格字典
            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);

        }
    }

    //标的物图片
    module.showImgInformation = function (data) {
        if (data.code == 200) {
            var content = data.data;
            module.data.showImgInformationDetail = content;
            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);
        }
    }

    //焦点图滚动
    module.swiperBanner = function (type) {
        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);
        }
    }

    //经济类型
    module.economicTypeDictionaries = function (data) {
        if (data.code == 200) {
            var content = data.data;
            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) {
            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) {
            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) {
            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) {
            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) {
            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) {
            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) {
            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) {
            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) {
            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);
        }
    }



    return module;
});