<template slot-scope="scope">
  <el-popover placement="top" width="160" v-model="model[theType + 'Flag']">
    <el-input-number v-if="model[theType + 'Flag']" class="w150" size="small" controls-position="right" :precision="precision" v-model="model.inputValue" :min="0"></el-input-number>
    <div style="text-align: right; margin:10px 0 0 0;">
      <el-button size="mini" type="text" @click="model[theType + 'Flag'] = false">取消</el-button>
      <el-button type="primary" size="mini" @click="submit">确定</el-button>
    </div>
    <div slot="reference" @click="edit">
      <span>{{ model[theType] }}{{ typeName }}</span> <i class="el-icon-edit cursor-hover"></i>
    </div>
  </el-popover>
</template>

<script>
import { updateStockService, updateIntegralCostService, updateCashCostService } from '@/service/api/mallApi.js';

export default {
  props: {
    model: {
      type: Object,
      default() {
        return {};
      }
    },
    theType: String,
    typeName: String,
    precision: {
      type: Number,
      default: 0
    }
  },
  methods: {
    // 提交更新并刷新列表
    async submit() {
      // try {
      if (this.theType === 'cashCost' && this.model.inputValue > this.model.costValue) {
        this.$tips({ type: 'warning', message: '现金费用不能大于礼品成本' });
        return;
      }
      let res = null;
      if (this.theType === 'virtualStock') {
        // 库存
        res = await updateStockService({ proId: this.model.integralMallProId, stock: this.model.inputValue });
      } else if (this.theType === 'cashCost') {
        // 现金费用
        res = await updateCashCostService({ proId: this.model.integralMallProId, cost: this.model.inputValue });
      } else if (this.theType === 'integralCost') {
        // 积分费用
        res = await updateIntegralCostService({ proId: this.model.integralMallProId, cost: this.model.inputValue });
      }
      if (res.errorCode === 0) {
        if (this.theType === 'virtualStock') {
          this.$tips({ type: 'success', message: '积分商城修改库存时，请同步修改对应卡券的库存' });
        } else {
          this.$tips({ type: 'success', message: '更新成功' });
        }
        this.$emit('refresh');
      } else {
        this.$tips({ type: 'error', message: '更新失败' });
      }
      // } catch (err) {
      //   this.$tips({type:'error',message: '更新失败'});
      // }
    },
    // 编辑操作
    edit() {
      // 这里precision不能更新 所以加上nextTick
      this.$nextTick(_ => {
        this.model[this.theType + 'Flag'] = true;
        this.$set(this.model, 'inputValue', this.model[this.theType]);
      });
    }
  }
};
</script>
