/**
 * Created by CSL on 2017-05-12.
 */

var gljView = {
    spreadBook: null,
    setting: {
        header: [
            {headerName: "编码", headerWidth: 60, dataCode: "ID", dataType: "String", formatter: "@"},
            {headerName: "名称", headerWidth: 80, dataCode: "name", dataType: "String"},
            {headerName: "规格型号", headerWidth: 80, dataCode: "specs", dataType: "String", hAlign: "center"},
            {headerName: "单位", headerWidth: 60, dataCode: "unit", dataType: "String", hAlign: "center"},
            {headerName: "类别", headerWidth: 50, dataCode: "gljType", dataType: "String", hAlign: "center"},
            {headerName: "定额消耗量", headerWidth: 80, dataCode: "orgRQuantity", dataType: "String", hAlign: "right"},    // dataType: "Number", formatter: "0.00"
            {headerName: "自定义消耗量", headerWidth: 80, dataCode: "customQuantity", dataType: "String", hAlign: "right"},
            {headerName: "消耗量", headerWidth: 80, dataCode: "quantity", dataType: "String", hAlign: "right"},
            {headerName: "数量", headerWidth: 80, dataCode: "rationItemQuantity", dataType: "String", hAlign: "right"},
            {headerName: "基价单价", headerWidth: 80, dataCode: "rationPrice", dataType: "String", hAlign: "right"},
            {headerName: "调整基价", headerWidth: 80, dataCode: "adjustPrice", dataType: "String", hAlign: "right"},
            {headerName: "市场单价", headerWidth: 80, dataCode: "marketPrice", dataType: "String", hAlign: "right"},
            {headerName: "市场单价调整", headerWidth: 80, dataCode: "price", dataType: "String", hAlign: "right"}
        ],
        view: {
            comboBox: [{row: -1, col: 2, rowCount: -1, colCount: 1}],
            lockedCells: [{row: -1, col: 3, rowCount: -1, colCount: 1}]
        }
    },

    buildSheet: function (container) {
        var me = this;
        me.spreadBook = new GC.Spread.Sheets.Workbook(container, { sheetCount: 1 });
        me.spreadBook.options.tabStripVisible = false;
        me.spreadBook.options.showHorizontalScrollbar = false;

        var sheet = me.spreadBook.getSheet(0);
        sheet.suspendPaint();
        sheet.suspendEvent();
        var shts = GC.Spread.Sheets;
        sheet.setRowCount(1, shts.SheetArea.colHeader);
        sheet.setColumnCount(me.setting.header.length, shts.SheetArea.viewport);
        sheet.options.colHeaderAutoTextIndex = 1;
        sheet.options.colHeaderAutoText = shts.HeaderAutoText.numbers;
        sheet.showRowOutline(false);

        for (var i = 0; i < me.setting.header.length; i++) {
            sheet.setValue(0, i, me.setting.header[i].headerName, shts.SheetArea.colHeader);
            sheet.setColumnWidth(i, me.setting.header[i].headerWidth? me.setting.header[i].headerWidth:100);
        }

        sheet.setRowCount(20);
        sheet.resumeEvent();
        sheet.resumePaint();

        return me.spreadBook;
    },

    showDatas: function(datas) {
        var me = this;
        var sheet = me.spreadBook.getSheet(0);
        sheet.suspendPaint();
        sheet.suspendEvent();
        for (var col = 0; col < me.setting.header.length; col++) {
            var hAlign = "left", vAlign = "center";
            if (me.setting.header[col].hAlign) {
                hAlign = me.setting.header[col].hAlign;
            } else if (me.setting.header[col].dataType !== "String"){
                hAlign = "right";
            }
            vAlign = me.setting.header[col].vAlign?me.setting.header[col].vAlign:vAlign;
            me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
            if (me.setting.header[col].formatter) {
                sheet.setFormatter(-1, col, me.setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
            }
            for (var row = 0; row < datas.length; row++) {
                sheet.setValue(row, col, datas[row][me.setting.header[col].dataCode], GC.Spread.Sheets.SheetArea.viewport);
            }
        }
        sheet.resumeEvent();
        sheet.resumePaint();
    },

    setAreaAlign: function(area, hAlign, vAlign){
        if (!(hAlign) || hAlign === "left") {
            area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
        } else if (hAlign === "right") {
            area.hAlign(GC.Spread.Sheets.HorizontalAlign.right);
        } else if (hAlign === "center") {
            area.hAlign(GC.Spread.Sheets.HorizontalAlign.center);
        } else {
            area.hAlign(GC.Spread.Sheets.HorizontalAlign.left);
        }
        if (!(vAlign) || vAlign === "center") {
            area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
        } else if (vAlign === "top") {
            area.vAlign(GC.Spread.Sheets.VerticalAlign.top);
        } else if (vAlign === "bottom") {
            area.vAlign(GC.Spread.Sheets.VerticalAlign.bottom);
        } else {
            area.vAlign(GC.Spread.Sheets.VerticalAlign.center);
        }
    }

}

function objTest(obj) {
    var str = "";
    var spr = "";
    for (var x in obj) {
        if (obj.hasOwnProperty(x)) {
            if(str == ''){ spr = '' } else { spr = ', '};
            str += spr + x + ':' + obj[x];
        }
    }
    return str;
}

gljView.buildSheet($("#gljSpread")[0]);
