<template>
  <div>
    <el-dialog title="设置增补库存" 
    width="880px"
    :visible.sync="dialogVisibleStock" 
    :before-close="handleDialogClose">
      <el-table :data="skuList" max-height="500px" v-loading="loading">
        <el-table-column property="mainImg" label="SKU图片" align="center">
          <template slot-scope="scope">
            <img style="width:50px;" :src="scope.row.mainImg" alt="">
          </template>
        </el-table-column>
        <el-table-column property="model" label="SKU型号" align="center"></el-table-column>
        <el-table-column property="stock" label="增补库存" align="center">
          <template slot-scope="scope">
            <el-input type="number" 
            v-model.number="addStockVal" 
            placeholder="请输入增补库存数量"
            oninput="value=value.replace(/[^\d]/g,'')"></el-input>
            <p>库存数量 = 可用库存 + 增补库存</p>
          </template>
        </el-table-column>
        <el-table-column property="stock" label="活动交货期" align="center">
          <template slot-scope="scope">
           <el-radio-group v-model="radio">
                <el-radio :label="0">现货</el-radio>
                <el-radio :label="1">15天</el-radio>
            </el-radio-group>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelForm(false)">取 消</el-button>
        <el-button type="primary" @click="setSkuStock(false)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "addStock",
  data() {
    return {
      dialogVisibleStock:false,
      skuList:[],
      loading:false,
      radio:1,
      addStockVal:''
    };
  },
  props:{
    objProps:{
      type:Object,
      default:()=>{ return {} }
    }
  },
  watch:{
     objProps:{
          deep:true,
          handler: function(value) {
              this.dialogVisibleStock =value.dialogVisibleStock
          }
      }
  },
  mounted(){
    this.dialogVisibleStock = this.objProps.dialogVisibleStock
    this.skuList = this.objProps.gridData
    if(this.skuList && this.skuList.length){
          this.skuList.map(res=>{
            if(res.deliveryTime === 0){
                this.radio=0
            }else{
                this.radio=1
            }
          })
      }
  },
  methods: {
    setSkuStock(val){
      let params = {};
      
      let arr =[]
      if(this.skuList && this.skuList.length){
          this.skuList.map(res=>{
            arr.push({
              skuId:res.skuId,
              stock:this.addStockVal,
              beforeStock: res.stock,
              // warnStock:res.warnStock
            })
            params.spuId = res.spuId,
            params.deliveryTime = this.radio
          })
          params.skuStocks = arr
      }
      this.$axios.post("/products/addStockByAct", params)
      .then(res => {
         let {code,data,msg,message} =res.data;
         if(code ==200){
            this.$message.success(msg || message);
            this.cancelForm(false,'ok')
         }else{
            this.$message.error(msg || message);
         }
      }).catch(res => {});
    },
    cancelForm(val,type) {
      this.objProps.gridData = []
      this.skuList =[]
      this.addStockVal =''
      this.$emit("resData", false,type);
    },
    handleDialogClose(){
      this.cancelForm(false);
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
