<template>
  <el-dialog
    title="选择会员类型"
    :visible.sync="visible"
    append-to-body>
    <div
      class="tag-wrap"
      v-loading="loading">
      <div style="margin-bottom:20px">
        当前会员类型：<b class="name">{{ memberObj&&memberObj.name || '空' }}</b>
      </div>
      <el-tag
        @click="handleClick(item)"
        :class="{active:(memberObj&&memberObj.id)===item.id}"
        v-for="(item) in memberTypeList"
        :key="item.id">
        {{ item.name }}
      </el-tag>

      <el-pagination
        background
        :current-page.sync="page.pageNum"
        @current-change="getMemberList()"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <div
        slot="footer"
        class="dialog-footer">
        <el-button @click="visible = false">
          取 消
        </el-button>
        <el-button
          type="primary"
          @click="handleSubmit">
          确 定
        </el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getMemberType } from '@/api/member'

export default {
  props: {
    value: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      loading: false,
      visible: false,
      memberObj: null,
      total: 0,
      currentMemberType: '',
      page: {
        pageNum: 1,
        pageSize: 10
      },
      memberTypeList: []
    }
  },
  methods: {
    show () {
      this.visible = true
      // 会员临时变量，确定后才提交
      this.memberObj = this.value
      this.getMemberList()
    },
    getMemberList () {
      this.loading = true
      getMemberType(this.page).then(res => {
        this.total = res.total
        this.memberTypeList = res.list
      }).finally(() => {
        this.loading = false
      })
    },
    handleClick (item) {
      this.memberObj = item
    },
    handleSubmit () {
      this.visible = false
      this.$emit('input', JSON.parse(JSON.stringify(this.memberObj)))
    }
  }
}
</script>

<style lang="scss" scoped>
.tag-wrap {
  padding: 20px;
  .name {
    font-size: 16px; color: $blue;
  }
  ::v-deep .el-tag {
    margin: 0 10px 10px 0; cursor: pointer;
    &.active {
      color: #fff;  background: $blue;
    }
  }
}
.dialog-footer{
  text-align: right;
}
</style>
