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

    module.init = function (page) {

        tools.doGet(outProject + '/id/'+getQueryVariable('id'), {}, module.outProjectInformation,true);

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

        $("#nowDateNYR").html(new Date().getFullYear()+'.'+(new Date().getMonth()+1)+'.'+new Date().getDate())

        setInterval(function () {
            $("#nowDateSFM").html(new Date().toLocaleTimeString());
            $("#nowTime").html('服务器时间：'+new Date().getFullYear()+'.'+(new Date().getMonth()+1)+'.'+new Date().getDate()+' '+new Date().toLocaleTimeString());
        },1000)
        tools.doGet(outProject + '/id/'+getQueryVariable('id'), {}, module.outProjectInformation,true);

        if (getQueryVariable('type') == 'Y'){
            setInterval(function (){
                tools.doGet(biddinglist, {projectId:getQueryVariable('id')}, module.biddinglistInformation,true);
            },5000)
        }
    };

    //获取地址栏参数
    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);
    }

    //获取地址栏参数
    rechSelect = function (){
        tools.doGet(outProject + '/id/'+getQueryVariable('id'), {}, module.outProjectInformation,true);
    }

    //项目基本信息
    module.outProjectInformation = function (data) {
        if (data.code == 200) {
            var content = data.data;
            //倒计时
            var timer = setInterval(function(){
                module.dataCompare(content.signupStartTime,content.signupStopTime,content.biddingStartTime,content.biddingStopTime);
            },1000);
            module.data.outProjectInformationDetail = content;
            // var outProjectInformationData = template('outProjectInformationData', module.data);
            // $("#outProjectInformationContent").html(outProjectInformationData);
            //挂牌价格字典
            tools.doGet(Dictionaries+'/price_unit', {}, module.unitDictionaries,true);
            //竞价方式字典
            tools.doGet(Dictionaries+'/bidding_type', {}, module.biddingTypeDictionaries,true);
            //期满处理字典
            tools.doGet(Dictionaries+'/bidding_direct', {}, module.biddingDirectDictionaries,true);
            //交易项目竞价记录
            tools.doGet(biddinglist, {projectId:getQueryVariable('id')}, module.biddinglistInformation,true);

        }
    }

    //竞价记录
    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 biddingInformationData = template('biddingInformationData', module.data);
            $("#biddingInformationContent").html(biddingInformationData);

        }
    }

    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;
            // console.log(((Math.floor(leftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[0])
            $('#d0').html(((Math.floor(leftTime / 1000 / 60 / 60 / 24) + '').length < 1 ? '00' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : (Math.floor(leftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[0]);
            $('#d1').html(((Math.floor(leftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[1]);
            $('#d2').html(((Math.floor(leftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[2]);

            $('#h0').html(((Math.floor(leftTime / 1000 / 60 / 60 % 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 % 24) : Math.floor(leftTime / 1000 / 60 / 60 % 24) + '')[0]);
            $('#h1').html(((Math.floor(leftTime / 1000 / 60 / 60 % 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 % 24) : Math.floor(leftTime / 1000 / 60 / 60 % 24) + '')[1]);

            $('#m0').html(((Math.floor(leftTime / 1000 / 60 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 % 60) : Math.floor(leftTime / 1000 / 60 % 60) + '')[0]);
            $('#m1').html(((Math.floor(leftTime / 1000 / 60 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 % 60) : Math.floor(leftTime / 1000 / 60 % 60) + '')[1]);

            $('#s0').html(((Math.floor(leftTime / 1000 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 % 60) : Math.floor(leftTime / 1000 % 60) + '')[0]);
            $('#s1').html(((Math.floor(leftTime / 1000 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 % 60) : Math.floor(leftTime / 1000 % 60) + '')[1]);

            //tools.doGet(biddinglist, {projectId:getQueryVariable('id')}, module.biddinglistInformation,true);

            $('#countType').html('距竞价结束还有')
        }else if (nowTime>Date.parse(biddingStopTime)){
            //竞价结束
            $('#d0').html('0')
            $('#d1').html('0')
            $('#d2').html('0')

            $('#h0').html('0')
            $('#h1').html('0')

            $('#m0').html('0')
            $('#m1').html('0')

            $('#s0').html('0')
            $('#s1').html('0')
            $('#countType').html('竞价结束')
            module.data.biddingTimeType = 2;
        }else if (nowTime<Date.parse(biddingStartTime)){
            //竞价暂未开始
            $('#d0').html('0')
            $('#d1').html('0')
            $('#d2').html('0')

            $('#h0').html('0')
            $('#h1').html('0')

            $('#m0').html('0')
            $('#m1').html('0')

            $('#s0').html('0')
            $('#s1').html('0')
            $('#countType').html('暂未开始')
            module.data.biddingTimeType = 3;
        }

        if(Date.parse(signupStartTime)<nowTime&&nowTime<Date.parse(signupStopTime)){
            //正在报名
            module.data.signupTimeType = 1;
            $('#d0').html(((Math.floor(signDownLeftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[0]);
            $('#d1').html(((Math.floor(signDownLeftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[1]);
            $('#d2').html(((Math.floor(signDownLeftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[2]);

            $('#h0').html(((Math.floor(signDownLeftTime / 1000 / 60 / 60 % 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 % 24) : Math.floor(leftTime / 1000 / 60 / 60 % 24) + '')[0]);
            $('#h1').html(((Math.floor(signDownLeftTime / 1000 / 60 / 60 % 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 % 24) : Math.floor(leftTime / 1000 / 60 / 60 % 24) + '')[1]);

            $('#m0').html(((Math.floor(signDownLeftTime / 1000 / 60 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 % 60) : Math.floor(leftTime / 1000 / 60 % 60) + '')[0]);
            $('#m1').html(((Math.floor(signDownLeftTime / 1000 / 60 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 % 60) : Math.floor(leftTime / 1000 / 60 % 60) + '')[1]);

            $('#s0').html(((Math.floor(signDownLeftTime / 1000 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 % 60) : Math.floor(leftTime / 1000 % 60) + '')[0]);
            $('#s1').html(((Math.floor(signDownLeftTime / 1000 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 % 60) : Math.floor(leftTime / 1000 % 60) + '')[1]);

            $('#countType').html('距报名结束还有')
        }else if (nowTime>Date.parse(signupStopTime)){
            //报名结束
            module.data.signupTimeType = 2;
        }else if (nowTime<Date.parse(signupStartTime)){
            //报名暂未开始
            module.data.signupTimeType = 3;
            $('#d0').html(((Math.floor(signupLeftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[0]);
            $('#d1').html(((Math.floor(signupLeftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[1]);
            $('#d2').html(((Math.floor(signupLeftTime / 1000 / 60 / 60 / 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 / 24) : Math.floor(leftTime / 1000 / 60 / 60 / 24) + '')[2]);

            $('#h0').html(((Math.floor(signupLeftTime / 1000 / 60 / 60 % 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 % 24) : Math.floor(leftTime / 1000 / 60 / 60 % 24) + '')[0]);
            $('#h1').html(((Math.floor(signupLeftTime / 1000 / 60 / 60 % 24) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 / 60 % 24) : Math.floor(leftTime / 1000 / 60 / 60 % 24) + '')[1]);

            $('#m0').html(((Math.floor(signupLeftTime / 1000 / 60 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 % 60) : Math.floor(leftTime / 1000 / 60 % 60) + '')[0]);
            $('#m1').html(((Math.floor(signupLeftTime / 1000 / 60 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 / 60 % 60) : Math.floor(leftTime / 1000 / 60 % 60) + '')[1]);

            $('#s0').html(((Math.floor(signupLeftTime / 1000 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 % 60) : Math.floor(leftTime / 1000 % 60) + '')[0]);
            $('#s1').html(((Math.floor(signupLeftTime / 1000 % 60) + '').length < 2 ? '0' + Math.floor(leftTime / 1000 % 60) : Math.floor(leftTime / 1000 % 60) + '')[1]);

            $('#countType').html('距报名开始还有')
        }
        // if(module.data.outProjectInformationDetail.biddingType != '拍卖' && module.data.biddingTimeType != 2){
        //     document.getElementById('biddingTable').style.display = 'none';
        //     document.getElementById('biddingTitle').style.display = 'block';
        // }
    }

    //网站配置信息
    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.biddingDirectDictionaries = function (data) {
        if (data.code == 200 && module.data.outProjectInformationDetail.biddingDirect!='') {
            var content = data.data;
            var biddingDirect = content[(module.data.outProjectInformationDetail.biddingDirect-1)].dictLabel;
            module.data.outProjectInformationDetail.biddingDirect = biddingDirect;
            var outProjectInformationData = template('outProjectInformationData', module.data);
            $("#outProjectInformationContent").html(outProjectInformationData);
            var outProjectInformationData2 = template('outProjectInformationData2', module.data);
            $("#outProjectInformationContent2").html(outProjectInformationData2);
        }
    }

    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);
            var outProjectInformationData2 = template('outProjectInformationData2', module.data);
            $("#outProjectInformationContent2").html(outProjectInformationData2);
        }
    }

    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);
            var outProjectInformationData2 = template('outProjectInformationData2', module.data);
            $("#outProjectInformationContent2").html(outProjectInformationData2);
        }
    }

    return module;
});
