<template>
  <div>
    <el-alert
      align="left"
      title="请上传格式完全相同的excel,格式如下:"
      type="warning"
      :closable="false"
      show-icon>
    </el-alert>
    <table style="text-align: center;border-collapse: collapse" cellspacing="0px" cellpadding="0px">
      <tr>
        <td style="border: 1px solid black">统筹区</td>
        <td style="border: 1px solid black">街道或乡镇名称</td>
        <td style="border: 1px solid black">社区名称</td>
        <td style="border: 1px solid black">所属组</td>
        <td style="border: 1px solid black">户口所在地</td>
        <td style="border: 1px solid black">户口性质</td>
        <td style="border: 1px solid black">个人编号</td>
        <td style="border: 1px solid black">证件号码</td>
        <td style="border: 1px solid black">姓名</td>
        <td style="border: 1px solid black">性别</td>
        <td style="border: 1px solid black">民族</td>
        <td style="border: 1px solid black">出生日期</td>
        <td style="border: 1px solid black">险种类型</td>
        <td style="border: 1px solid black">参保状态</td>
        <td style="border: 1px solid black">是否老农保</td>
        <td style="border: 1px solid black">缴费状态</td>
        <td style="border: 1px solid black">缴费档次</td>
        <td style="border: 1px solid black">人员类别</td>
        <td style="border: 1px solid black">离退休标志</td>
        <td style="border: 1px solid black">个人身份</td>
        <td style="border: 1px solid black">参保日期</td>
        <td style="border: 1px solid black">档案编号</td>
      </tr>
    </table>
    <el-upload
      style="margin-top: 10px"
      class="upload-demo"
      ref="upload1"
      :action="baseInfoUrl"
      multiple
      drag
      accept=".xls,.xlsx"
      :limit="1"
      :headers="myHeader"
      :before-upload="handleBeforeUpload"
      :file-list="fileList"
      :on-exceed="handleExceed"
      :on-success="uploadSuccess"
      :on-error="uploadError"
      :auto-upload="false">
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
      <div slot="tip" class="el-upload__tip">只能上传xls/xlsx文件,一次最多只能上传1个文件</div>
    </el-upload>
    <el-button
      style="margin-top: 10px"
      size="small"
      type="success"
      @click="submitUpload1"
      v-loading.fullscreen.lock="loading">数据导入</el-button>
    <el-dialog
      ref="dialog"
      :title="dialogTitle"
      width="30%"
      :visible.sync="dialogVisible">
      <span v-text="dialogContent" style="font-size: 16px"></span>
    </el-dialog>
  </div>
</template>

<script>
  import {baseUrl} from "../../utils/global";
  import Cookies from "js-cookie";

  let uploadedList = [];
  export default {
    name: "importBaseInfoByExcel",
    data() {
      return {
        baseInfoUrl: baseUrl + "/baseInfo/importFromNewExcel",
        myHeader: {
          token: Cookies.get("token")
        },
        fileList: [],
        dialogTitle: "",
        dialogVisible: false,
        dialogContent: "",
        loading: false
      };
    },
    methods: {
      submitUpload1() {
        this.$refs.upload1.submit();
      },
      handleExceed() {
        this.$message.warning("当前限制选择 1 个文件，请删除后继续上传");
      },
      handleBeforeUpload(file) {
        this.loading = true;
        if (!file.name.endsWith(".xls") && !file.name.endsWith(".xlsx")) {
          this.$message.warning(file.name + "不是excel文件");
          this.loading = false;
          return false;
        }
        // if (uploadedList.includes(file.name)) {
        //   this.$message.warning(file.name + "已被选中过,请勿重复选择");
        //   return false;
        // }
        // uploadedList.push(file.name);
      },
      async uploadSuccess(response) {
        this.fileList = [];
        this.dialogTitle = "导入结果";
        this.dialogContent = response.data;
        // this.dialogVisible = true;
        this.loading = false;
        // console.log("upload success");
        this.$message({
          type: "success",
          message: response.data,
          showClose: true,
          duration: 0
        });
      },
      async uploadError(response) {
        this.fileList = [];
        this.dialogTitle = "导入结果";
        this.dialogContent = "导入失败";
        // this.dialogVisible = true;
        this.loading = false;
        // console.log(response);
        this.$alert(response.data, '导入结果', {
          confirmButtonText: '确定',
          callback: action => {
            // this.$message({
            //   type: 'info',
            //   message: `action: ${ action }`
            // });
          }
        });
      },
    }
  }
</script>

<style scoped>

</style>
