<template>
  <el-dialog title="赠送积分" :visible.sync="show" width="420px" @closed="close" :close-on-click-modal="false">
    <el-form :model="form" :rules="rules" ref="form" label-width="0">
      <template v-if="form.integralType === 1">
        <p class="regular-font-color mb20">请输入需要赠送的积分<span class="fz12 gray ml10">* 仅支持给认证会员赠送积分</span></p>
        <el-form-item prop="integralCount">
          <el-input v-model.number="form.integralCount" maxLength="7" />
        </el-form-item>
      </template>
    </el-form>
    <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
    },
    integralMultiple: {
      // 积分 是否需要倍数（场景：消费触发）
      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: '',
      old: {}, // 旧数据
      form: {
        integralCount: '',
        comName: 'integral',
        integralType: 1, // 1固定积分  2订单金额倍数
        multipleNum: undefined // 倍数值
      },
      rules: {
        integralCount: [{ required: true, message: '请输入积分值', trigger: 'blur' }, { validator: validInteral, trigger: 'blur' }], // eslint-disable-line
        multipleNum: [{ required: true, message: '请输入倍数', trigger: 'blur' }] // eslint-disable-line
      },
      loading: false
    };
  },
  watch: {
    reset() {
      this.$refs.form.resetFields();
    },
    show(val) {
      if (val) {
        this.old = { ...this.item };
        this.form.integralCount = this.item.integralCount;
        this.form.multipleNum = this.item.multipleNum || undefined;
        this.form.integralType = this.item.integralType || 1;
        this.ecmMarketingTypeRelationId = this.item.ecmMarketingTypeRelationId;
      } else {
        this.old = {};
        this.form.integralCount = '';
        this.form.multipleNum = undefined;
        this.form.integralType = 1;
        this.ecmMarketingTypeRelationId = '';
      }
    }
  },
  methods: {
    close() {
      this.$refs.form.resetFields();
      this.$emit('update:show', false);
    },
    addItem() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.integralMultiple) {
            this.integralType = this.integralType || 1; // 设置默认1 防止''
          }
          this.$emit('sendItem', { ...this.form, ...{ ecmMarketingTypeRelationId: this.ecmMarketingTypeRelationId } });
          this.close();
        }
      });
    }
  }
};
</script>
