<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="searchDate"
        :picker-options="pickerOptions"
        class="filter-item"
        type="daterange"
        format="yyyy年MM月dd日"
        value-format="yyyy-MM-dd"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"/>
      <el-input v-model="keyword" placeholder="请输入关键词" class="filter-item"/>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add">添加</el-button>
    </div>

    <el-table v-loading="loading" :data="dataLists" border stripe>
      <el-table-column type="index" label="编号" align="center" fixed width="80"/>
      <el-table-column prop="title" label="标题" align="center" min-width=""/>
      <el-table-column prop="content" label="内容" align="center" min-width=""/>
      <el-table-column prop="createon" label="创建时间" align="center" min-width=""/>
      <el-table-column prop="createby" label="创建人" align="center" min-width=""/>
      <el-table-column label="操作" width="250" align="center" class-name="oparate_btn" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" plain type="primary" @click="btn_details(scope.row.id)">详情</el-button>
          <el-button size="mini" plain type="primary" @click="btn_edit(scope.row.id)">编辑</el-button>
          <el-button size="mini" plain type="danger" @click="btn_delete(scope.row.id)">删除</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 { getScheduleLists, deleteBlack } from '@/api/notice/manage'
import addOrEditManege from './addOrEditManege'
import { pickerOptions } from '@/utils'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装
import seeManege from './seeManege'

export default {
  // name: 'Manage',
  components: {
    Pagination
  },
  filters: {
    blackTypeFilter(status) {
      const statusMap = {
        0: '',
        1: 'danger',
        2: 'warning'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      loading: false,
      keyword: '',
      searchDate: '',
      pickerOptions,
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [] // 列表数据
    }
  },
  created() {
    this.getList()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          keyword: this.keyword, //	模糊查询
          stime: this.searchDate && this.searchDate[0],
          etime: this.searchDate && this.searchDate[1]
        }
        getScheduleLists(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.pageParams.total = response.data.total
            this.dataLists = response.data.rows
          }
        })
        resolve()
      })
    },
    btn_search() {
      this.pageParams.pageindex = 1
      this.getList()
    },
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addOrEditManege, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': '' }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['40%', '600px'],
        title: '添加公告'
      })
    },
    btn_edit(editId) {
      this.$layer.iframe({
        content: {
          content: addOrEditManege, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': editId }// props
        },
        area: ['40%', '600px'],
        title: '编辑公告'
      })
    },
    btn_delete(editId) {
      this.$confirm('您确定要将此公告删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteBlack(editId).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 详情
    btn_details(editId) {
      this.$layer.iframe({
        content: {
          content: seeManege, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {
            'rowid': editId
          } // props
        },
        area: ['65%', '660px'],
        title: '查看公告'
      })
    }
  }
}
</script>

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

</style>
