<template>
  <div id="root">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="50px" class="ruleForm">
      <el-form-item  prop="announcementTitle">
        <div>公告标题&nbsp;&nbsp;<el-input v-model="ruleForm.announcementTitle" placeholder="20字以内"  style="width: 80%;padding-left:3.5%;"/></div>
        
      </el-form-item>
      <el-form-item  prop="announcementCarouselImg" >
        <div >
          <div style="transform:translateY(250%);">公告轮播图</div>
        <div  style="width: 500px;padding-left:20%;">
        <el-upload 
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          class="avatar-uploader"
          name="uploadFiles"
          :action="uploadImgUrl">
          <img v-if="imageUrl" :src="imageUrl" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="">
        
        <div><span style="position:relative;top:-100px">公告详细描述&nbsp;&nbsp;</span><el-input v-model="ruleForm.announcementContent" :rows="10" type="textarea" placeholder="公告内容" style="width: 80%;"/></div>
      </el-form-item>
      <el-form-item>
         <div >
          <div style="transform:translateY(250%);">公告配图</div>
        <div  style="width: 500px;padding-left:13%;">
        <el-upload
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handleSuccessContentImg"
          name="uploadFiles"
          :action="uploadImgUrl"
          list-type="picture-card">
          <i class="el-icon-plus"/>
        </el-upload>
        </div>
        </div>
        <el-dialog :visible.sync="dialogVisible">
          <img :src="dialogImageUrl" width="100%" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item  prop="sort">
        <div>权重值&nbsp;&nbsp;<el-input-number  v-model="ruleForm.sort" label="权重值" @change="sortHandleChange" style="margin-left:5%"/></div>
        
      </el-form-item>
      <el-form-item style="margin-left:13%">
        <el-button @click="resetForm('ruleForm','2')">存为草稿</el-button>
        <el-button type="primary" @click="submitForm('ruleForm','1')">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'Index',
  data() {
    return {
      ruleForm: {
        announcementTitle: '',
        announcementCarouselImg: '', // 轮播图
        announcementContent: '', // 内容详细
        contentImg: [], // 内容配图
        sort: 1, // 权重
        status: '' // 状态 0 是已作废 1 是已发布   2 是草稿 3 是已修改
      },
      imageUrl: '', // 轮播图预览
      dialogVisible: false, // 活动配图, 默认不显示 (以单张为单位)
      dialogImageUrl: '', // 活动配图, 图片链接 (以单张为单位)
      rules: {
        announcementTitle: [
          { required: true, message: '请输入公告名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 20 个字符以内', trigger: 'blur' }
        ],
        announcementContent: [
          { required: true, message: '公告详细描述', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入权重值', trigger: 'change' }
        ]
      },
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
    }
  },
  methods: {
    submitForm(formName, status) {
      console.log(status)
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.ruleForm.announcementCarouselImg<=0){
            this.$message.error('轮播图未上传')
            return    
          }
          if (this.ruleForm.announcementContent.length <= 0 && this.ruleForm.contentImg.length <= 0) {
            this.$message.error('公告内容描述 和 公告配图至少要存在一个!')
            return
          }
          this.ruleForm.status = status // 发布状态
          this.$store.dispatch('AddAnnouncement', this.ruleForm).then((res) => {
            if (res.code === '0') {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.$router.push({ name: 'announcement-index' })
              loading.close()
              return
            }
            this.$message.error(res.message)
            loading.close()
          }).catch(() => {
            console.log('error AddActivity')
          })
        } else {
          console.log('error submit!!')
          loading.close()
          return false
        }
      })
      // 关闭加载框
      loading.close()
    },
    resetForm(formName, status) {
      console.log(status)
      this.submitForm(formName, status)
    },
    handleAvatarSuccess(res, file) { // 轮播图上传回调
      this.ruleForm.announcementCarouselImg = res.data[0]
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) { // 轮播图上传前操作
      // const isJPG = file.type === 'image/jpeg'
      // const isLt2M = file.size / 1024 / 1024 < 2
      //
      // if (!isJPG) {
      //   this.$message.error('上传头像图片只能是 JPG 格式!')
      // }
      // if (!isLt2M) {
      //   this.$message.error('上传头像图片大小不能超过 2MB!')
      // }
      // return isJPG && isLt2M
    },
    handleRemove(file, fileList) { // 活动配图移除时
      this.ruleForm.contentImg = []
      for (let i = 0; i < fileList.length; i++) {
        this.ruleForm.contentImg.push(fileList[i].response.data[0])
      }
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) { // 点击活动配图文件列表中已上传的文件时的钩子
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleSuccessContentImg(response, file, fileList) { // 活动配图上传成功时回调
      const resImg = response.data[0]
      this.ruleForm.contentImg.push(resImg)
    },
    sortHandleChange(value) { // 权重值
      this.ruleForm.sort = value
      console.log(value)
    }
  }
}
</script>

<style scoped>
#root {
  display: flex;
}
.ruleForm {
  width: 800px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
