<template>
  <div class="app-container" style="width: 800px; margin: 0 auto">
    <el-form ref="bannerForm" :model="form" label-width="100px">
      <el-form-item label="弹窗图片:" prop="image" :rules="rules.image" style="margin-bottom: 40px">
        <div class="form-el-upload" style="width: 148px; height: 148px">
          <el-upload
            class="avatar-uploader"
            :action="actionUrl"
            :headers="getToken"
            :show-file-list="false"
            :on-success="handleSuccess"
            :before-upload="beforeUpload"
          >
            <div v-loading="loadImgLoading" class="avatar" style="width: 148px; height: 148px; display: block">
              <img
                v-if="form.image"
                :src="form.image"
                style="width: 148px; height: 148px; display: block"
                @error="reloadImage"
                @load="loadSucess"
              />
              <i
                v-else
                class="el-icon-plus avatar-uploader-icon"
                style="width: 148px; height: 148px; line-height: 148px"
              ></i>
            </div>
          </el-upload>
          <p class="uploader-tips">注：请尽量上传260*260(px)尺寸的图片。</p>
        </div>
      </el-form-item>
      <el-form-item label="引导语1:" prop="guidingOne" :rules="rules.guidingOne">
        <el-input
          v-model="form.guidingOne"
          placeholder="请输入第一句引导语，不多余15个汉字"
          maxlength="15"
          style="width: 350px"
        />
      </el-form-item>
      <el-form-item label="引导语2:" prop="guidingTwo" :rules="rules.guidingTwo">
        <el-input
          v-model="form.guidingTwo"
          placeholder="请输入第二句引导语，不多余15个汉字"
          maxlength="15"
          style="width: 350px"
        />
      </el-form-item>
    </el-form>
    <div class="el-dialog__footer" style="text-align: center">
      <el-button type="primary" @click="onSubmit('bannerForm')"> 保存 </el-button>
      <el-button plain @click="backToList"> 返回 </el-button>
    </div>
  </div>
</template>

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

export default {
  name: 'CustomerPopupEdit',
  data() {
    const defaultForm = () => {
      return {
        id: '',
        shopId: this.$route.query.shopId,
        image: '', // 弹窗图片
        guidingOne: '', // 引导语1
        guidingTwo: '', // 引导语2
        addTime: ''
      }
    }
    return {
      form: defaultForm(),
      loadImgLoading: false,
      rules: {
        image: [{ required: true, message: '请上传弹窗图片', trigger: 'blur' }],
        guidingOne: [{ 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;
}
.span_tip {
  font-size: 12px;
  color: red;
  margin-left: 5px;
}
.filter-item-temp {
  width: 100px;
}
</style>
