<template>
  <el-dialog title="赠送积分" :visible.sync="show" width="420px" :before-close="close">
    <p class="regular-font-color mb20">请输入需要赠送的积分<span class="fz12 gray ml10">* 仅支持给认证会员赠送积分</span></p>
    <el-form :model="form" :rules="rules" ref="form" label-width="0">
      <el-form-item prop="integral">
        <el-input v-model.number="form.integral" maxLength="7" />
      </el-form-item>
    </el-form>
    <p class="minor-font-color mt10 line-height1_5 fz12">赠送积分无自动通知。如有通知需要，请额外添加【文本】、【图片】、【小程序】或【短信】营销事件。</p>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" @click="addItem">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  name: 'lib-integral',
  props: {
    item: {
      type: Object,
      default() {
        return {};
      }
    },
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validInteral = (rule, value, callback) => {
      if (!Number.isInteger(value)) {
        callback(new Error('积分值为数字值'));
      } else if (value <= 0) {
        callback(new Error('积分值最小为1'));
      } else if (value > 1000000) {
        callback(new Error('单次赠送值最大值为1000000'));
      } else {
        callback();
      }
    };
    return {
      ecmMarketingTypeRelationId: '',
      form: {
        integral: '',
        comName: 'integral'
      },
      rules: {
        integral: [{ required: true, message: '请输入积分值', trigger: 'blur' }, { validator: validInteral, trigger: 'blur' }]
      },
      loading: false
    };
  },
  watch: {
    show(val) {
      if (val) {
        this.form.integral = this.item.integralCount;
        this.ecmMarketingTypeRelationId = this.item.ecmMarketingTypeRelationId;
      } else {
        this.form.integral = '';
        this.ecmMarketingTypeRelationId = '';
      }
    }
  },
  methods: {
    close() {
      this.$refs.form.resetFields();
      this.$emit('update:show', false);
    },
    addItem() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$emit('sendItem', { ...this.form, ...{ ecmMarketingTypeRelationId: this.ecmMarketingTypeRelationId } });
          this.close();
        }
      });
    }
  }
};
</script>
