<template>
  <div class="">
    <toolbar @on-filter="filterData" @on-reset="filterData" />

    <div class="m-10 bg-w p-20 br-10">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd"
        >新增</el-button
      >
      <base-table
        :columns="columns"
        :items="items"
        :pagination="pagination"
        :page-change="pageChange"
      />
    </div>
  </div>
</template>

<script>
import toolbar from './toolbar';
import mxFilterList from '@/mixins/filterList';
import { getPage, delItem } from '@/api/base/banner';
import { getList as getDict } from '@/api/dict';

export default {
  name: 'BannerManagement',

  components: { toolbar },

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

  data() {
    return {
      columns: [
        {
          key: 'type',
          name: '位置',
          minWidth: this.$col.b,
          render: (h, { row }) => {
            const item = this.options.find(x => x.value === row.type);
            if (item) {
              return h('span', item.label);
            }
            return h('span', '-');
          }
        },
        {
          key: 'imgUrl',
          name: '图片',
          width: this.$col.b,
          render: (h, { row }) =>
            h('img', {
              style: {
                width: '160px',
                height: '90px'
              },
              class: 'pre-img',
              attrs: {
                src: row.imgUrl
              },
              on: {
                click: () =>
                  this.$AdvanceViewImageModal({
                    items: [{ src: row.imgUrl }]
                  })
              }
            })
        },
        {
          key: 'name',
          name: '标题',
          minWidth: this.$col.s
        },
        {
          key: 'link',
          name: '链接',
          minWidth: this.$col.m,
          showOverflowTooltip: true
        },
        {
          key: 'sort',
          name: '排序',
          width: this.$col.s
        },
        {
          key: 'createAt',
          name: '创建时间',
          width: this.$col.b
        },
        {
          key: 'updateAt',
          name: '修改时间',
          width: this.$col.b
        },
        {
          key: 'isShow',
          name: '显示/隐藏',
          width: this.$col.s,
          type: 'tag',
          fetchTagType: val => (val ? 'success' : 'info'),
          tagName: row => (row.isShow ? '显示' : '隐藏')
        },
        {
          key: 'action',
          name: '操作',
          width: '120',
          render: (h, { row }) => {
            const action = [];
            action.push(
              h(
                'el-button',
                {
                  props: {
                    type: 'text'
                  },
                  on: {
                    click: () =>
                      this.$BannerItemModal({
                        id: row.id
                      })
                  }
                },
                '编辑'
              )
            );
            action.push(
              h(
                'BaseBtn',
                {
                  props: {
                    popip: true,
                    txt: '删除',
                    type: 'text'
                  },
                  class: 'ml-10',
                  on: {
                    ok: () => this.handleDelItem(row)
                  }
                },
                '删除'
              )
            );

            return h('div', action);
          }
        }
      ],
      options: []
    };
  },

  created() {
    this.$g_on('banner_reload', this.reload);
    this.loadDict();
  },

  beforeDestroy() {
    this.$g_off('banner_reload', this.reload);
  },

  methods: {
    async loadDict() {
      const { data } = await getDict({
        dictCode: this.$dict.BANNER_TYPE
      });
      this.options = data.map(x => ({
        value: x.value,
        label: x.name
      }));
    },
    handleAdd() {
      this.$BannerItemModal();
    },
    async handleDelItem(item) {
      const { success, msg } = await delItem({
        id: item.id
      });
      if (success) {
        this.$success('删除成功！');
        this.$g_emit('banner_reload');
      }
    }
  }
};
</script>

<style type="scss" scoped></style>
