<template>
  <a-card size="small" :bordered="false" class="noticeList-wrap">
    <!-- 搜索条件 -->
    <div 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="发布时间">
              <a-range-picker
                style="width:100%"
                id="noticeList-creatTime"
                :disabledDate="disabledDate"
                v-model="time"
                :format="dateFormat"
                :placeholder="['开始时间', '结束时间']" />
            </a-form-item>
            </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"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :dataSource="loadData"
      :loading="listLoading"
      :pagination="paginationProps"
      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>
    <!-- 公告详情 -->
    <notice-detail-modal :openModal="openModal" :itemId="itemId" @close="closeModal" />
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import { mapActions } from 'vuex'
import noticeDetailModal from './detailModal.vue'
export default {
  components: { STable, noticeDetailModal },
  data () {
    return {
      queryParam: { //  查询条件
        notice: {
          title: ''
        }
      },
      disabled: false, //  查询、重置按钮是否可操作
      dateFormat: 'YYYY-MM-DD',
      time: [], //  创建时间
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '发布时间', dataIndex: 'realseDate', 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: [],
      pageNo: 1, //  分页页码
      pageSize: 10, //  分页 每页多少条
      paginationProps: {
        showSizeChanger: true, //  是否可以改变 pageSize
        total: 0, //  分页总条数
        current: 1,
        onShowSizeChange: (current, pageSize) => this.changePageSize(current, pageSize),
        onChange: (current) => this.changePage(current)
      },
      listLoading: false,
      openModal: false, //  查看客户详情  弹框
      itemId: '' //  当前id
    }
  },
  computed: {
    // 接受到ws消息
    wsMessage () {
      return this.$store.getters.wsMessageData()
    }
  },
  watch: {
    // 监听ws消息通知
    wsMessage (a, b) {
      if (a !== b && a) {
        // 新未读消息
        if (a.type == 'no_read_count') {
          this.resetSearchForm()
        }
      }
    }
  },
  methods: {
    ...mapActions([
      'getMessageList', // 查询列表
      'hasRead' // 设置已读
    ]),
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    // 查询列表
    handelSearch (pageNo) {
      this.pageNo = pageNo || this.pageNo
      this.disabled = true
      // 创建时间
      if (this.time && this.time.length > 0) {
        this.queryParam.beginDate = moment(this.time[0]).format(this.dateFormat)
        this.queryParam.endDate = moment(this.time[1]).format(this.dateFormat)
      } else {
        this.queryParam.beginDate = undefined
        this.queryParam.endDate = undefined
      }
      const params = {
        pageNo: this.pageNo,
        pageSize: this.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.loadData = []
        }
      })
    },
    //  分页  一页多少条change
    changePageSize (current, pageSize) {
      this.pageNo = current
      this.pageSize = pageSize
      this.handelSearch()
    },
    //  分页 页码change
    changePage (current) {
      this.pageNo = current
      this.handelSearch()
    },
    //  重置
    resetSearchForm () {
      this.queryParam.notice.title = undefined
      this.time = []
      this.pageNo = 1
      this.pageSize = 10
      this.paginationProps.total = 0
      this.paginationProps.current = 1
      this.handelSearch(1)
    },
    //  详情
    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
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.openModal = false
      vm.handelSearch(1)
    })
  }
}
</script>
