/**
 * Created by CSL on 2017-08-03.
 * 计算程序管理。
 */
let rationPM = {
    datas: [],
    mainSpread: null,
    detailSpread: null,

    mainSetting: {
        header:[
            // {headerName:"ID",headerWidth:80,dataCode:"ID", hAlign: "center"},
            {headerName:"名称",headerWidth:200,dataCode:"name", dataType: "String"}
        ],
        view:{
            comboBox:[],
            lockColumns:[0,1]
        }
    },

    detailSetting: {
        header:[
            // {headerName:"ID",headerWidth:80,dataCode:"ID", hAlign: "center"},
            {headerName:"费用代号",headerWidth:80,dataCode:"code", dataType: "String"},
            {headerName:"费用名称",headerWidth:200,dataCode:"name", dataType: "String"},
            {headerName:"计算基数",headerWidth:180,dataCode:"dispExpr", dataType: "String"},
            {headerName:"基数说明",headerWidth:300,dataCode:"statement", dataType: "String"},
            {headerName:"费率",headerWidth:80,dataCode:"feeRate", dataType: "Number"},
            {headerName:"字段名称",headerWidth:140,dataCode:"displayFieldName", dataType: "String", hAlign: "center"},
            {headerName:"备注",headerWidth:100,dataCode:"memo", dataType: "String"}
        ],
        view:{
            comboBox:[],
            lockColumns:[0,1,2,3,4,6]
        }
    },

    buildSheet: function (){
        let me = this;
        me.datas = calcTemplates;
        if (me.mainSpread) {
            me.mainSpread.destroy();
            me.mainSpread = null;
        };
        if (me.detailSpread) {
            me.detailSpread.destroy();
            me.detailSpread = null;
        };
        me.mainSpread = sheetCommonObj.buildSheet($('#mainSpread')[0], me.mainSetting, me.datas.length);
        me.detailSpread = sheetCommonObj.buildSheet($('#detailSpread')[0], me.detailSetting, me.datas[0].calcItems.length);

        var fieldName = new GC.Spread.Sheets.CellTypes.ComboBox();
        fieldName.items(projectObj.project.calcProgram.calc.compiledFeeTypeNames);
        // fieldName.items(["直接费","人工费","材料费","机械费","主材费","企业管理费","利润","风险费","人工价差","材料价差","机械价差","工程造价","调整人工费","调整机上人工费","甲供材料费"]);
        me.detailSpread.getSheet(0).getRange(-1, 5, -1, 1).cellType(fieldName);

        me.mainSpread.getSheet(0).bind(GC.Spread.Sheets.Events.EnterCell, me.onMainEnterCell);
        me.detailSpread.getSheet(0).bind(GC.Spread.Sheets.Events.CellChanged, me.onDetailCellChanged);

        let mSheet = me.mainSpread.getSheet(0);
        sheetCommonObj.showData(mSheet, me.mainSetting, me.datas);

        let dSheet = me.detailSpread.getSheet(0);
        sheetCommonObj.showData(dSheet, me.detailSetting, me.datas[0].calcItems);
    },

    onMainEnterCell: function(sender, args) {
        var me = rationPM;
        var row = args.sheet.getActiveRowIndex();

        me.detailSpread.suspendPaint();
        var dSheet = me.detailSpread.getSheet(0);
        var dData = me.datas[row].calcItems;
        sheetCommonObj.showData(dSheet, me.detailSetting, dData);
        me.detailSpread.resumePaint();
    }
};

$(document).ready(function(){
    $('#tab_calc_program_manage').on('shown.bs.tab', function (e) {
        rationPM.buildSheet();
    });
});


