<template>
  <el-dialog title="调整库存" :visible.sync="dialogVisible" width="750px" @close="cancelSubmit()">
    <div v-loading="loading">
      <el-input type="textarea" :rows="3" placeholder="请填入调整备注" v-model="textarea" :maxlength="240" show-word-limit></el-input>
      <div class="upload-btn mt20">
        <el-upload class="upload-ele" show-file-list :action="uploadAction" :on-success="uploadFile" with-credentials>
          <el-button><i class="el-icon-upload2" style="margin-right:5px"></i>点击上传</el-button>
        </el-upload>
      </div>
      <div class="uploadText">
        <p class="mt10">1.发放内容内部模板导出，点击<el-button type="text">下载模板</el-button>；</p>
        <p>2.填充空缺内容，导出所有项均为必填项；</p>
        <p>3.请勿导入重复的内容，导入的内容在原来基础上增加库存；</p>
        <p>4.上传完整仅支持.xlsx .xls文件的导入，填充数据不超过“10万”条；</p>
      </div>
    </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,
      // tableData: [],
      tableData: [
        {
          color: '红色',
          size: 'L',
          remainedStock: '10',
          giftSkuId: '1111',
          stock: '10',
          stockType: 1
        },
        {
          color: '红色',
          size: 'XL',
          remainedStock: '20',
          giftSkuId: '2222',
          stock: '20',
          stockType: 1
        },
        {
          color: '红色',
          size: 'M',
          remainedStock: '30',
          giftSkuId: '3333',
          stock: '12',
          stockType: 1
        },
        {
          color: '黑色',
          size: 'L',
          remainedStock: '10',
          giftSkuId: '4444',
          stock: '13',
          stockType: 1
        },
        {
          color: '黑色',
          size: 'M',
          remainedStock: '20',
          giftSkuId: '55555',
          stock: '14',
          stockType: 1
        }
      ],
      spanArr: [], // 用于存放每一行记录的合并数
      // costChangeObj: [], // 用于存放批量修改后的数据
      banthStock: '',
      stockType: 0,
      updateList: [],
      textarea: '',
      // uploadAction: window.location.origin + '/api-marketing/upload/upload-image?requestProject=marketing',
      uploadAction: 'https://four.gicdev.com/api-marketing/upload/upload-image?requestProject=marketing',
    };
  },
  mounted() {
    this.dialogVisible = true;
    this.getData();
    this.getSpanArr(this.tableData);
  },
  methods: {
    getData(){
      console.log('获取成本数据');
    },
    submitProNanme() {
      if(this.loading) return;
      let params = {
        giftId: this.stockData.id,
        updateList: this.updateList,
        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);
    },
    uploadFile(file){
      console.log(file);
    },
    // 合并单元格，行合并
    getSpanArr(data) {
      // data就是我们从后台拿到的数据
      for (let i = 0; i < data.length; i++) {
        if (i === 0) {
          this.spanArr.push(1);
          this.pos = 0;
        } else {
          // 判断当前元素与上一个元素是否相同
          if (data[i].color === data[i - 1].color) {
            this.spanArr[this.pos] += 1;
            this.spanArr.push(0);
          } else {
            this.spanArr.push(1);
            this.pos = i;
          }
        }
        // console.log(this.spanArr);
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 0 || columnIndex === 1) {
      if (columnIndex === 0) {
        const _row = this.spanArr[rowIndex];
        const _col = _row > 0 ? 1 : 0;
        // console.log(`rowspan:${_row} colspan:${_col}`);
        return {
          // [0,0] 表示这一行不显示， [2,1]表示行的合并数
          rowspan: _row,
          colspan: _col
        };
      }
    },
    // 批量修改成本
    handleStock(){
      // for (let item in this.costChangeObj) {
      //   this.costChangeObj[item].costPrice = this.banthCost;
      //   // this.costChangeObj[item].stockType = this.stockType;
      // }
      this.updateList = [];
      // if(this.stockType === 1){
      //   this.tableData.forEach(item => {
      //     // item.stock = Number(item.stock) + Number(this.banthStock);
      //     item.stock = this.banthStock;
      //     item.stockType = this.stockType;
      //     this.updateList.push({ giftSkuId: item.giftSkuId, stock: item.stock, stockType: item.stockType });
      //   });
      // }else{
      //   this.tableData.forEach(item => {
      //     // item.stock = Number(item.stock) - Number(this.banthStock);
      //     item.stock = this.banthStock;
      //     item.stockType = this.stockType;
      //     this.updateList.push({ giftSkuId: item.giftSkuId, stock: item.stock, stockType: item.stockType });
      //   });
      // }
      this.tableData.forEach(item => {
        item.stock = this.banthStock;
        item.stockType = this.stockType;
        this.updateList.push({ giftSkuId: item.giftSkuId, stock: item.stock, stockType: item.stockType });
      });
      console.log(this.updateList);
    },
    changeSingleStock(id, stock, stockType){
      if(!stock){
        this.$message.error('请完善库存');
      }
      if(this.updateList.length > 0){
        let iii;
        let result = this.updateList.some((item, index) =>{
          if(item.giftSkuId == id){
            iii = index;
            return true;
          }
        });
        if(result){ // 如果存在
          this.updateList[iii].stock = stock;
        }else{
          this.updateList.push({ giftSkuId: id, stock: stock, stockType: stockType });
        }
      }else{
        this.updateList.push({ giftSkuId: id, stock: stock, stockType: stockType });
      }
      console.log(this.updateList);
    }
  }
};
</script>
<style scoped>
.upload-btn {
  height: 40px;
  border: 1px dashed rgba(192, 196, 204, 1);
  display: inline-block;
  font-size: 14px;
  color: #c0c4cc;
  cursor: pointer;
  margin-right: 10px;
  width: 250px;
  border-radius: 4px;
}
.upload-btn .el-icon-upload2 {
  font-size: 18px;
  color: #c0c4cc;
}
.upload-btn:hover {
  border-color: #1890ff;
}
.upload-ele .el-button {
  border: none;
  color: #c0c4cc;
  margin-left: 60px;
}
.upload-ele .el-button--small {
  padding: 0;
}
.uploadText p{
  line-height: 26px;
}
</style>
