/**
 * 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(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);
        }
        /*if(data.length === 0){
            for(let i = 0; i < sheet.getRowCount(); i++){
                sheet.getCell(i, 4, GC.Spread.Sheets.SheetArea.viewport).locked(false);
            }
        }*/
        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) {
                //var style = new GC.Spread.Sheets.Style();
                //style.formatter = setting.header[col].formatter;
                //sheet.setStyle(row,col,style,GC.Spread.Sheets.SheetArea.viewport);
                sheet.setFormatter(-1, col, setting.header[col].formatter, GC.Spread.Sheets.SheetArea.viewport);
            }
            for (var row = 0; row < data.length; row++) {
                //var cell = sheet.getCell(row, col, GC.Spread.Sheets.SheetArea.viewport);
                if(setting.header[col].dataCode === 'gljType' && data[row].gljType){
                   /* if(typeof repositoryGljObj !== 'undefined' && typeof repositoryGljObj.allowComponent !== 'undefined' && repositoryGljObj.allowComponent.indexOf(data[row].gljType) !== -1){
                        sheet.getCell(row, 4, GC.Spread.Sheets.SheetArea.viewport).locked(true);
                    }
                    else if(typeof repositoryGljObj !== 'undefined' && typeof repositoryGljObj.allowComponent !== 'undefined' && repositoryGljObj.allowComponent.indexOf(data[row].gljType) === -1){
                        sheet.getCell(row, 4, GC.Spread.Sheets.SheetArea.viewport).locked(false);
                    }*/
                    let distTypeVal =  distTypeTree.distTypes[distTypeTree.prefix + data[row].gljType].data.fullName;
                    sheet.setValue(row, col, distTypeVal, ch);
                }
                else {
                    sheet.setValue(row, col, data[row][setting.header[col].dataCode], ch);
                    sheet.setTag(row, 0, data[row].ID, ch);
                    /*if(typeof setting.owner !== 'undefined' && setting.owner !== 'gljComponent'){
                        sheet.getCell(row, 0, GC.Spread.Sheets.SheetArea.viewport).locked(true);
                    }*/
                }
            }
          /*  for(let i = data.length; i < sheet.getRowCount(); i++){
                sheet.getCell(i, 4, GC.Spread.Sheets.SheetArea.viewport).locked(false);
            }*/
        }
        sheet.resumeEvent();
        sheet.resumePaint();
        //me.shieldAllCells(sheet);
    },
    showDataForGljSel: function(sheet, setting, data, distTypeTree) {
        var me = this, ch = GC.Spread.Sheets.SheetArea.viewport;
        sheet.suspendPaint();
        sheet.suspendEvent();
        let checkBoxType = new GC.Spread.Sheets.CellTypes.CheckBox();
        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(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 {
                    sheet.setValue(row, col, data[row][setting.header[col].dataCode], ch);
                    sheet.setTag(row, 0, data[row].ID, ch);
                }
                //复选框
                if(setting.header[col].dataCode === 'isComplementary'){
                    sheet.setCellType(row, col, checkBoxType);
                    sheet.getCell(row, col).value(1);
                }
                //新增组成物表，选择复选框
                if(setting.header[col].dataCode === 'select'){
                    sheet.setCellType(row, col, checkBoxType)
                    if(data[row].isChecked === true){
                        sheet.getCell(row, col).value(1);
                    }
                }
            }
            for(let i = data.length; i < sheet.getRowCount(); i++){
                sheet.setCellType(i, 6, null);
            }
        }
        sheet.resumeEvent();
        sheet.resumePaint();
        //me.shieldAllCells(sheet);
    },
    combineRowData: function(sheet, setting, row, repositoryGljObj) {
        let me = this;
        var rst = {};
        let comboBoxCellType = sheet.getCellType(row, 5);
        let items = comboBoxCellType.items();
        for (var col = 0; col < setting.header.length; col++) {
            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'){
                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){
                           // sheetCommonObj.lockAllCells(sheet);
                            $('#alertText').text("输入的编号已存在，请重新输入!");
                            $('#codeAlertBtn').click();
                            $('#codAleConfBtn').click(function () {
                                // me.reLockSomeCodes(sheet, 0, repositoryGljObj.currentCache.length);
                               // sheetCommonObj.unLockAllCells(sheet);
                                //me.reLockSomeCodes(sheet, 0, repositoryGljObj.currentCache.length);
                                sheet.setValue(row, 0, orgCode);
                                sheet.getCell(row, 0).formatter("@");
                                sheet.setActiveCell(row, 0);
                            });
                            $('#codAleClose').click(function () {
                                //me.reLockSomeCodes(sheet, 0, repositoryGljObj.currentCache.length);
                                //sheetCommonObj.unLockAllCells(sheet);
                                //me.reLockSomeCodes(sheet, 0, repositoryGljObj.currentCache.length);
                                sheet.setValue(row, 0, orgCode);
                                sheet.getCell(row, 0).formatter("@");
                                sheet.setActiveCell(row, 0);
                            });
                            // sheet.setValue(row, col, orgCode);
                            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();
    }
};