import { reactive } from 'vue'
import { getArrJsonValue2Arr } from '@/utils'
import _ from 'lodash'
import { export_json_to_excel } from '@/utils/Export2Excel'

const useExport2Excel = () => {
    const exportExcelData = reactive({
        header: [],
        data: [],
    })

    const setExcelHeaders = (data, key) => {
        exportExcelData.header = getArrJsonValue2Arr(data, key)
    }

    const setExcelTableData = (headerKey, tableHeader, tableData) => {
        const propsArr = getArrJsonValue2Arr(tableHeader, headerKey)
        const copyTableData = _.cloneDeep(tableData)
        if (propsArr.length) {
            const newExportData = copyTableData.map(v => {
                return propsArr.map(j => v[j])
            })
            exportExcelData.data = newExportData
        }
    }

    /**
     * 
     * @param {*} filename 文件名
     * @param {*} merges 合并单元格结构
     * @param {*} multiHeader 复合表头
     */
    const exportExcel = (filename, merges, multiHeader) => {
        export_json_to_excel({
            header: exportExcelData.header,
            data: exportExcelData.data,
            filename,
            merges,
            multiHeader
        })
    }

    return {
        setExcelHeaders,
        setExcelTableData,
        exportExcel
    }
}

export default useExport2Excel;