
<template>
  <div class="root">
    <el-form :inline="true" :model="listQuery" class="form-listQuery">
      <el-form-item class="filter-item" label="话题编号">
        <el-input v-model.trim="listQuery.id" onkeyup="this.value=this.value.replace(/\D/g,'')" placeholder="话题编号" />
      </el-form-item>
      <el-form-item class="filter-item" label="话题标题" >
        <el-input v-model.trim="listQuery.transactionTitle" placeholder="话题标题" />
      </el-form-item>
      <el-form-item class="filter-item" label="话题发布人" >
        <el-input v-model.trim="listQuery.userName" placeholder="话题发布人" />
      </el-form-item>
      <el-form-item class="filter-item" label="话题类型" >
        <el-select v-model="listQuery.status" placeholder="请选择">
         <el-option label="选择状态" value="" />
         <el-option label="议事大厅" value="0"/>
         <el-option label="友令互助" value="1"/>
         <el-option label="二手租赁" value="2"/>
         <el-option label="其他" value="3"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="info" class="filter-item" @click="handleFilter">清空</el-button>
        <el-button type="primary" @click="search">查询</el-button>
      </el-form-item>
    </el-form>
    <div class="button">
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="warning" @click="edit">修改</el-button>
      <el-button type="danger" @click="deleteId">作废</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="transactionsList"
      border
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column prop="id" label="话题编号" align="center"/>
       <el-table-column prop="type" label="话题类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type=='0'">议事大厅</span>
            <span v-if="scope.row.type=='1'">友令互助</span>
            <span v-if="scope.row.type=='2'">二手租赁</span>
            <span v-if="scope.row.type=='3'">其他</span>
            </template>
       </el-table-column>
      <el-table-column prop="transactionTitle" label="话题标题" align="center">
      <template slot-scope="scope"><a><span style="color: #63B8FF" @click="clickTitle(scope.row.id)">{{ scope.row.transactionTitle }}</span></a></template>
      </el-table-column>
      <el-table-column prop="status" label="是否作废" align="center">
      <template slot-scope="scope">{{ scope.row.status=='0'?'已作废':'' }}</template>
      </el-table-column>
      <el-table-column prop="userName" label="发布人" align="center"/>
      <el-table-column prop="createDate" label="发布时间" align="center"><template slot-scope="scope">{{ formatDate(scope.row.createDate) }}</template></el-table-column>

      <el-table-column prop="viewCount" label="查看人数" align="center"/>
      <el-table-column prop="transactionLikeSum" label="点赞数" align="center"/>
      <el-table-column prop="transactionNoLikeSum" label="点衰数" align="center"/>
      <el-table-column prop="replySum" label="回复数" align="center">
        <template slot-scope="scope"><a><span style="color: #63B8FF" @click="clickReply(scope.row.replySum,scope.row.id)">{{ scope.row.replySum }}</span></a></template>
      </el-table-column>
      <el-table-column prop="reportSum" label="举报数量" align="center">
        <template slot-scope="scope"><a><span style="color: #63B8FF" @click="report(scope.row.reportSum,scope.row.id)">{{ scope.row.reportSum }}</span></a></template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page.sync="listQuery.pageNum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size.sync="listQuery.pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  data() {
    return {
      listQuery: {
        communityId: '',
				createDate:'',
				createUser:'',
				id:'',
				isReported:'',
				status:'',
				taUserId:'',
				transactionContent:'',
				transactionTitle:'',
				type:'',
				updateDate:'',
				updateUser:'',
				userName:'',
        viewCount:'',
        pageNum: 1,
        pageSize: 10
      },
      total: 0, // 数据总数
      transactionsList: [],
      deleteIds: [], // 选择的id集合
      listLoading: true, // 加载圈
    }
  },
  mounted() {
    // 获取数据
    this.dataQuery()
  },
  methods: {
    ...mapActions('listAnnouncement', [
      'BserPassCertification'
    ]),
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.listQuery.pageSize = val
      this.dataQuery()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.listQuery.pageNum = val
      this.dataQuery()
    },
    padDate(value) {
      value = value < 10 ? '0' + value : value
      return value
    },
    getStatus(row) { // 获取状态
      // let statusName = ''

      // // 判断状态
      // // switch (row.status) {
      // //   case '0':
      // //     statusName = '已作废'
      // //     break
      // //   case '1':
      // //     statusName = '已发布'
      // //     break
      // //   case '2':
      // //     statusName = '草稿'
      // //     break
      // // }

      // // 如果修改时间和创建时间不一致, 就显示已修改
      // if (row.status === '1' && (row.updateDate > row.createDate)) {
      //   statusName = '已修改'
      // }

      // return statusName
    },
    dataQuery() { // 查询数据
    
    console.log('this.listQuery.transactionTitle',this.listQuery.transactionTitle)
      this.listLoading = true
      this.$store.dispatch('transaction/TransactionsList', this.listQuery).then((res) => {
        const resData = res.data
        this.transactionsList = resData.list
        this.listQuery.pageNum = resData.pageNum
        this.listQuery.pageSize = resData.pageSize
        this.total = resData.total
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
        console.log('error TransactionsList')
      })
    },
    handleSelectionChange(data) {
      // 设置为 空
      this.deleteIds = []
      for (let i = 0; i < data.length; i++) {
        this.deleteIds.push(data[i].id)
      }
    },
	  search() {
      this.listQuery.pageNum = 1
      this.dataQuery()
    },
    handleFilter() {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = 10
      this.listQuery.ownerName =''
      this.listQuery.id=''
      this.listQuery.status=''
      this.listQuery.userName=''
      this.dataQuery()
    },
    // 添加公告
    add() {
      this.$router.push({ name: 'transaction-add' })
    },
    edit(){// 编辑公告
      const ids = this.deleteIds
      if (ids.length > 1) {
        this.$message.error('只能选择一行数据进行修改!')
        return
      }
      if (ids < 1) {
        this.$message.error('请选择一行数据进行修改!')
        return
      }
       let id= this.deleteIds[0]
       this.$router.push({ name: 'transaction-edit', query: { id: id }})
      },
    deleteTransaction(){
      const ids = this.deleteIds
      if (ids < 1) {
        this.$message.error('请选择一行数据进行修改!')
        return
      }
      // this.$store.dispatch('AnnouncementById', this.listQuery).then((res) => {
      //   const resData = res.data
      //   const announcement = resData.tpAnnouncement
      //   const imgList = resData.studentList
      //   this.listQuery.status= announcement.status
      //   console.log("1",this.listQuery.status)
      //   if(this.listQuery.status == 0){
      //     this.$message.error('已作废不可以作废')
      //     return
      //   }
      //   this.deleteId()     
      // }) 
      this.deleteId()   
    },

    deleteId(){
      const ids = this.deleteIds
      this.listLoading = true
      console.log(ids)
      this.$store.dispatch('transaction/TransactionsDelete', ids).then((res) => {
        this.listLoading = false
        this.dataQuery()
      }).catch(() => {
        this.listLoading = false
        console.log('error TransactionsDelete')
      })
    },

    // 二手帖子详情
    clickTitle(id) {
      console.log('id',id)
      this.$router.push({ name: 'transaction-info', query: { id: id }})
    },
      // 回复列表
    clickReply(sum,id) {
      if(sum == '0'){
        return
      }
      console.log('id',id)
      this.$router.push({ name: 'transactionreply-index', query: { id: id }})
    },
    formatDate(val) {
      if (val === null) {
        return ''
      }
      var value = new Date(val)
      var year = value.getFullYear()
      var month = value.getMonth() + 1
      var day = value.getDate()
      return year + '-' + month + '-' + day
    },
    // 举报数量
    report(sum,id){
      console.log('sum',sum)
      if(sum =='0'){
        return
      }
      console.log('id',id)
      this.$router.push({ name: 'transactionReport-index', query: { id: id }})
    }
  }
}
</script>

<style scoped>
.root{
  display: flex;
  flex-flow: column;
}
.form-listQuery{
  margin-top: 20px;
  margin-left: 30px;
}
.operation{
  display: flex;
  justify-content: space-between;
  margin-left: 20px;
  margin-right: 20px;
}
.block{
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
.button {
  margin-left: 20px;
}
</style>

