<template>
  <div>
    <white-board>
      <div style="display: flex;justify-content: flex-end;">
        <el-button icon="el-icon-plus" type="primary" @click="handleAddItem">新增</el-button>
      </div>
      <table-with-page
        :columns="columns"
        :items="items"
        :show-page="false"
      >
        <template #btn-box="scope">
          <el-button type="danger" @click="handleDeleteSelect(scope.selected)">删除</el-button>
        </template>
      </table-with-page>
    </white-board>
  </div>
</template>

<script>
import EMITKEY from '@/const/emitNameKey'
import { getTagTypeInArr, setStatus } from '@/utils'

import { getList, deleteWords, deleteWord } from '@/model/hotwords'

export default {
  name: 'HotWordsPage',

  data() {
    return {
      items: [],
      columns: [
        {
          key: 'word',
          name: '热搜词',
          minWidth: '120'
        }, {
          key: 'sort',
          name: '排序（倒序）',
          width: '100'
        }, {
          key: 'isTopF',
          name: '置顶状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['已置顶', '未置顶'], ['success', 'danger']), val)
        }, {
          key: 'isUseF',
          name: '可用状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['使用中', '未使用'], ['success', 'danger']), val)
        }, {
          key: 'action',
          name: '操作',
          width: '200',
          render: (h, { row }) => {
            const used = row.isUse
            const action = []
            action.push(h('el-button', {
              props: {
                type: 'primary',
                size: 'small'
              },
              on: {
                click: () => this.view(row)
              }
            }, '详情'))
            action.push(h('el-button', {
              props: {
                type: used ? 'info' : 'primary',
                size: 'small'
              },
              on: {
                click: () => this.changeShowStatus(row)
              }
            }, used ? '禁用' : '开启'))
            return h('div', action)
          }
        }
      ],
      dialogVisible: false,
      item: {},
      num: null
    }
  },

  created() {
    this.loadData()
    this.bus.$on(EMITKEY.HOTWORDSRELOAD, this.loadData)
  },

  beforedestroy() {
    this.bus.$off(EMITKEY.HOTWORDSRELOAD, this.loadData)
  },

  methods: {
    async loadData() {
      const { success, data, msg } = await getList()
      if (success) {
        this.items.splice(0, this.items.length)
        this.items.push(...data)
      } else {
        this.$message.error(msg)
      }
    },

    async handleDeleteSelect(selected) {
      if (selected.length < 1) {
        this.$message.error(`请选择要删除的记录~`)
        return false
      }
      const { success, msg } = await deleteWords({
        data: selected.map(x => x.word)
      })
      if (success) {
        this.$message.success('操作成功~')
        this.bus.$emit(EMITKEY.HOTWORDSRELOAD)
      } else {
        this.$message.error(msg)
      }
    },

    handleAddItem() {
      this.$hotWordsItemModal({
        modalType: 'add'
      })
    },

    view(row) {
      this.$hotWordsItemModal({
        modalType: 'edit',
        id: row.id
      })
    },

    async changeShowStatus(row) {
      const { success, msg } = await deleteWord({
        id: row.id
      })
      if (success) {
        this.$message.success('切换成功~')
        this.bus.$emit(EMITKEY.HOTWORDSRELOAD)
      } else {
        this.$message.error(msg)
      }
    }
  }
}
</script>

<style scoped>
.header {
  padding: 4px 20px 6px 20px;
}
.toolbar-txt {
  font-size: 13px;
  padding: 5px;
  line-height: 1.7;
}
.flex-end {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>
