<template>
  <el-dialog title="修改数量" :visible.sync="show" width="435px" :before-close="close" append-to-body>
    <div v-loading="loading">
      奖品数量 <el-radio class="ml10" v-model="calculate" :label="true">增加</el-radio>
      <el-radio v-model="calculate" :label="false">减少</el-radio>
      <div class="vl">
        <el-input-number controls-position="right" :min="0" :max="max" v-model="count" class="w200 block-center"></el-input-number>
      </div>
      <div class="fz14 vl" v-if="needCheck && calculate">可用库存：{{ maxStock }}</div>
      <div class="fz14 vl" v-if="!calculate">当前库存：{{ currentCount }}</div>
      <div class="fz14 vl mb10">结果：{{ resNum }}</div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updatePrizeStock } from '@/service/api/gameApi.js';
export default {
  props: {
    currentCount: {
      type: Number,
      require: true
    },
    totalCount: {
      type: Number,
      default: 0
    },
    show: {
      type: Boolean,
      default: false
    },
    prizeReferType: {
      type: Number,
      default: 0
    },
    prizeId: {
      type: String,
      default: ''
    },
    prizeReferId: {
      type: String,
      default: ''
    },
    useStrategy: {
      type: Number,
      default: 1
    },
    gameId: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      count: 0,
      calculate: true,
      loading: false,
      maxStock: 0, // 库存最大值
      needCheck: false // 是否需要进行库存最大值校验
    };
  },
  watch: {
    calculate() {
      this.count = 0;
    }
  },
  computed: {
    resNum() {
      const num = this.calculate ? parseInt(this.totalCount) + parseInt(this.count) : parseInt(this.totalCount) - parseInt(this.count);
      return isNaN(num) ? 0 : num;
    },
    max() {
      if (this.needCheck) {
        return this.calculate ? this.maxStock - this.totalCount : this.currentCount;
      } else {
        return this.calculate ? 1000000 : this.currentCount;
      }
      // return this.needCheck && this.calculate ? this.maxStock - this.totalCount : 1000000;
    }
  },
  methods: {
    // 父组件传入更改是否需要校验库存最大值
    needCheckFn(maxStock) {
      this.needCheck = true;
      this.maxStock = maxStock;
    },
    // checkCalc() {
    //   if (!this.calculate) {
    //     if (this.count > this.totalCount) {
    //       this.count = this.totalCount;
    //       this.$tips({ type: 'warning', message: '输入数量不能大于总数' });
    //     }
    //   }
    // },
    close() {
      this.$emit('update:show', false);
      this.count = 0;
      this.loading = false;
      this.needCheck = false;
      this.maxStock = 0;
    },
    submit() {
      if (!this.calculate) {
        if (this.count > this.totalCount) {
          this.count = this.totalCount;
          this.$tips({ type: 'warning', message: '输入数量不能大于总数' });
          return;
        }
      }

      let params = {
        optType: this.calculate ? 1 : 2, //是	int	1: 增加; 2: 减少
        addCount: this.count, //是	int	增加或者减少数量
        prizeReferType: this.prizeReferType, //	是	int	奖品类型: 积分: 1; 卡券: 2; 礼品: 3
        useStrategy: this.useStrategy, //是	int	首奖特殊策略: 1; 奖项设置: 0
        prizeReferId: this.prizeReferType === 1 ? null : this.prizeReferId, //是	string	卡券, 礼品都存在, 积分传null
        prizeId: this.prizeId,
        gameId: this.gameId
      };
      updatePrizeStock(params).then(res => {
        this.$emit('countRefresh', { calc: this.calculate, num: this.count });
        this.close();
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.vl {
  margin-left: 70px;
  margin-top: 15px;
}
</style>
