/**
 * Created by Administrator on 2021/4/5.
 */
define(['jquery', "template", "Tools", "biddingApi", "itemsApi", "paging",'dateTime', 'swiper'], function ($, template, Tools ) {
    //数据存储
    var module = {
        data: {
            //焦点图数据
            focusListTop: [],
            //标的物类型
            deptTypeList:[],
            //标的物所在地
            deptLocationList:[],
            //竞价大厅列表
            biddingInformationList:'',
            //页码
            pageNum: 1,
            //页码集合
            pageList: [],
            //每页数量
            pageSize: 20,
            //总页数
            pageCount:0,
            //标的物选中项id
            deptId:'100',
            //标的所在地选中
            secondDeptId:'',
            //标的物类型ID
            projectNumber:'',
            //项目状态
            projectShowStatus:'',
            //报名开始时间
            signupStartTime:'',
            //报名结束时间
            signupStopTime:'',
            //竞价开始时间
            biddingStartTime:'',
            //竞价结束时间
            biddingStopTime:'',
            form:{
                deptId:100,
                projectNumber:'',
                projectShowStatus:'',
                // signupStartTime:'',
                // signupStopTime:'',
                // biddingStartTime:'',
                // biddingStopTime:'',
            }
        },
    };
    var tools = new Tools();

    module.init = function (page) {
        //获取焦点图信息
        tools.doGet(websitePicture, {picType:1,status:0,orderByColumn:'picSort',isAsc:'desc'}, module.focusNewsTop , true);
        //底部友情链接
        tools.doGet(friendsLinks, {}, module.bottomFriendsLinks, true);
        //新闻资讯
        tools.doGet(biddingList, {
            deptId:100,
            pageNum:module.data.pageNum,
            pageSize:module.data.pageSize,
        }, module.biddingList,true);

        //标的物类型
        tools.doGet(webDeptType+"/project_type", {}, module.deptType,true);

        //标的物所在地
        tools.doGet(webDept, {deptId:''}, module.deptLocation, true);

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

        $("#signStartTimeStr").datetime({
            type: "date",
            value: [new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate()],
            success: function (res) {
                console.log(res)
            }
        })

        $("#signEndTimeStr").datetime({
            type: "date",
            value: [new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate()],
            success: function (res) {
                console.log(res)
            }
        })

        $("#auctionStartTimeStr").datetime({
            type: "date",
            value: [new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate()],
            success: function (res) {
                console.log(res)
            }
        })

        $("#endTimeStr").datetime({
            type: "date",
            value: [new Date().getFullYear(),new Date().getMonth()+1,new Date().getDate()],
            success: function (res) {
                console.log(res)
            }
        })

        setTimeout(function(){
            //主题图片切换
            module.switchTheme();
        },500)
    };

    //焦点图数据
    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.switchTheme = function(){
        if(themeColor == 'red'){
            document.getElementById('bannerBottom').src = '../../static/images/bannerBottom_red.jpg'
        }else if(themeColor == 'green'){
            document.getElementById('bannerBottom').src = '../../static/images/bannerBottom.jpg'
        }
    }
    //交易项目列表
    module.biddingList = function (data) {
        if (data.code == 200) {
            console.log(data)
            var content = data.rows;
            var pageCount = (data.total/module.data.pageSize).toFixed(0);
            if (pageCount < 1){
                pageCount = 1;
            }
            if(module.data.biddingInformationList == ''){
                // 初始化 分页器
                var page_s1=createPage('.page_s1');
                // 设置分页
                setPage(page_s1,{
                    pageTotal: data.total, // 数据总条数
                    pageSize: module.data.pageSize,    // 每页显示条数
                    pageCurrent: 1,  //  当前页
                    maxBtnNum: 5,   // 最多按钮个数  （最少5个）
                })
                $('#page_s1').html('共'+pageCount+'页')
            }
            module.data.pageCount = pageCount;
            for(var i = 0 ; i < content.length ; i++){
                if(content[i].biddingStopTime == null || content[i].biddingStartTime == null){continue;}
                var endTimes=content[i].biddingStopTime.substring(0,10).split('-');
                var biddingStopTime=endTimes[1]+'/'+endTimes[2]+'/'+endTimes[0]+' '+content[i].biddingStopTime.substring(10,19);

                var startTimes=content[i].biddingStartTime.substring(0,10).split('-');
                var biddingStartTime=startTimes[1]+'/'+startTimes[2]+'/'+startTimes[0]+' '+content[i].biddingStartTime.substring(10,19);

                var nowTime = Date.parse(new Date());
                var leftTime = Date.parse(biddingStopTime)-nowTime;
                var rightTime = Date.parse(biddingStartTime)-nowTime;
                console.log(biddingStopTime)
                console.log(leftTime)

                if(leftTime>0){
                    console.log('进行中')
                    content[i].timeType = '进行中'
                }
                if(leftTime<0){
                    console.log('已结束')
                    content[i].timeType = '已结束'
                }
                if(rightTime>0){
                    console.log('未开始')
                    content[i].timeType = '未开始'
                }
            }
            module.data.biddingInformationList = content;
            var biddingInformationData = template('biddingInformationData', module.data);
            $("#biddingInformationContent").html(biddingInformationData);
        }
    }

    //标的物类型
    module.deptType = function (data) {
        console.log(data)
        if (data.code == 200) {
            var content = data.data ;
            module.data.deptTypeList = content;
            var deptTypeData = template('deptTypeData', module.data);
            $("#deptTypeContent").html(deptTypeData);
        }
    }

    //标的物所在地
    module.deptLocation = function (data) {
        if (data.code == 200) {
            var content = data.data ;
            module.data.deptLocationList = content;
            var deptLocationData = template('deptLocationData', module.data);
            $("#deptLocationContent").html(deptLocationData);
        }
    }

    //标的物所在地(二级)
    module.deptSecondLocation = function (data) {
        if ( data.data.length == 0){
            document.getElementById('secondDept').style.display = 'none';
            return;
        }else{
            document.getElementById('secondDept').style.display = 'block';
        }
        if (data.code == 200) {
            console.log(data)
            var content = data.data;
            module.data.deptSecondLocationList = content;
            var deptSecondLocationData = template('deptSecondLocationData', module.data);
            $("#secondDept").html(deptSecondLocationData);
            console.log(module.data.deptId)
        }
    }

    tabCheck = function (deptId) {
        module.data.deptId = deptId;

        $('#locInfoall').attr("class","auction_hall_table_tab auction_loc");
        $("#deptLocationContent div").attr("class","auction_hall_table_tab auction_loc");
        if(deptId == ''){
            $('#locInfoall').addClass('auction_hall_table_tab_select');
            document.getElementById('secondDept').style.display = 'none';
        }else{
            $('#locInfoall'+deptId).addClass('auction_hall_table_tab_select');
            tools.doGet(webDept, {deptId:deptId}, module.deptSecondLocation, true);
        }
    }

    secondCheck = function(id,secondDeptId){
        $('#locInfoSecondall').attr("class","auction_hall_table_tab auction_loc");
        $("#secondDept div").attr("class","auction_hall_table_tab auction_loc");
        if(id == '' && secondDeptId == ''){
            $('#locInfoSecondall').addClass('auction_hall_table_tab_select');
            module.data.deptId = $('#deptLocationContent div.auction_hall_table_tab_select').attr("data");
        }else{
            $('#locInfoall'+id).addClass('auction_hall_table_tab_select');
            module.data.deptId = secondDeptId;
        }
    }

    allDept = function (){
        module.data.deptSize = 30
        document.getElementById('allDept').innerHTML = "<i class='sanjiao-top'></i>";
        document.getElementById('allDept').onclick = upDept;
        tools.doGet(webDept + '/0', {}, module.deptLocation, true);
    }

    turnThePage = function (pageNum) {
        module.data.pageNum = pageNum ;
        tools.doGet(biddingList, {deptId:100,pageNum:module.data.pageNum,pageSize:module.data.pageSize}, module.biddingList,true);
    }

    typeCheck = function(id,projectNumber){
        if(projectNumber==undefined){
            // projectNumber为undefined时,置空(直接传递给后台接口导致搜索问题)
            projectNumber = "";
        }
        module.data.projectNumber = projectNumber;//标的物类型ID
        document.getElementById('standardTypeAll').className = 'auction_hall_table_tab';
        if(id == ''){
            id = 'All';
        }
        if(projectNumber == undefined){
            module.data.projectNumber = '';
        }
        for (var i = 0 ; i < module.data.deptTypeList.length ; i++){
            document.getElementById('standardType'+(i+1)).className = 'auction_hall_table_tab';
        }
        document.getElementById('standardType'+id).className = 'auction_hall_table_tab auction_hall_table_tab_select';
    }

    selectedStatus = function(id,projectShowStatus){
        module.data.projectShowStatus = projectShowStatus;
        document.getElementById('statusall').className = 'auction_hall_table_tab'
        for (var i = 0 ; i < 5 ; i++){
            document.getElementById('status'+(1+i)).className = 'auction_hall_table_tab'
        }
        document.getElementById('status'+id).className = 'auction_hall_table_tab auction_hall_table_tab_select'
    }
    //底部友情链接
    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);
        }
    }

    goSearch = function(){
        module.data.itemsInformationList = '';
        module.data.biddingInformationList = [];
        $('.page_s1').html('')
        $('#page_s1').html('')
        module.data.form.deptId = module.data.deptId;
        module.data.form.projectNumber = module.data.projectNumber;
        module.data.form.projectShowStatus= module.data.projectShowStatus;

        // module.data.form.signupStartTime= $('#signStartTimeStr').val();
        // module.data.form.signupStopTime= $('#signEndTimeStr').val();
        // module.data.form.biddingStartTime= $('#auctionStartTimeStr').val();
        // module.data.form.biddingStopTime= $('#endTimeStr').val();
        module.data.form.pageNum= module.data.pageNum;
        module.data.form.pageSize= module.data.pageSize;

        if (module.data.form.signupStartTime){
            delete module.data.form.signupStartTime;
        }
        if (module.data.form.signupStopTime){
            delete module.data.form.signupStopTime;
        }
        if (module.data.form.biddingStartTime){
            delete module.data.form.biddingStartTime;
        }
        if (module.data.form.biddingStopTime){
            delete module.data.form.biddingStopTime;
        }

        if ($('#signStartTimeStr').val()!=''){
            module.data.form.signupStartTime = $('#signStartTimeStr').val();
        }
        if ($('#signEndTimeStr').val()!=''){
            module.data.form.signupStopTime = $('#signEndTimeStr').val();
        }
        if ($('#auctionStartTimeStr').val()!=''){
            module.data.form.biddingStartTime = $('#auctionStartTimeStr').val();
        }
        if ($('#endTimeStr').val()!=''){
            module.data.form.biddingStopTime = $('#endTimeStr').val();
        }

        console.log(module.data.form)

        tools.doGet(biddingList, module.data.form, module.biddingList, true);
    }
    goDetail = function (id) {
        tools.skip('../listingItems/itemsDetail.html?id='+id);
    }
    return module;
});
