<template>
  <div id="root">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="150px" class="ruleForm">
      <el-form-item  prop="activityTitle">
        <div>活动标题&nbsp;&nbsp;<el-input v-model="ruleForm.activityTitle" placeholder="20字以内" style="width: 80%;padding-left:5.5%;"/></div>
      </el-form-item>
      <el-form-item  prop="activityCarouselImg">
        <div >
        <div style="transform:translateY(250%);">活动轮播图</div>
        <div  style="width: 500px;padding-left:14%;">
        <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>
        <span style="transform:translateY(700%)">活动详情描述</span>
       <div style="padding-left:14%;transform:translateY(-15%)"><wangeditor :content="ruleForm.activityContent" @wangeditorEvent="wangeditorValue"/></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="signUpMax">
        <div style="display: flex;">
          <span style="width: 140px;">报名人数上限</span>
          <el-input v-model="ruleForm.signUpMax" placeholder="填写0则不限制报名人数"/>
        </div>
      </el-form-item>
      <el-form-item prop="registrationEndTime">
        <div style="display: flex;">
          <span style="width: 120px;">报名截止时间</span>
          <el-date-picker
            v-model="ruleForm.registrationEndTime"
            value-format="timestamp"
            type="date"
            placeholder="选择日期"/>
        </div>
      </el-form-item>
      <el-form-item>
        <div style="display: flex;">
          <span style="width: 200px;transform:translateY(1%);">是否需要报名人扫码签到</span>
          <el-radio v-model="ruleForm.radio" label="1" style="padding-left:4%;font-weight:bold;font-size:12px;transform:translateY(30%);">需要</el-radio>
          <el-radio v-model="ruleForm.radio" label="2" style="padding-left:6%;font-weight:bold;font-size:12px;transform:translateY(30%);">不需要</el-radio>  
        </div>
      </el-form-item>
      <el-form-item prop="sort">
        <div style="display: flex; justify-content: flex-start;">
          <span style="width: 120px;">权重值</span>
          <el-input-number v-model="ruleForm.sort" :min="1" label="权重值" @change="sortHandleChange"/>
        </div>
      </el-form-item>
      <el-form-item style="display: flex; justify-content: center;">
        <el-button type="primary" @click="resetForm('ruleForm')">存为草稿</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Wangeditor from '@/components/Wangeditor'
export default {
  name: 'Index',
  components: { Wangeditor },
  data() {
    return {
      ruleForm: {
        activityTitle: '',
        activityCarouselImg: '', // 轮播图
        activityContent: '', // 活动内容详细
        contentImg: [], // 活动内容配图
        signUpMax: '', //  活动人数上限
        registrationEndTime: '', // 报名活动结束时间
        sort: 1, // 权重
        status: '', // 状态 0 是已作废 1 是已发布   2 是草稿 3 是已修改
        radio:''
      },
      imageUrl: '', // 轮播图预览
      dialogVisible: false, // 活动配图, 默认不显示 (以单张为单位)
      dialogImageUrl: '', // 活动配图, 图片链接 (以单张为单位)
      rules: {
        activityTitle: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 20 个字符以内', trigger: 'blur' }
        ],
        activityContent: [
          { required: true, message: '活动详细描述', trigger: 'blur' }
        ],
        signUpMax: [
          { required: true, message: '请输入报名人数上限', trigger: 'blur' }
        ],
        registrationEndTime: [
          { type: 'date', required: true, message: '报名截止时间', trigger: 'change' }
        ],
        sort: [
          { required: true, message: '请输入权重值', trigger: 'change' }
        ]
      },
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addActivity(1)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      // this.$refs[formName].resetFields()
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addActivity(2)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleAvatarSuccess(res, file) { // 轮播图上传回调
      this.ruleForm.activityCarouselImg = 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)
    //   console.log('上传活动配图成功时回调!')
    // },
    sortHandleChange(value) { // 权重值
      this.ruleForm.sort = value
      console.log(value)
    },
    addActivity(status) { // 添加活动
      // 加载框
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      if (this.ruleForm.activityCarouselImg <= 0) {
        this.$message.error('轮播图未上传')
        loading.close()
        return
      }
       console.log('this.ruleForm.activityContent',this.ruleForm.activityContent)
       if(this.ruleForm.activityContent == '<p><br></p>'){
             this.$message.error('活动详情描述不能为空!')
             loading.close()
             return
          }
      if (this.ruleForm.activityContent =='') {
        this.$message.error('活动详情描述不能为空')
        loading.close()
        return
      }
      this.ruleForm.status = status // 发布状态  1 是已发布   2 是草稿
      console.log(' this.ruleForm', this.ruleForm.activityContent)
      this.$store.dispatch('AddActivity', this.ruleForm).then((res) => {
        if (res.code === '0') {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'activity-index' })
          loading.close()
          return
        }
        this.$message.error(res.message)
        loading.close()
      }).catch(() => {
        console.log('error AddActivity')
      })
    },
    // 富文本
    wangeditorValue(value) {
    console.log('value',value)
    this.ruleForm.activityContent = value // 在这里接受子组件传过来的参数，赋值给data里的参数
    }
  }
}
</script>

<style>
.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>

<style scoped>
#root {
  display: flex;
}
.ruleForm {
  width: 800px;
  margin-left: auto;
  margin-right: auto;
  margin-top: 50px;
}
</style>
