/**
 * Created by CSL on 2017-05-19.
 * 通用的SpreadSheet创建显示接口，应用数据源绑定列、脏数据等。
 * var colDef = [{name: "coeType", displayName: "类型", size: 100, hAlign: "center"},..,{} ];
 * var datas = [{},..,{}];
 */

sheetObj = {
    spread: null,

    create: function (container, colDef, datas){
        var me = this;
        me.spread = new GC.Spread.Sheets.Workbook(container, {sheetCount:1});
        me.spread.suspendPaint();

        me.spread.options.tabStripVisible = false;
        me.spread.options.showHorizontalScrollbar = false;

        var sheet = me.spread.getSheet(0);
        var sheetArea = GC.Spread.Sheets.SheetArea;
        sheet.setRowCount(20, sheetArea.viewport);
        sheet.setRowHeight(0, 30, sheetArea.colHeader);
        sheet.autoGenerateColumns = false;
        sheet.setDataSource(datas);
        sheet.addRows(sheet.getRowCount(), 1);      // 扩展1行，用于直接输入以新增记录。
        sheet.bindColumns(colDef);
        me.setColAlign(colDef);
        me.lockCols(colDef);
        me.setRowHeight(datas, 25);

        me.spread.resumePaint();
        return me.spread;
    },

    setColAlign: function(colDef){
        var me = this;
        var sheet = me.spread.getSheet(0);

        for (var col = 0; col < colDef.length; col++) {
            var ha = colDef[col].hAlign;
            var va = colDef[col].vAlign;
            var area = sheet.getRange(-1, col, -1, GC.Spread.Sheets.SheetArea.viewport);

            if (ha === "right") {
                area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
            } else if (ha === "center") {
                area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
            } else {
                area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
            };

            if (va === "top") {
                area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
            } else if (va === "bottom") {
                area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
            } else {
                area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
            };
        };

    },

    lockCols: function(colDef){
        var me = this;
        var sheet = me.spread.getSheet(0);
        sheet.options.isProtected = true;
        sheet.options.isProtected = true;
        sheet.getRange(-1, 0, -1, colDef.length, GC.Spread.Sheets.SheetArea.viewport).locked(false);

        for (var col = 0; col < colDef.length; col++) {
            if (colDef[col].readOnly == undefined) {
                var ro = false
            }else{
                var ro = colDef[col].readOnly;
            };

            sheet.getRange(-1, col, -1, 1, GC.Spread.Sheets.SheetArea.viewport).locked(ro);
        };
    },

    setRowHeight: function(datas, hValue){
        var me = this;
        var sheet = me.spread.getSheet(0);
        for (var row = 0; row < datas.length; row++) {
            sheet.setRowHeight(row, hValue, GC.Spread.Sheets.SheetArea.viewport);
        }
    }
};
