/**
 * Created by vian on 2017/3/16.
 */
var setSheet = {
    initSheet: function(spread, setting){
        var spreadNS = GC.Spread.Sheets, sheet = spread.getActiveSheet();
        sheet.suspendPaint();
        spread.options.showHorizontalScrollbar = false;
        spread.options.showVerticalScrollbar =false;
        spread.options.tabStripVisible = false;
        spread.options.scrollbarMaxAlign = true;
        sheet.showRowOutline(false);
        sheet.defaults.rowHeight = 30;
        setting.cols.forEach(function(col, colIdx){
            sheet.getRange(-1,colIdx,-1,1, GC.Spread.Sheets.SheetArea.viewport).hAlign(GC.Spread.Sheets.HorizontalAlign.left);
            sheet.getRange(-1,colIdx,-1,1, GC.Spread.Sheets.SheetArea.viewport).vAlign(GC.Spread.Sheets.VerticalAlign.center);
            sheet.getRange(-1,colIdx,-1,1, GC.Spread.Sheets.SheetArea.viewport).textIndent(0.5);
        });
        sheet.resumePaint();
    },
    setEditCol: function(sheet, colIdx){
        var style = new GC.Spread.Sheets.Style();
        style.locked = false;
        sheet.setStyle(-1, colIdx, style);
        sheet.options.isProtected = true;
    },
    setMaxRowCount: function(sheet, rowCount){
        sheet.setRowCount(rowCount, GC.Spread.Sheets.SheetArea.viewport);
    }
}

function setScrollBar(spread, sheet, initRow){
    var events = GC.Spread.Sheets.Events;
    sheet.bind(events.EnterCell, function(e, data){
        var rowCount = sheet.getRowCount();
        //��ǰ�������ڳ�ʼ����
        if(rowCount >initRow){
            spread.options.showVerticalScrollbar = true;
        }
        else{
            spread.options.showVerticalScrollbar = false;
        }
    });
}

