let getNumber = (item, index, arr, props) => {
    let number = 1

    while (arr[++index]) {
        for (let prop of props) {
            if (arr[index][prop].trim() != item[prop].trim()) {
                return number;
            }
        }
        number++
    }
    return number;
};

export let base = {
    methods: {
        reduceData(data) {
            try {
                let arr = JSON.parse(JSON.stringify(data));
                let year, code, quota1;
                return arr.map((item, index) => {
                    if (item.quota1.trim() != quota1 && (code == undefined || item.code.trim() == code) && (item.year.trim() == year || year == undefined)) {
                        quota1 = item.quota1.trim();
                        item.quota1Row = getNumber(item, index, arr, ["quota1", "code", "year"])
                    }
                    if (item.code.trim() != code && (year == undefined || year == item.year.trim())) {
                        code = item.code.trim();
                        quota1 = item.quota1.trim();
                        item.quota1Row = getNumber(item, index, arr, ['quota1', "code", "year"])
                        item.codeRow = getNumber(item, index, arr, ['code', "year"])
                    }
                    if (item.year.trim() != year) {
                        year = item.year.trim();
                        code = item.code.trim();
                        item.yearRow = getNumber(item, index, arr, ['year'])
                        item.codeRow = getNumber(item, index, arr, ['code', 'year'])
                    }
                    return item;
                })
            } catch (e) {
                console.log(e)
            }
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex == 1) {
                if (row.yearRow) {
                    return {
                        rowspan: row.yearRow,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }
            if (columnIndex == 2 || columnIndex == 3) {
                if (row.codeRow) {
                    return {
                        rowspan: row.codeRow,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }
            if (columnIndex == 4) {
                if (row.quota1Row) {
                    return {
                        rowspan: row.quota1Row,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }
        }
    },
}


export let assessing = {
    methods: {
        reduceData(data) {
            let arr = data;
            let quota1, quota2;
            return arr.map((item, index) => {
                if (item.quota2.trim() != quota2 && (quota1 == undefined || quota1 == item.quota1.trim())) {
                    quota2 = item.quota2.trim();
                    item.quota2Row = getNumber(item, index, arr, ['quota2', 'quota1']);
                }

                if (item.quota1.trim() != quota1) {
                    quota1 = item.quota1.trim();
                    quota2 = item.quota2.trim();
                    item.quota1Row = getNumber(item, index, arr, ['quota1'])
                    item.quota2Row = getNumber(item, index, arr, ['quota2', 'quota1'])
                }
                return item;
            })
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex == 1) {
                if (row.quota1Row) {
                    return {
                        rowspan: row.quota1Row,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }
            if (columnIndex == 2) {
                if (row.quota2Row) {
                    return {
                        rowspan: row.quota2Row,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }
        }
    },
}

export let quota = {
    methods: {
        reduceData(data) {
            console.log(data)
            try {
                let arr = data;
                let quota;
                return arr.map((item, index) => {
                    if (item.quota.trim() != quota) {
                        quota = item.quota.trim();
                        item.quotaRow = getNumber(item, index, arr, ['quota'])
                    }
                    return item;
                })
            } catch (e) {
                console.log(e)
            }
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex == 1) {
                if (row.quotaRow) {
                    return {
                        rowspan: row.quotaRow,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }

        }
    },
}

export let categoryName = {
    methods: {
        reduceData(data) {
            console.log(data)
            let arr = data;
            let categoryName;
            return arr.map((item, index) => {
                console.log(item)
                if (item.categoryName.trim() != categoryName) {
                    categoryName = item.categoryName.trim();
                    item.quotaRow = getNumber(item, index, arr, ['categoryName'])
                }
                return item;
            })
        },
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (columnIndex == 1) {
                if (row.quotaRow) {
                    return {
                        rowspan: row.quotaRow,
                        colspan: 1
                    }
                } else {
                    return {
                        rowspan: 0,
                        colspan: 1
                    }
                }
            }

        }
    },
}

/**
 * 
 * @param {*} params 
 * [{name: 'quota, col: [1,2]}] 
 */
export function mergeSpan(params) {

    return {
        methods: {
            reduceData(data) {
                let arr = data;
                let prev = new Array(params.length).fill(undefined);
                try {
                    return arr.map((item, index) => {
                        params.forEach((param, ind) => {
                            if (item[param.name].trim() != prev[ind]) {
                                let num = ind;
                                while (num--) {
                                    if (prev[num] !== undefined && item[params[num].name].trim() !== prev[num]) {
                                        break;
                                    }
                                }
                                if (num < 0) {
                                    prev[ind] = item[param.name].trim();
                                    let spans = [];
                                    for (let i = 0; i <= ind; i++) {
                                        spans.push(params[i].name)
                                    }
                                    item[param.name + 'Row'] = getNumber(item, index, arr, spans)
                                }
                                if (params[ind + 1]) {
                                    prev[ind+1] = item[params[ind+1].name].trim();
                                    let spans = [];
                                    for (let i = 0; i <= ind+1; i++) {
                                        spans.push(params[i].name)
                                    }
                                    item[params[ind+1].name + 'Row'] = getNumber(item, index, arr, spans)
                                }
                            }
                        })
                        return item;
                    })
                } catch (e) {
                    console.log(e)
                }
            },
            arraySpanMethod({ row, column, rowIndex, columnIndex }) {
                for (let param of params) {
                    if (param.col.includes(columnIndex)) {
                        if (row[param.name + 'Row']) {
                            return {
                                rowspan: row[param.name + "Row"],
                                colspan: 1
                            }
                        } else {
                            return {
                                rowspan: 0,
                                colspan: 1
                            }
                        }
                    }
                }
            },
        }
    }
}