<template>
  <div class="uploadwin">
    <div class="upload" v-if="addfile==false">
      <div class="up_title">批量导入{{titletext}}</div>
      <div class="up_box">
        <el-upload :class="{'none':isUpload == true}"
                   class="upload-demo"
                   :action="action"
                   :multiple="false"
                   accept=".xls,.xlsx"
                   drag
                   ref="upload"
                   :auto-upload="false"
                   :file-list="fileList"
                   :on-change="handleFileListChange"
                   :headers="headers"
                   :on-success="onSuccess">
          <img class="up_img" src="@/assets/images/project/upload.png">
          <div class="up_text">点击选择或将文件(xls，xlsx)拖拽至此上传成员名录</div>
          <div class="up_tip">导入的文件内容必须依照下载模板的要求填写</div>
          <div class="up_tip">上传文件最大为2M，仅支持Excel表格文件（xls,xlsx)</div>
          <div class="up_tip">导入已存在的客户将自动跳过</div>
        </el-upload>
        <div class="up_success" v-if="isUpload == true">
          <img src="@/assets/images/project/success.png">上传成功
        </div>
        <div class="btn_download" v-if="isUpload == false" @click="downloadClick"><div class="img"></div>点击下载</div>
      </div>
      <div class="btns">
        <div class="btn btn_primary btn_disabled h34" v-if="isUpload==false">确定导入</div>
        <div class="btn btn_primary h34" @click="importConfirmClick" v-else>确定导入</div>
        <div class="btn btn_default h34" @click="importCancel">取消</div>
      </div>
    </div>
    <div class="success" v-if="addfile==true">
      <div v-if="addsuccess==false">
        <img class="img" src="@/assets/images/project/clock.png">
        <div class="p1">查询客户中...</div>
        <div class="p2">请耐心等待，过程大概30秒</div>
      </div>
      <div v-if="addsuccess == true">
        <div class="p3">
          <img src="@/assets/images/project/success.png">查询成功
        </div>
        <div class="p2">成功导入{{titletext}}信息</div>
        <div class="btns">
          <div class="btn btn_primary h32" @click="getmsg">查看</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import { getToken } from "@/utils/auth";
  import "@/assets/styles/project.scss"
  import {importData} from '@/api/custom/custom'
  export default {
    name: 'batchImport',
    props:{
      importtype:''
    },
    data(){
      return{
        isUpload:false,//有上传的文件
        addfile:false,//已上传文件
        addsuccess:false,//已成功加入数据
        //批量导入
        action:"",
        fileList: [],
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        downloadhref:'',//样例地址
        titletext:'',
      }
    },
    created(){
      console.log(this.importtype )
      if(this.importtype == 'project'){//项目管理
        this.downloadhref = '/file/projectTemplate.xlsx'
        this.titletext = '商机'
        this.action = process.env.VUE_APP_BASE_API + '/business/info/upload'
      }
      if(this.importtype == 'custom'){//客户管理
        this.downloadhref = '/file/Template.xlsx'
        this.titletext = '客户'
        this.action = process.env.VUE_APP_BASE_API + "/customer/importData"
      }
    },
    methods:{
      getmsg(){
        this.$emit('getdatas')
      },
      handleFileListChange(file, fileList) {
        if (fileList.length > 0) {
          this.fileList = [fileList[fileList.length - 1]];
          this.isUpload = true
        }
      },
      onSuccess(res, file, fileList) {
        if(res.code == 200 )
          this.addsuccess = true
        else {
          this.importCancel()
          this.$message.error({ message: res.msg, showClose: true })
        }
      },

      downloadClick() {
        let a = document.createElement("a");
        a.setAttribute("href", this.downloadhref);
        a.setAttribute("download", "批量导入模版.xlsx");
        document.body.appendChild(a);
        a.click();
      },
      // 批量导入
      importConfirmClick() {
        if (this.fileList.length > 0) {
          this.$refs["upload"].submit();
          this.addfile = true
        } else {
          this.$message("请先选择文件");
        }
      },
      importCancel(){
        this.addfile = false
        this.isUpload = false
        this.addsuccess = false
        this.$emit('cancels')
      },
    }
  }
</script>

<style scoped>

</style>
