/**
 * Created by Zhong on 2017/9/29.
 */
let sheetsOprObj = {
    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);
        }
    },
    showData: function(obj, sheet, setting, data, distTypeTree) {
        var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
        sheet.suspendPaint();
        sheet.suspendEvent();
        if(typeof setting.owner !== 'undefined' && setting.owner === 'gljComponent'){
            sheet.setRowCount(data.length + 5);
        }
        else{
            sheet.setRowCount(typeof repositoryGljObj !== 'undefined' && repositoryGljObj.currentOprParent === 1 ? data.length : data.length + 10);
        }
        for (var col = 0; col < setting.header.length; col++) {
            var hAlign = "left", vAlign = "center";
            if (setting.header[col].hAlign) {
                hAlign = setting.header[col].hAlign;
            } else if (setting.header[col].dataType !== "String"){
                hAlign = "right";
            }
            vAlign = setting.header[col].vAlign?setting.header[col].vAlign:vAlign;
            me.setAreaAlign(sheet.getRange(-1, col, -1, 1), hAlign, vAlign);
            if (setting.header[col].formatter) {
                sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
            }
            for (var row = 0; row < data.length; row++) {
                if(obj.pricePropertyCols.includes(col)){
                    let price = data[row]['priceProperty'] && data[row]['priceProperty'][obj.colMapping.colToField[col]] !== undefined
                        && data[row]['priceProperty'][obj.colMapping.colToField[col]] !== null
                        ? data[row]['priceProperty'][obj.colMapping.colToField[col]] : '';
                    sheet.setValue(row, col, price);
                }
                else if(obj.consumeAmtPropertyCols && obj.consumeAmtPropertyCols.includes(col)){
                    let consumeAmt = data[row]['consumeAmtProperty'] && data[row]['consumeAmtProperty'][obj.colMapping.colToField[col]] !== undefined
                    && data[row]['consumeAmtProperty'][obj.colMapping.colToField[col]] !== null
                        ? data[row]['consumeAmtProperty'][obj.colMapping.colToField[col]] : '';
                    sheet.setValue(row, col, consumeAmt);
                }
                else if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
                    let distTypeVal =  distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
                    sheet.setValue(row, col, distTypeVal, ch);
                }
                else if(setting.header[col].dataCode === 'select'){
                    if(data[row].isChecked === true){
                        sheet.getCell(row, col).value(1);
                    }
                }
                else {
                    sheet.setValue(row, col, data[row][setting.header[col].dataCode], ch);
                    sheet.setTag(row, 0, data[row].ID, ch);
                }
            }
        }
        sheet.resumeEvent();
        sheet.resumePaint();
    },
    combineRowData: function(sheet, setting, row, repositoryGljObj) {
        let me = this;
        var rst = {priceProperty: {}};
        let gljTypeCombo = sheet.getCellType(row, repositoryGljObj.colMapping.fieldToCol['gljType']);
        let items = gljTypeCombo.items();
        let codeCol = 0;
        for (var col = 0; col < setting.header.length; col++) {
            if(repositoryGljObj.pricePropertyCols.includes(col)){
                v = sheet.getValue(row, col);
                rst.priceProperty[setting.header[col].dataCode] = v && v !== '' ? v : 0;
            }
            else if(setting.header[col].dataCode === 'gljType'){
                items.forEach(function(item){
                    if(sheet.getValue(row, col) === item.text){
                        rst[setting.header[col].dataCode] = item.value;
                        if(repositoryGljObj){
                            rst.shortName = repositoryGljObj.distTypeTree.distTypes[repositoryGljObj.distTypeTree.prefix + item.value].data.shortName;
                        }
                    }
                });
            }
            else if (setting.header[col].dataCode === 'code'){
                codeCol = col;
                if(repositoryGljObj){
                    let gljList = repositoryGljObj.gljList,
                        orgCode = repositoryGljObj.orgCode,
                        isExist = false;
                    for(let i=0; i< gljList.length; i++){
                        if(gljList[i].code === sheet.getValue(row, col) && sheet.getValue(row, col)!== orgCode){
                            $('#alertText').text("输入的编号已存在，请重新输入!");
                            $('#codeAlertBtn').click();
                            $('#codAleConfBtn').click(function () {
                                sheet.setValue(row, codeCol, orgCode);
                                sheet.getCell(row, codeCol).formatter("@");
                                sheet.setActiveCell(row, codeCol);
                            });
                            $('#codAleClose').click(function () {
                                sheet.setValue(row, codeCol, orgCode);
                                sheet.getCell(row, codeCol).formatter("@");
                                sheet.setActiveCell(row, codeCol);
                            });
                            isExist = true
                        }
                    }
                    if(!isExist){
                        rst[setting.header[col].dataCode] = sheet.getValue(row, col);
                    }
                }
                else{
                    rst[setting.header[col].dataCode] = sheet.getValue(row, col);
                }
            }
            else{
                rst[setting.header[col].dataCode] = sheet.getValue(row, col);
            }
        }
        return rst;
    },
    combineRationRowData: function(sheet, setting, row) {
        var rst = {};
        for (var col = 0; col < setting.header.length; col++) {
            rst[setting.header[col].dataCode] = sheet.getValue(row, col);
        }
        return rst;
    },
    reLockSomeCodes: function (sheet, beginRow, endRow) {
        sheet.suspendPaint();
        sheet.suspendEvent();
        let defaultStyle = new GC.Spread.Sheets.Style();
        defaultStyle.locked = false;
        sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
        let unLockStyle = new GC.Spread.Sheets.Style();
        unLockStyle.locked = false;
        let lockStyle = new GC.Spread.Sheets.Style();
        lockStyle.locked = true;
        for(let row = beginRow; row < endRow; row++){
            sheet.setStyle(row, 0, lockStyle);
        }
        for(let row = endRow; row < sheet.getRowCount(); row++){
            sheet.setStyle(row, 0, unLockStyle);
        }
        sheet.options.isProtected = true;
        sheet.resumePaint();
        sheet.resumeEvent();
    },
    lockSomeCodes: function (sheet, beginRow, endRow) {
        let defaultStyle = new GC.Spread.Sheets.Style();
        defaultStyle.locked = false;
        sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
        let style = new  GC.Spread.Sheets.Style();
        style.locked = true;
        sheet.suspendPaint();
        sheet.suspendEvent();
        for(let i = beginRow; i < endRow; i++){
            sheet.setStyle(i, 0, style);
        }
        sheet.options.isProtected = true;
        sheet.resumePaint();
        sheet.resumeEvent();
    },
    lockCodeCells: function (sheet, rowCount) {
        sheet.suspendPaint();
        sheet.suspendEvent();
        let sheetRowCount = sheet.getRowCount();
        let defaultStyle = new GC.Spread.Sheets.Style();
        defaultStyle.locked = false;
        sheet.setDefaultStyle(defaultStyle, GC.Spread.Sheets.SheetArea.viewport);
        let style = new GC.Spread.Sheets.Style();
        style.locked = true;
        sheet.setStyle(-1, 0, style);
        for(let i =rowCount; i<sheetRowCount; i++){
            sheet.setStyle(i, -1, style);
        }
        sheet.options.isProtected = true;
        sheet.resumePaint();
        sheet.resumeEvent();
    }
};