
<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.announcementNumber" 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.announcementTitle" placeholder="公告标题" />
      </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="deleteAnnouncement">作废</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="listAnnouncement"
      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="announcementTitle" label="标题" align="center" >
        <template slot-scope="scope"><a><span style="color: #63B8FF" @click="clickTitle(scope.row.id)">{{ scope.row.announcementTitle }}</span></a></template>
      </el-table-column>
      <el-table-column prop="viewCount" label="查看数量" align="center"/>
      <el-table-column prop="sort" label="权重" align="center"/>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <span>{{ getStatus(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="发布时间" align="center"><template slot-scope="scope">{{ formatDate(scope.row.createDate) }}</template></el-table-column>
      <el-table-column prop="createUserName" label="发布人" align="center"/>
      <el-table-column prop="updateDate" label="修改时间" align="center" ><template slot-scope="scope">{{ formatDate(scope.row.updateDate) }}</template></el-table-column>
      <el-table-column prop="updateDateName" label="修改人" align="center"/>
    </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: {
        id: '',
        announcementNumber: '',
        number: '',
        announcementTitle: '',
        viewCount: '',
        sort: '',
        status: '',
        createDate: '',
        createUser: '',
        updateUser: '',
        updateDate: '',
        createUserName: '',
        updateDateName: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0, // 数据总数
      listAnnouncement: [],
      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() { // 查询数据
      this.listLoading = true
      this.$store.dispatch('ListAnnouncement', this.listQuery).then((res) => {
        const resData = res.data
        this.listAnnouncement = 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 ListAnnouncement')
      })
    },
    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.announcementNumber=''
      this.listQuery.announcementTitle=''
      this.dataQuery()
    },
    // 添加公告
    add() {
      this.$router.push({ name: 'announcement-add' })
    },
    edit(){// 编辑公告
      const ids = this.deleteIds
      if (ids.length > 1) {
        this.$message.error('只能选择一行数据进行修改!')
        return
      }
      if (ids < 1) {
        this.$message.error('请选择一行数据进行修改!')
        return
      }
       let ide= this.deleteIds[0]
       this.listQuery.id = ide
      this.$store.dispatch('AnnouncementById', this.listQuery).then((res) => {
        const resData = res.data
        const announcement = resData.tpAnnouncement
        const imgList = resData.studentList
        this.listQuery = announcement
        this.listQuery.announcementTitle = ''
        console.log("1",this.listQuery.status)
        if(this.listQuery.status == 0){
          this.$message.error('已作废不可以修改')
          return
        } 
       this.$router.push({ name: 'announcement-edit', query: { id: ide }})
      }) 
      },
    deleteAnnouncement(){
       let ide= this.deleteIds[0]
       this.listQuery.id = ide 
      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()     
      })    
    },

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


    clickTitle(id) {
      this.$router.push({ name: 'contentParticulars-details', 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()
      var hour = value.getHours()
      var minutes = value.getMinutes()
      var seconds = value.getSeconds()
      return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
      return year + '-' + month + '-' + day
    },
  }
}
</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>

