<template>
  <div>
    <BaseDialog
      width="580px"
      :isShow.sync="isShow"
      title="会员充值"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="充值金额(元)：" prop="rechargeCardMoney">
          <el-input
            v-model="formData.rechargeCardMoney"
            placeholder="请输入充值金额"
          ></el-input>
        </el-form-item>
        <el-form-item label="积分：" prop="pointsValue">
          <el-input
            disabled
            clearable
            v-model="formData.pointsValue"
            placeholder="积分"
          ></el-input>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    activeData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      formData: {
        rechargeCardMoney: "",
        pointsValue: "",
      },
      rules: {
        rechargeCardMoney: [
          { required: true, message: "请输入充值金额", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              var reg_tel = /^[1-9]*[1-9][0-9]*$/;
              if (reg_tel.test(value)) {
                this.$set(
                  this.formData,
                  "pointsValue",
                  parseInt(value) * this.ruleData.oneExchangeValue
                );
                return callback();
              } else {
                this.$set(this.formData, "pointsValue", "");
                return callback(new Error("充值金额必须大于0,且必须为整数"));
              }
            },
            // 触发器可以同时用blur和change
            trigger: ["blur"],
          },
        ],
        pointsValue: [
          { required: false, message: "请输入积分", trigger: "blur" },
        ],
      },
      ruleData: {},
    };
  },
  mounted() {},

  methods: {
    init() {
      this.$api.mallstoreconfigKeypointsRule().then((res) => {
        this.ruleData = res.data;
      });
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api
            .appuserrechargeBatchVip({
              userIdList: this.activeData.userIdList,
              rechargeCardMoney: this.formData.rechargeCardMoney,
            })
            .then((res) => {
              this.$message.success("充值成功");
              this.isShow = false;
              this.$emit("search", 3);
            });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        if (this.dialogVisible) {
          this.init();
        }
        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>
