<template>
  <div>
    <dialog-view
      :visible.sync="importFileDialogVisible"
      :width="importFileWidth"
      :dialogTitle="importFileTitle"
      :buttons="importDialogButtons"
      @button-click="importDialogButtonClick"
    >
      <template #dialogBody>
        <div class="import_box">
          <slot name="importHeadSlot"></slot>
          <el-form ref="form" :model="form" :rules="rules">
            <el-form-item label="" label-width="0" prop="url">
              <div class="select-content">
                <div class="item">
                  <el-upload
                    drag
                    :action="importDialogAction"
                    :on-success="uploadSuccess"
                    :before-upload="handleBeforeUpload"
                    :show-file-list="false"
                    :disabled="importDisabled"
                    accept=".xlsx, .xls"
                  >
                    <div v-if="form.fileName == ''">
                      <i class="el-icon-upload"></i>
                      <div class="el-upload__text">
                        将文件拖到此处，或<em>点击上传</em>
                      </div>
                    </div>
                    <div v-else>
                      <i class="import_file" style="">
                        <!-- <span class="icon-close" @click.stop="handleDelFile">x</span> -->
                        <i
                          class="el-icon-close fixed_close"
                          @click.stop="handleDelFile"
                        ></i>
                      </i>
                      <div class="el-upload__text set_line_hieght file_name">
                        {{ form.fileName }}
                      </div>
                      <div class="el-upload__text set_line_hieght file_size">
                        文件大小:{{ fileSize | formatFileSize }}
                      </div>
                      <div class="el-upload__text set_line_hieght">
                        文件上传成功,请选择上传数据,或者<span
                          style="font-size: 12px; color: #fc6510"
                          >重新上传</span
                        >
                      </div>
                    </div>
                  </el-upload>
                </div>
                <div class="import_tips">
                  <span style="color: rgba(250, 85, 85, 1)">*</span
                  >只能上传xlsx/xls,数据记录最多不可以超过300条且文件大小不超过2M
                </div>
              </div>
            </el-form-item>
          </el-form>
          <div>
            <div class="wrap-content" :class="clsoeStatus ? '' : 'close-wrap'">
              <!-- <i class="el-icon-close close"></i> -->
              <p style="font-weight: 600; margin-bottom: 20px">
                批量导入指导：
              </p>
              <p class="tips_text">
                1. 企业<span class="color_text">下载</span>批量导入<span
                  class="color_text"
                  >模板</span
                >，并按照模板填写表格
              </p>
              <p class="tips_text">
                2. 单击<span class="color_text">选取文件</span
                >，选择已填写完并保存的表格进行<span class="color_text"
                  >导入</span
                >
              </p>
              <p>
                3.导入后点击上传至服务器，上传完成后会弹出确认窗口，<span
                  class="color_text"
                  >点击确认</span
                >即可录入表格中的信息
              </p>
              <p style="color: rgba(250, 85, 85, 1); margin-top: 20px">
                注：批量录入时请务必输入准确的信息，否则可能会导入失败
              </p>
            </div>
          </div>
        </div>
      </template>
    </dialog-view>
  </div>
</template>

