<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    :width="600"
    :visible="visible"
    :maskClosable="false"
    :title="`导入设备`"
    :body-style="{ paddingBottom: '28px' }"
    @update:visible="updateVisible"
    :footer="null"
  >
    <a-upload-dragger
      name="file"
      :show-upload-list="false"
      accept=".xls,.xlsx"
      :before-upload="handleImport"
    >
      <p class="ant-upload-drag-icon">
        <InboxOutlined />
      </p>
      <p class="ant-upload-text">单击或拖动文件到此区域进行上传</p>
      <p class="ant-upload-hint" style="max-width: 550px">
        提示：附件上传中，请不要做任何操作！ 支持的文件类型为*.xls, *.xlsx!
      </p>
    </a-upload-dragger>
    <div style="margin: 20px auto; display: flex; justify-content: center">
      <a
        href="http://1.14.159.185:10042/template/2f5ffa06b0684e138184c6786f6fbb68.xlsx"
        target="_blank"
      >下载设备导入模板</a
      >
    </div>
  </ele-modal>
</template>

<script lang="ts" setup>
import { InboxOutlined } from "@ant-design/icons-vue";
import { message } from "ant-design-vue";
import { addBatchTowerEquipment } from "@/api/tower/equipment";
import { TowerEquipment } from "@/api/tower/equipment/model";
import { read, utils } from "xlsx";
import { ref } from "vue";
import dayjs from "dayjs";

const emit = defineEmits<{
  (e: "done"): void;
  (e: "update:visible", visible: boolean): void;
}>();

/* 更新visible */
const updateVisible = (value: boolean) => {
  emit("update:visible", value);
};

// 导入的数据
const importData = ref<TowerEquipment[]>([]);
const handleImport = (file: File) => {
  if (
    ![
      "application/vnd.ms-excel",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    ].includes(file.type)
  ) {
    message.error("只能选择 excel 文件");
    return false;
  }
  if (file.size / 1024 / 1024 > 20) {
    message.error("大小不能超过 20MB");
    return false;
  }
  const reader = new FileReader();
  importData.value = []
  reader.onload = (e) => {
    const data = new Uint8Array(e.target?.result as any);
    const workbook = read(data, { type: "array" });
    const sheetNames = workbook.SheetNames;
    const worksheet = workbook.Sheets[sheetNames[0]];
    // 解析成二维数组
    const aoa = utils.sheet_to_json<string[]>(worksheet, { header: 1 });
    aoa.slice(1).map((d) => {
      const array = ["闲置", "报废", "注销", "转卖", "在用", "停工"];
      if (d[1]) {
        importData.value.push({
          name: d[1],
          filingNo: d[2],
          factoryNo: d[3],
          model: d[4],
          manufactor: d[5],
          company: d[6],
          factoryDate: d[7],
          status: array.indexOf(d[8]),
          lifeYear: d[9],
          buyDate: d[10],
          source: d[11],
          warehouse: d[12],
          users: d[13],
          equipmentNo: d[14],
          ratedLoad: d[15],
          maxLiftingHeight: d[16],
          armLength: d[17],
          scrapDate: (d[7] && d[9]) ? (dayjs(d[7] ).add(parseInt(d[9]) , 'year').format('YYYY-MM-DD')) : ''
        });
      }
    });
    addBatchTowerEquipment(importData.value).then(() => {
      message.success("导入成功");
      updateVisible(false);
      emit("done");
    });
  };
  reader.readAsArrayBuffer(file);
  return false;
};
</script>
