<template>
  <table-template :soul="this">
    <template #action="{ record }">
      <a-space>
        <a
          v-if="record.releaseStatus == 1 || record.releaseStatus == 3"
          @click="release(record)"
        >
          发布
        </a>
        <a v-if="hasPerm('SYS02103.PUBLISH') && record.releaseStatus == 2" @click="revoke(record)">撤销</a>
        <a @click="detail(record.id)">详情</a>
        <a
          style="color: red"
          v-if="record.releaseStatus == 1 || record.releaseStatus == 3"
          @click="deleteNotice(record.id)"
        >
          删除
        </a>
      </a-space>
    </template>
    <template #free>
      <NoticeDetail ref="NoticeDetail" :success="reset" />
    </template>
  </table-template>
</template>
<script>
import { TableTemplate, TableScript, SearchType } from '@/components/table'
import { getPage, save, deleteOne } from '@/api/notice'
import NoticeDetail from './NoticeDetail'
import dayjs from 'dayjs'

const columns = [
  {
    title: '名称',
    width: 130,
    dataIndex: 'title',
    filter: { type: SearchType.STRING }
  },
  {
    title: '内容',
    dataIndex: 'content'
  },

  { title: '状态', dataIndex: 'releaseStatusValue', width: 100 },
  {
    title: '状态',
    dataIndex: 'releaseStatus',
    filter: { type: SearchType.ENUM },
    hidden: true,
    enum: 'BIZ0003'
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 220,
    sorter: true,
    filter: { type: SearchType.RANGE },
    customRender: (text) => dayjs(text).format('YYYY-MM-DD HH:mm:ss')
  },
  {
    title: '操作',
    width: 150,
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'HomeNotice',
  mixins: [TableScript],
  components: { TableTemplate, NoticeDetail },
  data() {
    return {
      columns,
      useYScroll: true,
      title: '首页通知管理',
      codes: [['BIZ0003'], []],
      toolbar: [
        {
          text: '新建',
          handler: 'add',
          bizCode: 'SYS02103.EDIT'
        }
      ]
    }
  },
  methods: {
    queryData: getPage,
    release(record) {
      const _this = this
      this.$confirm({
        title: '是否发布此通知',
        onOk() {
          save({ id: record.id, releaseStatus: 2 }).then(({ code }) => {
            if (code == 200) {
              _this.$message.success('发布成功')
              _this.reset()
            }
          })
        }
      })
    },
    revoke(record) {
      const _this = this
      this.$confirm({
        title: '是否撤销此通知',
        onOk() {
          save({ id: record.id, releaseStatus: 3 }).then(({ code }) => {
            if (code == 200) {
              _this.$message.success('撤销成功')
              _this.reset()
            }
          })
        }
      })
    },
    deleteNotice(id) {
      const _this = this
      this.$confirm({
        title: '是否删除此通知',
        onOk() {
          deleteOne(id).then(({ code }) => {
            if (code == 200) {
              _this.$message.success('删除成功')
              _this.reset()
            }
          })
        }
      })
    },
    detail(id) {
      this.$refs.NoticeDetail.show(id)
    },
    add() {
      this.$refs.NoticeDetail.show(null)
    }
  }
}
</script>