<script>
import dialogView from './baseView/dialog.vue'
export default {
  name: 'import-file-view',
  components: {
    dialogView
  },
  props: {
    importFileTitle: {
      type: String,
      default: '导入'
    },
    importFileWidth: {
      type: String,
      default: '800px'
    },
    importDialogAction: {
      type: String,
      default: '/api/attachment/upload'
    },
    // 是否禁用上传
    importDisabled: Boolean,
    // 导入url
    importFileUrl: {
      type: String
    },
    // 导出模板url
    importFileDownloadUrl: {
      type: String
    },
    // 下载模板的参数
    downloadParams: {
      type: Object,
      default: () => ({})
    },
    // 下载模板请求方式 get /post
    downloadRequestType: {
      type: String,
      default: 'get'
    },
    // 导入的参数
    importFileFormData: {
      type: Object,
      default: () => ({})
    },
    importDialogButtons: {
      type: Array,
      default: () => {
        return [
          {
            text: '模板下载',
            id: 'download'
          },
          {
            text: '上传数据',
            id: 'upload',
            type: 'primary'
          }
        ]
      }
    },
    // 是否自定义导入成功之后的提示
    isCustomMessage: {
      type: Boolean
    }
  },
  data() {
    return {
      clsoeStatus: true,
      importFileDialogVisible: false,
      rules: {},
      file: {},
      loading: false,
      form: {
        url: '',
        fileName: '',
        tsuId: ''
      }
    }
  },
  filters: {
    formatFileSize(v) {
      return v + 'M'
    }
  },
  methods: {
    open(arg) {
      this.clsoeStatus = true
      this.form.fileName = ''
      this.form.url = ''
      this.file = {}
      this.importFileDialogVisible = true
    },
    importDialogButtonClick(arg) {
      switch (arg.id) {
        case 'download':
          this.download()
          break
        case 'upload':
          this.confirm()
          break
        default:
          break
      }
      this.$emit('import-dialog-button-click', arg)
    },
    handleDelFile() {
      this.form.url = ''
      this.form.fileName = ''
      this.file = {}
    },
    handleBeforeUpload(file) {
      this.fileSize = (file.size / 1000000).toFixed(2)
    },
    download() {
      if (!this.importFileDownloadUrl) return
      this.$api[
        this.downloadRequestType === 'get' ? 'fileDownload' : 'filePostDownload'
      ](this.importFileDownloadUrl, this.downloadParams || {})
        .then(res => {
          console.log(res)
        })
        .finally(() => {
          this.loading = false
        })
    },
    uploadSuccess(res, file) {
      this.$set(this.form, 'url', res.data.url)
      this.$set(this.form, 'fileName', res.data.name)
      this.$set(this, 'file', file.raw)
      this.$notify.success({
        title: '消息',
        message: '文件上传成功',
        duration: '1000'
      })
    },
    confirm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true
          let file = this.file
          console.log('file', file)
          if (!Object.keys(file).length) {
            this.$api.warning('请先上传数据')
            return
          }
          let formData = new FormData()
          formData.append('file', file)
          formData.append('tsuId', this.form.tsuId)
          if (Object.keys(this.importFileFormData).length) {
            Object.keys(this.importFileFormData).forEach(p => {
              formData.append(p, this.importFileFormData[p])
            })
          }
          console.log('formData', formData, this.$api.file, this.importFileUrl)
          if (!this.importFileUrl) return
          this.$api
            .file(this.importFileUrl, formData)
            .then(res => {
              console.log('res', res)
              if (res !== undefined) {
                if (!this.isCustomMessage) {
                  let num = Array.isArray(res) ? res.length : res
                  let msg =
                    typeof res === 'boolean'
                      ? '导入成功'
                      : `导入成功${num}条数据!`
                  this.$api.success(msg)
                }
                this.$refs.form.resetFields()
                this.$emit('import-dialog-upload-success', res)
                this.importFileDialogVisible = false
              }
            })
            .finally(() => {
              this.loading = false
            })
        } else {
          this.$api.warning('请填写相关信息!')
        }
      })
    },
    handleChangeClose() {
      this.clsoeStatus = !this.clsoeStatus
    }
  }
}
</script>
<style lang="less" scoped>
/deep/ .el-dialog {
  overflow: hidden;
}
/deep/ .el-form-item {
  margin-bottom: 26px;
}
.import_box {
  margin: 16px;
}
.el-upload__text {
  color: rgba(0, 0, 0, 0.85);
}
.set_line_hieght {
  line-height: 26px;
}
.file_name {
  color: #707070;
  font-weight: bold;
}
.file_size {
  color: #707070;
  font-size: 12px;
}
/deep/ .el-form-item__content {
  line-height: 0;
}
.import_tips {
  padding-top: 26px;
  color: rgba(0, 0, 0, 0.65);
  font-size: 13px;
}
.wrap-content {
  position: relative;
  // margin-top: 16px;
  padding: 30px;
  color: rgba(0, 0, 0, 0.85);
  background: rgba(255, 240, 231, 1);
  // border-radius: 15px;
}
.close {
  position: absolute;
  top: 20px;
  right: 20px;
  cursor: pointer;
}
.close-wrap {
  display: none;
}
/deep/.el-upload-dragger {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 180px;
  width: 100%;
  border: 0;

  // padding:16px 0 0;
  .el-icon-upload {
    margin: 0 0 16px;
  }
}
/deep/.el-upload--text {
  border: 1px dashed #d2d2d7;
  // border-radius: 6px;
  box-sizing: border-box;
  width: 100%;
  // height: 180px;
}
.el-icon-folder-checked {
  color: #666;
  position: relative;
  font-size: 67px;
  margin: 15px 0 16px;
  line-height: 50px;
}
.import_file {
  // color: #3ac877;
  position: relative;
  // font-size: 67px;
  margin: 0;
  // line-height: 50px;
  display: inline-block;
  height: 66px;
  width: 66px;
  background: url('../img/import_file.svg');
  .fixed_close {
    position: absolute;
    top: -7px;
    right: -5px;
    font-size: 12px;
    color: #fff;
    border-radius: 50%;
    text-align: center;
    padding: 1px;
    background: rgba(0, 0, 0, 0.25);
  }
}
.color_text {
  color: rgba(24, 161, 251, 1);
}
.icon-close {
  width: 14px;
  height: 14px;
  text-align: center;
  line-height: 12px;
  position: absolute;
  top: -12px;
  right: -6px;
  font-size: 14px;
  border-radius: 50%;
  color: #666;
  border: 1px solid #666;
}
.tips_text {
  margin-bottom: 5px;
}
</style>
