/**
 * Created by vian on 2017/3/16.
 */
var setSheet = {
    initSheet: function(spread, setting, isExtendPaste){
        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;
        spread.options.allowCopyPasteExcelStyle = false;
        spread.options.allowExtendPasteRange = isExtendPaste? true : false;
        spread.options.allowUserDragDrop = false;
        spread.options.allowContextMenu = false;
        spread.getActiveSheet().options.allowCellOverflow = false;
        spread.getActiveSheet().options.clipBoardOptions = GC.Spread.Sheets.ClipboardPasteOptions.values;
        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, arr){
        let arrL = arr.length;
        let rowCount = arrL + 10;
        /*if(arrL <10){
            rowCount = 10;
        }
        else {
            rowCount = arrL + 3;
        }*/
        sheet.setRowCount(rowCount, GC.Spread.Sheets.SheetArea.viewport);
    },
    formatter: function(sheet){
        sheet.setFormatter(-1, 0, "@", 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.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("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.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("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);
                let classifyStr = classify === 'jobs' ? 'job' : 'item';
                tools.deleteELes(controller.tree.selected[classify], ids, classifyStr);
                    //deleteFrontData
                    //tools.reshowData(sheet, controllers.tree.selected[classify], setting, true);
                    //deleteDB
                    billsAjax.updateBillsArr(userAccount, billsLibId, controller.tree.selected.getID(), ids, null, 'delete', classify, function(){
                        tools.orderReshowData(sheet, controller.tree.selected[classify], setting, classifyStr, true);
                    });
                    /*if(result.length > 0){
                        if(classify === 'jobs'){
                            /!*result.forEach(function(id){
                                controllers.tree.selected.jobs.forEach(function(obj){
                                    if(id === obj.job.data.id){
                                        controllers.tree.selected.jobs.splice(obj);
                                    }
                                });
                                /!*if(totalObj.findJob(id)){
                                    totalObj.jobsArr.splice(totalObj.jobsArr.indexOf(totalObj.findJob(id)), 1);
                                    delete  totalObj.jobs[totalObj.prefix + id];
                                }*!/
                            });*!/
                            //jobsAjax.deleteJobContent(billsLibId, result);
                            tools.orderReshowData(sheet, controllers.tree.selected.jobs, setting, 'job', true);
                        }
                        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(){
                let ids = tools.delIds(sheet);
                if(ids.length === 0){
                    return;
                }
                $.bootstrapLoading.start();
                CommonAjax.post('/stdBillsEditor/isUsed', {field: 'jobs', delIds: ids}, function (rstData) {
                    //被引用了
                    if(rstData.isUsed){
                        $('#delText').text('当前工作内容已被引用，不允许删除。');
                        $('#delConfirm').unbind('click');
                        $('#delConfirm').bind('click', function () {
                            $('#delAlert').modal('hide');
                            spread.focus(true);
                        });
                    }
                    else {
                        $('#delText').text('是否删除当前行？');
                        $('#delConfirm').unbind('click');
                        $('#delConfirm').bind('click', function () {
                            spread.suspendEvent();
                            ids.forEach(function(id){
                                var job = totalJobs.findJob(id);
                                totalJobs.jobsArr.splice(totalJobs.jobsArr.indexOf(job), 1);
                            });
                            function myCompareCode(a, b){
                                let valA = a.data.code,
                                    valB = b.data.code;
                                return valB- valA;
                            }
                            totalJobs.jobsArr.sort(myCompareCode);
                            maxJobsNumber = totalJobs.jobsArr.length > 0 ? totalJobs.jobsArr[0].data.code : 0;
                            tools.reshowData(sheet, totalJobs.jobsArr, setting, true);
                            billsAjax.removeTotal(userAccount, billsLibId, billsIds, ids, 'jobs');
                            spread.resumeEvent();
                            $('#delAlert').modal('hide');
                            spread.focus(true);
                        });
                    }
                    $.bootstrapLoading.end();
                }, function () {
                    $.bootstrapLoading.end();
                });
                $('#delAlert').modal('show');
            });
            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();
            /*$('#delConfirm').click(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);
                    });
                    function myCompareCode(a, b){
                        let valA = a.data.code,
                            valB = b.data.code;
                        return valB- valA;
                    }
                    totalItems.itemsArr.sort(myCompareCode);
                    maxItemsNumber = totalItems.itemsArr.length > 0 ? totalItems.itemsArr[0].code : 0;
                    tools.reshowData(sheet, totalItems.itemsArr, setting, true);
                    billsAjax.removeTotal(userAccount, billsLibId, billsIds, ids, '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();
                $('#delAlert').modal('hide');
            });*/
            spread.commandManager().register('myItemDelete', function(){
                let ids = tools.delIds(sheet);
                if(ids.length === 0){
                    return;
                }
                $.bootstrapLoading.start();
                CommonAjax.post('/stdBillsEditor/isUsed', {field: 'items', delIds: ids}, function (rstData) {
                    //被引用了
                    if(rstData.isUsed){
                        $('#delText').text('当前项目特征已被引用，不允许删除。');
                        $('#delConfirm').unbind('click');
                        $('#delConfirm').bind('click', function () {
                            $('#delAlert').modal('hide');
                            spread.focus(true);
                        });
                    }
                    else {
                        $('#delText').text('是否删除当前行？');
                        $('#delConfirm').unbind('click');
                        $('#delConfirm').bind('click', function () {
                            spread.suspendEvent();
                            ids.forEach(function(id){
                                var item = totalItems.findItem(id);
                                totalItems.itemsArr.splice(totalItems.itemsArr.indexOf(item), 1);
                            });
                            function myCompareCode(a, b){
                                let valA = a.data.code,
                                    valB = b.data.code;
                                return valB- valA;
                            }
                            totalItems.itemsArr.sort(myCompareCode);
                            maxItemsNumber = totalItems.itemsArr.length > 0 ? totalItems.itemsArr[0].data.code : 0;
                            tools.reshowData(sheet, totalItems.itemsArr, setting, true);
                            billsAjax.removeTotal(userAccount, billsLibId, billsIds, ids, '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();
                            $('#delAlert').modal('hide');
                            spread.focus(true);
                        });
                    }
                    $.bootstrapLoading.end();
                }, function () {
                    $.bootstrapLoading.end();
                });
                $('#delAlert').modal('show');
            });
            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(userAccount, billsLibId, 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(){

        }

    }
}

