<template>
  <div class="container">
    <!--     <toolbar
      @onSearch="filterData"
    /> -->
    <white-board>
      <el-row class="header" :gutter="20">
        <el-col class="toolbar-txt" :span="6">共有banner个数：{{ info.totalNum }}</el-col>
        <el-col class="toolbar-txt" :span="6">发布个数：{{ info.usedNum }}</el-col>
        <el-col class="flex-end" :span="12">
          <el-button icon="el-icon-plus" type="primary" @click="handleAddItem">创建banner</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>

  </div>
</template>

<script>
// import toolbar from './toolbar'
import mxFilterList from '@/mixins/filterList'
import { getList, deleteBanners, changeShowStatus, getShowNum } from '@/model/banner'
import { getTagTypeInArr, setStatus } from '@/utils'

import EMITKEY from '@/const/emitNameKey'

export default {
  name: 'BannerManagement',

  // components: { toolbar },

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

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

  data() {
    return {
      columns: [
        {
          key: 'selection'
        }, {
          key: 'title',
          name: 'banner名称',
          minWidth: '80'
        }, {
          key: 'url',
          name: 'banner链接',
          minWidth: '150'
        }, {
          name: 'Banner',
          width: '200',
          render: (h, { row }) => {
            return h('img', {
              style: {
                width: '100%',
                height: 'auto'
              },
              attrs: {
                src: row.imgUrl
              }
            })
          }
        }, {
          key: 'sort',
          name: '排序（倒序）',
          width: '100'
        }, {
          key: 'isTopF',
          name: '置顶状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['已置顶', '未置顶'], ['success', 'danger']), val)
        }, {
          key: 'isShowF',
          name: '展示状态',
          width: '80',
          type: 'tag',
          fetchTagType: (val) => getTagTypeInArr(setStatus(['已展示', '未展示'], ['success', 'danger']), val)
        }, {
          key: 'action',
          name: '操作',
          width: '180',
          render: (h, { row }) => {
            const action = []
            const isShow = row.isShow
            action.push(h('el-button', {
              props: {
                type: 'primary'
              },
              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: {
        totalNum: 0,
        usedNum: 0
      }
    }
  },

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

  mounted() {
    // 启用toolbar后关闭下面请求
    this.filterData()
  },

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

  methods: {
    async loadCallBack(source) {
      this.info.totalNum = source.total
      const { success, data, msg } = await getShowNum()
      if (success) {
        this.info.usedNum = data.showNum
      } else {
        this.$message.error(msg)
      }
    },

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

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

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

    view(row) {
      this.$bannerItemModal({
        id: row.id,
        modalType: 'edit'
      })
    }
  }
}
</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>
