<template>
  <div class="app-container usermanage">
    <div class="filter-container">
      <el-input v-model="keyword" placeholder="请输入标题或内容" class="filter-item" size="medium" clearable />
      <el-button type="primary" class="filter-item" @click="btn_search">搜索</el-button>
      <el-button v-if="isnotice == '0'" type="primary" class="filter-item" @click="btn_add()">发布公告</el-button>
      <el-button v-if="isnotice == '0'" type="danger" class="filter-item" @click="btn_del">批量删除</el-button>
    </div>
    <el-table v-loading="loading" :data="dataLists" border stripe @selection-change="changeSelects">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="F_Title" label="公告标题" align="center">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="btn_detail(scope.row.F_NoticeId)">
            {{ scope.row.F_Title.length>20?scope.row.F_Title.substring(0,20)+'...':scope.row.F_Title }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="F_CreateByName" label="发布人" align="center" min-width />
      <el-table-column prop="F_CreateOn" label="发布时间" align="center" min-width />
      <el-table-column prop="F_EndDate" label="有效期" align="center" min-width />
      <el-table-column prop="F_Frequency" label="点击量" align="center" min-width />
      <el-table-column v-if="isnotice == '0'" label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="btn_edit(scope.row.F_NoticeId)">编辑</el-button>
          <el-button type="text" @click="btn_top(scope.row.F_NoticeId)">置顶</el-button>
          <el-button type="text" @click="btn_delete(scope.row.F_NoticeId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  getGetList,
  getAddNotice,
  getSeeNotice,
  getDelNotice,
  getTopNotice
} from '@/api/AnnounceManagement/announce'
import Pagination from '@/components/context/Pagination' // 对el-pagination 二次封装
import noticeAddOrEdit from './components/AddOrEdit.vue'
import Detail from './components/detail.vue'
export default {
  name: 'Notice',
  components: {
    Pagination
  },
  props: {
  },
  data() {
    return {
      loading: false,
      isnotice: '0',
      keyword: '',
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 1 // 总共多少数据
      },
      multipleSelection: [], // 选中的数据
      selectArr: [],
      dataLists: [] // 列表数据
    }
  },
  watch: {
    // '$route'(to, from) {
    //   console.log(from)
    //   if (from.name == "Dashboard") {
    //     this.isnotice = this.$route.params.isNotice
    //   }else{
    //     this.isnotice = '0'
    //   }
    // }
  },
  activated() {
    this.isnotice = this.$route.params.isNotice || '0'
  },
  created() {
    this.getList()
  },
  methods: {
    // 复选框状态改变
    changeSelects(val) {
      this.multipleSelection = val
      this.selectArr = val.map(item => item.F_NoticeId)
    },
    getList() {
      const params = {
        page: this.pageParams.pageindex,
        pagesize: this.pageParams.pagesize,
        content: this.keyword
      }
      getGetList(params).then(res => {
        this.dataLists = res.rows
        this.pageParams.total = res.total
      })
    },
    btn_search() {
      this.getList()
    },
    // 添加
    btn_add() {
      this.$layer.iframe({
        content: {
          content: noticeAddOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {

          } // props
        },
        area: ['70%', '72%'],
        title: '添加公告'
      })
    },
    btn_edit(id) {
      this.$layer.iframe({
        content: {
          content: noticeAddOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            rowid: id.toString()
          } // props
        },
        area: ['70%', '72%'],
        title: '编辑公告'
      })
    },
    btn_detail(rowdata) {
      this.$layer.iframe({
        content: {
          content: Detail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            rowdata: rowdata.toString()
          } // props
        },
        area: ['70%', '70%'],
        title: '公告详情'
      })
    },
    btn_top(rid) {
      this.$confirm('您确定要将此公告置顶吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const params = {
            noticeid: rid
          }
          getTopNotice(params).then(res => {
            if (res.state == 'success') {
              this.$message.success('操作成功')
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message('已取消')
        })
    },
    btn_delete(rid) {
      this.ondel(rid)
    },
    btn_del() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('没有可删除的选项')
        return
      }
      const rid = this.selectArr.toString()
      this.ondel(rid)
    },
    ondel(rid) {
      this.$confirm('您确定要将此公告删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const params = {
            ids: rid
          }
          getDelNotice(params).then(res => {
            if (res.state == 'success') {
              this.$message.success('删除成功')
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message('已取消')
        })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
