<template>
  <div id="root">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" label-width="150px" class="ruleForm">

    <el-form-item class="filter-item" >
        <div>话题类型&nbsp;&nbsp;<el-select v-model="ruleForm.type" placeholder="请选择" style="padding-left:4%;">
         <el-option label="议事大厅(投诉，建议，表扬，社区明星评比，业委员会选举，组织活动等)" value="0"/>
         <el-option label="友令互助(借物，宠物寄养，搭伙用餐，搭手帮忙，邻里提醒，组织春游等)" value="1"/>
         <!-- -1包括，二手，租赁，求购 -->
         <el-option label="二手租赁(咸鱼太贵，快递费太贵，想租个车位？小区里面就能买卖)" value="-1"/>
         <el-option label="其他(还有其他的？畅所欲言吧)" value="3"/>
        </el-select>
        </div>
      </el-form-item>
      <el-form-item  prop="transactionTitle">
        <div>话题标题&nbsp;&nbsp;<el-input style="width: 80%;padding-left:4%;" v-model="ruleForm.transactionTitle" placeholder="20字以内"/></div>
      </el-form-item>
      <el-form-item >
        <div><span style="position:relative;top:-100px">话题详细描述&nbsp;&nbsp;</span><el-input v-model="ruleForm.transactionContent" :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 style="display: flex; justify-content: flex-start;">
          <span style="width: 120px;">权重值</span>
          <el-input-number style="width: 130px;margin-left:-2%" v-model="ruleForm.sort" label="权重值" @change="sortHandleChange"/>
        </div>
      </el-form-item>
      <el-form-item>
        <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: {
        sort: '',
        communityId: '',
        createDate:'',
        createUser:'',
        id:'',
        isReported:'',
        status:'',
        taUserId:'',
        transactionContent:'',
        transactionTitle:'',
        type:'',
        updateDate:'',
        updateUser:'',
        userName:'',
        viewCount:'',
        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' }
        ]
      },
      lock: false,
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
    }
  },
  methods: {
    submitForm(formName, 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.transactionContent.length <= 0 && this.ruleForm.contentImg.length <= 0) {
            this.$message.error('话题内容描述 和 话题配图至少要存在一个!')
            return
          }
          this.ruleForm.status = status // 发布状态
          this.$store.dispatch('transaction/TransactionsAdd', this.ruleForm).then((res) => {
            if (res.code === '0') {
              this.$message({
                message: res.message,
                type: 'success'
              })
              this.$router.push({ name: 'transaction-import' })
              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)
    },
    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>
