<template>
  <a-modal
    title="授权VIP"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-card :bordered="false">
      <a-alert message="每次授权VIP均为自动根据周期进行续期" banner />
      <div>
        <a-table
          size="middle"
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :columns="columns"
          :dataSource="loadData"
          :pagination="false"
          :loading="loading"
          :rowKey="record => record.id"
        >
          <span slot="expirevalue" slot-scope="text, record">
            <span>{{ text }}{{ GetClientDataItem('vipexpiretype', record.etype) }}</span>
          </span>
        </a-table>
      </div>
    </a-card>
  </a-modal>
</template>

<script>
import { AllList } from '@/api/modular/main/vip'
import { sysUserOwnVIP, sysUserGrantVIP } from '@/api/modular/system/userManage'
import { GetClientDataItem } from '@/utils/clientdata'

const columns = [
  {
    title: 'VIP名称',
    dataIndex: 'name'
  },
  {
    title: '价格(元)',
    dataIndex: 'price'
  },
  {
    title: '周期',
    dataIndex: 'expirevalue',
    scopedSlots: {
      customRender: 'expirevalue'
    }
  },
  {
    title: '备注',
    dataIndex: 'remark'
  }
]

export default {
  name: 'UserRoleIndex',

  data() {
    return {
      columns,
      loadData: [],
      selectedRowKeys: [], // Check here to configure the default column
      loading: true,
      visible: false,
      confirmLoading: false,
      recordEntity: []
    }
  },
  computed: {
    hasSelected() {
      return this.selectedRowKeys.length > 0
    }
  },
  methods: {
    GetClientDataItem,
    // 初始化方法
    userVIP(record) {
      this.recordEntity = record
      this.visible = true
      // 加载已有数据
      this.sysUserOwnRole()
      // 获取全部列表,无需分页
      AllList().then(res => {
        this.loadData = res.data
      })
    },

    /**
     * 获取用户已有角色
     */
    sysUserOwnRole() {
      this.loading = true
      sysUserOwnVIP({ id: this.recordEntity.id }).then(res => {
        // 选中多选框
        this.selectedRowKeys = res.data
        this.loading = false
      })
    },

    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    handleSubmit() {
      // eslint-disable-next-line no-unused-expressions
      debugger;
      this.confirmLoading = false

      this.selectedRowKeys = this.selectedRowKeys.filter(key => key !== "00000000-0000-0000-0000-000000000001");

      if (this.selectedRowKeys.length > 1) {
        this.$message.error('只能选择一个VIP')
        return
      }
      sysUserGrantVIP({ id: this.recordEntity.id, ids: this.selectedRowKeys })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('授权成功')
            this.confirmLoading = false
            this.$emit('ok', this.recordEntity)
            this.handleCancel()
          } else {
            this.$myerror('授权失败：' + JSON.stringify(res.message))
          }
        })
        .finally(res => {
          this.confirmLoading = false
        })
    },
    handleCancel() {
      this.recordEntity = []
      this.selectedRowKeys = []
      this.visible = false
    }
  }
}
</script>
