<template>
  <div style="padding: 10px;background-color:#F2F3F5">
    <div class="integralPage">
      <div style="height:58px;line-height:58px;">
        <div style="color:#0006"> <span>积分管理</span>   <span style="padding:0 5px;">></span>  <span style="color:#000000e6">积分配置</span></div>
      </div>
      <div class="configuration">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="240px" class="demo-ruleForm">
          <el-form-item label="积分" prop="switch">
            <el-radio-group v-model="ruleForm.switch" @change="updateConfig('credit_switch', ruleForm.switch)">
              <el-radio label="1">开启</el-radio>
              <el-radio label="0">关闭</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="下单后每1元赠送多少积分给客户" prop="proportion">
            <el-input v-model.number="ruleForm.proportion" type="number" @blur="updateConfig('credit_order_rate', ruleForm.proportion)" />
          </el-form-item>
          <el-form-item label="满足多少元可以抵扣积分" prop="deduction">
            <el-input v-model="ruleForm.deduction" @blur="updateConfig('credit_order_amount_threshold', ruleForm.deduction)" />
          </el-form-item>
          <el-form-item label="积分抵扣金额比例" prop="priceNum" class="priceNum">
            <el-input v-model="ruleForm.priceNum" @blur="updateConfig('credit_exchange_rate', ruleForm.priceNum)" />
            <span>1积分可抵扣多少额度</span>
          </el-form-item>
          <el-form-item label="每笔订单最多抵扣多少积分" prop="priceNum">
            <el-input v-model="ruleForm.integralNum" @blur="updateConfig('credit_deduct_limit', ruleForm.integralNum)" />
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { dictGetChilds, dictUpdate } from '@/api/setup'
export default {
  data () {
    // 积分比例校验规则
    const checkIntegralProportion = (rule, value, callback) => {
      const reg = /^[0-9]*[1-9][0-9]*$/
      if (!value || (value > 10000 || value < 1) || !reg.test(value)) {
        return callback(new Error('请输入1-10000之间的整数'))
      } else {
        callback()
      }
    }
    // 满多少抵扣
    const checkFullMinus = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入满多少可以抵扣！'))
      } else if (!Number(value)) {
        return callback(new Error('请输入正确的数值！'))
      } else {
        callback()
      }
    }
    // 积分支付比例
    const checkProportion = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入积分支付金额比例！'))
      } else if (!Number(value)) {
        return callback(new Error('请输入正确的数值！'))
      } else {
        callback()
      }
    }
    // 最高使用积分
    const checkHighestUsed = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('请输入每笔订单最多抵扣多少积分！'))
      } else if (!Number(value)) {
        return callback(new Error('请输入正确的数值！'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        proportion: '',
        deduction: '',
        switch: '',
        priceNum: '',
        integralNum: ''
      },
      addForm: {
        createTime: '',
        dictDescribe: '',
        dictId: '',
        dictName: '',
        dictPid: 1900,
        updateTime: ''
      },
      oldData: {
        proportion: '',
        deduction: '',
        switch: '',
        priceNum: '',
        integralNum: ''
      },
      sonInline: {
        dictPid: '1900',
        page: 1,
        pageSize: 10
      },
      dictList: [],
      rules: {
        proportion: [
          { required: true, validator: checkIntegralProportion, trigger: 'blur' },
        ],
        deduction: [
          { required: true, validator: checkFullMinus, trigger: 'blur' },
        ],
        priceNum: [
          { required: true, validator: checkProportion, trigger: 'blur' },
        ],
        integralNum: [
          { required: true, validator: checkHighestUsed, trigger: 'blur' },
        ],
        switch: [
          { required: true, message: '请选择积分是否开关', trigger: 'change' }
        ]
      }
    }
  },
  created () {
  },
  mounted () {
    this.getInfo()
  },
  methods: {
    updateConfig (dictName, value) {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (value !== '') {
            for (let i = 0; i < this.dictList.length; i++) {
              if (this.dictList[i].dictName === dictName) {
                this.addForm.dictId = this.dictList[i].dictId
                break
              }
            }
            if (dictName === 'credit_order_rate') {
              if (this.ruleForm.proportion !== this.oldData.proportion) {
                this.changeFn(dictName, value)
              }
            } else if (dictName === 'credit_order_amount_threshold') {
              if (this.ruleForm.deduction !== this.oldData.deduction) {
                this.changeFn(dictName, value)
              }
            } else if (dictName === 'credit_switch') {
              this.changeFn(dictName, value)
            } else if (dictName === 'credit_exchange_rate') {
              if (this.ruleForm.priceNum !== this.oldData.priceNum) {
                this.changeFn(dictName, value)
              }
            } else if (dictName === 'credit_deduct_limit') {
              if (this.ruleForm.integralNum !== this.oldData.integralNum) {
                this.changeFn(dictName, value)
              }
            }
          }
        }
      })
    },
    changeFn (dictName, value) {
      this.addForm.dictName = dictName
      this.addForm.dictDescribe = value
      dictUpdate(this.addForm).then(res => {
        console.log(res)
        if (res.code === '') {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getInfo()
        }
      })
    },
    // 初始化查询所有数据
    getInfo () {
      dictGetChilds(this.sonInline).then(res => {
        this.dictList = res.data.list
        this.dictList.forEach((item) => {
          if (item.dictName === 'credit_switch') {
            this.ruleForm.switch = item.dictDescribe
            this.oldData.switch = item.dictDescribe
          }
          if (item.dictName === 'credit_order_amount_threshold') {
            this.ruleForm.deduction = item.dictDescribe
            this.oldData.deduction = item.dictDescribe
          }
          if (item.dictName === 'credit_order_rate') {
            this.ruleForm.proportion = item.dictDescribe
            this.oldData.proportion = item.dictDescribe
          }
          if (item.dictName === 'credit_exchange_rate') {
            this.ruleForm.priceNum = item.dictDescribe
            this.oldData.priceNum = item.dictDescribe
          }
          if (item.dictName === 'credit_deduct_limit') {
            this.ruleForm.integralNum = item.dictDescribe
            this.oldData.integralNum = item.dictDescribe
          }
        })
      })
    },
  },
}
</script>

<style lang="scss" scpoed>
.integralPage {
padding: 0  20px 20px 20px;
  min-height: calc(100vh - 50px - 20px);
  background-color: #Fff;
  .configuration {
    width: 600px;
  }
  .integralBox {
    span {
      color: #999999;
      font-size: 14px;
    }
  }
}
</style>
