<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/member/level';

export default {
  name: 'MemberLever',

  components: { toolbar },

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

  data() {
    return {
      columns: [
        {
          key: 'name',
          name: '等级名称',
          minWidth: this.$col.auto(240)
        },
        {
          key: 'yearPrice',
          name: '会员年费',
          minWidth: this.$col.m,
          render: (h, { row }) => h('span', `￥${row.yearPrice}`)
        },
        {
          key: 'monthPrice',
          name: '会员月费',
          minWidth: this.$col.m,
          render: (h, { row }) => h('span', `￥${row.monthPrice}`)
        },
        {
          key: 'sale',
          name: '商品优惠折扣',
          minWidth: this.$col.m,
          render: (h, { row }) => h('span', `￥${row.sale}`)
        },
        {
          key: 'isEnable',
          name: '状态',
          width: this.$col.s,
          type: 'tag',
          fetchTagType: val => (val ? 'success' : 'info'),
          tagName: row => (row.isEnable ? '启用' : '停用')
        },
        {
          key: 'action',
          name: '操作',
          width: '120',
          render: (h, { row }) => {
            const action = [];
            action.push(
              h(
                'el-button',
                {
                  props: {
                    type: 'text'
                  },
                  on: {
                    click: () =>
                      this.$MemberLeverItemModal({
                        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);
          }
        }
      ]
    };
  },

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

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

  methods: {
    handleAdd() {
      this.$MemberLeverItemModal();
    },
    async handleDelItem(item) {
      const { success, msg } = await delItem({
        id: item.id
      });
      if (success) {
        this.$success('删除成功！');
        this.$g_emit('member_lever_reload');
      }
    }
  }
};
</script>

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