<template>
  <el-dialog title="调整库存" :visible.sync="dialogVisible" width="500px" @close="cancelSubmit()">
    <div v-loading="loading">
      <el-select v-model="stockType" style="width:100px;display:inline-block;margin-right:10px">
        <el-option label="增加" :value="0"></el-option>
        <el-option label="减少" :value="1"></el-option>
      </el-select>
      <el-input-number v-model="stock" precision="0" :min="0" :max="999999"  style="width:300px"></el-input-number>
      <el-input class="mt20" type="textarea" :rows="3" placeholder="请填入调整备注" v-model="textarea" :maxlength="240" show-word-limit></el-input>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelSubmit()">取消</el-button>
      <el-button type="primary" @click="submitProNanme()">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
// import request from '../service/request.js';
import api from '@/api/gift.js';
const { realStockUpdate } = api;
export default {
  props: {
    stockData: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      stockType: 0,
      stock: '',
      textarea: ''
    };
  },
  mounted() {
    this.dialogVisible = true;
    console.log(this.stockData);
  },
  methods: {
    submitProNanme() {
      if(this.loading) return;
      let params = {
        giftId: this.stockData.id,
        updateList: [
          {
            giftSkuId: '',
            stock: this.stock,
            stockType: this.stockType
          }
        ],
        remark: this.textarea
      };
      this.loading = true;
      realStockUpdate(params).then(res =>{
        if(res.code === '0000'){
          this.dialogVisible = false;
          setInterval(() => {
            this.$emit('closeUpdateStock');
          }, 10);
          this.$message.success('修改成功');
        }else{
          this.$message.error(res.message);
        }
      }).finally(() => this.loading = false);
    },
    // 取消修改
    cancelSubmit() {
      this.dialogVisible = false;
      setInterval(() => {
        this.$emit('closeUpdateStock');
      }, 10);
    }
  }
};
</script>
<style scoped></style>
