(function(){
    X.pageLogic['kanbanadmin'] = {
        init : function(parms){
            var me = this;
            this.parms = parms;
            this.callback = parms.callback;
            var data = parms.extData;

            $('.kanbanadmin-box').css('height',window.innerHeight+'px');

            var reportlistdata;
            var toadddata = [];//已添加报表数据
            var submitdata = [];//提交数据

            // 更新看板
            kanbanupdata();
            function kanbanupdata(){
                X.api("report/read_report","post",{project_id: X['DATA']['projectid'],dashboard_id:X.DATA['dashboard_id']},function(d){
                    reportlistdata = d;
                    X.laytpldata('#to-be-added-baobiao-dot',d,'.to-be-added-baobiao');
                    X.laytpldata('#to-added-baobiao-dot',d,'.to-added-baobiao');
                    var datalist = [
                        {title:'全部',id:'0'},
                        {title:'事件',id:'event'},
                        {title:'留存',id:'retention'},
                        {title:'ltv',id:'ltv'},
                        {title:'漏斗',id:'funnel'},
                        {title:'属性',id:'0'},
                        {title:'分布',id:'scatter'},
                        {title:'SQL',id:'0'}
                    ]
    
                    X.laydropdown('#kanbanadmin-ss-right',datalist,function(d){
                        $('#kanbanadmin-ss-right span').html(d.title);
                        updatalist(d.id);
                    })
                    toadddataprocessing();
                })
            };

            function updatalist(title){
                console.log(title);
                var screendata=[];//筛选后的数据
                for(let i in reportlistdata){
                   if(title == 0){
                       screendata = reportlistdata;
                    }else {
                       if(reportlistdata[i]['cat'] == title){
                           screendata.push(reportlistdata[i]);
                       }
                    }
                }
                
                X.laytpldata('#to-be-added-baobiao-dot',screendata,'.to-be-added-baobiao');
            };

            // 搜索
            $(document).off('click','.kanbanadmin-ss input').on('blur','.kanbanadmin-ss input',function(){
                var screendata=[];//筛选后的数据
                for(let i in reportlistdata){
                    if($(this).val() != ""){
                        if(reportlistdata[i]['name'].indexOf($(this).val()) != -1){
                            screendata.push(reportlistdata[i]);
                        }
                    }else {
                        screendata = reportlistdata;
                    }
                }
                X.laytpldata("#to-be-added-baobiao-dot",screendata,".to-be-added-baobiao");
            });
            
            //添加报表
            $(document).off('click','.kanbanadd').on('click','.kanbanadd',function(){
                var index = $(this).attr('data-index');
                reportlistdata[index]['added'] = true;
                X.laytpldata('#to-be-added-baobiao-dot',reportlistdata,'.to-be-added-baobiao');
                X.laytpldata('#to-added-baobiao-dot',reportlistdata,'.to-added-baobiao');
                toadddataprocessing();
            });

            // 修改报表类型
            // $(document).off('click','.to-added-table').on('click','.to-added-table',function(){
            //     var obj = $(this);
            //     var id = obj.attr("data-id");
            //     for(let i in submitdata){
            //         if(submitdata[i]['report_id'] == id){
            //             submitdata[i]['graph_type'] = 'table';
            //             layer.msg('修改为表格展示',{time: 1000});
            //         }
            //     }
            // });
            
            // $(document).off('click','.to-added-chart').on('click','.to-added-chart',function(){
            //     var obj = $(this);
            //     var id = obj.attr("data-id");
            //     console.log(id);
            //     for(let i in submitdata){
            //         if(submitdata[i]['report_id'] == id){
            //             submitdata[i]['graph_type'] = 'echarts';
            //             layer.msg('修改为图表展示',{time: 1000});
            //         }
            //     }
            // });

            $(document).off('click','.kanbanadmin-menu').on('click','.kanbanadmin-menu',function(){
                var obj = $(this);
                var id = obj.attr("data-id");
                var configdata = [];
                for(let i in submitdata){
                    if(submitdata[i]['report_id'] == id){
                        configdata = submitdata[i];
                    }
                }
                X.parametersopen(configdata,'kanbantypeset','auto',function(d){
                    for(let i in submitdata){
                        if(submitdata[i]['report_id'] == id){
                            submitdata[i]['graph_type'] = d['graph_type'];
                            submitdata[i]['graph_size'] = d['graph_size'];
                            submitdata[i]['sort'] = d['sort'];
                        }
                    }
                })
            });
            
            //渲染已添加报表
            function toadddataprocessing(){ 
                toadddata = [];
                for(let i in reportlistdata){
                    if(reportlistdata[i]['added'] == true){
                        toadddata.push(reportlistdata[i]);
                    }
                };
                submitdata = [];
                console.log(toadddata);
                for(let i in toadddata){
                    var arr = {
                        report_id:toadddata[i]['_id'],
                        graph_type: toadddata[i]['show_config']['graph_type'] || 'table',
                        model:toadddata[i]['cat'],
                        name:toadddata[i]['name'],
                        graph_size:toadddata[i]['show_config']['graph_size'] || 'in',
                        sort: toadddata[i]['show_config']['sort']
                    };
                    submitdata.push(arr);
                }
                submitdata.sort( X.compare("sort") );
                console.log(submitdata);
            };

            $(document).off('click','.to-added-del').on('click','.to-added-del',function(){
                var id = $(this).attr('data-id');
                X.api('dashboard/del_report','post',{_id:X['DATA']['dashboard_id'],report_id:id},function(){
                    kanbanupdata();
                });
            });

            $(document).off('click','.kanbanadmin-but-qx').on('click','.kanbanadmin-but-qx',function(){
                layer.closeAll();
            });

            $(document).off('click','.kanbanadmin-but-yy').on('click','.kanbanadmin-but-yy',function(){
                X.api('dashboard/add_report','post',{_id:X['DATA']['dashboard_id'],report_ids:submitdata},function(d){
                    if(d == 'ok'){
                        layer.closeAll();
                        layer.msg('应用成功');
                        me.callback && me.callback();
                    }
                });
            });

        }
    };
})();