<template>
  <el-dialog title="会员卡升级" :visible.sync="show" width="340px" @closed="close">
    等级：
    <el-select style="width:220px" v-model="cardLevel" placeholder="等级选择">
      <el-option v-for="(el, i) in cardLevelList" :key="i" :label="el.value" :value="el.key"></el-option>
    </el-select>
    <p class="minor-font-color mt10 line-height1_5 fz12">*若当前会员卡等级超过该等级，则不会进行会员升级。</p>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" @click="addItem">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getCardLevelList } from '../assets/api.js';
export default {
  name: 'lib-grade',
  props: {
    item: {
      type: Object,
      default() {
        return {};
      }
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      cardLevelList: [],
      cardLevel: ''
    };
  },
  mounted() {
    getCardLevelList({ requestProject: 'marketing', key: 'memberGrade' }).then(res => {
      if (res.errorCode === 0) {
        this.cardLevelList = res.result;
      } else {
        this.$message({ type: 'warning', message: '接口异常' });
      }
    });
  },
  watch: {
    show(val) {
      if (val) {
        this.cardLevel = this.item.relationId || '';
      } else {
        this.cardLevel = '';
      }
    }
  },
  methods: {
    close() {
      this.$emit('update:show', false);
    },
    addItem() {
      if (!this.cardLevel) {
        return this.$message({ type: 'warning', message: '未选择会员卡等级' });
      }
      let title = this.cardLevelList.find(item => item.key === this.cardLevel).value;
      this.$emit('sendItem', { title, relationId: this.cardLevel, comName: 'grade' });
      this.close();
    }
  }
};
</script>
