<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>
      </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: "importRightsRecordByExcel",
    data() {
      return {
        baseInfoUrl: baseUrl + "/rightsRecordInfo/importFromExcel",
        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>
