<template>
  <a-card size="small" :bordered="false" class="noticeList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <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-salesBillNo" v-model.trim="queryParam.salesBillNo" allowClear placeholder="请输入公告名称"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :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>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="default"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="primary" class="button-success" @click="handleDetail(record)" id="noticeList-detail-btn">详情</a-button>
      </template>
    </s-table>
    <!-- 公告详情 -->
    <notice-detail-modal :openModal="openModal" :itemId="itemId" @close="closeModal" />
  </a-card>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import noticeDetailModal from './detailModal.vue'
import { urgentList } from '@/api/urgent'
export default {
  components: { STable, noticeDetailModal },
  data () {
    return {
      queryParam: { //  查询条件
        buyerName: undefined //  客户名称
      },
      disabled: false, //  查询、重置按钮是否可操作
      dateFormat: 'YYYY-MM-DD',
      time: [], //  创建时间
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', align: 'center' },
        { title: '公告名称', dataIndex: 'buyerName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 200, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        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
        }
        return urgentList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          return data
        })
      },
      openModal: false, //  查看客户详情  弹框
      itemId: '' //  当前客户id
    }
  },
  methods: {
    // 不可选日期
    disabledDate (date, dateStrings) {
      return date && date.valueOf() > Date.now()
    },
    //  重置
    resetSearchForm () {
      this.queryParam.buyerName = undefined
      this.time = []
      this.$refs.table.refresh(true)
    },
    //  详情
    handleDetail (row) {
      this.itemId = row.id
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openModal = false
    }
  }
}
</script>
