<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="" />
         <el-option label="议事大厅" value="0"/>
         <el-option label="友令互助" value="1"/>
         <!-- -1包括，二手，租赁，求购 -->
         <el-option label="二手租赁" value="2"/>
         <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"
          :file-list="contentImgList"
          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 v-model="ruleForm.sort" label="权重值" @change="sortHandleChange"/>
        </div>
      </el-form-item>
      <el-form-item>
        <div style="padding-left:15%;">
        <el-button type="primary" @click="submitForm('1')" >发布</el-button>
        </div>
      </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 是已修改
      },
      contentImgList: [],
      imageUrl: '', // 轮播图预览
      dialogVisible: false, // 活动配图, 默认不显示 (以单张为单位)
      dialogImageUrl: '', // 活动配图, 图片链接 (以单张为单位)
      rules: {
        announcementTitle: [
          { required: true, message: '请输入公告名称', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 15 个字符以内', trigger: 'blur' }
        ],
        announcementContent: [
          { required: true, message: '公告详细描述', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入权重值', trigger: 'change' }
        ]
      },
      uploadImgUrl: process.env.BASE_API + '/uploadimage',
    }
  },
  mounted() {
    this.ruleForm.id = this.$route.query.id
    console.log('this.ruleForm.id',this.ruleForm.id)
    this.transactionById()
  },
  methods: {
    submitForm(status) {
      console.log(status)
      // 加载框
    //   const loading = this.$loading({
    //     lock: true,
    //     text: 'Loading',
    //     spinner: 'el-icon-loading',
    //     background: 'rgba(0, 0, 0, 0.7)'
    //   })

          if (this.ruleForm.transactionContent.length <= 0 && this.ruleForm.contentImg.length <= 0) {
            this.$message.error('内容描述 和 图至少要存在一个!')
            return
          }
          this.ruleForm.status = status // 发布状态
          this.$store.dispatch('transaction/TransactionsEdit', this.ruleForm).then((res) => {
            this.$router.push({ name: 'transaction-import' })
            // this.$message.error(res.message)
            // loading.close()
          }).catch(() => {
            console.log('error AddActivity')
          })
        
  
      // 关闭加载框
      // 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].url)
      }
    },
    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)
    },
    transactionById(){
      this.$store.dispatch('transaction/TransactionById', this.ruleForm.id).then((res) => {
         this.ruleForm.transactionTitle = res.data.tpTransaction.transactionTitle
         this.ruleForm.type = res.data.tpTransaction.type
         this.ruleForm.transactionContent = res.data.tpTransaction.transactionContent
         this.ruleForm.sort = res.data.tpTransaction.sort
        //  this.contentImgList = res.data.list
         console.log(' res.data.tpTransaction.list', res.data.list)

         // 多张图片进行遍历
        for (let i = 0; i < res.data.list.length; i++) {
          this.contentImgList.push({ url: res.data.list[i] })
          this.ruleForm.contentImg.push(res.data.list[i])
        }
          }).catch(() => {
            console.log('error TransactionById')
          })


    }
    
  }
}
</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>
