<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="yyyy-MM"
          value-format="yyyy-MM"
          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="filePath" :rules="rules.filePath">
        <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.filePath" type="text" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleImportConfirm"> 保存 </el-button>
        <el-button plain @click="backToList"> 返回 </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

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

export default {
  name: 'MarketEdit',
  data() {
    return {
      form: {
        marketId: this.$route.query.marketId,
        id: '',
        filePath: '', // 数据文件
        reportDate: '', // 报表月份
        reportName: '' // 报表月份
      },
      loadImgLoading: false,
      fileList: [],
      rules: {
        filePath: [{ 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.getMarketReportById({ id: this.$route.query.id })
    }
  },
  methods: {
    // 获取供应商弹窗详情
    async getMarketReportById(params) {
      try {
        const res = await getMarketReportById(params)
        this.form = { ...this.form, ...res.data }
        const file = {
          name: res.data.fileName,
          url: ''
        }
        this.fileList.push(file)
      } catch (error) {
        console.log('error', error)
      }
    },
    // 确定保存
    async handleImportConfirm() {
      try {
        await this.$refs.importForm.validate()
        this.saveReport()
      } catch (error) {
        console.log(error)
      }
    },
    // 保存数据报表
    async saveReport() {
      try {
        await saveReport(this.form)
        this.fileList = []
        this.$message.success('数据报表保存成功')
        this.backToList()
      } catch (error) {
        console.log(error)
      }
    },
    // 文件上传成功
    handleUploadSuccess(response, file, fileList) {
      this.form.filePath = response.url
      this.fileList = fileList
    },
    // 上传文件删除
    handleUploadRemove(file, fileList) {
      this.fileList = fileList
    },
    handleUploadChange(file, fileList) {
      this.fileList = fileList
      this.form.filePath = file.name
      console.log('222222222222', this.fileList)
      this.saveMarketReport()
    },
    // 上传文件
    async saveMarketReport() {
      const file = this.fileList.length > 0 && this.fileList[0]
      if (!file) return
      try {
        const formData = new FormData()
        formData.append('reportFile', file.raw)
        const res = await saveMarketReport(formData)
        this.form.filePath = res.data
      } catch (error) {
        console.log(error)
      }
    },
    backToList() {
      this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$nextTick(() => {
          this.$router.replace({
            path: '/user/customer/market-report-list',
            query: { marketId: this.$route.query.marketId, shopName: this.$route.query.shopName }
          })
        })
      })
    }
  }
}
</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>
