import fs from 'file-saver'
import XLSX from 'xlsx'
import {clone} from "./clone";
import {ElMessage, ElMessageBox} from "element-plus";
import {listIsBlank} from "./blank-utils";

export function Export(json, fields, filename) {
    const temp = clone(json)
    temp.forEach(item => {
        for (let i in item) {
            if (fields.hasOwnProperty(i)) {
                item[fields[i]] = item[i];
            }
            delete item[i]; //删除原先的对象属性
        }
    })
    let sheetName = filename //excel的文件名称
    let wb = XLSX.utils.book_new()  //工作簿对象包含一SheetNames数组，以及一个表对象映射表名称到表对象。XLSX.utils.book_new实用函数创建一个新的工作簿对象。
    let ws = XLSX.utils.json_to_sheet(temp, {header: Object.values(fields)}) //将JS对象数组转换为工作表。
    wb.SheetNames.push(sheetName)
    wb.Sheets[sheetName] = ws
    const defaultCellStyle = {
        font: {name: "Verdana", sz: 13, color: "FF00FF88"},
        fill: {fgColor: {rgb: "FFFFAA00"}}
    };//设置表格的样式
    let wopts = {
        bookType: 'xlsx',
        bookSST: false,
        type: 'binary',
        cellStyles: true,
        defaultCellStyle: defaultCellStyle,
        showGridLines: false
    }  //写入的样式
    let wbout = XLSX.write(wb, wopts)
    let blob = new Blob([s2ab(wbout)], {type: 'application/octet-stream'})
    fs.saveAs(blob, filename + '.xlsx')
}

export function ExcelName(json, fields) {
    if (listIsBlank(json)) {
        return ElMessage.error("没有可以导出的数据。")
    }
    ElMessageBox.prompt('请输入导出的文件名称。', '提示', {
        type: "warning",
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /\S/,
        inputErrorMessage: '文件名不能为空 (∩•̀ω•́)⊃-*⋆',
    }).then(({value}) => {
        Export(json, fields, value)
    }).catch(() => {
    })
}


const s2ab = s => {
    let buf;
    let i;
    if (typeof ArrayBuffer !== 'undefined') {
        buf = new ArrayBuffer(s.length);
        const view = new Uint8Array(buf);
        for (i = 0; i !== s.length; ++i) view[i] = s.charCodeAt(i) & 0xff
        return buf
    } else {
        buf = new Array(s.length);
        for (i = 0; i !== s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
}
