<template>
  <a-card :bordered="false">
    <a-tabs default-active-key="2" @change="tabsCallback">
      <a-tab-pane key="2" tab="我接收的">
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item label="公告标题"><a-input v-model="queryParam.title" placeholder="请输入公告标题" @pressEnter="loadData" /></a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => {queryParam = {}, this.loadData()}">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <s-table
          ref="table2"
          :columns="columns"
          :data="loadData"
          :rowKey="(record) => record.id"
        >
          <template slot="type" slot-scope="text, record">
            <a-tag>{{ record.type | typeFilter }}</a-tag>
          </template>
          <template slot="publishTime" slot-scope="text, record">
            {{ record.publishTime | moment('YYYY-MM-DD HH:mm:ss') }}
          </template>
          <span slot="action" slot-scope="text, record">
            <a v-if="hasPerm('notice:list')" @click="showGetPage(record.id, 2)">查看</a>
            <a-divider type="vertical" />
          </span>
        </s-table>
      </a-tab-pane>
      <a-tab-pane key="1" tab="我发布的" v-if="hasPerm('notice:edit')" >
        <div class="table-page-search-wrapper">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item label="公告标题"><a-input v-model="queryParam.title" placeholder="请输入公告标题" @pressEnter="loadData" /></a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => {queryParam = {}, this.loadData()}">重置</a-button>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <div class="table-operator">
          <a-button @click="showAddPage()" icon="plus" type="primary" v-if="hasPerm('notice:edit')">发布</a-button>
        </div>

        <s-table
          ref="table1"
          :columns="columns"
          :data="loadData"
          :rowKey="(record) => record.id"
        >
          <template slot="type" slot-scope="text, record">

            <a-tag>{{ record.type | typeFilter }}</a-tag>
          </template>
          <template slot="publishTime" slot-scope="text, record">
            {{ record.publishTime | moment('YYYY-MM-DD HH:mm:ss') }}
          </template>

          <span slot="action" slot-scope="text, record">
            <a v-if="hasPerm('notice:edit')" @click="showAddPage(record.id)">编辑</a>
            <a-divider type="vertical" v-if="hasPerm('notice:edit')" />
            <a v-if="hasPerm('notice:list')" @click="showGetPage(record.id, 1)">查看</a>
            <a-divider type="vertical" v-if="hasPerm('notice:list')"/>
            <a-popconfirm v-if="hasPerm('notice:del')" placement="topRight" title="确认删除？" @confirm="() => handleDel(record)">
              <a>删除</a>
            </a-popconfirm>
          </span>
        </s-table>
      </a-tab-pane>
    </a-tabs>
  </a-card>
</template>
<script>
  import { STable } from '@/components'
  import { noticePage, noticeDel } from '@/api/notice/notice'

  export default {
    components: {
      STable
    },
    data () {
      return {
        // 查询参数
        queryParam: { noticeRange: 2 },
        // 表头
        columns: [
          {
            title: '公告标题',
            dataIndex: 'title'
          },
          {
            title: '发布单位/人',
            dataIndex: 'createOrgName'
          },
          {
            title: '发布时间',
            width: 200,
            dataIndex: 'publishTime',
            key: 'publishTime',
            scopedSlots: { customRender: 'publishTime' }
          },
          {
            title: '菜单类型',
            dataIndex: 'type',
            scopedSlots: { customRender: 'type' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return noticePage(Object.assign(parameter, this.queryParam)).then((res) => {
            console.log(res)
            return res
          })
        }
      }
    },
    filters: {
      typeFilter (type) {
        const typeMap = {
          1: '新闻资讯',
          2: '公告详情',
          3: '通知'
        }
        return typeMap[type]
      }
    },
    created () {
      if (this.hasPerm('notice:edit') || this.hasPerm('notice:del') || this.hasPerm('notice:list')) {
        this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        })
      }
    },
    methods: {
      tabsCallback (key) {
        if (key === '1') {
          this.queryParam.noticeRange = 1
          this.$refs.table1.refresh(true)
        }
        if (key === '2') {
          this.queryParam.noticeRange = 2
          this.$refs.table2.refresh(true)
        }
      },
      /**
       * 删除
       */
      handleDel (record) {
        noticeDel({ id: record.id, deleteReason: '' }).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh(true)
          } else {
            this.$message.error('删除失败：' + res.msg)
          }
        })
      },
      showAddPage (id) {
        console.log(id)
        this.$router.push({
          path: '/notice/add',
          query: {
            id: id
          }
        })
      },
      showGetPage (id, type) {
        this.$router.push({
          path: '/notice/detail',
          query: {
            id: id,
            type: type
          }
        })
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
