/**
 * Created by CSL on 2017-06-14.
 */
var rationAssistOprObj = {
    sheet: null,
    libID: null,
    ration: null,
    setting: {
        header:[
            {headerName:"调整名称",headerWidth:110,dataCode:"name", dataType: "String", hAlign: "left"},
            {headerName:"参数",headerWidth:60,dataCode:"param", dataType: "String", hAlign: "right"},
            {headerName:"辅助定额号",headerWidth:90,dataCode:"assistCode", dataType: "String", hAlign: "center", formatter: "@"},
            {headerName:"参数名称",headerWidth:90,dataCode:"paramName", dataType: "String", hAlign: "left"},
            {headerName:"标准值",headerWidth:60,dataCode:"stdValue", dataType: "String", hAlign: "right"},
            {headerName:"步距",headerWidth:60,dataCode:"stepValue", dataType: "String", hAlign: "right"},
            {headerName:"精度",headerWidth:60,dataCode:"decimal",  dataType: "String", hAlign: "right"},
            {headerName:"进位方式",headerWidth:80,dataCode:"carryBit", dataType: "String", hAlign: "center"},
            {headerName:"最小值",headerWidth:70,dataCode:"minValue", dataType: "String", hAlign: "right"},
            {headerName:"最大值",headerWidth:70,dataCode:"maxValue", dataType: "String", hAlign: "right"},
            {headerName:"第三定额",headerWidth:90,dataCode:"thirdRationCode", dataType: "String", hAlign: "center", formatter: "@"}
        ],
        view:{},
        comboItems: ["四舍五入", "进一",'舍一']
    },

    buildSheet: function(sheet) {
        var me = this;
        me.sheet = sheet;
        me.libID = pageOprObj.rationLibId; // 不可靠，有时取不到
        if (me.libID == undefined){me.libID = getQueryString('repository')};

        sheetCommonObj.initSheet(me.sheet, me.setting, 30);

        me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasting, me.onClipboardPasting);
        me.sheet.bind(GC.Spread.Sheets.Events.ClipboardPasted, me.onClipboardPasted);
        me.sheet.bind(GC.Spread.Sheets.Events.EditStarting, me.onEditStarting);
        me.sheet.bind(GC.Spread.Sheets.Events.EditEnded, me.onEditEnded);
        me.sheet.bind(GC.Spread.Sheets.Events.EnterCell, me.onEnterCell);
    },

    onEnterCell: function (sender, args) {
        let me = rationAssistOprObj;
        args.sheet.repaint();
        let cellType = args.sheet.getCellType(args.row, 5);
        if(cellType.typeName !== 'undefined' && cellType.typeName === '1'){
          //  sheetCommonObj.setStaticCombo(args.sheet, 0, 5, 0, me.setting.comboItems, false);
            sheetCommonObj.setDynamicCombo(args.sheet, 0, 7, me.sheet.getRowCount(), me.setting.comboItems, false, false);
        }
    },

    onClipboardPasting: function(sender, args) {
        let me = rationAssistOprObj;
        let rationSection = rationOprObj.getCache();
        let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
        me.ration = rationRow < rationSection.length ? rationSection[rationRow] : null;
        if (!me.ration) {
            args.cancel = true;
        }
    },

    onClipboardPasted: function(e, info) {
        var me = rationAssistOprObj;
        if (!me.ration) {return;};
        var tempArr = sheetCommonObj.analyzePasteData(me.setting, info);
        var assList = me.ration.rationAssList;
        if (assList == undefined) {
            me.ration.rationAssList = tempArr;
        }else{
            assList = assList.concat(tempArr);
            me.ration.rationAssList = assList;
        };

        rationOprObj.mixUpdateRequest([me.ration], [], [], function () {
            me.sheet.getParent().focus(true);
        });
        sheetCommonObj.cleanData(me.sheet, me.setting, -1);
        //sheetCommonObj.setStaticCombo(me.sheet, 0, 5, me.ration.rationAssList.length, me.setting.comboItems, false, false);
        sheetCommonObj.setDynamicCombo(me.sheet, 0, 7, me.sheet.getRowCount(), me.setting.comboItems, false, false);
        sheetCommonObj.showData(me.sheet, me.setting, me.ration.rationAssList);
    },

    onEditStarting: function (sender, args) {
        let me = rationAssistOprObj;
        let rationSection = rationOprObj.getCache();
        let rationRow = rationOprObj.workBook.getSheet(0).getSelections()[0].row;
        me.ration = rationRow < rationSection.length ? rationSection[rationRow] : null;
        if(!me.ration){
            args.cancel = true;
        }
    },

    onEditEnded: function(sender, args){
        var me = rationAssistOprObj;
        if (!me.ration) {return;};
        if(typeof me.ration.rationAssList === 'undefined'){
            me.ration.rationAssList = [];
        }
        var assList = me.ration.rationAssList;
        var assObj = sheetsOprObj.combineRationRowData(me.sheet, me.setting, args.row);
        let dataCode = me.setting.header[args.col].dataCode;
        if((dataCode === 'stdValue' || dataCode === 'stepValue' || dataCode === 'minValue' || dataCode === 'maxValue')
            && args.editingText && args.editingText.toString().trim().length > 0 && isNaN(args.editingText)){
            args.sheet.setValue(args.row, args.col, args.row < assList.length ? assList[args.row][dataCode] : '');
            alert(me.setting.header[args.col].headerName + '只能为数值！');
            return;
        }
        else if(dataCode === 'decimal' && args.editingText && (args.editingText.toString().trim().length === 0 ||
                isNaN(args.editingText) || args.editingText % 1 !== 0)){
            args.sheet.setValue(args.row, args.col, args.row < assList.length ? assList[args.row][dataCode] : 0);
            alert(me.setting.header[args.col].headerName + '只能为整数！');
            return;
        }
        // 新增
        if (args.row >= assList.length) {
            if (assObj.decimal == undefined || assObj.decimal == null){assObj.decimal = '0';};
            if (assObj.carryBit == undefined || assObj.carryBit == null){assObj.carryBit = '四舍五入';};
            assList.push(assObj);
        }
        // 修改
        else{ assList[args.row] = assObj; };
        rationOprObj.mixUpdateRequest([me.ration], [], [], function () {
            me.sheet.getParent().focus(true);
        });
        sheetCommonObj.cleanData(me.sheet, me.setting, -1);
        //sheetCommonObj.setStaticCombo(me.sheet, 0, 5, assList.length, me.setting.comboItems, false, false);
        sheetCommonObj.setDynamicCombo(me.sheet, 0, 7, me.sheet.getRowCount(), me.setting.comboItems, false, false);
        sheetCommonObj.showData(me.sheet, me.setting, assList);
    },

    onRangeChanged: function(sender, args) {
        if (args.action == GC.Spread.Sheets.RangeChangedAction.clear) {
            if (!confirm(`确定要删除选中的 ${args.rowCount} 条辅助定额吗？`)){return; }

            var me = rationAssistOprObj;
            if (!me.ration) {return;};
            var assList = me.ration.rationAssList;
            for (var i = args.rowCount - 1; i >= 0; i--) {
                if (args.row + i < assList.length) {
                    assList.splice(args.row + i, 1);
                };
            };

            rationOprObj.mixUpdateRequest([me.ration], [], []);
            sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
            sheetCommonObj.showData(me.sheet, me.setting, assList);
        };
    },
    bindRationAssDel: function () {
        if (locked) {
            return;
        }
        let me = rationAssistOprObj;
        let workBook = me.sheet.getParent();
        workBook.commandManager().register('rationAssDel', function () {
            let sels = me.sheet.getSelections(), isUpdate = false;
            if(me.ration){
                let curCahe = me.ration.rationAssList;
                for(let i = 0, len = sels.length; i < len; i ++ ){
                    if(sels[i].colCount === me.setting.header.length){
                        if(sels[i].row < curCahe.length){
                            isUpdate = true;
                            curCahe.splice(sels[i].row, sels[i].rowCount);
                        }
                    }
                }
                if(isUpdate){
                    rationOprObj.mixUpdateRequest([me.ration], [], [], function () {
                        workBook.focus(true);
                    });
                    sheetCommonObj.cleanData(me.sheet, me.setting, -1);
                    //sheetCommonObj.setStaticCombo(me.sheet, 0, 5, curCahe.length, me.setting.comboItems, false);
                    sheetCommonObj.setDynamicCombo(me.sheet, 0, 7, me.sheet.getRowCount(), me.setting.comboItems, false);
                    sheetCommonObj.showData(me.sheet, me.setting, curCahe);
                }
            }
        });
        workBook.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
        workBook.commandManager().setShortcutKey('rationAssDel', GC.Spread.Commands.Key.del, false, false, false, false);
    },

    getAssItems: function(ration) {
        var me = this;
        me.ration = ration;

        sheetCommonObj.cleanData(me.sheet, me.setting, -1);
        if (!locked) {
            sheetCommonObj.unShieldAllCells(me.sheet);
        }

        if (ration == undefined || ration.rationAssList == undefined ||
            ration.rationAssList.length == 0){
            //sheetCommonObj.setStaticCombo(me.sheet, 0, 5, 0, me.setting.comboItems, false);
            sheetCommonObj.setDynamicCombo(me.sheet, 0, 7, me.sheet.getRowCount(), me.setting.comboItems, false, false);
            return;
        }
        else {
            //sheetCommonObj.setStaticCombo(me.sheet, 0, 5, ration.rationAssList.length, me.setting.comboItems, false);
            sheetCommonObj.setDynamicCombo(me.sheet, 0, 7, me.sheet.getRowCount(), me.setting.comboItems, false, false);
        }
        sheetCommonObj.showData(me.sheet, me.setting, ration.rationAssList);
    }
}