<template>
  <button class="button_search button_spacing" style="width: 90px" @click.prevent="createInfoForFile()">{{ btnName }}
  </button>
  <modal :show.sync="batchShow" width="570px" title="文件导入" v-ref:modal backdrop="false">
    <article slot="modal-body" class="modal-body">
      <file-upload class="my-file-uploader col-sm-9" name="chargePriceUploadFile" action="rs/file/uploadFile" tagname="确定" multiple v-ref:file></file-upload>
      <button  class="button_clear btn-gn"  style="margin-top: 10px;background-color: #6aa6e2;" @click.prevent="downloadFiles()">{{templateBtnName}}
      </button>
    </article>
    <footer slot="modal-footer" class="modal-footer">
    </footer>
  </modal>
</template>

<script>
import axios from 'axios'

export default {
  // logicName:执行逻辑的logic
  // importJson：导入对应的字段
  // props: ['data', 'sqlurl', 'field', 'total', 'bean', 'chooseCol','sumName'],
  data () {
    return {
      batchShow: false
    }
  },
  props: {
    btnName: {
      type: String,
      default: '导入'
    },
    templateBtnName: {
      type: String,
      default: '模板下载'
    },
    templateName: {
      type: String
    },
    logicName: {
      type: String
    },
    importJson: {
      type: String
    },
    alertMessage: {
      type: String,
      default: '具体失败数据请在稽查查询错误查询中查看！！'
    }
  },
  beforeDestroy () {
    // this.closeTimer()
  },
  methods: {
    downloadFiles () {
      let downurl = 'rs/downloadfile/file?filename=' + this.templateName
      this.$downFile(downurl,this.templateName + '.xlsx')
    },
    createInfoForFile () {
      this.batchShow = true
    }

  },
  watch: {
  },
  events: {
    'showfiles' (val) {
      console.log(val)
    },
    // 文件上传监听
    FileUpload: function (file, res) {
      let data = {
        tablename: 't_record',
        filepath: res.path
      }
      this.$resetpost('rs/logic/import', data, {resolveMsg: null, rejectMsg: '上传失败'}).then((res) => {
        this.close()
        this.$showAlert(`一共上传${res.data.total}条数据, 成功${res.data.successnum}条`, 'success', 2000)
        this.search()
      })
    },
    'onFileUpload': async function (file, res) {
      this.iswork = true
      let param = {
        filepath: res.f_downloadpath,
        importJson: this.importJson,
        logicName: this.logicName,
        f_operator: this.$login.f.name,
        f_operatorid: this.$login.f.id,
        f_orgid: this.$login.f.orgid,
        f_orgname: this.$login.f.orgs,
        f_depid: this.$login.f.depids,
        f_depname: this.$login.f.deps
      }
      let self = this
      try {
        await axios.post('rs/logic/fileImport',
          {data: param}, {timeout: 10 * 60 * 1000}).then((res) => {
            if (res.data) {
              console.log(res.data.totalsum)
              if (res.data.errorMsg) {
                self.$showMessage(`${res.data.errorMsg}` + this.alertMessage)
              } else {
                self.$showMessage(`本次上传共上传${res.data.totalsum}条数据, 成功${res.data.successnum}条,失败${res.data.errorsum}条` + this.alertMessage)
              }
              self.iswork = false
            }
          })
      } catch (error) {
        if (error.response) {
          if (error.response.status == 500) {
            this.$showAlert('服务繁忙,请重试!!!', 'danger', 3000)
          } else if (error.response.status == 404) {
            this.$showAlert('服务运行中,请耐心等待后进行重试!!', 'danger', 3000)
          } else {
            this.$showAlert(error.response.data, 'danger', 2000)
          }
          console.log(error.response.data)
          console.log(error.response.status)
          console.log(error.response.headers)
        } else if (error.request) {
          console.log(error.request)
          this.$showAlert(error.request, 'danger', 2000)
        } else {
          console.log('Error', error.message)
          this.$showAlert(error.message, 'danger', 2000)
        }
        this.iswork = false
      }
      this.batchShow = false
    }
  }
}
</script>
