<template>
  <a-card :bordered="false">
    <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" allow-clear placeholder="请输入标题" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
            <a-button style="margin-left: 8px" @click="() => {queryParam = {}, $refs.table.refresh(true)}">重置</a-button>
          </a-col>

        </a-row>

      </a-form>
    </div>
    <div class="table-operator">
      <a-button @click="handleCreate()" icon="plus" type="primary" v-if="hasPerm('honor:show:add')">新增</a-button>
    </div>

    <s-table
      ref="table"
      :columns="columns"
      :data="loadData"
      :rowKey="(record) => record.id"
    >
      <template slot="createTime" slot-scope="text, record">
        {{ record.createTime | moment('YYYY-MM-DD HH:mm:ss') }}
      </template>

      <template slot="action" slot-scope="text, record">
        <template v-if="record.ifTop == 0">
        <a-popconfirm title="是否确认置顶?" v-if="hasPerm('honor:show:add')" @confirm="() => handleSetTop(record)">
          <a href="javascript:;">置顶</a>
        </a-popconfirm>
        <a-divider type="vertical" />
        </template>
        <a href="javascript:;" v-if="hasPerm('honor:show:add')" @click="handleEdit(record)">修改</a>
        <a-divider type="vertical" />
        <a-popconfirm title="是否确认删除?" v-if="hasPerm('honor:show:del')" @confirm="() => handleDel(record)">
          <a href="javascript:;">删除</a>
        </a-popconfirm>
        <a-divider type="vertical" />
        <a href="javascript:;" v-if="hasPerm('honor:show:detail')" @click="handleDetail(record)" >详情</a>
      </template>
    </s-table>
    </div>
  </a-card>
</template>
<script>
  import { STable } from '@/components'
  import { honorPage, honorDel, honorSetTop } from '@/api/honor/honor'
  export default {
    components: {
      STable
    },
    data () {
      return {
        // 查询参数
        queryParam: { title: '' },
        // 表头
        columns: [
          {
            title: '标题',
            dataIndex: 'title'
          },
          // {
          //   title: '内容',
          //   width: 500,
          //   dataIndex: 'content'
          // },
        {
          title: '创建时间',
          dataIndex: 'createTime',
            width: 200,
          scopedSlots: { customRender: 'createTime' }
        },
          {
            title: '操作',
            key: 'operation',
            fixed: 'right',
            width: 200,
            scopedSlots: { customRender: 'action' }
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: (parameter) => {
          return honorPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res
          })
        }
      }
    },
    // 生命周期 - 创建完成
    created () {
    },
    // 生命周期 - 销毁完成
    destroyed () { },
    // 监控data中的数据变化
    watch: {
    },
    methods: {
      // 新增
      handleCreate () {
        this.$router.push({
          path: '/honor/add',
          query: {}
        })
      },
      // 编辑
      handleEdit (record) {
        this.$router.push({
          path: '/honor/add',
          query: {
            id: record.id
          }
        })
      },
      // 详情
      handleDetail (record) {
        this.$router.push({
          path: '/honor/detail',
          query: {
            id: record.id
          }
        })
      },
      // 删除
      handleDel (record) {
        honorDel({ id: record.id }).then((res) => {
          if (res.code == 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh(false)
          } else {
            this.$message.success('删除失败')
          }
        })
      },
      // 置顶
      handleSetTop (record) {
        honorSetTop({ id: record.id }).then((res) => {
          if (res.code == 200) {
            this.$message.success('置顶成功')
            this.$refs.table.refresh(false)
          } else {
            this.$message.success('置顶失败')
          }
        })
      }
    }
  }
  </script>

<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
</style>
