<template>
  <div class="app-container">
    <el-form ref="bannerForm" :model="form" label-width="100px">
      <el-form-item label="数据总结:" prop="content" :rules="rules.content">
        <Tinymce ref="editor" v-model="form.content" :height="400" />
      </el-form-item>
    </el-form>
    <div class="el-dialog__footer" style="text-align: center">
      <el-button plain @click="backToList"> 返回 </el-button>
      <el-button type="primary" @click="onSubmit('bannerForm')"> 保存 </el-button>
    </div>
  </div>
</template>

<script>
import { saveShopPopUp, getShopPopUp } from '@/api/user/customer/customer'
import Tinymce from '@/components/Tinymce'
export default {
  name: 'CustomerPopupEdit',
  components: { Tinymce },
  data() {
    const defaultForm = () => {
      return {
        id: '',
        shopId: this.$route.query.shopId,
        content: '', // 引导语1
        guidingTwo: '', // 引导语2
        addTime: ''
      }
    }
    return {
      form: defaultForm(),
      loadImgLoading: false,
      rules: {
        content: [{ required: true, message: '请填写数据总结', trigger: 'blur' }],
        guidingTwo: [{ required: true, message: '请填写引导语', trigger: 'blur' }]
      }
    }
  },
  computed: {
    getToken() {
      return {
        token: this.$store.getters.token
      }
    },
    actionUrl() {
      return process.env.VUE_APP_BASE_API + '/formData/MultiPictareaddData'
    }
  },
  created() {
    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)
      }
    },
    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
        }
      })
    },
    async saveShopPopUp(params) {
      // 保存
      await saveShopPopUp(params)
      this.$message.success('保存成功')
      setTimeout(() => {
        this.$router.push({ path: '/user/customer/list' })
      }, 1000)
    },
    // 上传图标事件
    handleSuccess(res, file) {
      this.loadImgLoading = true
      this.$nextTick(() => {
        setTimeout(() => {
          this.form.image = res.data
        }, 1000 * 2)
      })
    },
    // 对上传图片的大小、格式进行限制
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isJPG2 = file.type === 'image/jpg'
      const isPNG = file.type === 'image/png'
      const isLt5M = file.size / 1024 / 1024 < 5
      if (!isJPG && !isJPG2 && !isPNG) {
        this.$message.error('只支持jpg或png格式图片')
      }
      if (!isLt5M) {
        this.$message.error('请上传5MB以内的图片!')
      }
      return (isJPG || isJPG2 || isPNG) && isLt5M
    },
    reloadImage() {
      this.loadImgLoading = true
      setTimeout(() => {
        this.temp.classifyImage = this.temp.classifyImage.split('?')[0] + '?r=' + Math.floor(Math.random() * 1000)
      }, 1000 * 2)
    },
    loadSucess() {
      this.loadImgLoading = false
    },
    backToList() {
      this.$store.dispatch('tagsView/delView', this.$route).then(() => {
        this.$nextTick(() => {
          this.$router.replace({ path: '/user/customer/list' })
        })
      })
    }
  }
}
</script>

<style>
.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;
  position: absolute;
  right: -50%;
  bottom: 0;
}
.span_tip {
  font-size: 12px;
  color: red;
  margin-left: 5px;
}
.filter-item-temp {
  width: 100px;
}
</style>
