//cc
(function(){
    X.pageLogic['dashboard'] = {
        init :function(){
            var me = this;
            
            var popid='';

            layui.element.init();

            // 项目列表
            // http://10.0.0.7:6789/
            X.api("project/","get",{},function(d){
                X.laytpldata('#layui-project-dot',d,'.layui-project-box');
                X.DATA['projectarr'] = d;
                X.DATA['projectid'] = d[0]["_id"];
                X.DATA["game"] = d[0]["game"];
                me.freshMenu(d[0]["_id"]);
                // X.template("toptab","analysis-zhanwei",d);
            });

            

            // $(".username").html(X.DATA.userinfo.name);

            // 选择项目
            $(document).off('click','.selectproject').on('click','.selectproject',function(){
                var id = $(this).attr("data-id");
                var game = $(this).attr("data-game");
                $(".projecttit").html($(this).html());
                X.DATA['projectid'] = id;
                X.DATA["game"] = game;
                //侧边栏数据渲染
                me.freshMenu(id);
                // X.pageLogic.dashboard.freshMenu(id);
            });

            //a标签点击跳转事件
            $(document).on('click','.layui-nav-child a',function(){
                var url = $(this).attr("data-url");
                X.gourl(url);
            });

            // 修改面板名
            $(document).on('click','.layui-left-dd',function(){
                $(".tab-tit-box .tab-tit").html($(this).find("span").html());
                X.DATA['dashboard_id'] = $(this).attr('data-id'); //看板id
                $("#chart-box").html("");
                // 获取报表数据
                me.gettabledata();
            });

            // 渲染添加按钮侧边栏下拉框
            X.laydropdown("#chuangjian",X.DATA.createsidebar,function(d){
                if(d.url){
                    X.open({
                        type: 1,
                        title: false,
                        closeBtn: 0,
                        shadeClose: true,
                        url: d.url
                    })
                }
              this.elem.val(d.title);
            });

            // 时间控件渲染
            X.daterender(".tab-right-box .time");

            // X.echartsdata("#charts");

            //更新时间按钮
            $(".updatetime").click(function(){
                X.updatatime(".updatetime");
            });

            //管理我创建的看板
            $(document).off('click','.sousuo-box .kbglkanbanbtn').on("click",".sousuo-box .kbglkanbanbtn",function(){
                X.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    offset: 'lt',
                    anim: 5,
                    skin: 'yourclass',
                    url: 'adminkanban'
                })
            });

            //渲染时区下拉框
            layui.dropdown.render({
                elem: ".tab-tit-box .tab-shiqvshezhi"
                ,data: X.DATA.timezone
                ,click: function(obj){
                    console.log(obj)
                    $(".tab-tit-box .tab-shiqvshezhi").html(obj.title);

                    //数据更新
                //   this.elem.val(obj.title);
                }
            });

            //筛选
            $(document).off('click','.tab-right-box .tjsx').on("click",".tab-right-box .tjsx",function(){
                X.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shade:0,
                    shadeClose: false,
                    offset: [120,270],
                    url: "screen",
                    extData : {}
                })
            })

            // 侧边栏搜索
            $('.sousuo-txt').bind('input propertychange', function() {
                var tit = $(this).val()
                var KanBanData = X.DATA['KanBanData'];
                var ProjectData = X.DATA['ProjectData'];
                if(tit != ''){
                    var ssKanBanData = [],
                        ssProjectData = []
                    for(let i in KanBanData){
                        if(KanBanData[i]['title'].indexOf(tit) != -1){
                            ssKanBanData.push(KanBanData[i]);
                        }else {
                            for(let j in KanBanData[i]['children']){
                                if(KanBanData[i]['children'][j]['title'].indexOf(tit) != -1 ){
                                    var arr = KanBanData[i];
                                    arr['children'].push(KanBanData[i]['children'][j]);
                                    ssKanBanData.push(arr);
                                }
                            }
                        }
                    }
                    for(let i in ProjectData){
                        for(let i in ProjectData){
                            if(ProjectData[i]['children'].length > 0){
                                if(ProjectData[i]['title'].indexOf(tit) != -1){
                                    ssProjectData.push(ProjectData[i]);
                                }else {
                                    var towProjectData = ProjectData[i]['children'];
                                    for(let j in towProjectData){
                                        if(towProjectData[j]['children'].length > 0){
                                            if(towProjectData[j]['children'].length > 0){
                                                var threProjectData = ProjectData[i]['children'][j]['children'];
                                                for(let z in threProjectData){
                                                    if(threProjectData[z]['title'].indexOf(tit) != -1){
                                                        var arr = ProjectData[i];
                                                        arr['children'] = towProjectData[j];
                                                        arr['children']['children'] = threProjectData[z];
                                                        // arr['children'].push( threProjectData[z] )
                                                        ssProjectData.push(arr);
                                                    }
                                                }
                                            }
                                        }else {
                                            if(towProjectData[j]['title'].indexOf(tit) != -1 ){
                                                var arr = ProjectData[i];
                                                arr['children'].push(towProjectData[j]);
                                                ssProjectData.push(arr);
                                            }
                                        }
    
                                    }
                                }
                            }else {
                                if(ProjectData[i]['title'].indexOf(tit) != -1){
                                    ssProjectData.push(ProjectData[i]);
                                }
                            }
                            
                        }
                    }
                    X.laytpldata('#mykanban-dot',ssKanBanData,'#mykanban');
                    X.laytpldata('#kongjian-dot',ssProjectData,'#kongjian');

                }else {
                    me.freshMenu(X.DATA['projectid']);
                }

            });
    
            // 用户搜索
            $("#search").click(function(){
                X.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    skin: 'yourclass',
                    url: 'search'
                  })
            });
            
            $(document).off('click','.tab-right-box .shezhi').on("click",".tab-right-box .shezhi",function(){
                X.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true,
                    offset: 'rt',
                    anim: 5,
                    skin: 'yourclass',
                    url: 'kanbanshezhi'
                })
            });
            // 保存报表列表
            $(document).off('click','.baobiao').on('click','.baobiao',function(){
                X.parametersopen({},'kanbanadmin','rt',function(){
                    me.gettabledata();
                })
            });
    
            //侧边栏更多控制按钮
            // taboperation = function(e,type,id,name){
            //     console.log(X.DATA.taboperationdata[type])
            //     X.laydropdown('.kanbanwenjianimg',X.DATA.taboperationdata[type]);
            // }

         

            
        },

        // X.pageLogic.dashboard.freshMenu();
        freshMenu : function(id){
            //获取对应侧边栏
            X.api('project/kanban',"post",{"_id":id},function (d){
                X.DATA['KanBanData'] = d.kanban;
                X.DATA['ProjectData'] = d.spaces;
                X.laytpldata('#mykanban-dot',d.kanban,'#mykanban');
                X.laytpldata('#kongjian-dot',d.spaces,'#kongjian');
            });
        },

         // 获取表格数据
        gettabledata:function(){
            
            X.api('dashboard/','post',{id:X.DATA['dashboard_id']},function(d){
                var eventdata=[];
                var index = d.length;
                for(let i in d){
                    X.api("ck/"+d[i]['model']+"_model","post",{report_id:d[i]['report_id']},function(val){
                        // console.log(val);
                        if(d[i]['model'] == "event"){
                            // 事件分析
                            if(d[i]['graph_size'] == 'small'){
                                X.eventsmall(val,function(data){//表格数据处理
                                    data['name'] = d[i]['name'];
                                    data['modeltype'] = d[i]['graph_type'];
                                    data['id'] = d[i]['report_id'];
                                    data['modelsize'] = d[i]['graph_size'];
                                    eventdata.push(data);
                                })
                            }else {
                                if(d[i]['graph_type'] == 'table'){
                                    //表格样式
                                    X.eventtable(val,function(data){//表格数据处理
                                        data['name'] = d[i]['name'];
                                        data['modeltype'] = d[i]['graph_type'];
                                        data['id'] = d[i]['report_id'];
                                        data['modelsize'] = d[i]['graph_size'];
                                        eventdata.push(data);
                                    })
                                }else if(d[i]['graph_type'] == 'echarts'){
                                    //图形
                                    X.eventchart(val,function(data){
                                        data['name'] = d[i]['name'];
                                        data['modeltype'] = d[i]['graph_type'];
                                        data['id'] = d[i]['report_id'];
                                        data['modelsize'] = d[i]['graph_size'];
                                        eventdata.push(data);
                                    })
                                }
                            }

                        }
                        index --;
                        console.log(eventdata);
                        if(index <= 0){
                            X.template("modeltable","chart-box",eventdata,function(){
                                for(let i in eventdata){
                                    if(eventdata[i]['modeltype'] == 'echarts' && eventdata[i]['modelsize'] != 'small' ){
                                        var myChart = echarts.init(document.getElementById(eventdata[i]['id']));
                                        var option = {
                                            title: {
                                                text: ''
                                            },
                                            color: X.DATA.echartscolor,
                                            tooltip: {
                                                trigger: 'axis',
                                                axisPointer: {
                                                    type: 'cross',
                                                    label: {
                                                        backgroundColor: '#6a7985'
                                                    }
                                                }
                                            },
                                            legend: {
                                                type:'scroll',
                                                data: eventdata[i]['legenddata']
                                            },
                                            grid: {
                                                left: '2%',
                                                right: '2%',
                                                bottom: '2%',
                                                top: '10%',
                                                containLabel: true
                                            },
                                            xAxis: [
                                                {
                                                    type: 'category',
                                                    boundaryGap: false,
                                                    data: eventdata[i]['xAxisData']
                                                }
                                            ],
                                            yAxis: [
                                                {
                                                    type: 'value'
                                                }
                                            ],
                                            series: eventdata[i]['seriesData']
                                        };
                                        myChart.setOption(option,true);
                                    }
                                }
                            });
                        }
                    })
                    
                }
               
                
                
                
                

            })
        },

    }; 
})();
