<template>
  <div class="right-wrap">
    <topNavNew :navpath="navpath"></topNavNew>
    <div class="right-content">
      <div class="right-box">
        <div class="attention-wrap">
          <!-- 常规卡 -->
          <member-card-table
            :tableData="generalCardList"
            :logoImg="logoImg"
            :enterpriseId="cliqueId"
            type="1"
            @add="addGrade"
            @edit="editGrade"></member-card-table>
          <!-- 特殊卡 -->
          <member-card-table
            :tableData="specialCardList"
            :logoImg="logoImg"
            :enterpriseId="cliqueId"
            type="2"
            @add="addGrade"
            @edit="editGrade"></member-card-table>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import topNavNew from 'components/nav/navNew';
import MemberCardTable from './components/MemberCardTable.vue';

import errMsg from '@/common/js/error';
import { getRequest } from '@/api/api';

export default {
  name: 'memberGrade',
  data() {
    return {
      navpath: [
        {
          name: '集团管理',
          path: ''
        },
        {
          name: '会员制度',
          path: ''
        },
        {
          name: '会员卡设置',
          path: '/memberCardSet'
        },
        {
          name: '编辑商户会员等级',
          path: ''
        }
      ],
      generalCardList: [], // 常规卡列表数据
      specialCardList: [], // 特殊卡列表数据
      logoImg: '', // 商户logo
      cliqueId: '', // 商户id
      cliqueName: '', //商户 name
    };
  },
  mounted() {
    const { enterpriseId, name } = this.$route.query;
    this.cliqueId = enterpriseId;
    this.cliqueName = name;
    this.navpath[3].name = '编辑【' + name + '】会员等级';
    sessionStorage.removeItem('enterpriseId');
    this.getList();
  },
  methods: {
    //获取列表
    getList() {
      const para = { enterpriseId: this.cliqueId };
      getRequest('/api-admin/get-enterprise-grade-info', para).then(res => {
        const { errorCode, message, result } = res.data || {};
        if (errorCode != 0) {
          errMsg.errorMsg(message);
          return;
        }
        this.logoImg = result.logoImg;
        // 常规卡
        this.generalCardList = result.generalCardList || [];
        // 特殊卡
        this.specialCardList = result.specialCardList || [];
      })
    },
    // 编辑
    editGrade(row, type) {
      console.log(type);
      this.$router.push({
        path: '/editMemberGrade',
        query: {
          gradeId: row.gradeId,
          enterpriseId: this.cliqueId,
          name: this.cliqueName,
          type
        }
      });
    },
    // 新增等级
    addGrade(type) {
      this.$router.push({
        path: '/addMemberGrade',
        query: {
          enterpriseId: this.cliqueId,
          name: this.cliqueName,
          type
        }
      })
    }
  },
  components: { topNavNew, MemberCardTable },
  beforeRouteLeave(to, from, next) {
    if (to.path == '/editMemberGrade' || to.path == '/addMemberGrade') {
      sessionStorage.setItem('enterpriseId', this.cliqueId);
    }
    next();
  }
};
</script>
<style lang="less" scoped>
.attention-wrap {
  padding: 24px;
  background: #fff;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
</style>
