<template>
  <div>
    <input type="file" accept=".xlsx,.xls" id="file" @change="handleFileChange" />
  </div>
</template>
<script setup>
import * as XLSX from 'xlsx';
const form = ref({});
const emits = defineEmits();
const columnList = ref([]);
const datas = ref([]);

function handleFileChange(e) {
  const file = e.target.files[0];
  const reader = new FileReader();
  reader.readAsBinaryString(file);
  reader.onload = function (e) {
    const data = e.target.result;
    const workbook = XLSX.read(data, { type: 'binary' });
    const firstSheetName = workbook.SheetNames[0];
    const worksheet = workbook.Sheets[firstSheetName];
    const json = XLSX.utils.sheet_to_json(worksheet);
    let list = [];
    let column = [];
    let first = true;
    let re = /[\u4e00-\u9fa5]/;
    json.map(item => {
      let obj = {};
      Object.keys(item).map(key => {
        if (first) {
          if (re.test(key)) {
            columnList.value.unshift({ prop: 'area', label: key });
          } else {
            columnList.value.push({ prop: key, label: key });
          }
        }
        if (re.test(key)) {
          obj['area'] = item[key];
        } else {
          obj[key] = item[key];
        }
      });
      first = false;
      list.push(obj);
    });

    datas.value = list;
    emits('inportExcel', { datas, columnList });
  };
}
</script>
<style>
.container {
  margin-top: 20px;
}
</style>
