<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '200px' : '120px'">
          <el-form-item label="积分规则编码" prop="ruleCode">
          <el-input v-model="dataForm.ruleCode" maxlength="10" placeholder="请输入积分规则编码" show-word-limit style="width: 740px;"></el-input>
      </el-form-item>
          <el-form-item label="动作" prop="behaviorCode">
          <el-select v-model="dataForm.behaviorCode" placeholder="请选择动作" filterable style="width: 740px;">
            <el-option
              v-for="item in optionBehaviorCode"
              :key="item.behaviorCode"
              :label="item.behaviorDesc"
              :value="item.behaviorCode">
            </el-option>
          </el-select>
      </el-form-item>
          <el-form-item label="积分规则描述" prop="ruleDesc">
          <el-input v-model="dataForm.ruleDesc" maxlength="80" type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入积分规则描述, 80字以内" style="width: 740px;"></el-input>
      </el-form-item>
          <el-form-item label="规则操作类型" prop="operationType">
          <el-select v-model="dataForm.operationType" placeholder="请选择规则操作类型" style="width: 740px;">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
          <el-form-item label="积分规则值" prop="points">
          <el-input-number v-model="dataForm.points" :min="0" label="积分规则值"></el-input-number>
      </el-form-item>
          <el-form-item label="上线统计指标" prop="limitType">
          <el-select v-model="dataForm.limitType" placeholder="请选择规则操作类型" style="width: 740px;">
            <el-option
              v-for="item in limitTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
          <el-form-item label="积分上限值" prop="upperLimitVal">
          <el-input-number v-model="dataForm.upperLimitVal" :min="0" label="积分上限值"></el-input-number>
      </el-form-item>
          <el-form-item label="上限次数指标" prop="limitNum">
          <el-input-number v-model="dataForm.limitNum" :min="0" label="上限次数指标"></el-input-number>
      </el-form-item>
          <el-form-item label="启用标识" prop="enableFlag">
          <el-select v-model="dataForm.enableFlag" placeholder="启用标识" style="width: 740px;">
            <el-option
              v-for="item in enableFlagOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
      </el-form-item>
          <el-form-item label="附加值" prop="addedVal">
          <el-input v-model="dataForm.addedVal"  maxlength="2000"  type="textarea" :autosize="{ minRows: 4, maxRows: 4}" placeholder="请输入附加值, 2000字以内" style="width: 740px;"></el-input>
      </el-form-item>
          <el-form-item label="备注" prop="remark">
          <el-input v-model="dataForm.remark"  maxlength="100"  type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入备注, 100字以内" style="width: 740px;"></el-input>
        </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        ruleCode: '',
        behaviorCode: '',
        ruleDesc: '',
        operationType: '1',
        points: '',
        limitType: '2',
        limitNum: '',
        upperLimitVal: '',
        enableFlag: '1',
        addedVal: '',
        remark: '',
        revision: '',
        delFlag: '',
        createdBy: '',
        createdTime: '',
        updatedBy: '',
        updatedTime: ''
      },
      options: [{
        value: '0',
        label: '减积分'
      }, {
        value: '1',
        label: '加积分'
      }],
      limitTypeOptions: [{
        value: '0',
        label: '分钟'
      }, {
        value: '1',
        label: '小时'
      }, {
        value: '2',
        label: '日'
      }, {
        value: '3',
        label: '月'
      }, {
        value: '4',
        label: '年'
      }],
      enableFlagOptions: [{
        value: '0',
        label: '否'
      }, {
        value: '1',
        label: '是'
      }],
      optionBehaviorCode: []
    }
  },
  computed: {
    dataRule () {
      return {
        ruleCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        behaviorCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ruleDesc: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        operationType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        points: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        limitType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        upperLimitVal: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        limitNum: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        enableFlag: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
      // 获取动作信息
      this.getBehaviorDesc()
    },
    // 获取动作信息
    getBehaviorDesc () {
      this.$http
        .get(`/points/pointsbehavior/getBehaviorDesc`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.optionBehaviorCode = res.data
        })
        .catch(() => {})
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/points/pointsrule/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/points/pointsrule/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
