<template>
  <el-dialog :visible.sync="dialogVisible" width="600px" @close="cancel()">
    <div v-loading="loading">
      <el-table :data="stockTableData">
        <el-table-column label="规格名称" v-if="giftPropValues">
          <template slot-scope="{ row }">{{ row.skuName }}</template>
        </el-table-column>
        <el-table-column label="总库存">
          <template slot-scope="{ row }">{{ row.allStock }}</template>
        </el-table-column>
        <el-table-column label="常兑库存">
          <template slot-scope="{ row }">{{ row.stock }}</template>
        </el-table-column>
        <el-table-column label="秒杀活动">
          <template slot-scope="{ row }">{{ row.activityStock }}</template>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script>
// import request from '../../../service/request.js';
// import qs from 'qs';
import request from '../service/request.js';
export default {
  props: {
    integralMallProId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      id: '',
      stockTableData: [],
      loading: false,
      giftPropValues: ''
    };
  },
  created() {
    // console.log(this.ladderPriceData);
    this.dialogVisible = true;
    this.getInfo(this.integralMallProId);
  },
  methods: {
    getInfo(integralMallProId) {
      this.loading = true;
      request.post(`/api-integral-mall/view-distribution?giftProId=${integralMallProId}`).then(res => {
        if (res.data.errorCode === 0) {
          this.loading = false;
          this.stockTableData = res.data.result;
          let totalAllStock = 0;
          let totalStock = 0;
          let totalActivityStock = 0;
          this.stockTableData.forEach(item => {
            item.skuName = '';
            let skuName = [];
            if (item.giftPropValues) {
              let skuNameValues = JSON.parse(item.giftPropValues);
              skuNameValues.forEach(it => {
                skuName.push(it.valueName);
              });
            }
            item.skuName = skuName.join(',');
            totalAllStock += item.allStock;
            totalStock += item.stock;
            totalActivityStock += item.activityStock;
          });
          if (this.stockTableData.length > 1) {
            this.stockTableData.unshift({
              allStock: totalAllStock,
              stock: totalStock,
              activityStock: totalActivityStock
            });
          }
          if (this.stockTableData.length > 1) {
            this.giftPropValues = this.stockTableData[1].giftPropValues;
          } else {
            this.giftPropValues = '';
          }
        } else {
          this.$message.error(res.data.message);
          this.loading = false;
        }
      });
    },
    cancel() {
      this.dialogVisible = false;
      setInterval(() => {
        this.$emit('closeStockDistirbute');
      }, 10);
    }
  }
};
</script>
<style scoped>
.w-100 {
  width: 100px;
}
</style>
