<template>
  <div class="app-container" style="width: 800px; margin: 0 auto">
    <div class="form-tips">
      <p>
        文件上传完成后，可先预览报表，若不是最终想要的数据效果，可先暂时保存，等数据最终确定了，再重新上传新文件，最后生成报表。
      </p>
    </div>
    <el-form ref="importForm" :model="form" label-width="100px">
      <el-form-item label="报表月份:" prop="reportDate" :rules="rules.reportDate">
        <el-date-picker
          v-model="form.reportDate"
          type="month"
          format="M月"
          placeholder="选择月份"
          style="width: 280px"
        />
      </el-form-item>
      <el-form-item label="报表名称:" prop="reportName" :rules="rules.reportName">
        <el-input v-model="form.reportName" placeholder="请输入报表名称" maxlength="30" style="width: 280px" />
      </el-form-item>
      <el-form-item label="报表文件" prop="fileUrl" :rules="rules.fileUrl">
        <el-upload
          ref="upload"
          accept=".zip, .rar"
          action="https://jsonplaceholder.typicode.com/posts/"
          :limit="1"
          :file-list="fileList"
          :on-success="handleUploadSuccess"
          :on-remove="handleUploadRemove"
          :on-change="handleUploadChange"
          :auto-upload="false"
        >
          <el-button slot="trigger" size="small" type="primary"> 选取文件 </el-button>
          <div slot="tip" class="el-upload__tip">只能上传.zip,.rar格式文件</div>
        </el-upload>
        <input v-show="false" v-model="form.fileUrl" type="text" />
      </el-form-item>
    </el-form>
    <div class="el-dialog__footer" style="text-align: left; margin-top: 100px">
      <el-button type="primary" @click="handleImportConfirm"> 保存 </el-button>
      <el-button plain @click="backToList"> 返回 </el-button>
    </div>
  </div>
</template>

<script>
import { saveMarketReport, getShopPopUp } from '@/api/user/customer/customer'

export default {
  name: 'MarketEdit',
  data() {
    const defaultForm = () => {
      return {
        id: '',
        reportFile: '', // 数据文件
        reportDate: '', // 报表月份
        reportName: '' // 报表月份
      }
    }
    return {
      form: defaultForm(),
      loadImgLoading: false,
      fileList: [],
      rules: {
        fileUrl: [{ required: true, message: '请导入数据文件', trigger: ['change'] }],
        reportDate: [{ required: true, message: '请选择报表数据月份', trigger: 'blur' }],
        reportName: [{ required: true, message: '请输入报表名称', trigger: 'blur' }]
      }
    }
  },
  computed: {
    getToken() {
      return {
        token: this.$store.getters.token
      }
    },
    actionUrl() {
      return process.env.VUE_APP_BASE_API + '/formData/MultiPictareaddData'
    }
  },
  created() {
    if (this.$route.query.type === 'edit') {
      this.getShopPopUp({ shopId: this.$route.query.shopId })
    }
  },
  methods: {
    // 获取供应商弹窗详情
    async getShopPopUp(params) {
      try {
        const res = await getShopPopUp(params)
        this.form = { ...this.form, ...res.data }
      } catch (error) {
        console.log('error', error)
      }
    },
    // 确定保存
    async handleImportConfirm() {
      try {
        await this.$refs.importForm.validate()
        this.importSumbit()
      } catch (error) {
        console.log(error)
      }
    },
    async importSumbit() {
      const file = this.fileList.length > 0 && this.fileList[0]
      if (!file) return
      try {
        const formData = new FormData()
        this.fileList = []
        formData.append('keywordFile', file.raw)
        this.form.reportFile = formData
        await saveMarketReport(this.form)
        this.$message.success('保存成功')
        // setTimeout(() => {
        //   this.$router.push({ path: '/user/customer/list' })
        // }, 1000)
      } catch (error) {
        console.log(error)
      }
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$confirm('是否提交保存信息', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            console.log('form', this.form)
            this.saveShopPopUp(this.form)
          })
        } else {
          return false
        }
      })
    },
    // 文件上传成功
    handleUploadSuccess(response, file, fileList) {
      this.importForm.fileUrl = response.url
      this.fileList = fileList
    },

    // 上传文件删除
    handleUploadRemove(file, fileList) {
      this.fileList = fileList
    },

    handleUploadChange(file, fileList) {
      this.fileList = fileList
      this.importForm.fileUrl = file.name
    },
    backToList() {
      this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$nextTick(() => {
          this.$router.replace({ path: '/user/customer/list' })
        })
      })
    }
  }
}
</script>

<style>
.form-tips {
  width: 100%;
  height: 48px;
  line-height: 48px;
  background: #f7f7f7;
  font-size: 12px;
  color: #333333;
  border-radius: 8px;
  box-sizing: border-box;
  padding: 0 10px;
  margin-bottom: 40px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  float: left;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 30px;
  color: #999999;
}
.el-form-item__label {
  text-align: right !important;
}
.el-upload__tip {
  line-height: 20px;
  color: red;
  text-align: left;
}
.span_tip {
  font-size: 12px;
  color: red;
  margin-left: 5px;
}
.filter-item-temp {
  width: 100px;
}
.uploader-tips {
  position: absolute;
  bottom: 0;
  left: 160px;
  line-height: 28px;
  color: red;
  margin: 0;
}
</style>
