<template>
  <a-card size="small" :bordered="false" class="noticeList-wrap">
    <!-- 搜索条件 -->
    <div ref="tableSearch" class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="handelSearch(1)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="发布时间">
              <rangeDate ref="rangeDate" @change="dateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="公告名称">
              <a-input id="noticeList-title" v-model.trim="queryParam.notice.title" allowClear placeholder="请输入公告名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="handelSearch(1)" :disabled="disabled" id="noticeList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="noticeList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <a-table
      class="sTable fixPagination"
      ref="table"
      :style="{ height: tableHeight+84.5+'px' }"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :dataSource="loadData"
      :loading="listLoading"
      :pagination="false"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-badge :count="record.readFlag=='0'? 1 : 0" dot>
          <a-button size="small" type="primary" class="button-success" @click="handleDetail(record)" id="noticeList-detail-btn">详情</a-button>
        </a-badge>
      </template>
    </a-table>
    <!-- 分页 -->
    <tablePagination
      ref="tablePagination"
      :total="paginationProps.total"
      :current="paginationProps.current"
      :pageSize="paginationProps.pageSize"
      @changePageSize="changePageSize"
      @changePage="changePage" />
    <!-- 公告详情 -->
    <notice-detail-modal :openModal="openModal" :itemId="itemId" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import tablePagination from '@/views/common/tablePagination.vue'
import rangeDate from '@/views/common/rangeDate.vue'
import { mapActions } from 'vuex'
import noticeDetailModal from './detailModal.vue'
export default {
  name: 'Notice',
  mixins: [commonMixin],
  components: { STable, noticeDetailModal, rangeDate, tablePagination },
  data () {
    return {
      tableHeight: 0,
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        notice: {
          title: ''
        }
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '发布时间', dataIndex: 'createDate', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '公告名称', dataIndex: 'notice.title', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center' }
      ],
      loadData: [],
      paginationProps: {
        total: 0, //  分页总条数
        current: 1,
        pageSize: 20
      },
      listLoading: false,
      openModal: false, //  查看客户详情  弹框
      itemId: '' //  当前id
    }
  },
  computed: {
    // 接受到ws消息
    wsMessage () {
      return this.$store.getters.wsMessageData()
    }
  },
  methods: {
    ...mapActions([
      'getMessageList', // 查询列表
      'hasRead' // 设置已读
    ]),
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0] ? date[0] : ''
      this.queryParam.endDate = date[1] ? date[1] : ''
    },
    // 查询列表
    handelSearch (pageNo) {
      this.paginationProps.current = pageNo || this.paginationProps.current
      this.disabled = true
      const params = {
        pageNo: this.paginationProps.current,
        pageSize: this.paginationProps.pageSize,
        notice: {
          title: this.queryParam.notice.title
        },
        beginDate: this.queryParam.beginDate,
        endDate: this.queryParam.endDate
      }
      // 开始查询
      this.listLoading = true
      this.getMessageList(params).then((res) => {
        this.listLoading = false
        if (res.status == 200) {
          const data = res.data
          this.paginationProps.total = Number(res.data.count) || 0
          this.paginationProps.current = data.pageNo
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.loadData = data.list
          this.disabled = false
        } else {
          this.paginationProps.total = 0
          this.paginationProps.current = 1
          this.paginationProps.pageSize = 20
          this.loadData = []
        }
      })
    },
    //  分页  一页多少条change
    changePageSize (current, pageSize) {
      this.paginationProps.current = current
      this.paginationProps.pageSize = pageSize
      this.handelSearch()
    },
    //  分页 页码change
    changePage (current) {
      this.paginationProps.current = current
      this.handelSearch()
    },
    //  重置
    resetSearchForm () {
      this.resetData()
      this.handelSearch(1)
    },
    resetData () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.notice.title = undefined
      this.paginationProps.total = 0
      this.paginationProps.current = 1
      this.paginationProps.pageSize = 20
    },
    //  详情
    handleDetail (row) {
      this.itemId = row.notice.id
      this.openModal = true
      // 设置已读
      if (row.readFlag == '0') {
        this.hasRead({ msg_id: row.id }).then(res => {
          // 刷新列表
          if (res.status == 200) {
            this.handelSearch()
          }
        })
      }
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.openModal = false
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 195
    }
  },
  watch: {
    // 监听ws消息通知
    wsMessage (a, b) {
      if (a !== b && a) {
        // 新未读消息
        if (a.type == 'no_read_count') {
          this.resetData()
          this.handelSearch()
        }
      }
    },
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.handelSearch(1)
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .noticeList-wrap{
    margin: -12px -12px 0px;
  }
</style>
