<template>
  <div>
    <BaseDialog
      width="580px"
      :isShow.sync="isShow"
      title="会员卡开通"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="手机号码：" prop="telphone">
          <el-input
            v-model="formData.telphone"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="姓名：" prop="nickname">
          <el-input disabled v-model="formData.nickname"></el-input>
        </el-form-item>
        <el-form-item label="身份证号：" prop="idCard">
          <el-input disabled v-model="formData.idCard"></el-input>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      formData: { telphone: "", nickname: "", idCard: "" },
      rules: {
        telphone: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              var reg_tel =
                /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
              if (reg_tel.test(value)) {
                this.$api.appuserfindByTel({ telphone: value }).then((res) => {
                  this.$set(this.formData, "nickname", res.data?.realname);
                  this.$set(this.formData, "idCard", res.data?.idCard);
                  this.$set(this.formData, "userId", res.data?.userId);
                });
                return callback();
              } else {
                this.$set(this.formData, "nickname", "");
                this.$set(this.formData, "idCard", "");
                this.$set(this.formData, "userId", "");
                return callback(new Error("手机号码格式不正确"));
              }
            },
            // 触发器可以同时用blur和change
            trigger: ["blur"],
          },
        ],
      },
    };
  },
  mounted() {},

  methods: {
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api
            .appuseropenVip({ userId: this.formData.userId })
            .then((res) => {
              this.$message.success("成功");
              this.isShow = false;
              this.$emit("search");
            });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 220px;
  margin-right: 10px;
}
.el-select {
  width: 220px;
  margin-right: 10px;
}
</style>
