<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="会员类型" prop="openVipType">
        <el-select v-model="form.openVipType" placeholder="会员类型" clearable size="small">
          <el-option
              v-for="dict in vipTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开通月份" prop="openVipMonthNum">
        <el-input-number v-model='form.openVipMonthNum' :min="1" />
      </el-form-item>
      <el-form-item :label="'用户编号'" prop="usercode">
        <el-autocomplete
            class="inline-input"
            v-model="form.usercode"
            :fetch-suggestions="querySearch"
            placeholder="请输入内容"
            @select="handleSelect"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="手机号" v-if="info.mobile">
        {{ info.mobile }}
      </el-form-item>
      <el-form-item label="昵称" v-if="info.nickname">
        {{ info.nickname }} 【{{ info.usercode }}】
      </el-form-item>
      <el-form-item label="头像" v-if="info.avatar">
        <image-avatar :src="info.avatar"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getUserByUsercode, listUserByUserCode} from "@/api/xq/user";
import { vipTypeList } from '@/constant/statusMap'
import { addUserVip } from '@/api/xq/userVip'

export default {
  components: {
  },
  data () {
    return {
      vipTypeList,
      open: false,
      title: '',
      form:{
        usercode: undefined,
        openVipType: undefined,
        openVipMonthNum: undefined
      },
      info:{
      },
      // 表单校验
      rules: {
        usercode: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        openVipType: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        openVipMonthNum: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init () {
      this.open = true;
      this.title = "新增会员"
      this.info = {};
      this.form.usercode = undefined
    },
    querySearch(querySearch,cb){
      listUserByUserCode(querySearch).then(res => {
        cb(res.data.map((terminal) => {
          return {
            value: terminal,
            name: terminal,
          };
        }))
      })
    },
    handleSelect(item){
      getUserByUsercode(item.value).then(res => {
        this.info = res.data
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          addUserVip(this.form).then(data => {
            this.$modal.msgSuccess("新增会员成功");
            this.buttonLoading = false;
            this.open = false
            this.$emit('refreshDataList')
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>
