<script>
import Base from './basegrid.vue';
export default {
    name: 'editgridbase',
    extends: Base,
    data() {
        return {
            myConfig: {
                resources: '',
                method: '',
                response: '',
                searchParam: null,
                readOnly: false,
                showIndex: true,
                showCheckBox: true,
                multiSelect: true,
                addRowBefore: '',
                name: '',
                editItemMaxNum: 8,
                lineNum: 4,
                labelWidth: 100,
                showAddBtn: true,
                showDelBatchBtn: true,
                showZoom:false,
                delBatchBtnName: '删除',
                addBtnName: '新增',
                editBtnName: '编辑',
                cancelBtnName: '放弃',
                showPage: false
            },
            showAddBtn: true,
            showDelBatchBtn: true,
            showDelBtn: true,
            showZoom:false,
            delBatchBtnName: '删除',
            addBtnName: '新增',
            showEditRowForm: false,
            defaultRow: {},
            editItemNum: 0,
            rowAction: [{
                iconcls: 'row',
                name: 'editRowForm',
                text: '编辑',
            }, ],
            openChange: false,
            focusIndex: null,
            focusKey: null,
        };
    },
    computed: {
        colKeys() {
            return this.soltTableColumns.map(c => c.key);
        }
    },
    /*beforeUnmount() {
        document.removeEventListener('keydown', this.handleKeydown);
    },*/
    methods: {
        customInit() {
            if (this.myConfig.items && this.myConfig.items.length > 0) {
                this.myConfig.items.forEach(el => {
                    if (el.visible !== false) {
                        this.editItemNum++;
                    }
                });
            }
            if (this.editItemNum > Number(this.myConfig.editItemMaxNum)) {
                //启动行表单编辑模式
                this.showRowAction = true;
                this.myToolConfig.items = this.$Method.copy(this.rowAction);
                this.editRowFlag = false;
            } else {
                this.toolConfig.items = [];
                this.showRowAction = false;
                this.editRowFlag = true;
            }

            if (this.showDelRow) {
                if (!this.myToolConfig.items) {
                    this.myToolConfig.items = [];
                }
                this.myToolConfig.items = this.myToolConfig.items.concat(this.delRowAction);
            }
            if (this.myConfig.showAddBtn !== undefined) {
                this.showAddBtn = this.myConfig.showAddBtn;
            }
            if (this.myConfig.showAddBtn !== undefined) {
                this.showAddBtn = this.myConfig.showAddBtn;
            }
            if (this.myConfig.addBtnName !== undefined) {
                this.addBtnName = this.myConfig.addBtnName;
            }
            if (this.myConfig.showDelBatchBtn !== undefined) {
                this.showDelBatchBtn = this.myConfig.showDelBatchBtn;
            }
            if (this.myConfig.showZoom !== undefined) {
                this.showZoom = this.myConfig.showZoom;
            }

            if (this.myConfig.showPage) {
                this.myConfig.indexMethod = this.customIndexMethod;
                if (this.myConfig.pageSizeNum) {
                    this.myConfig.pageConfig.page_size = Number(this.myConfig.pageSizeNum);
                } else {
                    this.myConfig.pageConfig.page_size = 20;
                }
            }
            if (this.customInit2) {
                this.customInit2();
            }
            this.myConfig.getGridObject = this.getGridObject;
            //监听键盘事件
           /* document.addEventListener('keydown', (e) => {       
                this.handleKeydown(e);
            })*/
        },
        getGridObject() {
            let that = this;
            return that;
        },
        conditionAction(temp) {
            let data = [];
            switch (temp.action) {
                case 'condition_search':
                    if (temp.para) {
                        this.conditionSearchParam = this.$Method.copy(temp.para);
                    }
                    this.refurbish();
                    break;
                case 'condition_clear':
                    this.conditionSearchParam = {};
                    break;
                case 'condition_ok':
                    if (this.myConfig.multiSelect) {
                        data = this.getSelection();
                    } else {
                        if (this.singleData.currentRow) {
                            data.push(this.singleData.currentRow);
                        }
                    }
                    if (data.length === 0) {
                        this.alert('请选择');
                    } else {
                        temp.action = 'viewgrid_ok';
                        temp.rows = data;
                        this.$emit('viewGridAction', temp);
                    }
                    break;
                case 'condition_cancel':
                    temp.action = 'viewgrid_cancel';
                    this.$emit('viewGridAction', temp);
                    break;
            }
        },
        /*getSelection() {
            return this.$refs.table.getSelection();
        },*/
        addRow(obj) {
            if (this.checkIsEdit) {
                if (!this.checkIsEdit()) {
                    return;
                }
            }
            if (this.myConfig.readOnly) {
                this.setReadOnly(false);
            }
            let row = null;
            if (this.myConfig.addRowBefore) {
                let temp = {};
                temp.name = this.myConfig.name;
                temp.grid = this.$refs.table;
                row = this.myConfig.addRowBefore(temp);
                if (row === false) {
                    return;
                }
            }
            if (!row || row === true) {
                row = {};
            }
            if (obj) {
                row = Object.assign({}, row, obj);
            }
            this.initScrollLeft();
            this.cancelCellEditing();
            this.$nextTick(() => {
                row.flag = 'I';
                let newRow = Object.assign({}, this.newRow, row);
                this.data.push(newRow);
                let tempObj = {};
                tempObj.name = 'editGrid';
                tempObj.gridname = this.myConfig.name;
                this.$emit('doAction', tempObj);
            });
        },
        setPro(obj) {
            if (obj) {
                this.myConfig = Object.assign({}, this.myConfig, obj);
            }
        },
        setReadOnly(value) {
            let obj = {};
            obj.readOnly = value;
            if (this.config.showCheckBox !== false) {
                if (obj.readOnly) {
                    obj.showCheckBox = false;
                } else {
                    obj.showCheckBox = true;
                }
            }
            this.cancelCellEditing();
            this.setPro(obj);
        },
        delBatchRow() {
            this.delCommonRow(null);
        },
        delRow(index) {
            this.delCommonRow(index);
        },
        delCommonRow(index) {
            let row = null;
            let self = this;
            if (this.myConfig.delRowBefore) {
                let obj = {};
                obj.name = this.myConfig.name;
                obj.grid = this.$refs.table;
                obj.index = index;
                row = this.myConfig.delRowBefore(obj);
                if (row === false) {
                    return;
                }
            }
            this.cancelCellEditing();
            if (index || index === 0) {
                this.remove(index);
            } else {
                let selectionRows = this.getSelection();
                const length = selectionRows.length;
                for (let i = 0; i < length; i++) {
                    this.remove(selectionRows[i]._customIndex, true);
                }
            }
            if (this.myConfig.delRowAfter) {
                let obj = {};
                obj.name = this.myConfig.name;
                obj.grid = self;
                this.$nextTick(() => {
                    this.myConfig.delRowAfter(obj);
                });
            }
        },
        updataRow(index, obj) {
            let data = this.getData();
            if (this.myConfig.showPage) {
                let start = (this.myConfig.pageConfig.page_no - 1) * this.myConfig.pageConfig.page_size,
                    end = start + this.myConfig.pageConfig.page_size;
                let row = data[index];
                let flag = true;
                if (index >= start && index < end) {
                    flag = false;
                }
                if (row.flag === undefined) {
                    obj.flag = 'U';
                }
                obj = Object.assign({}, row, obj);
                if (!flag) {
                    let curlIndex = index - start;
                    this.data.splice(curlIndex, 1, obj);
                } else {
                    if (row.flag === 'I') {
                        let addIndex = this.getAddIndex(index);
                        if (addIndex > -1) {
                            this.data.splice(this.getAddCurlIndex(addIndex), 1, obj);
                        }
                    } else {
                        this.initData.splice(index, 1, obj);
                    }
                }
            } else {
                if (data[index].flag === undefined) {
                    obj.flag = 'U';
                }
                obj = Object.assign({}, data[index], obj);
                this.data.splice(index, 1, obj);
            }
            if (this.showEditRowForm) {
                if (this.$refs.rowForm) {
                    if (index === this.$refs.rowForm.getEditIndex()) {
                        this.$refs.rowForm.setData(obj);
                    }
                }
            }
        },
        updateRow(index, obj) {
            this.updataRow(index, obj);
        },
        initRowForm(obj) {
            if (obj.rowinfo.row.flag === 'D') {
                return;
            }
            this.showEditRowForm = true;
            this.$refs.rowForm.setEditIndex(obj.rowinfo.index);
            this.$refs.rowForm.setData(obj.rowinfo.row);
        },
        OnSelectionChange(row) {
            if (this.myConfig.onSelectionChange) {
                this.myConfig.onSelectionChange(row);
            }
            let data = this.$Method.copy(this.data);
            let flag = false;
            data.forEach((el, index) => {
                flag = false;
                row.forEach(el2 => {
                    if (index === el2._customIndex) {
                        flag = true;
                        return true;
                    }
                });
                el._checked = flag;
            })
            this.data = data;
        },
        customInitOperColumn(action, columns) {
            if (this.myConfig.readOnly !== true) {
                if (this.showDelRow === false) {
                    let index = this.myToolConfig.items.findIndex(el => el.name === 'delRow');
                    if (index > -1) {
                        this.myToolConfig.items.splice(index, 1);
                    }
                    if (this.myToolConfig.items.length > 0) {
                        columns.push(action);
                    }
                } else {
                    columns.push(action);
                }
            }
        },
        editRow() {
            let row = this.$refs.rowForm.getData();
            this.$refs.rowForm.cancelCellEditing();
            this.updateRow(this.$refs.rowForm.getEditIndex(), row);
        },
        getChangeData() {
            let changeData = [];
            let data = this.getData();
            if (data.length > 0) {
                data.forEach(el => {
                    if (el.flag !== undefined) {
                        changeData.push(el);
                    }
                });
            }
            if (this.myConfig.showPage) {
                return changeData;
            } else {
                return this.delData.concat(changeData);
            }
        },
        isChange() {
            let data = this.getChangeData();
            if (data.length > 0) {
                return true;
            }
            return false;
        },
        setShowAddBtn(flag) {
            this.showAddBtn = flag;
        },
        setShowDelBatchBtn(flag) {
            this.showDelBatchBtn = flag;
        },
        setCustomColumn(column) {
            //column.render= this.commonRendel;
            column.slot = column.key;
        },
        addRowInfo(obj, column) {
            let index = this.editIndex;
            obj.rowinfo = {};
            obj.rowinfo.column = column;
            if (index === -1) {
                index = this.oldEditIndex;
            }
            obj.rowinfo.index = index;
            if (index > -1) {
                obj.rowinfo.row = this.$Method.copy(this.data[index]);
            }
            let that = this;
            obj.rowinfo.gridObject = that;
        },
        valueChanged(obj) {
            const column = this.findColumnConfig(obj.name, this.myConfig.items);
            this.addRowInfo(obj, column);
            return column.valueChanged(obj);
        },
        onLinkTo(obj) {
            if (this.myConfig.readOnly) {
                const column = this.findColumnConfig(obj.name, this.myConfig.items);
                this.addRowInfo(obj, column);
                obj.rowinfo.index = obj.rowIndex;
                if (obj.rowIndex > -1) {
                    obj.rowinfo.row = this.$Method.copy(this.data[obj.rowIndex]);
                }
                return column.onLinkTo(obj);
            } else {
                return;
            }
        },
        searchBefore(that) {
            const column = this.findColumnConfig(that.myConfig.name, this.myConfig.items);
            this.addRowInfo(that, column);
            return column.searchBefore(that);
        },
        setDataBefore(obj, that) {
            const column = this.findColumnConfig(that.myConfig.name, this.myConfig.items);
            let temp = {};
            temp.data = obj;
            temp.tabname = this.myConfig.name;
            this.addRowInfo(temp, column);
            return column.setDataBefore(temp, that);
        },
        cancelCellEditing() {
            let name = 'cell_' + this.editIndex + '_' + this.editKey;
            if (this.$refs[name] && this.$refs[name].setFocus) {
                this.$refs[name].setFocus(false);
            }
            let self = this;
            this.$nextTick(() => {
                self.editKey = '';
                self.oldEditIndex = this.editIndex;
                self.editIndex = -1;
            });
        },
        IsCellError(index, config) {
            let error = '';
            let row = this.$Method.copy(this.data[index]);
            if (row.flag === 'D') {
                error = 'efutre-del-row';
            } else {
                if (config.required === true) {
                    if (!row[config.name] && row[config.name] !== 0) {
                        let checkIndex = index;
                        if (this.myConfig.showPage) {
                            if (row.flag === 'I') {
                                checkIndex = this.getAddIndex(index, true);
                                if (this.checkAddInfo.length > checkIndex && this.checkAddInfo[checkIndex][config.key]) {
                                    error = 'efuture-grid-cell-error';
                                }
                            } else {
                                checkIndex = checkIndex + (this.myConfig.pageConfig.page_no - 1) * this.myConfig.pageConfig.page_size;
                                if (this.checkInfo.length > checkIndex && this.checkInfo[checkIndex][config.key]) {
                                    error = 'efuture-grid-cell-error';
                                }
                            }
                        } else {
                            if (this.checkInfo.length > checkIndex && this.checkInfo[checkIndex][config.key]) {
                                error = 'efuture-grid-cell-error';
                            }
                        }
                    }
                }
            }
            if (!error) {
                error = 'efuture-grid-cell';
            }
            return error;
        },
        EditCell(index, config) {
            //this.setEditGridFocus(index, config.key);
            if (this.myConfig.readOnly) {
                this.cancelCellEditing();
                return;
            }
            if (config.readOnly) {
                this.cancelCellEditing();
                return;
            }
            let row = this.getCurRow(index);
            if (this.myConfig.showPage) {
                if (row.flag === 'D') {
                    return;
                }
            }
            if (this.editIndex === index && this.editKey === config.key) {
                return;
            }
            if (this.myConfig.cellbeginedit) {
                let rowinfo = {};
                rowinfo.index = index;
                rowinfo.column = config;
                rowinfo.tabname = this.myConfig.name;
                rowinfo.row = row;
                if (!this.myConfig.cellbeginedit(rowinfo)) {
                    this.cancelCellEditing();
                    return;
                } else {
                    this.editIndex = index;
                    this.editKey = config.key;
                }
                this.setcheckInfo(index, config.key);
            } else {
                this.editIndex = index;
                this.editKey = config.key;
                this.setcheckInfo(index, config.key);
            }
        },
        // 设置焦点位置
        setEditGridFocus(rowIdx, colKey) {
            this.focusIndex = rowIdx;
            this.focusKey = colKey;
            this.editIndex = rowIdx;
            this.editKey = colKey;
            this.focusCell(rowIdx, colKey);
        },
        // 设置键盘选中单元格样式
        focusCell(rowIdx, colKey) {
            this.$nextTick(() => {
                const tableEl = this.$refs.table.$el;
                const selector = `div[cell-row="${rowIdx}"][cell-col="${colKey}"]`;
                let curElement = tableEl.querySelector(selector);
                if(curElement) {
                    const arr = tableEl.querySelectorAll('.ivu-table-column-selected');
                    if(arr.length != 0) {
                        arr.forEach(el => {
                            el.classList.remove('ivu-table-column-selected');
                        })
                    }

                    const cellDiv = curElement.closest('td');
                    if (cellDiv) {
                        cellDiv.classList.add('ivu-table-column-selected');
                        cellDiv.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
                    }
                }
            });
        },
        // 键盘导航事件
        handleKeydown(event) {
            if (this.focusIndex === null || this.focusKey === null) return;
            const colIdx = this.colKeys.findIndex(it => { return it == this.focusKey});

            this.focusKey = this.colKeys[colIdx];
            let newRow = this.focusIndex;
            let newCol = colIdx;

            let name = 'cell_' + this.editIndex + '_' + this.editKey;
            const { key } = event;
            
            if(key == 'ArrowUp') {
                if (this.focusIndex > 0) newRow--;
                this.extEditStatus();
            }
            if(key == 'ArrowDown') {
                if (this.focusIndex < this.data.length - 1) newRow++;
                this.extEditStatus();
            }
            if(key == 'ArrowLeft') {
                if (colIdx > 1) newCol--;
                this.extEditStatus();
            }
            if(key == 'ArrowRight') {
                if (colIdx < this.colKeys.length - 1) newCol++;
                this.extEditStatus();
            }
            
            if(key == 'Enter') {
                if (colIdx < this.colKeys.length - 1) newCol++;
            }

            this.setEditGridFocus(newRow, this.colKeys[newCol]);
            if(!this.myConfig.readOnly) {
                // 解决键盘按下input框的值不修改问题           
                if (this.$refs[name] && this.$refs[name].autofocus) {
                    return true;
                }
            } else {
                this.handleOut();
            }
            event.preventDefault();
        },
        extEditStatus() {
            if(!this.myConfig.readOnly) {
                //this.handleOut();
                let name = 'cell_' + this.editIndex + '_' + this.editKey;
                if (this.$refs[name] && this.$refs[name].setFocus) {
                    this.$refs[name].setFocus(false);
                }
                let self = this;
                this.$nextTick(() => {
                    self.editKey = '';
                    self.oldEditIndex = this.editIndex;
                    //self.editIndex = -1;
                });
            }
        },
        setcheckInfo(index, key) {
            let checkIndex = index;
            let row = this.getCurRow(index);
            if (this.myConfig.showPage) {
                checkIndex = checkIndex + (this.myConfig.pageConfig.page_no - 1) * this.myConfig.pageConfig.page_size;
                if (row.flag === 'I') {
                    this.setCheckCommonInfo(this.checkAddInfo, this.getAddIndex(index, true), key);
                } else {
                    this.setCheckCommonInfo(this.checkInfo, checkIndex, key);
                }
            } else {
                this.setCheckCommonInfo(this.checkInfo, checkIndex, key);
            }
        },
        setCheckCommonInfo(data, index, key) {
            if (data.length > index) {
                this.checkInfo[index][key] = true;
            } else {
                let curNum = data.length;
                for (let i = 0; i < index - curNum + 1; i++) {
                    data.push({});
                }
            }
            data[index][key] = true;
        },
        formatValue(index, column) {
            let value = '';
            let row = this.$Method.copy(this.data[index]);
            if (column.name) {
                value = row[column.name];
            }
            let config = column;
            if (column.updateCellConfig) {
                let rowinfo = {};
                rowinfo.index = index;
                rowinfo.column = column;
                rowinfo.row = this.$Method.copy(this.data[index]);
                config = column.updateCellConfig(rowinfo);
            }
            let valueFormat = this.$Method.formatValue(value, row, config);
            let obj = {};
            obj.value = value;
            obj.row = row;
            obj.config = config;
            obj.valueFormat = valueFormat;
            if (column.formatValue) {
                return column.formatValue(obj);
            } else if (this.myConfig.formatValue) {
                return this.myConfig.formatValue(obj);
            } else {
                return valueFormat;
            }
        },
        getCellConfig(index, config) {
            const column = this.findColumnConfig(config.name, this.myConfig.items);
            if (column.updateCellConfig) {
                let rowinfo = {};
                rowinfo.index = index;
                rowinfo.column = config;
                rowinfo.row = this.$Method.copy(this.data[index]);
                let newConfig = column.updateCellConfig(rowinfo);
                newConfig.onOpenChange = this.onOpenChange;
                newConfig.place = 'grid';
                return newConfig;
            } else {
                config.onOpenChange = this.onOpenChange;
                config.place = 'grid';
                return config;
            }
        },
        onOpenChange(value) {
            this.openChange = value;
        },
        getCurRow(index) {
            return this.$Method.copy(this.data[index]);
        },
        handleOut() {
            if (this.editKey) {
                if (!this.openChange) {
                    // 去除编辑状态
                    this.cancelCellEditing();
                }
            }
        },
        checkValidate() {
            // check 去除编辑状态
            this.cancelCellEditing();
            let flag = false;
            let data = this.getData();
            if (data.length > 0) {
                let keys = [];
                this.soltTableColumns.some(el => {
                    if (el.name || el.sname) {
                        if (el.visible !== false && el.required === true) {
                            if (el.name) {
                                keys.push(el.name);
                            }
                            if (el.sname) {
                                keys.push(el.sname);
                            }
                            if (el.ename) {
                                keys.push(el.ename);
                            }
                        }
                    }
                });
                let addIndex = -1;
                data.forEach((el, index) => {
                    if (el.flag === 'I') {
                        addIndex++;
                    }
                    if (el.flag !== 'D') {
                        keys.forEach(key => {
                            if (!el[key] && el[key] !== 0) {
                                if (!flag) flag = true;
                                if (this.myConfig.showPage) {
                                    if (el.flag === 'I') {
                                        this.setCheckCommonInfo(this.checkAddInfo, addIndex, key);
                                    } else {
                                        this.setCheckCommonInfo(this.checkInfo, index, key);
                                    }
                                } else {
                                    this.setCheckCommonInfo(this.checkInfo, index, key);
                                }

                            }
                        });
                    }
                });
            }
            return flag;
        },
        setShowBtn(value) {
            this.showBtn = value;
        },
        doAction(obj) {
            if (this[obj.name]) {
                this[obj.name](obj);
            }
        },
        enterKeyDown() {
            let nextKey = this.findNextItems(this.editIndex, this.editKey);
            if (nextKey) {
                this.editIndex = nextKey.editIndex;
                this.editKey = nextKey.editKey;
                this.setcheckInfo(nextKey.editIndex, nextKey.editKey);
                //this.setEditGridFocus(this.editIndex, this.editKey);
            }
        },
        findNextItems(editIndex, editName) {
            let index = this.soltTableColumns.findIndex(el => {
                return el.name === editName || el.sname === editName;
            });
            let nextIndex = index + 1;
            if (index > -1) {
                if (nextIndex >= this.soltTableColumns.length) {
                    if (editIndex + 1 >= this.data.length) {
                        editIndex = this.findNextRow(0);
                        nextIndex = 0;
                        if (this.soltTableColumns[0].type === 'selection' || this.soltTableColumns[0].type === 'index') {
                            nextIndex++;
                        }
                        if (this.soltTableColumns[1].type === 'index') {
                            nextIndex++;
                        }
                    } else {
                        let temp = editIndex + 1;
                        editIndex = this.findNextRow(temp);
                        nextIndex = 0;
                        if (this.soltTableColumns[0].type === 'selection' || this.soltTableColumns[0].type === 'index') {
                            nextIndex++;
                        }
                        if (this.soltTableColumns[1].type === 'index') {
                            nextIndex++;
                        }
                    }
                }
            } else {
                return null;
            }
            let column = this.soltTableColumns[nextIndex];
            let config = this.getCellConfig(editIndex, column);
            let name = config.name;
            if (config.sname) {
                name = config.sname;
            }
            if (config.readOnly === true || config.visible === false || config.type === 'PictureBox' || config.type === 'VideoBox' || config.type === 'RichTextBox' || config.type === 'wRichTextBox'|| config.type === 'selection' || config.type === 'index') {
                return this.findNextItems(editIndex, name);
            } else {
                if (this.myConfig.cellbeginedit) {
                    let rowinfo = {};
                    rowinfo.index = editIndex;
                    rowinfo.column = config;
                    rowinfo.tabname = this.myConfig.name;
                    rowinfo.row = this.$Method.copy(this.data[editIndex]);
                    if (!this.myConfig.cellbeginedit(rowinfo)) {
                        return this.findNextItems(editIndex, name);
                    } else {
                        let nextKey = {};
                        nextKey.editIndex = editIndex;
                        nextKey.editKey = name;
                        return nextKey;
                    }
                } else {
                    let nextKey = {};
                    nextKey.editIndex = editIndex;
                    nextKey.editKey = name;
                    return nextKey;
                }
            }
        },
        customIndexMethod(row, index) {
            if (row.flag === 'I') {
                return '+';
            } else {
                return index;
            }
        },
        findNextRow(editIndex) {
            let resultIndex = editIndex;
            if (this.myConfig.showPage) {
                this.data.some((el, index) => {
                    if (index >= editIndex) {
                        if (el.flag !== 'D') {
                            resultIndex = index;
                            return true;
                        }
                    }
                });
            }
            return resultIndex;
        },
        rowClassName(row) {
            if (row.flag === 'D') {
                return 'efutre-del-row';
            }
            return '';
        },
        getCellClass(index) {
            let row = this.getCurRow(index);
            if (row && row.flag === 'D') {
                return 'efutre-del-row';
            } else {
                return '';
            }
        },
        changePageSize(pageSize) {
            this.saveCurlData(this.myConfig.pageConfig.page_no, this.initData);
            this.myConfig.pageConfig.page_no = 1;
            this.data = this.fillData(this.myConfig.pageConfig.page_no, pageSize);
            this.myConfig.pageConfig.page_size = pageSize;
            this.cancelCellEditing();
        },
        changePage(page) {
            this.saveCurlData(this.myConfig.pageConfig.page_no, this.initData);
            this.data = this.fillData(page, this.myConfig.pageConfig.page_size);
            this.myConfig.pageConfig.page_no = page;
            this.cancelCellEditing();
        },
        customClearData() {
           this.initScrollLeft();
        },
        saveCurlData(pageIndex, data) {
            let addData = [];
            this.data.forEach((el, index) => {
                let curlIndex = (pageIndex - 1) * this.myConfig.pageConfig.page_size + index;
                if (el.flag === 'U' || el.flag === 'D') {
                    data.splice(curlIndex, 1, el);
                } else {
                    if (el.flag === 'I') {
                        addData.push(el);
                    }
                }
            });
            this.addData = addData;
        },
        setData(data) {
            if (!data) {
                data = [];
            }
            if (this.myConfig.showPage) {
                this.initData = this.$Method.copy(data);
                this.myConfig.pageConfig.page_no = 1;
                this.myConfig.pageConfig.total_results = data.length;
                this.data = this.fillData(this.myConfig.pageConfig.page_no, this.myConfig.pageConfig.page_size);
            } else {
                this.data = data;
                this.initData = this.$Method.copy(this.data);
            }
            this.addData = [];
            this.delData = [];
            this.checkInfo = [];
            this.initScrollLeft();
            this.cancelCellEditing();
        },
        fillData(pageIndex, pageSize) {
            let arr = [];
            let start = (pageIndex - 1) * pageSize,
                end = start + pageSize;
            if (end > this.myConfig.pageConfig.total_results) {
                end = this.myConfig.pageConfig.total_results;
            }
            for (var i = start, l = end; i < l; i++) {
                var record = this.initData[i];
                if (!record)
                    continue;
                arr.push(record);
            }
            this.addData.forEach(el => {
                arr.push(el);
            });
            return arr;
        },
        getData() {
            if (this.myConfig.showPage) {
                let initData = this.$Method.copy(this.initData);
                this.saveCurlData(this.myConfig.pageConfig.page_no, initData);
                this.addData.forEach(el => {
                    initData.push(el);
                });
                return initData;
            } else {
                return this.$Method.copy(this.data);
            }
        },
        remove(index, flag) {
            let _index = index;
            if (flag) {
                let tempIndex = this.data.findIndex(item => item._customIndex === index);
                if (tempIndex > -1) {
                    _index = tempIndex;
                }
            }
            let data = this.getData();
            if (_index > -1) {
                if (data[_index].flag === undefined || data[_index].flag === 'U') {
                    let row = this.$Method.copy(data[_index]);
                    row.flag = 'D';
                    if (this.myConfig.showPage) {
                        let start = (this.myConfig.pageConfig.page_no - 1) * this.myConfig.pageConfig.page_size,
                            end = start + this.myConfig.pageConfig.page_size;
                        let curlIndex = _index;
                        if (index >= start && index < end) {
                            curlIndex = index - start;
                            this.data.splice(curlIndex, 1, row);
                        } else {
                            this.initData.splice(curlIndex, 1, row);
                        }
                    } else {
                        this.delData.push(row);
                        this.data.splice(_index, 1);
                    }
                } else {
                    if (data[_index].flag === 'I') {
                        if (this.myConfig.showPage) {
                            let addIndex = this.getAddIndex(_index);
                            if (addIndex > -1) {
                                this.checkAddInfo.splice(addIndex, 1);
                                this.data.splice(this.getAddCurlIndex(addIndex), 1);
                            }
                        } else {
                            this.data.splice(_index, 1);
                            this.checkInfo.splice(_index, 1);
                        }
                    }
                }
            }
            this.initScrollLeft();
        },
        getAddIndex(index, flag) {
            let addIndex = -1;
            let data = [];
            if (flag) {
                data = this.data;
            } else {
                data = this.getData();
            }
            data.some((el, culIndex) => {
                if (el.flag === 'I') {
                    addIndex++;
                }
                if (culIndex === index) {
                    return true;
                }
            });
            return addIndex;
        },
        getAddCurlIndex(index) {
            let addIndex = -1;
            let result = -1;
            this.data.some((el, culIndex) => {
                if (el.flag === 'I') {
                    addIndex++;
                }
                if (addIndex === index) {
                    result = culIndex;
                    return true;
                }
            });
            return result;
        },
        setSummary(config,data,sums) {
            if (config.children) {
                config.children.forEach(el => {
                    this.setSummary(el,data.sums);
                });
            } else {
                const key = config.key;
                if(config.summary) {
                    const values = data.map(item => Number(item[key]));
                    if (!values.every(value => isNaN(value))) {
                        const v = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[key] = {
                            key,
                            value: this.$Method.formatNumber(v, config.format, config.notzero),
                            countValue:v
                        };
                    } else {
                        sums[key] = {
                            key,
                            value: 'N/A'
                        };
                    }
                } else {
                    sums[key] = {
                            key,
                            value: ''
                    };
                }
            }
        },
        handleSummary({ columns ,data}) {
            const sums = {};
            columns.forEach(column => {
                this.setSummary(column,data,sums);
            });
            let firstkey = this.findFirst(columns);
            sums[firstkey] = {key:firstkey,value:this.myConfig.sumText};
            if (this.myConfig.customSummary){
                return this.myConfig.customSummary(sums);
            } else {
                return sums;
            }
        },
        findFirst(columns) {
            let key = '';
            columns.some(el => {
                if (el.key !== 'selection' && el.key !== 'index') {
                    key = el.name;
                    return true;
                }
            });
            return key;
        },
    },
};
</script>
<style lang="less">
    .ivu-table-column-selected, .active-cell{
        box-shadow: inset 0 0 0 2px #AF292E;
    }
</style>
