// import XLSX from "xlsx";
import * as XLSX from 'xlsx'
import FileSaver from 'file-saver';

/**
 * 异步读取Excel文件的sheet表为json数据
 * 不支持合并单元格
 * @param {File对象} file
 */
export function readExcelToJson(file) {
    return new Promise((resolve, reject) => {
        const reader = new FileReader();

        reader.onload = (e) => {
            let data = new Uint8Array(e.target.result);
            let workbook = XLSX.read(data, { type: "array" });
            //  console.log("workbook: ", workbook);

            //将Excel 第一个sheet内容转为json格式
            let worksheet = workbook.Sheets[workbook.SheetNames[0]];
            let json = XLSX.utils.sheet_to_json(worksheet);
            //   console.log("jsonExcel:", jsonExcel);
            resolve(json);
        };

        reader.readAsArrayBuffer(file.raw);
    });
}

/**
 * 保存json为Excel文件
 * @param {*} data
 * @param {*} filename  文件名后缀为.xlsx
 */
/*export function saveJsonToExcel(data, filename) {
    let sheet = XLSX.utils.json_to_sheet(data);

    let workbook = {
        SheetNames: ["sheet1"],
        Sheets: {
            sheet1: sheet,
        },
    };

    let wbout = XLSX.write(workbook, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
    });

    FileSaver.saveAs(
        new Blob([wbout], { type: "application/octet-stream" }),
        filename
    );
}*/
export function saveJsonToExcel(data, filename, customHeaders) {
    // 使用 json_to_sheet 将数据转为表格
    let sheet = XLSX.utils.json_to_sheet(data);

    // 如果传入了自定义的表头，则更新表头
    if (customHeaders && customHeaders.length > 0) {
        customHeaders.forEach((header, index) => {
            const cellAddress = XLSX.utils.encode_cell({ r: 0, c: index }); // 表头在第0行
            sheet[cellAddress] = { v: header, t: 's' }; // 设置单元格内容为汉字
        });
    }

    // 创建 Excel 工作簿
    let workbook = {
        SheetNames: ["sheet1"],
        Sheets: {
            sheet1: sheet,
        },
    };

    // 导出 Excel
    let wbout = XLSX.write(workbook, {
        bookType: "xlsx",
        bookSST: true,
        type: "array",
    });

    FileSaver.saveAs(
        new Blob([wbout], { type: "application/octet-stream" }),
        filename
    );
}
