let selectFBObject = { 
    spread:null,
    datas:[],
    setting:{
        header: [
            {headerName: "编号", headerWidth: 180, dataCode: "code", dataType: "String"},
            {headerName: "名称", headerWidth: 210, dataCode: "name", dataType: "String"},
            {headerName: "单位", headerWidth: 50, dataCode: "unit", dataType: "String", hAlign: "center"},
        ],
        view:{ lockColumns: ["name","code","unit"]}
    },
    initSpread:function(){
        if(!this.spread){
            this.spread = SheetDataHelper.createNewSpread($("#selectFBSheet")[0]);
            sheetCommonObj.spreadDefaultStyle(this.spread);
            let sheet = this.spread.getSheet(0);
            sheetCommonObj.initSheet(sheet,this.setting,0);
              /*   sheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onSelectionChange); */
            if(projectReadOnly){
                sheetCommonObj.disableSpread(this.spread);
            }
        }else{
            this.spread.refresh();
        }
        
    },
    getSelectedData:function(){
        let sel = this.spread.getSheet(0).getSelections()[0];
        if(sel && gljUtil.isDef(sel.row)){
            return this.datas[sel.row]
        }
        return null;
    },
    showData:function(){
        let me = selectFBObject;
        billsGuidance.initBillsLibs(()=>{
            let parentIDs = [];
            let node = me.getSelectedNode();
            if(node) parentIDs = me.getAllParentIDs(node);
            this.datas = [];
            for(let r of billsGuidance.bills.tree.roots){
                selectFBObject.setDatas(r,parentIDs);
            }
            sheetCommonObj.showTreeData(me.spread.getSheet(0), me.setting, me.datas);
            let row = me.getSelectedRow(node);
            me.spread.getSheet(0).setSelection(row,0,1,1);
            me.spread.getSheet(0).showRow(row, GC.Spread.Sheets.VerticalPosition.center);
        });
    },

    getSelectedRow:function(node){
        let row = 0;//要选中的行;   
        if(node){
            return _.findIndex(this.datas,{'ID':node.data.ID}) 
        }
        return row;
    },

    getSelectedNode:function(){
        let node = null;
        if($("#selectFBFor").val() == "replace") return node;
      //如果是添加分部，则应选中对应的节点
        /*1.当前定位在“分项”，则弹出“选择分部”窗口，并且默认定位在：根据分项编号的前9位查找清单规则中的父项。如果查找不到父项，则默认定位在第一行。 */
        let selected = projectObj.project.mainTree.selected;
        if(selected.data.type==billType.FX || selected.data.type==billType.BX){
            let code = selected.data.code?selected.data.code:'';
            if(code.length === 12){
                let matchCode =  code.substring(0,9);
                for(let i of billsGuidance.bills.tree.items){
                    if (i.data.code == matchCode) return i.parent;
                } 
            } 
        }
        return node;
    },
    getAllParentIDs:function(node){
        let list = [];
        getID(node,list);
        return list;
        function getID(node,l){
            if(node.parent){
                l.push(node.parent.data.ID); 
                getID(node.parent,l)
            }
        }


    },


    setDatas:function(node,parentIDs){
        if(node.children.length > 0){//过滤叶子节点
            let nodeData = node.data;
            let d = {
                ID:nodeData.ID,
                ParentID:nodeData.ParentID,
                code:nodeData.code,
                name:nodeData.name,
                unit:nodeData.unit
            }
            if(parentIDs.includes(d.ID)) d.collapsed = false
            this.datas.push(d);
            for(let c of node.children){
                this.setDatas(c,parentIDs)
            }
        }
    }
}
$(function () {

    $('#selectFBDiv').on('shown.bs.modal', function (e) {
        selectFBObject.initSpread();
        selectFBObject.showData();
        sheetCommonObj.refreshWorkbookDelDefer(selectFBObject.spread, 200);
    })
    $("#selectFBConfirm").click(async ()=>{
        let data = selectFBObject.getSelectedData();
        if(!data) return;
        let project = projectObj.project;
        let controller =  projectObj.mainController;
        let selected = project.mainTree.selected;
        if($("#selectFBFor").val() == "replace"){
            let datas = [{
                type:'bills',
                data:{
                    ID:selected.data.ID,
                    name:data.name,
                    code:data.code,
                    unit:data.unit
                }
              }]
              await project.syncUpdateNodesAndRefresh(datas);
        }else{
            let ext = {name:data.name,code:data.code};
            if(selected.data.type==billType.FX || selected.data.type==billType.BX){
                //添加成分项的父亲
                ProjectController.addFXParent(selected,ext);   
           }else{//正常添加分部
               ProjectController.addFB(project, controller,null,ext);
               projectObj.selectColAndFocus(project.mainTree.selected);
           }
        } 

 
     
    })
})