define(['jquery', "template", "Tools", 'LeftNav', 'dateTime'], function ($, template, Tools, LeftNav) {
    //数据存储
    var module = {
        data: {
            //服务器地址
            serverApi: '',
            dataList: {
                children: [],
                haschildren: true,
                isopen: false,
                level: "0",
                name: "",
                url: "#",
            },
        },
    };
    var tools = new Tools();

    module.init = function (page) {
        //新闻管理列表
        // tools.doGet(dictType + '/training_type', {}, module.dictType , true);

        // $("#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)
        //     }
        // })
        var tabs = $('.tab').find('li');
        var informationBox = $("#information_box > div");
        tabs.on('click', function (e) {

            e.preventDefault();//防止打开链接

            var index = $(this).data('index');


            tabs.removeClass('active');

            informationBox.css("display",'none');

            $(this).addClass('active');

            $('#'+index).css("display",'block')

        });
        tools.doGet(dictType + '/supply_demand_unit', {}, module.dictType , true);
        tools.doGet(productCategories , {}, module.projectList , true);
        tools.getWebConfig();
    };

    module.dictType = function (data) {
        module.data.unitOptions = data.data;
        tools.doGet(depositInformation, {}, module.recommendList, true);
        tools.doGet(supplyInformation, {}, module.supplyInformationList , true);
        tools.doGet(management, {}, module.managementList , true);
        tools.doGet(serviceList, {}, module.serviceList , true);
    }

    //类别
    module.projectList = function (data) {
        if (data.code == 200) {
            var content = data.data;
            console.log(content)
            module.data.projectList = content;
            var projectData = template('projectData', module.data);
            $("#projectContent").html(projectData);
            var managementProjectData = template('managementProjectData', module.data);
            $("#managementProjectContent").html(managementProjectData);
            var serviceProjectData = template('serviceProjectData', module.data);
            $("#serviceProjectContent").html(serviceProjectData);
            // content.forEach(res => {
            //     module.data.dataList.children.push(res);
            // })
            // leftNav(".left", module.data.dataList);
        }
    }

    //求购信息
    module.managementList = function (data) {
        if (data.code == 200) {
            var content = data.data;

            for ( var i = 0 ; i < content.length ; i++ ){

                content[i].unit = module.selectDictLabel(module.data.unitOptions,content[i].unit);

                content[i].supplyMasterMap = serverApi + ((content[i].supplyMasterMap || content[i].supplyMasterMap != '')?content[i].supplyMasterMap: 'null') ;

            }

            module.data.managementList = content;
            var managementData = template('managementData', module.data);
            $("#managementContent").html(managementData);
        }
    }

    //服务信息
    module.serviceList = function (data) {
        if (data.code == 200) {
            var content = data.data;

            for ( var i = 0 ; i < content.length ; i++ ){

                content[i].unit = module.selectDictLabel(module.data.unitOptions,content[i].unit);

                content[i].supplyMasterMap = serverApi + ((content[i].supplyMasterMap || content[i].supplyMasterMap != '')?content[i].supplyMasterMap: 'null') ;

            }

            module.data.serviceList = content;
            var serviceData = template('serviceData', module.data);
            $("#serviceContent").html(serviceData);
        }
    }

    //推荐列表
    module.recommendList = function (data) {
        if (data.code == 200) {
            var content = data.data;
            for ( var i = 0 ; i < content.length ; i++ ){

                content[i].unit = module.selectDictLabel(module.data.unitOptions,content[i].unit);

                content[i].supplyMasterMap = serverApi + ((content[i].supplyMasterMap || content[i].supplyMasterMap != '')?content[i].supplyMasterMap: 'null') ;

            }
            module.data.recommendList = content;
            var recommendData = template('recommendData', module.data);
            $("#recommendContent").html(recommendData);


            if (content.length<6){return;}
            $("#recommendContent").width(213*content.length*2);


            var speed = 5;
            var parents = $('#supplyBox2');
            var allParents = $('#supplyBox2,.left,.right');
            var ul = $('ul',parents);
            var li = $('li',ul);
            ul.append(li.slice(0,content.length).clone());
            var timer = setInterval(actionMarquee,speed);

            allParents.hover(function() {
                if (content.length>5){$(".jt").css("display","block");}
                clearInterval(timer);
            },function(){
                $(".jt").css("display","none");
                //离开继续调用
                timer=setInterval(actionMarquee,speed);
            });
            function actionMarquee(){
                console.log(parents.scrollLeft())
                console.log($("#recommendContent").width()/2)
                if(parents.scrollLeft() >= ($("#recommendContent").width()/2)){
                    parents.scrollLeft(0);
                }
                else
                {
                    parents.scrollLeft(parents.scrollLeft()+1);
                }
            }

            $('.left').click(function(){
                parents.scrollLeft(0);
            })

            $('.right').click(function(){
                parents.scrollLeft($("#recommendContent").width());
            })
        }
    }

    //供应信息
    module.supplyInformationList = function (data) {
        if (data.code == 200) {
            var content = data.data;

            console.log(serverApi)

            for ( var i = 0 ; i < content.length ; i++ ){

                // content[i].unit = module.selectDictLabel(module.data.unitOptions,content[i].unit);

                content[i].supplyMasterMap = serverApi + ((content[i].supplyMasterMap || content[i].supplyMasterMap != '')?content[i].supplyMasterMap: 'null') ;

            }

            module.data.supplyInformationList = content;
            var supplyInformationData = template('supplyInformationData', module.data);
            $("#supplyInformationContent").html(supplyInformationData);
        }
    }

    module.selectDictLabel = function (datas, value) {
        var actions = [];
        Object.keys(datas).some((key) => {
            if (datas[key].dictValue == ('' + value)) {
                actions.push(datas[key].dictLabel);
                return true;
            }
        })
        return actions.join('');
    }


    switchTab = function (type) {
        $("#"+type+"Btn").addClass("active").siblings().removeClass("active");
        $("#"+type+"").css('display','block').siblings().css('display','none');
        module.data.switchTab = type;
    }

    searchType = function (id) {
        console.log(id)
        $(".left .left_nav_name").removeClass("nav_open");
        tools.doPost(technologyConsulting, {techniqueType: id}, module.technologyConsultingList, true);
    }

    goSearch = function (type) {
        if (type == 'expert'){
            tools.doPost(expertDatabase, {name: $("#searchInput").val()}, module.expertDatabaseList, true);
        }
        if (type == 'article'){
            tools.doPost(articleLibrary, {headline: $("#articleSearchInput").val()}, module.articleLibraryList, true);
        }
        if (type == 'video'){
            tools.doPost(videoLibrary, {headline: $("#videoSearchInput").val()}, module.videoLibraryList, true);
        }
    }

    return module;
})
