/**
 * Created by zhang on 2019/5/28.
 */

let indexObj= {
    nav_text:"工程信息指标",
    spread:null,
    engineerInfoSetting:{
        header: [
            {headerName: "名称", headerWidth: 160, dataCode: "dispName", dataType: "String"},
            {headerName: "内容", headerWidth: 200, dataCode: "value", dataType: "String"}
        ],
        view:{ lockColumns: ["dispName"]}
    },
    engineerFeatureSetting:{
        header: [
            {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String"},
            {headerName: "内容", headerWidth: 200, dataCode: "value", dataType: "String"}
        ],
        view:{ lockColumns: [0]}
    },
    engineerCostSetting:{
        header: [
            {headerName: "名称", headerWidth: 200, dataCode: "name", dataType: "String"},
            {headerName: "金额", headerWidth: 160, dataCode: "cost", dataType: "String", hAlign: "right"},
            {headerName: "单方造价", headerWidth: 160, dataCode: "unitCost", dataType: "String", hAlign: "right"},
            {headerName: "占造价比例", headerWidth: 160, dataCode: "per", dataType: "String", hAlign: "right"}
        ],
        view:{ lockColumns: ["name","cost","unitCost","per"]}
    },
    mainMaterialSetting:{
        header: [
            {headerName: "名称", headerWidth: 200, dataCode: "name", dataType: "String"},
            {headerName: "单位", headerWidth: 200, dataCode: "unit", dataType: "String"},
            {headerName: "综合单价", headerWidth: 160, dataCode: "unitPrice", dataType: "String", hAlign: "right"},
            {headerName: "数量", headerWidth: 160, dataCode: "quantity", dataType: "String", hAlign: "right"},
            {headerName: "单方指标", headerWidth: 160, dataCode: "unitIndex", dataType: "String", hAlign: "right"}
        ],
        view:{ lockColumns: ["name","unit","unitPrice","quantity","unitIndex"]}
    },
    economicSetting:{
        header: [
            {headerName: "分部分项", headerWidth: 200, dataCode: "name", dataType: "String"},
            {headerName: "分部工程造价", headerWidth: 160, dataCode: "cost", dataType: "String", hAlign: "right"},
            {headerName: "单方指标", headerWidth: 160, dataCode: "unitCost", dataType: "String", hAlign: "right"},
            {headerName: "占造价比例", headerWidth: 160, dataCode: "per", dataType: "String", hAlign: "right"}
        ],
        view:{ lockColumns: ["name","cost","unitCost","per"]}
    },
    quantitySetting:{
        header: [
            {headerName: "名称", headerWidth: 200, dataCode: "name", dataType: "String"},
            {headerName: "工程量指标", headerWidth: 160, dataCode: "quantity", dataType: "String", hAlign: "right"},
            {headerName: "单位", headerWidth: 160, dataCode: "quantityIndexUnit", dataType: "String", hAlign: "center"}
        ],
        view:{ lockColumns: ["name","quantity","quantityIndexUnit"]}
    },
    materialSetting:{
        header: [
            {headerName: "编码", headerWidth: 80, dataCode: "code", dataType: "String"},
            {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String"},
            {headerName: "规格型号", headerWidth: 120, dataCode: "specs", hAlign: "left", dataType: "String",cellType:'tipsCell'},
            {headerName: "单位", headerWidth: 45, dataCode: "unit", hAlign: "center", dataType: "String"},
            {headerName: "数量", headerWidth: 90, dataCode: "quantity", hAlign: "right", dataType: "Number",decimalField:'glj.quantity'},
            {headerName: "市场价", headerWidth: 70, dataCode: "marketPrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
            {headerName: "市场价合价", headerWidth: 120, dataCode: "totalPrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
            {headerName: "工料指标", headerWidth: 120, dataCode: "materialIndexType", hAlign: "center", dataType: "String",cellType:'comboBox',options:[],editable:true,maxDropDownItems:10},
            {headerName: "工料指标单位", headerWidth: 150, dataCode: "materialIndexUnit", hAlign: "center", dataType: "String"},
            {headerName: "单位转换系数", headerWidth: 150, dataCode: "materialIndexCoe", hAlign: "center", dataType: "String",validator:"number"}
        ],
        view: {
            lockColumns: ["code","name","specs","unit","marketPrice","quantity","materialIndexUnit"]
        }
    },
    ecoQuantitySetting:{
        header: [
            {headerName: "编码", headerWidth: 200, dataCode: "code", dataType: "String"},
            {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String"},
            {headerName: "单位", headerWidth: 45, dataCode: "unit", hAlign: "center", dataType: "String"},
            {headerName: "工程量", headerWidth: 90, dataCode: "quantity", hAlign: "right", dataType: "Number"},
            {headerName: "综合单价", headerWidth: 70, dataCode: "unitFee", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
            {headerName: "综合合价", headerWidth: 120, dataCode: "totalFee", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
            {headerName: "工程经济指标类别", headerWidth: 120, dataCode: "economicType", hAlign: "center", dataType: "String",cellType:'comboBox',options:[],editable:true,maxDropDownItems:10},
            {headerName: "工程量指标类别", headerWidth: 150, dataCode: "quantityIndexType", hAlign: "center", dataType: "String",cellType:'comboBox',options:[],editable:true,maxDropDownItems:10},
            {headerName: "工程量指标单位", headerWidth: 150, dataCode: "quantityIndexUnit", hAlign: "center", dataType: "String"},
            {headerName: "单位转换系数", headerWidth: 150, dataCode: "quantityIndexCoe", hAlign: "center", dataType: "String",validator:"number"}
        ],
        view: {
            lockColumns: ["code","name","unit","quantity","unitFee","totalFee","quantityIndexUnit"]
        }
    },
    engineerInfoDatas:[],
    engineerFeatureDatas:[],
    materialDatas:[],
    ecoQuantityDatas:[],
    initSpread:function () {
        if(!this.spread){
            this.spread = SheetDataHelper.createNewSpread($("#indexSpread")[0],8);
            sheetCommonObj.spreadDefaultStyle(this.spread);
            this.initEngineerInfoSheet();
            this.initEngineerFeatureSheet();
            this.initEngineerCostSheet();
            this.initMainMaterialSheet();
            this.initEconomicSheet();
            this.initQuantitySheet();
            this.initEcoQuantitySheet();
            this.initMaterialSheet();
        }
       // disableRightMenu("glj_from_sheet",this.spread);
        if(projectReadOnly){
            sheetCommonObj.disableSpread(this.spread);
        }
        sheetCommonObj.refreshWorkbookDelDefer(this.spread, 100);
    },
    initEngineerInfoSheet:function () {
        let sheet = this.spread.getSheet(0);
        sheetCommonObj.initSheet(sheet,this.engineerInfoSetting,30);
        sheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onSelectionChange);
        sheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.onEngineerInfoValueChange);
        sheet.bind(GC.Spread.Sheets.Events.RangeChanged, this.onEngineerInfoRangeChange);
        sheet.name('engineerInfo');
        //sheet.setRowHeight(0, 36, 1);
    },
    initEngineerFeatureSheet:function () {
        let sheet = this.spread.getSheet(1);
        this.engineerFeatureDatas = getDatas();
        sheetCommonObj.initSheet(sheet,this.engineerFeatureSetting,1);
        sheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.oEngineerFeatureValueChange);
        sheet.bind(GC.Spread.Sheets.Events.RangeChanged, this.onEngineerFeatureRangeChange); //
        sheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onSelectionChange);
        sheet.setRowCount(this.engineerFeatureDatas.length);
        function getDatas() {
            let datas = [];
            if(projectObj.project.property.engineerFeatures){
                for(let f of projectObj.project.property.engineerFeatures){
                    let tem = {
                        ID:f.ID,
                        name:f.name,
                        value:f.value,
                        ParentID:f.ParentID,
                        cellType:f.cellType,
                        options:f.options,
                        required:f.required,
                        collapsed:false
                    };
                    if(f.required == true) tem.foreColor = "#ff2a23";
                    datas.push(tem);
                }
            }
            return datas;
        }
    },
    initEngineerCostSheet:function () {
        let sheet = this.spread.getSheet(2);
        sheetCommonObj.initSheet(sheet,this.engineerCostSetting,30);
        sheet.name('engineerCost');
        //sheet.setRowHeight(0, 36, 1);
    },
    initMainMaterialSheet:function () {
        let sheet = this.spread.getSheet(3);
        this.setHeaderUnit(this.mainMaterialSetting,"unitIndex");
        sheetCommonObj.initSheet(sheet,this.mainMaterialSetting,30);
        sheet.name('mainMaterial');
        //sheet.setRowHeight(0, 36, 1);
    },
    initEconomicSheet:function () {
        let sheet = this.spread.getSheet(4);
        sheetCommonObj.initSheet(sheet,this.economicSetting,30);
        sheet.name('economic');
        //sheet.setRowHeight(0, 36, 1);
    },
    initQuantitySheet:function () {
        let sheet = this.spread.getSheet(5);
        this.setHeaderUnit(this.quantitySetting,"quantity");
        sheetCommonObj.initSheet(sheet,this.quantitySetting,30);
        sheet.name('quantity');
    },
    initEcoQuantitySheet:function () {
        let sheet = this.spread.getSheet(6);
        this.setComboOptions(projectObj.project.property.economics,this.ecoQuantitySetting,"economicType");
        this.setComboOptions(projectObj.project.property.mainQuantities,this.ecoQuantitySetting,"quantityIndexType");
        sheetCommonObj.initSheet(sheet,this.ecoQuantitySetting,1);
        sheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.onEcoQuantityValueChange);
        sheet.bind(GC.Spread.Sheets.Events.RangeChanged, this.onEcoQuantityRangeChange);
        sheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onSelectionChange);
        sheet.name('ecoQuantity');
    },
    initMaterialSheet:function () {
        let sheet = this.spread.getSheet(7);
        this.setComboOptions(projectObj.project.property.materials,this.materialSetting,"materialIndexType");
        sheetCommonObj.initSheet(sheet,this.materialSetting,30);
        sheet.bind(GC.Spread.Sheets.Events.SelectionChanged,this.onSelectionChange);
        sheet.bind(GC.Spread.Sheets.Events.ValueChanged, this.onMaterialChange);
        sheet.bind(GC.Spread.Sheets.Events.RangeChanged, this.onMaterialRangeChange);
        sheet.name('material');

    },
    showDatas:function(){
        let me = indexObj;
        if(me.nav_text == "工程信息指标") me.showEngineerInfo();
        if(me.nav_text == "工程特征指标") me.showEngineerFeature();
        if(me.nav_text == "工程造价指标") me.showEngineerCost();
        if(me.nav_text == "主要工料指标") me.showMainMaterial();
        if(me.nav_text == "主要经济指标") me.showEconomic();
        if(me.nav_text == "主要工程量指标") me.showQuantity();
        if(me.nav_text == "设置主要经济、工程量指标") me.showEcoQuantity();
        if(me.nav_text == "设置主要工料指标") me.showMaterial();

    },
    showEngineerInfo:function () {
        this.spread.setActiveSheetIndex(0);
        let sheet = this.spread.getActiveSheet();
        this.engineerInfoDatas = getEngineerInfoData();
        sheetCommonObj.showData(sheet, this.engineerInfoSetting,this.engineerInfoDatas);
        sheet.setRowCount(this.engineerInfoDatas.length);
        function getEngineerInfoData() {
            let datas = [];
            if(projectObj.project.property.engineerInfos){
                for(let info of projectObj.project.property.engineerInfos){
                    let d = {
                        dispName:info.dispName,
                        key:info.key,
                        value:info.value,
                        options:info.options,
                        cellType:info.cellType
                    };
                    if(info.required == true){
                        d.foreColor = "#ff2a23";
                        d.styleCol = 0;
                    }
                    if(info.cellType)d.dateCol = 1;
                    datas.push(d);
                }
            }
            return datas;
        }
    },
    showEngineerFeature:function () {
        this.spread.setActiveSheetIndex(1);
        let sheet = this.spread.getActiveSheet();
        refreshValue();
        sheetCommonObj.showTreeData(sheet, this.engineerFeatureSetting,this.engineerFeatureDatas);
        this.setRowCountAndLock(sheet,this.engineerFeatureDatas);
        function refreshValue() {
            let map = projectObj.project.property.engineerFeatures?_.indexBy(projectObj.project.property.engineerFeatures,'ID'):{};
            for(let f of indexObj.engineerFeatureDatas){
                if(map[f.ID]) f.value = map[f.ID].value;
            }
        }
    },
    showEngineerCost:function () {
        this.spread.setActiveSheetIndex(2);
        let sheet = this.spread.getActiveSheet();
        let costDatas = getEngineerCostData();
        sheetCommonObj.showData(sheet, this.engineerCostSetting,costDatas);
        sheet.setRowCount(costDatas.length);
        function getEngineerCostData() {
            let datas = [];
            let priceIndex = exportUtil.setEngineerPriceIndex({property:projectObj.project.property},{bills:projectObj.project.Bills.datas});
            for(let c of priceIndex.children){
                let tem = {
                    name:c.name,
                    cost:parseFloat(c.attrs[0].value),
                    unitCost:parseFloat(c.attrs[1].value),
                    per:parseFloat(c.attrs[2].value)
                };
                datas.push(tem);
            }
            return datas;
        }
    },
    showMainMaterial:function(){
        this.spread.setActiveSheetIndex(3);
        let property = projectObj.project.property;
        let sheet = this.spread.getActiveSheet();
        let mainDatas = this.getMainMaterialDatas(property,property.materials,projectObj.project.projectGLJ.datas,property.calcOptions,property.decimal,false,_,scMathUtil);
        sheetCommonObj.showData(sheet, this.mainMaterialSetting,mainDatas);
        sheet.setRowCount(mainDatas.length);
    },
    showEconomic:function () {
        this.spread.setActiveSheetIndex(4);
        let sheet = this.spread.getActiveSheet();
        let economicDatas = this.getEconomicDatas(projectObj.project.property.economics,projectObj.project.Bills.datas);
        sheetCommonObj.showData(sheet, this.economicSetting,economicDatas);
        sheet.setRowCount(economicDatas.length);
    },
    showQuantity:function () {
        this.spread.setActiveSheetIndex(5);
        let sheet = this.spread.getActiveSheet();
        let quantityDatas = this.getQuantityDatas(projectObj.project.property.mainQuantities,projectObj.project.Bills.datas);
        sheetCommonObj.showData(sheet, this.quantitySetting,quantityDatas);
        sheet.setRowCount(quantityDatas.length);
    },
    getQuantityDatas:function (mainQuantities,billsList) {
        return gljUtil.getQuantityDatas(projectObj.project.property.engineerFeatures,mainQuantities,billsList,fixedFlag,_,scMathUtil,projectObj.project.property.decimal)
       /* let datas = [];
        let [bills,totalFee] = exportUtil.getIndexBills(billsList);
        let billsGroup = _.groupBy(bills,'quantityIndexType');
        if(!mainQuantities) return datas;
        for(let m of mainQuantities){
            let tem = {
                name : m.name,
                quantityIndexUnit:m.unit,
                quantity:0
            };
            if(billsGroup[m.name]) setQuantities(billsGroup[m.name],tem);

            datas.push(tem);
        }

        function setQuantities(items,data) {
            let quantity = 0;
            for (let i of items){
                let coe = i.quantityIndexCoe && i.quantityIndexCoe!=""?parseFloat(i.quantityIndexCoe):0;
                i.quantity = scMathUtil.roundForObj(parseFloat(i.quantity)*coe,getDecimal("process"));
                quantity = scMathUtil.roundForObj(quantity +  i.quantity,getDecimal("process"));
            }
            data.quantity = exportUtil.calUnitWidthCoe(quantity);
        }     
        
        return datas;*/
    },
    getEconomicDatas:function (economics,billsList) {
        return gljUtil.getEconomicDatas(projectObj.project.property.engineerFeatures,economics,billsList,fixedFlag,_,scMathUtil);
    },

    getMainMaterialDatas:function (property,materials,projectGLJData,calcOptions,decimalObj,isRadio,_,scMathUtil) {
        return gljUtil.getMainMaterialDatas(property,projectObj.project.property.engineerFeatures,materials,projectGLJData,calcOptions,decimalObj,isRadio,_,scMathUtil);
    },
    showEcoQuantity:function () {
        let parentMap = {};
        this.spread.setActiveSheetIndex(6);
        let sheet = this.spread.getActiveSheet();
        let sel = sheet.getSelections()[0];
        let oldData = sel.row<this.ecoQuantityDatas.length?this.ecoQuantityDatas[sel.row]:null;
        this.ecoQuantityDatas = this.getIndexBillsData(parentMap);
        sheet.setRowCount(0);
        sheetCommonObj.showTreeData(sheet, this.ecoQuantitySetting,this.ecoQuantityDatas);
        this.setRowCountAndLock(sheet,this.ecoQuantityDatas);
        sel.row = oldData?_.findIndex(this.ecoQuantityDatas,{'ID':oldData.ID}):0;
        sheet.setSelection(sel.row==-1?0:sel.row,sel.col,sel.rowCount,sel.colCount);

        for(let row =0 ;row < this.ecoQuantityDatas.length;row ++){//锁定父清单
            if(parentMap[this.ecoQuantityDatas[row].ID]) sheet.getRange(row,-1 -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(true);
        }
    },
    getIndexBillsData:function (parentMap,old) {
        let datas = [],allNodes=[];
        let controller = projectObj.mainController, project = projectObj.project;
        let  FBFX = project.Bills.getFBFXNode(controller);
        let IDMap = _.indexBy(this.ecoQuantityDatas,"ID");
        if(FBFX){
            allNodes.push(FBFX);
            controller.tree.getAllSubNode(FBFX.source,allNodes);
        }
        let org = project.Bills.getOrgNode();
        let mNode = project.Bills.getMeasureNode(controller);
        if(mNode){
            allNodes.push(mNode);
            controller.tree.getAllSubNode(mNode.source,allNodes,org?org.data.ID:null);
        }
        for(let a of allNodes){
            let d = a.data;
            let tem = {
                ID:d.ID,
                ParentID:d.ParentID,
                code:d.code,
                name:d.name,
                unit:d.unit,
                quantity:d.quantity,
                totalFee:d.feesIndex && d.feesIndex.common && d.feesIndex.common.tenderTotalFee?d.feesIndex.common.tenderTotalFee:0,
                unitFee:d.feesIndex && d.feesIndex.common && d.feesIndex.common.tenderUnitFee?d.feesIndex.common.tenderUnitFee:0,
                economicType:d.economicType,
                quantityIndexType:d.quantityIndexType,
                quantityIndexUnit:d.quantityIndexUnit,
                quantityIndexCoe:d.quantityIndexCoe,
                collapsed:IDMap[d.ID]?IDMap[d.ID].collapsed:false
            };
            parentMap[d.ParentID] = tem;
            datas.push(tem);
        }
        for(let td of datas){
            if(parentMap[td.ID]){
                td.economicType = "";
                td.quantityIndexType = "";
                td.quantityIndexUnit = "";
                td.quantityIndexCoe = "";
            }
        }
        return datas;

    },
    showMaterial:function(){
        this.spread.setActiveSheetIndex(7);
        let sheet = this.spread.getActiveSheet();
        let sel = sheet.getSelections()[0];
        let oldData = sel.row<this.materialDatas.length?this.materialDatas[sel.row]:null;
        this.materialDatas = getMaterialData();
        sheetCommonObj.showData(sheet, this.materialSetting,this.materialDatas );
        sel.row = oldData?_.findIndex(this.materialDatas,{'id':oldData.id}):0;
        sheet.setSelection(sel.row==-1?0:sel.row,sel.col,sel.rowCount,sel.colCount);
        this.setRowCountAndLock(sheet,this.materialDatas);
        function getMaterialData() {
            let datas = [];
            let gljList = projectObj.project.projectGLJ.datas.gljList;
            gljList = _.filter(gljList,function(item){
                return item.quantity !== 0 && item.quantity !== '0'
            });
            gljList = sortProjectGLJ(gljList);
            for(let glj of gljList){
                let tem = {
                    id:glj.id,
                    code:glj.code,
                    name:glj.name,
                    specs:glj.specs,
                    unit:glj.unit,
                    type:glj.type,
                    quantity:glj.tenderQuantity,
                    materialIndexType:glj.materialIndexType,
                    materialIndexUnit:glj.materialIndexUnit,
                    materialIndexCoe:glj.materialIndexCoe
                };
                gljOprObj.setGLJPrice(tem,glj);
                tem.marketPrice =tem.tenderPrice;
                tem.totalPrice = scMathUtil.roundForObj(tem.tenderPrice * glj.tenderQuantity,getDecimal("glj.unitPrice"))
                datas.push(tem);
            }
            return datas;
        }
    },
    setRowCountAndLock:function (sheet,datas) {
        let rowCount = datas.length > 0 ?datas.length+11:0;
        sheet.setRowCount(rowCount);
        sheet.getRange(datas.length, -1, 11, -1, GC.Spread.Sheets.SheetArea.viewport).locked(true);//锁定空行
    },
    setComboOptions:function (property,setting,field) {
        if(!property) return;
        let options = [""];
        for(let p of property){
            options.push(p.name);
        }
        let h = _.find(setting.header,{dataCode:field});
        if(h) h.options = options;
    },
    onSelectionChange:function (sender, args) {
        args.sheet.repaint();
    },
    onEcoQuantityValueChange:function (sender,info) {
        let value = info.newValue;
        let dataCode = indexObj.ecoQuantitySetting.header[info.col].dataCode;
        if(!gljUtil.isDef(info.newValue)){
            return;
        }
        if (value&&!sheetCommonObj.checkData(info.col,indexObj.ecoQuantitySetting, value)) {
            alert('输入的数据类型不对，请重新输入!');
            return indexObj.showDatas();
        }
        let recode = indexObj.ecoQuantityDatas[info.row];
        let data = indexObj.getEcoQuantityUpdateData(recode,dataCode,value);
        if(data == null)  return indexObj.showDatas();
        projectObj.project.updateNodesAndRefresh([data],indexObj.showDatas)
    },
    onEngineerInfoValueChange:function (sender,info) {
        let value = info.newValue;
        value = indexObj.valueChecking(indexObj.engineerInfoDatas[info.row],value,info.sheet);
        let infos = projectObj.project.property.engineerInfos;
        let data = indexObj.getNewPropertyData([{row:info.row,value:value}],infos);
        if(!data) return indexObj.showDatas();
        indexObj.updateProjectProperty(data,"engineerInfos");
    },
    valueChecking:function (recode,value,sheet) {
        if(recode && recode.cellType == "date"){//日期类型要做个转换
            sheet.tag(value);
            let data = sheet.tag();
            value =  formatDate(new Date(data), 'yyyy-MM-dd');
        }
         //to do other checking
        return value
    },


    oEngineerFeatureValueChange:function(sender,info){
        let value = info.newValue;
        let features = projectObj.project.property.engineerFeatures;
        let data = indexObj.getNewPropertyData([{row:info.row,value:value}],features);
        if(!data) return indexObj.showDatas();
        indexObj.updateProjectProperty(data,"engineerFeatures");
    },
    onEngineerFeatureRangeChange:function (sender,info) {
        let features = projectObj.project.property.engineerFeatures;
        let datas = [];
        for(let c of info.changedCells){
            let value =  info.sheet.getCell(c.row, c.col).text();
            datas.push({row:c.row,value:value})
        }
        let data = indexObj.getNewPropertyData(datas,features);
        if(!data) return indexObj.showDatas();
        indexObj.updateProjectProperty(data,"engineerFeatures");
    },
    onEngineerInfoRangeChange:function (sender,info) {
        let infos = projectObj.project.property.engineerInfos;
        let datas = [];
        for(let c of info.changedCells){
            let value =  info.sheet.getCell(c.row, c.col).text();
            datas.push({row:c.row,value:value})
        }
        let data = indexObj.getNewPropertyData(datas,infos);
        if(!data) return indexObj.showDatas();
        indexObj.updateProjectProperty(data,"engineerInfos");

    },
    updateProjectProperty: function(datas,field){
        projectObj.project.updateProjectProperty(datas,field,function () {
            indexObj.showDatas();
        })
    },
    getNewPropertyData:function (datas,property) {
        if(!property) return null;
        property = _.cloneDeep(property);
        for(let d of datas){
            if(property[d.row])property[d.row].value = d.value;
        }
        return property;
    },

    onMaterialChange:function (sender,info) {
        let value = info.newValue,updateMap = {};
        let dataCode = indexObj.materialSetting.header[info.col].dataCode;
        if(!gljUtil.isDef(info.newValue)){
            return;
        }
        if (value&&!sheetCommonObj.checkData(info.col,indexObj.materialSetting, value)) {
            alert('输入的数据类型不对，请重新输入!');
            return indexObj.showDatas();
        }
        let recode = indexObj.materialDatas[info.row];
        let data = indexObj.getMaterialUpdateData(recode,dataCode,value);
        if(!data) return indexObj.showDatas() ;
        updateMap[recode.id] = data;
        projectObj.project.projectGLJ.batchUpdateGLJProperty(updateMap,indexObj.showDatas);

    },
    onEcoQuantityRangeChange:function (sender,info) {
        let nodes = [];
        for(let c of info.changedCells){
            let dataCode = indexObj.ecoQuantitySetting.header[c.col].dataCode;
            let value= info.sheet.getCell(c.row, c.col).text();
            if (value&&!sheetCommonObj.checkData(c.col,indexObj.ecoQuantitySetting, value)) {
                alert('输入的数据类型不对，请重新输入!');
                return indexObj.showDatas();
            }
            let recode = indexObj.ecoQuantityDatas[c.row];
            let data = indexObj.getEcoQuantityUpdateData(recode,dataCode,value);
            if(data) nodes.push(data);
        }
        if(_.isEmpty(nodes)) return indexObj.showDatas();
        projectObj.project.updateNodesAndRefresh(nodes,indexObj.showDatas)
    },
    onMaterialRangeChange:function (sender,info) {
        let updateMap = {};
        for(let c of info.changedCells){
            let value=  info.sheet.getCell(c.row, c.col).text();
            let dataCode = indexObj.materialSetting.header[c.col].dataCode;
            let recode = indexObj.materialDatas[c.row];
            if (value&&!sheetCommonObj.checkData(c.col,indexObj.materialSetting, value)) {
                alert('输入的数据类型不对，请重新输入!');
                return indexObj.showDatas();
            }
            let tem = indexObj.getMaterialUpdateData(recode,dataCode,value);
            if(tem) updateMap[recode.id] = tem;
        }
        if(_.isEmpty(updateMap)) return indexObj.showDatas();
        projectObj.project.projectGLJ.batchUpdateGLJProperty(updateMap,indexObj.showDatas);
    },
    getEcoQuantityUpdateData:function (recode,dataCode,newValue) {
        let tem = {
            type:ModuleNames.bills,
            data:{ID:recode.ID}
        };
        tem.data[dataCode] = newValue;
        if(!recode) return null;
        if(dataCode == "quantityIndexType"){
            if(newValue == ""){
                tem.data["quantityIndexUnit"] = "";
                tem.data["quantityIndexCoe"] = null;
            }else {
                let mainQuantities = projectObj.project.property.mainQuantities;
                if (!mainQuantities) return null;
                let m =  _.find(mainQuantities,{name:newValue});
                if(!m) return null;
                tem.data["quantityIndexUnit"] = m.unit;
                tem.data["quantityIndexCoe"] = recode.unit == m.unit? 1:null;
            }

        }
        if(dataCode == "economicType"){//这里就主要查找输入是否是下列选项中的一个，值已经在上面设置了
            if(newValue != ""){
                let economics = projectObj.project.property.economics;
                if (!economics) return null;
                let m =  _.find(economics,{name:newValue});
                if(!m) return null;
            }
        }
        return tem;

    },
    getMaterialUpdateData:function (recode,dataCode,newValue) {
        let data = {};
        data[dataCode] = newValue;
        if(dataCode == "materialIndexType"){
            if(newValue == ""){
                data["materialIndexUnit"] = "";
                data["materialIndexCoe"] =  null;
            }else {
                let materials = projectObj.project.property.materials;
                if (!materials) return null;
                if(!recode) return null;
                let m =  _.find(materials,{name:newValue});
                if(!m) return null;
                data["materialIndexUnit"] = m.unit;
                data["materialIndexCoe"] =  recode.unit == m.unit ?1: null;
                if(recode.name == "水泥" && m.name == "水泥")  data["materialIndexCoe"] = 0.001;
            }

        }
        return data
    },
    getIndexUnit:function () {
        let features  = projectObj.project.property.engineerFeatures;
        if(features){
            for(let f of features){
                if(f.index == true && f.indexUnit && f.indexUnit!="") return f.indexUnit;
            }
        }
        return null;
    },
    getQuantityUnit:function () {
        let features  = projectObj.project.property.engineerFeatures;
        if(features){
            for(let f of features){
                if(f.index == true && f.quantityUnit && f.quantityUnit!="") return f.quantityUnit;
            }
        }
        return null;
    },
    setHeaderUnit:function (setting,dataCode) {
      let header = _.find(setting.header,{dataCode:dataCode});
      let unit = this.getIndexUnit();
      if(dataCode === 'quantity'){
        let qunit = this.getQuantityUnit();
        if(qunit) unit = qunit
      }
      if(!unit || !header )return;
      header.headerName +=`(${unit})`
    }
};


$(function () {
    $("#index_nav").on("click","ul li a",function(e){
        let children = $('#index_nav a');
        for(let c of children){
            $(c).removeClass('active');
        }
        $(this).addClass('active');
        indexObj.nav_text = $(this).text();
        indexObj.showDatas();
    });

    $('#tab_index').on('shown.bs.tab', function (e) {
        indexObj.initSpread();
        indexObj.showDatas();
    });
});

function onResize() {
    if(indexObj.spread) indexObj.spread.refresh();
}