<template>
  <div>
    <toolbar
      @onSearch="filterData"
    />
    <white-board>
      <el-row class="header" :gutter="20">
        <el-col class="toolbar-txt" :span="6">共有标签：{{ info.totalTagNum }}个</el-col>
        <el-col class="toolbar-txt" :span="6">未用标签：{{ info.unusedTagNum }}个</el-col>
        <el-col class="flex-end" :span="12">
          <el-button icon="el-icon-plus" type="primary" @click="handleAddItem">创建标签</el-button>
        </el-col>
      </el-row>
      <table-with-page
        :columns="columns"
        :items="items"
        :pagination="pagination"
        :page-change="pageChange"
      >
        <template #btn-box="scope">
          <el-button type="danger" @click="handleDeleteSelect(scope.selected)">删除</el-button>
        </template>
      </table-with-page>
    </white-board>

    <!-- <add-item-modal :tag-type="tagType" /> -->
  </div>
</template>

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

import mxFilterList from '@/mixins/filterList'
import { getList, getNotUsed, changeShowStatus, deleteTag, deleteTags } from '@/model/tag'

export default {
  name: 'TagPage',

  components: { toolbar },

  mixins: [mxFilterList({
    fetchList: getList // 在下方data再声明一个 fetchList: getList 同等效果
  })],

  props: {
    tagType: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      columns: [
        {
          key: 'selection'
        }, {
          key: 'tagType',
          name: '标签类型',
          width: '130',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(MODULETYPE.TAG_MODAL_TYPE_TAG, val)
        }, {
          key: 'tagName',
          name: '标签名称',
          minWidth: '150'
        }, {
          key: 'usedNum',
          name: '使用内容数',
          width: '100'
        }, {
          key: 'usedStatus',
          name: '使用状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['已使用', '未使用'], ['success', 'danger']), val)
        }, {
          key: 'showStatus',
          name: '展示状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['展示中', '未展示'], ['success', 'danger']), val)
        }, {
          key: 'action',
          name: '操作',
          width: '180',
          render: (h, { row }) => {
            const isShow = row.isShow
            const action = []
            action.push(h('el-button', {
              props: {
                type: 'primary',
                size: 'small'
              },
              on: {
                click: () => this.view(row)
              }
            }, '详情'))
            action.push(h('el-button', {
              props: {
                type: isShow ? 'info' : 'primary',
                size: 'small'
              },
              on: {
                click: () => this.changeShowStatus(row)
              }
            }, isShow ? '禁用展示' : '开启展示'))
            return h('div', action)
          }
        }
      ],
      info: {
        totalTagNum: 0,
        unusedTagNum: 0
      }
    }
  },

  watch: {
    tagType: {
      handler(val) {
        this.internalFilterObj['moduleType'] = val
      },
      immediate: true
    }
  },

  created() {
    this.bus.$on(EMITKEY.TAGRELOAD, this.reload)
  },

  beforedestroy() {
    this.bus.$off(EMITKEY.TAGRELOAD, this.reload)
  },

  methods: {
    loadCallBack(source) {
      this.info.totalTagNum = source.total
      this.loadNotUsedCount()
    },

    async loadNotUsedCount() {
      const { success, data, msg } = await getNotUsed({
        moduleType: this.tagType,
        data: {
          ...this.filter
        }
      })
      if (success) {
        this.info.unusedTagNum = data.notUsed
      } else {
        this.$message.error(msg)
      }
    },

    handleAddItem() {
      this.$tagAddItemModal({
        tagType: this.tagType
      })
    },

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

    async handleDeleteItem(row) {
      const { success, msg } = await deleteTag({
        id: row.id
      })
      if (success) {
        this.bus.$emit(EMITKEY.TAGRELOAD)
        this.$message.success('删除成功~')
      } else {
        this.$message.error(msg)
      }
    },

    view(row) {
      this.$tagItemDrawer({
        id: row.id
      })
    },

    async changeShowStatus(row) {
      const { success, msg } = await changeShowStatus({
        id: row.id
      })
      if (success) {
        this.$message.success('切换成功~')
        this.bus.$emit(EMITKEY.TAGRELOAD)
      } 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>
