/**
 * Created by zhang on 2018/7/3.
 */
let gljCol = {
    showTaxRate: false,
    hideInfoPrice:true,
    ration_glj_setting: {
        header: [
            {headerName: "编码", headerWidth: 110, dataCode: "code", dataType: "String", formatter: "@"},
            {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String",cellType:'tipsCell'},
            {headerName: "规格型号", headerWidth: 120, dataCode: "specs", dataType: "String", hAlign: "left",cellType:'tipsCell'},
            {headerName: "单位", headerWidth: 45, dataCode: "unit", dataType: "String", hAlign: "center"},
            {headerName: "市场价", headerWidth: 65, dataCode: "marketPrice", dataType: "Number", hAlign: "right"},//, decimalField: "glj.unitPrice"
            {headerName: "调整价", headerWidth: 65, dataCode: "adjustPrice", dataType: "Number", hAlign: "right"},//, decimalField: "glj.unitPrice"1
            {headerName: "自定消耗", headerWidth: 65, dataCode: "customQuantity", dataType: "Number", hAlign: "right"},
            {headerName: "消耗量", headerWidth: 65, dataCode: "quantity", dataType: "Number", hAlign: "right"},
            {headerName: "定额价", headerWidth: 65, dataCode: "basePrice", dataType: "Number", hAlign: "right"},//, decimalField: "glj.unitPrice"
            {headerName: "定额消耗", headerWidth: 65, dataCode: "rationItemQuantity", dataType: "Number", hAlign: "right"},   // dataType: "Number", formatter: "0.00"
            {headerName: "总消耗量", headerWidth: 80, dataCode: "totalQuantity", dataType: "Number", hAlign: "right"},
            {headerName: "暂估", headerWidth: 45, dataCode: "isEstimate", dataType: "String", hAlign: "center", vAlign: "center", cellType: "checkBox"},
            {headerName: "调后市场价", headerWidth: 80, dataCode: "tenderPrice", dataType: "Number", hAlign: "right", visible: false},
            {headerName: "调后消耗量", headerWidth: 80, dataCode: "tenderQuantity", dataType: "Number", hAlign: "right", visible: false}
        ],
        view: {
            lockColumns: [ "adjustPrice", "quantity", "totalQuantity", "isEstimate",
                "tenderPrice", "tenderQuantity"],//这里以后改成dataCode好一点
            rowHeaderWidth:25
        },
        getStyle:function (data) {
            if(data&&data.type == gljType.MAIN_MATERIAL){//是主材的话字体蓝色显示
                return {foreColor:"#4D7BFF"};
            }
            return null;
        },
        navigationRightCol:['name','specs','unit'],//选中这几列时，按enter键跳到右边一列
        // 工料机类型是混凝土、砂浆、配合比、机械台班时，价格不可编辑。
        editedTyep:[GLJTypeConst.MAIN_MATERIAL,GLJTypeConst.EQUIPMENT]//主材设备
    },
    project_glj_setting:{
        header: [
            {headerName: "编码", headerWidth: 80, dataCode: "code", dataType: "String",spanRows: [2]},
            {headerName: "名称", headerWidth: 160, dataCode: "name", dataType: "String",cellType:'tipsCell',spanRows: [2]},
            {headerName: "规格型号", headerWidth: 120, dataCode: "specs", hAlign: "left", dataType: "String",cellType:'tipsCell',spanRows: [2]},
            {headerName: "单位", headerWidth: 45, dataCode: "unit", hAlign: "center", dataType: "String",spanRows: [2]},
            {headerName: "类型", headerWidth: 45, dataCode: "short_name", hAlign: "center", dataType: "String",spanRows: [2]},
            {headerName: "市场价", headerWidth: 70, dataCode: "marketPrice", hAlign: "right", dataType: "Number",validator:"number",spanRows: [2]},//,decimalField:"glj.unitPrice"
            {headerName: "价格来源", headerWidth: 90, dataCode: "priceFrom", dataType: "String",spanRows: [2],cellType:"tipsCell"},
            {headerName: "税率", headerWidth: 70, dataCode: "taxRate", hAlign: "right", dataType: "Number",validator:"number",spanRows: [2]},//,decimalField:"glj.unitPrice"
            {headerName: "调整价", headerWidth: 70, dataCode: "adjustPrice", hAlign: "right", dataType: "Number",spanRows: [2]},//,decimalField:"glj.unitPrice"
            {headerName: "定额价", headerWidth: 70, dataCode: "basePrice", hAlign: "right", dataType: "Number",validator:"number",spanRows: [2]},//decimalField:'glj.unitPrice',
            {headerName: "总消耗量", headerWidth: 90, dataCode: "quantity", hAlign: "right", dataType: "Number",decimalField:'glj.quantity',spanRows: [2]},
            {headerName: "暂估", headerWidth: 45, dataCode: "is_evaluate", hAlign: "center", dataType: "String",cellType:'checkBox',spanRows: [2]},
            {headerName: "主要\n材料", headerWidth: 45, dataCode: "is_main_material", hAlign: "center", dataType: "String",cellType:'checkBox',spanRows: [2]},
            {headerName: "不计税设备", headerWidth: 55, dataCode: "no_tax_eqp", dataType: "String",cellType: "checkBox",spanRows: [2]},
            {headerName: "评标材料", headerWidth: 35, dataCode: "is_eval_material", dataType: "String",cellType: "checkBox",spanRows: [2]},
            {headerName: "供货方式", headerWidth: 70, dataCode: "supply", hAlign: "center", dataType: "String",cellType:'comboBox',editorValueType:true,options:supplyComboMap,spanRows: [2]},
            {headerName: "甲供数量", headerWidth: 90, dataCode: "supply_quantity", hAlign: "right", dataType: "Number",validator:"number",decimalField:'glj.quantity',spanRows: [2]},
            {headerName: "三材类别", headerWidth: 70, dataCode: "materialType", hAlign: "center", dataType: "String",cellType:'comboBox',editorValueType:true,options:materialComboMap,spanRows: [2]},
            {headerName: "三材系数", headerWidth: 70, dataCode: "materialCoe", hAlign: "right", dataType: "Number",validator:"number",spanRows: [2]},//,decimalField:'material'
            {headerName: "交货方式", headerWidth: 70, dataCode: "delivery", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "送达地点", headerWidth: 70, dataCode: "delivery_address", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "产地", headerWidth: 80, dataCode: "originPlace", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "厂家", headerWidth: 80, dataCode: "vender", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "质量等级", headerWidth: 80, dataCode: "qualityGrace", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "品牌", headerWidth: 80, dataCode: "brand", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "备注", headerWidth: 100, dataCode: "remark", hAlign: "left", dataType: "String",spanRows: [2]},
            {headerName: "不调价", headerWidth: 55, dataCode: "is_adjust_price", dataType: "String",cellType: "checkBox",spanRows: [2], visible: true},
            {headerName: ["调价后","市场价"], headerWidth: 75, dataCode: "tenderPrice", hAlign: "right", dataType: "Number",validator:"number",spanCols: [2,1], visible: false},
            {headerName: ["","总消耗量"], headerWidth: 90, dataCode: "tenderQuantity", hAlign: "right", dataType: "Number",decimalField:'glj.quantity',spanCols: [0,1], visible: false}
        ],
        view: {
            lockColumns: ["code","name","specs","unit","short_name","tenderPrice","adjustPrice","quantity","tenderQuantity","priceFrom"]
        },
       /*  autoFit:true,
        fitRow:['priceFrom'], */
        frozenCols:4,
        headRows:2,
        getStyle:function (data,row,activeRow,dataCode) {
          let style = {};
          if(row === activeRow){//选中黄色显示
            style = new GC.Spread.Sheets.Style();
            style.backColor = "#FFFACD";
            style.borderLeft = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
            style.borderTop = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
            style.borderRight = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
            style.borderBottom = new GC.Spread.Sheets.LineBorder("#D4D4D4", GC.Spread.Sheets.LineStyle.thin);
          }
          if(dataCode === "marketPrice"){
              if(data.marketPrice == data.basePrice) style.foreColor = 'black';//改回相等的时候，要改回来
              if(data.marketPrice > data.basePrice) style.foreColor = 'red';
              if(data.marketPrice < data.basePrice) style.foreColor = 'green';
          }
          return _.isEmpty(style)?null:style;
      },
    },
    mixRatio_Setting:{
        header:[
            {headerName: "编码", headerWidth: 80, dataCode: "code", dataType: "String"},
            {headerName: "名称", headerWidth: 240, dataCode: "name", dataType: "String"},
            {headerName: "规格型号", headerWidth: 190, dataCode: "specs", dataType: "String"},
            {headerName: "单位", headerWidth: 45, dataCode: "unit", hAlign: "center", dataType: "String"},
            {headerName: "类型", headerWidth: 45, dataCode: "short_name", hAlign: "center", dataType: "String"},
            {headerName: "定额价", headerWidth: 80, dataCode: "basePrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:'glj.unitPrice'
            {headerName: "调整价", headerWidth: 80, dataCode: "adjustPrice", hAlign: "right", dataType: "Number"},//,decimalField:"glj.unitPrice"
            {headerName: "市场价", headerWidth: 80, dataCode: "marketPrice", hAlign: "right", dataType: "Number",validator:"number"},//,decimalField:"glj.unitPrice"
            {headerName: "消耗量", headerWidth: 80, dataCode: "consumption", hAlign: "right", dataType: "Number",decimalField:"glj.quantity",validator:"number"}
        ],
        view: {
            lockColumns: [0,1,2,3,4,5,6,7]
        }
    },
    scopeSetting:{
        "emptyRows":0,
        "headRows":1,
        "headRowHeight":[30],
        "defaultRowHeight": 21,
        "treeCol": 1,
        "cols":[
            {
                "width":40,
                "readOnly": true,
                "head":{
                    "titleNames":["选择"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"selected",
                    "vAlign":1,
                    "hAlign":1,
                    "font":"Arial",
                    "cellType":function (node) {
                        return new GC.Spread.Sheets.CellTypes.CheckBox();
                    }
                }
            },
            {
                "width":140,
                "readOnly": true,
                "head":{
                    "titleNames":["编号"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"code",
                    "vAlign":1,
                    "hAlign":0,
                    "font":"Arial"
                }
            },
            {
                "width":240,
                "readOnly": true,
                "head":{
                    "titleNames":["名称"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"name",
                    "vAlign":0,
                    "hAlign":0,
                    "font":"Arial"
                }
            },
            {
                "width":70,
                "readOnly": true,
                "head":{
                    "titleNames":["单位"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"unit",
                    "vAlign":1,
                    "hAlign":1,
                    "font":"Arial"
                }
            },
            {
                "width":80,
                "readOnly": true,
                "head":{
                    "titleNames":["工程量"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"quantity",
                    "vAlign":1,
                    "hAlign":2,
                    "font":"Arial"
                }
            },
            {
                "width":80,
                "readOnly": true,
                "head":{
                    "titleNames":["单价"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"unitPrice",
                    "vAlign":1,
                    "hAlign":2,
                    "font":"Arial"
                }
            },
            {
                "width":80,
                "readOnly": true,
                "head":{
                    "titleNames":["金额"],
                    "spanCols":[1],
                    "spanRows":[1],
                    "vAlign":[1],
                    "hAlign":[1],
                    "font":["Arial"]
                },
                "data":{
                    "field":"totalPrice",
                    "vAlign":1,
                    "hAlign":2,
                    "font":"Arial"
                }
            }
        ]
    },
    removeCol:function (dataCode,setting) {
        let colIndex = _.findIndex(setting.header,{'dataCode':dataCode});
        if(colIndex != -1){
            setting.header.splice(colIndex,1);//去掉列;
            let newArray = [];
            for(let l of setting.view.lockColumns){
                if(_.isString(l)) return;//如果是用dataCode表示的,不用处理
                if(l == colIndex)  continue;
                if(l > colIndex)  l = l -1;
                newArray.push(l);
            }
            setting.view.lockColumns = newArray;
        }
    },
    initGljCol: function (showAdjustPrice, showTenderFields,init=false) {
      let me = gljCol;
      if (showAdjustPrice !== true) {
        me.removeCol('adjustPrice', me.ration_glj_setting);
        me.removeCol('adjustPrice', me.project_glj_setting);
        me.removeCol('adjustPrice', me.mixRatio_Setting);
      };
      if (me.showTaxRate == false) me.removeCol('taxRate', me.project_glj_setting);
      if (me.hideInfoPrice == true) { 
        $("#info-nav-li").hide();
        me.removeCol('priceFrom', me.project_glj_setting);
      } 
      
      me.showTenderFields(showTenderFields, false,init);

      gljOprObj.setting = me.ration_glj_setting;
      projectGljObject.projectGljSetting = me.project_glj_setting;
      projectGljObject.mixRatioSetting = me.mixRatio_Setting;
      me.setScopeFormater();
      gljOprObj.scopeSetting = me.scopeSetting;
    },
    showTenderFields: function (showFields = false, needRefresh = false,init=false){
        let me = gljCol;

        let PGLJHeader = me.project_glj_setting.header;
        for (let e of PGLJHeader){
            if (e.dataCode == 'tenderPrice' || e.dataCode == 'tenderQuantity'){
                e.visible = showFields;
            }
        };

        let RGLJHeader = me.ration_glj_setting.header;
        for (let e of RGLJHeader){
            if (e.dataCode == 'tenderPrice' || e.dataCode == 'tenderQuantity'){
                e.visible = showFields;
            }
        };

        let CalcProgramHeader = calcProgramObj.setting.header;
        for (let e of CalcProgramHeader){
            if (e.dataCode == 'tenderUnitFee' || e.dataCode == 'tenderTotalFee'){
                e.visible = showFields;
            }
        };

        colSettingObj.setVisible('feesIndex.common.tenderUnitFee', showFields);
        colSettingObj.setVisible('feesIndex.common.tenderTotalFee', showFields);
        colSettingObj.updateColSetting(true,init);

        if (needRefresh){
            if(projectGljObject.projectGljSpread) {
                projectGljObject.projectGljSheet = projectGljObject.projectGljSpread .getSheet(0);
                projectGljObject.initSheet(projectGljObject.projectGljSheet, projectGljObject.projectGljSetting);
            }
            if (subSpread) {
                gljOprObj.refreshView();
                calcProgramObj.refreshCalcProgram(projectObj.project.mainTree.selected);

                //gljOprObj.initSheet(subSpread.getSheet(0), false);
                //gljOprObj.initSheet(subSpread.getSheet(2), false);
            }
        };
    },
    setScopeFormater:function () {
        // 综合单价、综合合价，小数部分应补0对齐。  CSL
        for(let col of this.scopeSetting.cols){
            if (col.data.field=="totalPrice") col.data.formatter = MainTreeCol.getNumberFormatter(decimalObj.ration.totalPrice, true);
            if (col.data.field== "unitPrice")  col.data.formatter = MainTreeCol.getNumberFormatter(decimalObj.ration.unitPrice, true);
        }
    }
};
/*
$(function () {
    $('#fullpath').click(function () {
        checkfeeRate();

    });

})

async function checkfeeRate() {
    let result = await ajaxPost('/feeRates/checkFeeRateName', data = {name: '编辑工程量表达式', rootProjectID: 3163});
    console.log(result);
}*/
