/**
 * Created by chen on 2017/7/25.
 */

var gljContextMenu = {
    selectedRow :null,
    selectedCol:null,
    clipboard:null,
    loadGLJSpreadContextMenu: function () {
        $.contextMenu({
            selector: '#subSpread',
            build: this.onbuild,
            items: {
                "delete_glj": {
                    name: '删除工料机',
                    icon: 'fa-remove',
                    disabled: function () {
                        var sheetData = gljOprObj.sheetData;
                        var disable = true;
                        if(subSpread.getActiveSheetIndex()==0&&sheetData!=null&&sheetData.length>0&&gljContextMenu.selectedRow<sheetData.length){
                            disable=false
                        }
                        return disable;
                    },
                    callback: function () {
                        var sheetData = gljOprObj.sheetData;
                        var deleteRow = sheetData[gljContextMenu.selectedRow];
                        projectObj.project.ration_glj.updataOrdelete(deleteRow);
                    }
                }
            }
        });
    },
    loadQuantityDetailMenu:function () {
        $.contextMenu({
            selector: '#subSpread',
            build: this.onbuild,
            items: {
                "insert_detail": {
                    name: '插入行',
                    icon: 'fa-sign-in',
                    disabled: function () {
                        var sheetData = gljOprObj.detailData;
                        return gljContextMenu.selectedRow>sheetData.length;
                    },
                    callback: function () {
                        projectObj.project.quantity_detail.insertQuantityDetail(gljContextMenu.selectedRow);
                    }
                },
                "delete_detail": {
                    name: '删除行',
                    icon: 'fa-remove',
                    disabled: function () {
                        var sheetData = gljOprObj.detailData;
                        return gljContextMenu.selectedRow>sheetData.length-1;
                    },
                    callback:function () {
                        projectObj.project.quantity_detail.deleteQuantityDetail(gljContextMenu.selectedRow);
                    }
                },
                "move_up": {
                    name: '上移',
                    icon: 'fa-arrow-up',
                    disabled: function () {
                        var sheetData = gljOprObj.detailData;
                        return gljContextMenu.selectedRow==0||gljContextMenu.selectedRow>sheetData.length-1;
                    },
                    callback: function () {
                        projectObj.project.quantity_detail.moveUp(gljContextMenu.selectedRow);
                    }
                },
                "move_down": {
                    name: '下移',
                    icon: 'fa-arrow-down',
                    disabled: function () {
                        var sheetData = gljOprObj.detailData;
                        return gljContextMenu.selectedRow>sheetData.length-2;
                    },
                    callback: function () {
                        projectObj.project.quantity_detail.moveDown(gljContextMenu.selectedRow);
                    }
                },
                "copy": {
                    name: '复制',
                    icon: 'fa-files-o',
                    disabled: function () {
                        var sheet = subSpread.getActiveSheet();
                        var sheetData = gljOprObj.detailData;
                        var value = sheet.getCell(gljContextMenu.selectedRow,gljContextMenu.selectedCol).value();
                        return gljContextMenu.selectedRow>sheetData.length-1||value==null;
                    },
                    callback: function () {
                        gljContextMenu.clipboard={
                          row:gljContextMenu.selectedRow,
                          col:gljContextMenu.selectedCol
                        };
                    }
                },
                "paste": {
                    name: '粘贴',
                    icon: 'fa-clipboard',
                    disabled: function () {
                        var sheetData = gljOprObj.detailData;
                        return gljContextMenu.selectedRow>sheetData.length||gljContextMenu.clipboard==null;
                    },
                    callback: function () {
                        var sheet = subSpread.getActiveSheet();
                        var c=gljContextMenu.clipboard;
                        console.log(sheet.getCell(c.row,c.col).value());
                        var args={
                            'row':gljContextMenu.selectedRow,
                            'col':gljContextMenu.selectedCol,
                            'editingText':sheet.getCell(c.row,c.col).value()
                        }
                        gljOprObj.onEditDetailSheet(args);
                    }
                }
            }
        });

    },
    onbuild:function ($trigger, e) {
        var target = SheetDataHelper.safeRightClickSelection($trigger, e, subSpread);
        gljContextMenu.selectedRow = target.row;
        gljContextMenu.selectedCol = target.col;
        //controller.setTreeSelected(controller.tree.items[target.row]);
        return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
    }

}