(function(){
    X.pageLogic['adminkanban'] = {
        init : function(){
            var me = this;
            // 弹窗占满整平
            $(".adminmykanban-box").css('height',window.innerHeight+'px');
            me.updata(X.DATA['projectid']);
            
            $(".adminmykanban-top-fh").click(function(){
                layer.closeAll();
            });

            $(document).off('click','.adminmykanban-sousuo-box .cha').on('click','.adminmykanban-sousuo-box .cha',function(){
                $(".adminmykanban-sousuo-box .adminmykanban-txt").val("");
                $(".adminmykanban-sousuo-box .cha").hide();
                me.updata(X.DATA['projectid']);
            });

            // 全选按钮
            $(document).off('click','#adminmykanban-quanxuan').on('click','#adminmykanban-quanxuan',function(){
                var KanBanData =  X.DATA['sxKanBanData'];
                var ProjectData =  X.DATA['sxProjectData'];
                if($(this).prop('checked')){
                    for(let i in KanBanData){
                        KanBanData[i]['checked'] = true;
                    }
                    for(let i in ProjectData){
                        ProjectData[i]['checked'] = true;
                    }
                }else {
                    for(let i in KanBanData){
                        KanBanData[i]['checked'] = false;
                    }
                    for(let i in ProjectData){
                        ProjectData[i]['checked'] = false;
                    }
                }
                X.treemenu("#adminmykanban-con-mykanban",KanBanData,'mykanban');
                X.treemenu("#adminmykanban-con-kongjian",ProjectData,'kongjian');
            });

            // 搜索
            $('.adminmykanban-sousuo-box .adminmykanban-txt').bind('input propertychange', function() {
                var txt = $(this).val();
                var KanBanData =  X.DATA['sxKanBanData'];
                var ProjectData =  X.DATA['sxProjectData'];
                if(txt != ""){
                    var ssKanBanData = [];
                    var ssProjectData = [];
                    for(let i in KanBanData){
                        if(KanBanData[i]['title'].indexOf(txt) != -1){
                            ssKanBanData.push(KanBanData[i]);
                        }else {
                            var arr = []
                            for(let j in KanBanData[i]['children']){
                                
                                if(KanBanData[i]['children'][j]['title'].indexOf(txt) != -1 ){
                                    // KanBanData[i]["children"].push(KanBanData[i]['children'][j]);
                                    arr.push(KanBanData[i]['children'][j]);
                                }
                            }
                            if(arr.length != 0){
                                var data = KanBanData[i];
                                data['children'] = arr
                                ssKanBanData.push(data);
                            }
                            
                        }
                    }

                    for(let i in ProjectData){
                        if(ProjectData[i]['title'].indexOf(txt) != -1){
                            ssProjectData.push(ProjectData[i])
                        }else {
                            if(ProjectData[i]['children'] != undefined){
                                var twoarr = [];
                                var trenarr = [];
                                for(let j in ProjectData[i]['children']){
                                    if(ProjectData[i]['children'][j]['title'].indexOf(txt) != -1){
                                        twoarr.push(ProjectData[i]['children'][j]);
                                        
                                        if(ssProjectData.length == 0){
                                            var data = ProjectData[i];
                                            data['children'] = twoarr;
                                            ssProjectData.push(data);
                                        }else {
                                            var data = twoarr;
                                            ssProjectData[i]['children'].push(data);
                                        }
                                    }else {
                                        if(ProjectData[i]['children'][j]['children'] != undefined){
                                            for(let z in ProjectData[i]['children'][j]['children']){
                                                if(ProjectData[i]['children'][j]['children'][z]['title'].indexOf(txt) != -1){
                                                    trenarr.push(ProjectData[i]['children'][j]['children'][z]);
                                                    // var dataarr = ProjectData[i]['children'][j];
                                                    // if(ssProjectData.length == 0){
                                                        
                                                    //     var arr = JSON.parse(JSON.stringify(ProjectData[i]));
                                                    //     var towarr = JSON.parse(JSON.stringify(ProjectData[i]['children'][j]));
                                                    //     var dataarr = [];
                                                    //     dataarr.push(towarr);
                                                    //     arr['children'] = [];
                                                    //     arr['children']=dataarr[0]
                                                    //     towarr['children'] = trenarr;
                                                    //     ssProjectData.push(arr);
                                                    //     console.log(ssProjectData)
                                                    // }else {
                                                    //     var towarr = arr['children'][j];
                                                    //     towarr['children'].push(trenarr);
                                                    //     ssProjectData[i]['children'].push(towarr);
                                                    // }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    X.treemenu("#adminmykanban-con-mykanban",ssKanBanData,'mykanban');
                    X.treemenu("#adminmykanban-con-kongjian",ssProjectData,'kongjian');
                    $(".adminmykanban-sousuo-box .cha").show();
                }else {
                    $(".adminmykanban-sousuo-box .cha").hide();
                    me.updata(X.DATA['projectid']);
                }
            });

            function cloneObjectFn(obj){ // 对象复制
                return JSON.parse(JSON.stringify(obj))
            };

            //移动至
            $(document).off('click','.adminmykanban-operation-but-yidong').on('click','.adminmykanban-operation-but-yidong',function(){
                var mykanban = layui.tree.getChecked('mykanban');
                var kongjian = layui.tree.getChecked('kongjian');
                console.log(kongjian);
                if(mykanban.length < 1 && kongjian.length < 1){
                        alert('请先选择需要移动的看板!!!');
                        return;
                }
                var data = {
                    'mykanban':selectkanbanidarr(mykanban),
                    'kongjian':selectkongjianidarr(kongjian)
                }
                var source_id = data['mykanban'].concat(data['kongjian']);
                X.parametersopen(source_id,'yidong-pop','auto',function(){
                    me.updata(X.DATA['projectid']);
                })
            });

            //删除
            $(document).off('click','.adminmykanban-operation-but-del').on('click','.adminmykanban-operation-but-del',function(){
                var mykanban = layui.tree.getChecked('mykanban');
                var kongjian = layui.tree.getChecked('kongjian');
                var kanbanid = selectkanbanidarr(mykanban);
                var kongjianid = selectkongjianidarr(kongjian);
                var data= kanbanid.concat(kongjianid);
                if(data.length == 0 ){
                    alert('请先选择要删除的看板!!!');
                    return;
                }

                $(".layui-layer").css('background-color','#fff !important');
                layer.confirm('确认删除选中的看板吗？该操作不可恢复', {
                    btn: ['取消','删除 '] //按钮
                  }, function(){
                    //取消
                    layer.close(layer.index);
                  }, function(){
                    //删除
                    X.api("dashboard/delete",'post',{ids:data},function(d){
                        layer.msg('删除成功');
                        layer.close(layer.index);
                        X.pageLogic.dashboard.freshMenu(X.DATA.projectid);//刷新侧边栏
                        me.updata(X.DATA.projectid);//刷新数据
                    })
                });
                
            });


            // 复制
            $(document).off('click','.adminmykanban-operation-but-copy').on('click','.adminmykanban-operation-but-copy',function(){
                var mykanban = layui.tree.getChecked('mykanban');
                var kongjian = layui.tree.getChecked('kongjian');
                var kanbanid = selectkanbanidarr(mykanban);
                var kongjianid = selectkongjianidarr(kongjian);
                var data= kanbanid.concat(kongjianid);
                if(data.length < 1){
                    alert('请先选择需要复制的看板!!!');
                    return;
                }
                X.parametersopen(data,'copykanban','auto',function(){
                    // me.updata(X.DATA['projectid']);
                })
            })

            function selectkanbanidarr(mykanban){
                var kanbanid =[];
                for(let i in mykanban){
                    if(mykanban[i]['children'].length > 0){
                        for(let j in mykanban[i]['children']){
                            kanbanid.push(mykanban[i]['children'][j]['id']);
                        }
                    }else {
                        kanbanid.push(mykanban[i]['id']);
                    }
                    
                }
                return kanbanid
            };

            function selectkongjianidarr(kongjian){
                var kongjianid = [];
                for(let i in kongjian){
                    if(kongjian[i]['children'].length > 0){
                        for(let j in kongjian[i]['children']){
                            
                            // if(kongjian[i]['children'][j]['children'].length > 0){
                            //     for(let z in kongjian[i]['children'][j]['children']){
                            //         kongjianid.push(kongjian[i]['children'][j]['children'][z]['id'])
                            //     }
                            // }else {
                                kongjianid.push(kongjian[i]['children'][j]['id']);
                            // }
                        }
                    }else {
                        kongjianid.push(kongjian[i]['id']);
                    }
                }
                return kongjianid
            }
            
        },

        updata:function(id){
            X.api("project/kanban","post",{"_id":id},function(d){
                console.log(d);
                var kanban = [];
                for(let i in d.kanban){
                    var arr = {};    
                    arr['title'] = d.kanban[i]['name'];
                    arr['id'] = d.kanban[i]['_id'];
                    if(d.kanban[i]['children'].length > 0){
                        var kanbanchidren = [];
                        for(let j in d.kanban[i]['children']){
                            var drr ={};
                            drr['title'] = d.kanban[i]['children'][j]['name'];
                            drr['id'] = d.kanban[i]['children'][j]['_id'];
                            kanbanchidren.push(drr);
                        }
                        arr['children'] = kanbanchidren;
                    }else {
                        arr['children'] = d.kanban[i]['children'];
                    }
                    kanban.push(arr);
                }
                var spaces = [];
                for(let i in d.spaces){
                    var arr = {};    
                    arr['title'] = d.spaces[i]['name'];
                    arr['id'] = d.spaces[i]['_id'];
                    if(d.spaces[i]['children'].length > 0){
                        var spaceschidren = [];
                        for(let j in d.spaces[i]['children']){
                            var drr ={};
                            drr['title'] = d.spaces[i]['children'][j]['name'];
                            drr['id'] = d.spaces[i]['children'][j]['_id'];
                            if(d.spaces[i]['children'][j]['isFolder']){
                                if(d.spaces[i]['children'][j]['children'].length > 0){
                                    var spacestwochidren = [];
                                    for(let z in d.spaces[i]['children'][j]['children']){
                                        var crr ={};
                                        crr['title'] = d.spaces[i]['children'][j]['children'][z]['name'];
                                        crr['id'] = d.spaces[i]['children'][j]['children'][z]['_id'];
                                        spacestwochidren.push(crr);
                                    }
                                    drr['children'] = spacestwochidren;
                                }else {
                                    drr['children'] = d.spaces[i]['children'][j]['children'];
                                }
                            }
                            spaceschidren.push(drr);
                        }
                        arr['children'] = spaceschidren;
                    }else {
                        arr['children'] = d.spaces[i]['children'];
                    }
                    spaces.push(arr);
                }

                // 渲染数据
                //树形看板
                X.DATA['sxKanBanData'] = kanban;
                X.DATA['sxProjectData'] = spaces;
                
                X.treemenu("#adminmykanban-con-mykanban",kanban,'mykanban');
                X.treemenu("#adminmykanban-con-kongjian",spaces,'kongjian');
            });
        }
    };
})();