var myKey = {
    enterKey: function(spread){
        var sheet = spread.getActiveSheet();
        spread.commandManager().register("myEnter", function(){
            var idxRow = sheet.getActiveRowIndex();
            var rowCount = sheet.getRowCount();
            if(idxRow+1 == rowCount){
                sheet.addRows(rowCount, 1);
                sheet.setRowHeight(rowCount, 30);
                sheet.getRange(rowCount, -1, 1, -1).vAlign(GC.Spread.Sheets.VerticalAlign.center);
                sheet.getRange(rowCount, -1, 1, -1).hAlign(GC.Spread.Sheets.HorizontalAlign.left);
                sheet.getRange(rowCount, -1, 1, -1).textIndent(0.5);
                sheet.getRange(rowCount, -1, 1, -1).font("14px Arial");
            }
        });
        spread.commandManager().setShortcutKey("myEnter", GC.Spread.Commands.Key.enter, false, false, false, false);
    },

    downKey: function(spread){
        var sheet = spread.getActiveSheet();
        spread.commandManager().register("myDown", function(){
            var idxRow = sheet.getActiveRowIndex();
            var rowCount = sheet.getRowCount();
            if(idxRow+1 == rowCount){
                sheet.suspendPaint();
                sheet.addRows(rowCount, 1);
                sheet.setRowHeight(rowCount, 30);
                sheet.getRange(rowCount, -1, 1, -1).vAlign(GC.Spread.Sheets.VerticalAlign.center);
                sheet.getRange(rowCount, -1, 1, -1).hAlign(GC.Spread.Sheets.HorizontalAlign.left);
                sheet.getRange(rowCount, -1, 1, -1).textIndent(0.5);
                sheet.getRange(rowCount, -1, 1, -1).font("14px Arial");
                sheet.resumePaint();
            }
        });
        spread.commandManager().setShortcutKey("myDown", GC.Spread.Commands.Key.down, false, false, false, false);
    },


    delKey: function(spread, controller, totalObj, setting, classify){
        if(classify === 'jobs' || classify === 'items'){
            bindRefDel(spread, controller, totalObj, setting, classify);
        }
        else if(classify === 'totalJobs'){
            bindTotalJobDel(spread, controller, totalObj, setting);
        }
        else if(classify === 'totalItems'){
            bindTotalItemDel(spread, spreadVal.getActiveSheet(), controller, totalObj, setting);
        }
        else if(classify === 'itemValue'){
            bindValDel(spread, totalObj, setting);
        }
        else if(!classify){
            rejectDel(spread);
        }

        function bindRefDel(spread, controller, totalObj, setting, classify){
            var sheet = spread.getActiveSheet();
            spread.commandManager().register('myDelete', function(){
                spread.suspendEvent();
                var ids = tools.delIds(sheet);
                console.log(ids);
                tools.deleteELes(controller.tree.selected[classify], ids, function(result){
                    //deleteFrontData
                    tools.reshowData(sheet, controller.tree.selected[classify], setting, true);
                    //deleteDB
                    billsAjax.updateBillsArr(billsLibId, controller.tree.selected.getID(), ids, null, 'delete', classify);
                    if(result.length > 0){
                        if(classify === 'jobs'){
                            result.forEach(function(id){
                                if(totalObj.findJob(id)){
                                    totalObj.jobsArr.splice(totalObj.jobsArr.indexOf(totalObj.findJob(id)), 1);
                                    delete  totalObj.jobs[totalObj.prefix + id];
                                }
                            });
                            jobsAjax.deleteJobContent(result);
                        }
                        else {
                            result.forEach(function(id){
                                if(totalObj.findItem(id)){
                                    totalObj.itemsArr.splice(totalObj.itemsArr.indexOf(totalObj.findItem(id)), 1);
                                    delete totalObj.items[totalObj.prefix + id];
                                }
                            });
                            itemsAjax.deleteItemCharacter(result);
                        }
                    }
                });
                spread.resumeEvent();
            });
            spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
            spread.commandManager().setShortcutKey('myDelete', GC.Spread.Commands.Key.del, false, false, false, false);
        }

        function rejectDel(spread){
            var sheet = spread.getActiveSheet();
            spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
        }

        function bindTotalJobDel(spread, billsIds, totalJobs, setting){
            var sheet = spread.getActiveSheet();
            spread.commandManager().register('myJobDelete', function(){
                spread.suspendEvent();
                var ids = tools.delIds(sheet);
                if(ids.length > 0){
                    ids.forEach(function(id){
                        var job = totalJobs.findJob(id);
                        totalJobs.jobsArr.splice(totalJobs.jobsArr.indexOf(job), 1);
                    });
                    tools.reshowData(sheet, totalJobs.jobsArr, setting, true);
                    jobsAjax.deleteJobContent(ids);
                    billsAjax.updateBillsArr(billsLibId, billsIds, ids, null, 'deleteAll', 'jobs');
                }
                spread.resumeEvent();
            });
            spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
            spread.commandManager().setShortcutKey('myJobDelete', GC.Spread.Commands.Key.del, false, false, false, false)
        }

        function bindTotalItemDel(spread, valSheet, billsIds, totalItems, setting){
            var sheet = spread.getActiveSheet();
            spread.commandManager().register('myItemDelete', function(){
                spread.suspendEvent();
                var ids = tools.delIds(sheet);
                if(ids.length > 0){
                    ids.forEach(function(id){
                        var item = totalItems.findItem(id);
                        totalItems.itemsArr.splice(totalItems.itemsArr.indexOf(item), 1);
                    });
                    tools.reshowData(sheet, totalItems.itemsArr, setting, true);
                    itemsAjax.deleteItemCharacter(ids);
                    billsAjax.updateBillsArr(billsLibId, billsIds, ids, null, 'deleteAll', 'items');
                    //reshowVal
                    if(totalItems.itemsArr.length > 0){
                        selectedId = totalItems.itemsArr[0].data.id;
                        tools.reshowValue(valSheet, totalItems.itemsArr[0].data.itemValue, eigenValueSetting, true);
                    }
                    else {
                        tools.clearData(valSheet);
                    }
                }
                spread.resumeEvent();
            });
            spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
            spread.commandManager().setShortcutKey('myItemDelete', GC.Spread.Commands.Key.del, false, false, false, false)
        }

        function bindValDel(spread, totalItems, setting){
            var sheet = spread.getActiveSheet();
            spread.commandManager().register('valDelete', function(){
                spread.suspendEvent();
                var ids = tools.delIds(sheet);
                //deleteFront
                var valsArr = valueController.getValues(totalItems, selectedId);
                ids.forEach(function(id){
                    valsArr.forEach(function(val){
                        if(val.code === id){
                            valsArr.splice(valsArr.indexOf(val), 1);
                        }
                    });
                });
                tools.reshowValue(sheet, valsArr, setting, true);
                //deleteDb
                itemsAjax.updateValue(selectedId, null, ids, 'delete');
                spread.resumeEvent();
            });
            spread.commandManager().setShortcutKey(null, GC.Spread.Commands.Key.del, false, false, false, false);
            spread.commandManager().setShortcutKey('valDelete', GC.Spread.Commands.Key.del, false, false, false, false);
        }

        function reSerialNo(){

        }

    }
}

