<template>
  <div class="stock">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="标题">
        <el-input v-model="dataForm.title" placeholder="请输入标题" clearable/>
      </el-form-item>
      <el-form-item label="发布时间">
        <el-date-picker
          v-model="dataForm.date"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:notice:save')" type="primary" @click="addOrUpdateHandle(0, false)">发布公告</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center">
      </el-table-column>
      <el-table-column
        prop="title"
        header-align="center"
        align="center"
        label="公告主题名称">
      </el-table-column>
      <el-table-column
        prop="levelDesc"
        header-align="center"
        align="center"
        label="级别">
      </el-table-column>
      <el-table-column
        prop="content"
        header-align="center"
        align="center"
        width="180"
        :show-overflow-tooltip="true"
        label="公告内容">
      </el-table-column>
      <el-table-column
        prop="-"
        header-align="center"
        align="center"
        label="附件">
      </el-table-column>
      <el-table-column
        prop="-"
        header-align="center"
        align="center"
        label="接收对象">
      </el-table-column>
      <el-table-column
        prop="-"
        header-align="center"
        align="center"
        label="已读">
      </el-table-column>
      <el-table-column
        prop="publisherName"
        header-align="center"
        align="center"
        label="发布人">
      </el-table-column>
      <el-table-column
        prop="publishTime"
        header-align="center"
        align="center"
        min-width="100"
        label="发布时间">
      </el-table-column>
      <el-table-column
        prop="notes"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        prop="stateDesc"
        header-align="center"
        align="center"
        label="状态">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:notice:info')" type="text" size="small" @click="addOrUpdateHandle(scope.row.noticeId, true)">查看</el-button>
          <el-button v-if="false" style="color: red" type="text" size="small" @click="cancel(scope.row.noticeId, false)">撤回</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './notice-add-or-update'
  export default {
    name: 'notice.vue',
    components: {
      AddOrUpdate
    },
    data () {
      return {
        addOrUpdateVisible: false,
        dataForm: {
          cusName: ''
        },
        options: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: []
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.addOrUpdateVisible = false
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/biz-service/notice/list'),
          method: 'get',
          params: this.$http.adornParams({
            'current': this.pageIndex,
            'size': this.pageSize,
            'title': this.dataForm.title ? this.dataForm.title : null,
            'dateStart': this.dataForm.date ? this.dataForm.date[0] : null,
            'dateEnd': this.dataForm.date ? this.dataForm.date[1] : null
          })
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id, display) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, display)
        })
      },
      // 删除
      deleteHandle (id) {
        if (!id) return
        let ids = []
        ids.push(id)
        this.$confirm(`确定删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/biz-service/notice/delete`),
            method: 'DELETE',
            data: ids
          }).then(({data}) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>

<style scoped>

</style>
