let compareObject = {
    setting:{
        header: [
            {headerName: "审定", headerWidth: 240, dataCode: "name", dataType: "String"},
            {headerName: "送审", headerWidth: 240, dataCode: "compare", dataType: "String"}
        ],
        view:{ lockColumns: ["name","compare"]}
    },
    initSpread:function(){
        if(!this.spread){
            this.spread = SheetDataHelper.createNewSpread($("#compareProjectSpread")[0]);
            sheetCommonObj.spreadDefaultStyle(this.spread);
            let sheet = this.spread.getSheet(0);
            sheetCommonObj.initSheet(sheet,this.setting,0);
        }else{
            this.spread.repaint();
        }
    },
    showDatas:function(){
        this.getCompareDatas();
        sheetCommonObj.showTreeData(this.spread.getSheet(0), this.setting,this.datas);
    },
    datas:[],
    getCompareDatas:function(){
        let datas = [];
        let me = compareObject;
        let compareID = projectObj.project.projectInfo.compareID;
        let rootProjectID = projectObj.project.property.rootProjectID;
        if(gljUtil.isNotEmpty(compareID)){
            let rootProject = _.find(me.projects,{'ID':rootProjectID});
            let compareProject = _.find(me.projects,{'ID':compareID});
            rootProject.compare = compareProject.name;
            rootProject.matchID = compareID; 
            rootProject.collapsed = false;
            datas.push(rootProject);

            setDatas(rootProjectID,compareID,datas);
        }
        this.datas = datas;
        function setDatas(nID,cID,mdatas){
            let nameMap = {};
            if(cID && me.parentMap[cID]){
                for(let c of me.parentMap[cID]){
                    nameMap[c.name] = c;
                }
            }
            if(me.parentMap[nID]){
                for(let n of me.parentMap[nID]){
                    if(nameMap[n.name]){//找到同名的项目时，匹配上
                       n.compare = n.name;
                       n.matchID = nameMap[n.name].ID             
                    }else{//找不到时清空
                        n.compare = '';
                        n.matchID = '';    
                    }
                    n.collapsed = false;
                    mdatas.push(n);
                    setDatas(n.ID,n.matchID,mdatas)
                }
            }
        }
    },
    initProjectDatas:async function(){
        let projects = await ajaxGet("/project/getCompareProjects");
        let parentMap=_.groupBy(projects, 'ParentID');
        for(let key in parentMap){
            parentMap[key] = sortTreeChildren(parentMap[key]);
        }
        compareObject.parentMap = parentMap;
        compareObject.projects = projects;
        let rootParjects = parentMap['-1'];
        let options = [];
        for(let r of rootParjects){
            [newPath,project] =  setProjectPath(r,parentMap,"");
            if(newPath && project)options.push({path:newPath,ID:project.ID})
        }   
        compareObject.options = options;
        let rootProject  = _.find(projects,{'ID':projectObj.project.property.rootProjectID});
        projectObj.project.projectInfo.compareID = rootProject.compareID;

        function setProjectPath(project,parentMap,namePath){
            let newPath = project.ParentID == -1?project.name:`${namePath}/${project.name}`;
            if(project.projType === commonConstants.projectType.Project) return [newPath,project];
            
            if(parentMap[project.ID]){
                for(let c of parentMap[project.ID]){
                   return setProjectPath(c,parentMap,newPath)
                }
            }
        }
    },
    getCompareID:async function(){
        if(!this.parentMap){
           await this.initProjectDatas();
        }
        this.getCompareDatas();
        for(let d of this.datas){
            if(d.ID == projectObj.project.ID()) return d.matchID;
        }
        return null;
    } 
}

$(document).ready(function () {
    $('#tab_compare').on('shown.bs.tab', async function () {
        await compareObject.initProjectDatas();
        let optionString = `<option value=""></option>`;
        for(let o of compareObject.options){
            if(o.ID === projectObj.project.property.rootProjectID) continue;
            optionString += `<option value="${o.ID}">${o.path}</option>`
        }
        $("#audiCompareID").html(optionString);
        $("#audiCompareID").val(projectObj.project.projectInfo.compareID);
        compareObject.initSpread();
        compareObject.showDatas();
    });
    $("#audiCompareID").change(async function(){
        //compareID 保存在建设项目上，为方便使用，获取后挂在projectInfo上
        let updateData = {type:ModuleNames.project,isInfo:true,data:{'ID' : projectObj.project.property.rootProjectID,compareID:parseInt(this.value)}};
        $.bootstrapLoading.start();
        await projectObj.project.syncUpdateNodesAndRefresh([updateData]);
        compareObject.showDatas();
        $.bootstrapLoading.end();
    })

})