<template>
  <el-dialog title="商品链接" :visible.sync="dialogVisible" width="800px" @close="cancel()">
    <div class="dialog-body">
      <el-table :data="goodsSkuList" :span-method="objectSpanMethod" border>
        <el-table-column width="140px" v-for="(i, k) in skuCols" :label="i" :key="k" :class-name="k == skuCols.length - 1 ? 'pad-l-20' : 'right-border'">
          <template slot-scope="scope">{{ scope.row.activitySkuList[k].valueName }}</template>
        </el-table-column>
        <el-table-column label="当前商品库存" width="130px">
          <template slot-scope="scope">
            {{ scope.row.commonStock }}
          </template>
        </el-table-column>
        <el-table-column label="活动库存">
          <template slot-scope="scope">
            <el-select v-model="scope.row.stockType" style="width:100px">
              <el-option label="增加" :value="1"></el-option>
              <!-- <el-option label="减少" :value="0"></el-option> -->
            </el-select>
            <el-input-number v-model="scope.row.stageStock" style="margin-left:-5px;width:140px" controls-position="right" @blur="changeSingleStock(scope.row.integralMallProId, scope.row.stageStock, scope.row.stockType)" precision="0" :min="0" :max="scope.row.commonStock"></el-input-number>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px;text-align:right">
        <el-button type="default" @click="cancel">取消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
// import request from '../service/request';
import qs from 'qs';
import request from '../service/request.js';
import copy from '../utils/clone.js';
export default {
  props: {
    skuStockData: {
      type: Object
    }
  },
  data() {
    return {
      dialogVisible: false,
      skuCols: [],
      goodsSkuList: [],
      activityProId: ''
    };
  },
  created() {
    this.dialogVisible = true;
    this.handelSkuData(this.skuStockData);
    this.goodsSkuList = copy(this.skuStockData.children);
    this.handleGoodsSkuList(this.goodsSkuList);
  },
  methods: {
    handelSkuData(value) {
      // let arr = [];
      this.skuCols = [];
      /** ****组合sku的数据*****/
      for (let item of this.skuStockData.children[0].activitySkuList) {
        this.skuCols.push(item.proName);
      }
      console.log(this.skuCols);
    },
    handleGoodsSkuList(value) {
      value.forEach(item => {
        // item.stockType = 1;
        // item.stageStock = '';
        this.$set(item, 'stockType', 1);
        this.$set(item, 'stageStock', 0);
      });
    },
    changeSingleStock(id, stageStock, stockType) {
      console.log(id, stageStock, stockType);
    },
    submit() {
      let list = [];
      this.goodsSkuList.forEach(item => {
        list.push({
          activityProId: item.activityProId,
          stockChange: item.stageStock,
          changeType: 1
        });
      });
      let params = {
        data: JSON.stringify(list)
      };
      request.post('/api-integral-mall/update-activity-stock', qs.stringify(params)).then(res => {
        if (res.data.errorCode === 0) {
          this.$message.success('库存修改成功');
          this.dialogVisible = false;
          setInterval(() => {
            this.$emit('closeSkuStock');
          }, 10);
        } else if (res.data.errorCode === 2500) {
          // this.goodsSkuList
          let errorMessage = res.data.result;
          errorMessage.forEach(item => {
            if (item.isSku) {
              this.goodsSkuList.forEach(itt1 => {
                if (itt1.integralMallProId === item.rowId) {
                  itt1.commonStock = item.currValue;
                  console.log(item.currValue);
                  itt1.stageStock = '';
                }
              });
            }
          });
          this.$message.error('商品sku库存不足，现已更新库存数据，请重新修改活动库存');
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    combine(arr) {
      arr.reverse();
      let r = [];

      (function f(t, a, n) {
        if (n == 0) return r.push(t);
        for (let i = 0; i < a[n - 1].length; i++) {
          f(t.concat(a[n - 1][i]), a, n - 1);
        }
      })([], arr, arr.length);
      // var t = r;
      return r; // r是排列组合后的数据与
    },
    cancel() {
      this.dialogVisible = false;
      setInterval(() => {
        this.$emit('closeSkuStock');
      }, 10);
    }
  }
};
</script>
<style scoped>
.wechat-img-box {
  margin: 0 auto;
  text-align: center;
}
.wechat-img {
  width: 200px;
  height: 200px;
}
.wechat-force {
  margin: 30px auto 50px;
  text-align: center;
}
/* .force-button {
  background: #1890ff;
  color: #fff;
  border-color: #1890ff;
  font-size: 14px;
  line-height: 14px;
  padding: 8px 15px;
} */
</style>
