<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>
        <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="upload2"
      :action="payInfoUrl"
      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="submitUpload2"
      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: "importPayInfoByExcel",
    data() {
      return {
        payInfoUrl: baseUrl + "/payInfo/importFromExcel",
        myHeader: {
          token: Cookies.get("token")
        },
        fileList: [],
        dialogTitle: "",
        dialogVisible: false,
        dialogContent: "",
        loading: false
      };
    },
    methods: {
      submitUpload2() {
        this.$refs.upload2.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>
