<template>
  <div>
    <p-modal
      title="选择库位"
      :visible="visible"
      :dialogStyle="{ top: '40px' }"
      :draggable="true"
      :width="'70%'"
      :destroyOnClose="true"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <div class="box-wrapper">
        <p-table-edit
          ref="mainlist"
          :rowKey="'rowId'"
          :columns="locationColumns"
          :autoLoad="false"
          :data-source.sync="dataList"
          :hasRowSelectCheck="true"
          :rowSelectType="2"
          @selectChange="selectChange"
          :scroll="{ y: 400 }"
        >
        </p-table-edit>
      </div>
    </p-modal>
  </div>
</template>

<script>
import { locationColumns } from '../js/config'

export default {
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    paramRowData: {
      type: Object,
      default: null,
    },
    paramData: {
      type: Array,
      default: null,
    },
    detailData: {
      type: Array,
      default: null,
    },
    newLocation: {
      type: Array,
      default: null,
    },
  },

  model: {
    prop: 'visible',
    event: 'change',
  },

  watch: {
    visible: {
      handler(visible) {
        if (!visible) return
        this.$nextTick(() => {
          this.dataList = []
          this.initLineData()
        })
      },
    },
  },
  data() {
    return {
      locationColumns: locationColumns,
      dataList: [],
      selectedRows: [],
    }
  },

  methods: {
    initLineData() {
      this.dataList = JSON.parse(JSON.stringify(this.paramData))
      var rowData = JSON.parse(JSON.stringify(this.paramRowData))
      var newLocation = JSON.parse(JSON.stringify(this.newLocation))
      var a = []
      // this.dataList.forEach((item, i) => {
      //   if (newLocation.length < 1) {
      //     return
      //   } else {
      //     console.log(item, newLocation, 'item')
      //     newLocation.forEach((tt) => {
      //       if (item.whLocatorId !== tt.whLocatorId) {
      //         a.push(item)
      //         console.log(a, 'aaaaaaaaaaaaa')
      //       }
      //     })
      //     this.dataList = a
      //   }
      // })
    },

    handleOk(e) {
      if (this.selectedRows.length < 1) {
        this.$message.warning('请先选择货位！')
        return
      }
      //明细行
      var rowData = JSON.parse(JSON.stringify(this.paramRowData))
      var num = this.selectedRows
      var split = []
      var newData = rowData
      for (let i = 0; i < this.selectedRows.length; i++) {
        split.push({
          ...newData,
          batchId: num[i].batchId,
          batchNum: num[i].batchNum,
          whLocatorId: num[i].whLocatorId,
          whLocatorName: num[i].whLocatorName,
          pieceQuantity: num[i].availableQuantity,
          pieceQty: num[i].availableQuantity,
          quantity: num[i].quantity,
          storeTemperature: num[i].storeTemperature,
          manufacturerName: num[i].manufacturerName,
          specification: num[i].specification,
          whAreaCategory: num[i].whAreaCategoryCode,
        })
        newData.prePieceQuantity -= num[i].availableQuantity
        //生成id
        split[i].id = Math.floor(Math.random() * 1000)
      }
      //得到拆分的总数
      var newPieceQty = 0
      // debugger
      split.forEach((t) => {
        console.log(t.pieceQuantity,'pieceQuantity');
        newPieceQty += t.pieceQuantity
      })
      let index = rowData._row_count - 1
      console.log(+newPieceQty,'newPieceQty');
      this.detailData.splice(index, 0, ...split)
      this.paramRowData.pieceQuantity = newPieceQty
      this.$emit('change', false)

      // //所选数量必须大于等于明细行数量
      // if (a >= rowData.prePieceQuantity) {
      //   var ab = []
      //   var c1 = rowData
      //   var i = 0
      //   var t = this.selectedRows
      //   while (c1.prePieceQuantity > 0) {
      //     if (c1.prePieceQuantity - t[i].availableQuantity >= 0) {
      //       ab.push({
      //         ...c1,
      //         batchId: t[i].batchId,
      //         batchNum: t[i].batchNum,
      //         whLocatorId: t[i].whLocatorId,
      //         whLocatorName: t[i].whLocatorName,
      //         pieceQuantity: t[i].availableQuantity,
      //         pieceQty: t[i].availableQuantity,
      //         quantity: t[i].quantity,
      //         storeTemperature: t[i].storeTemperature,
      //         manufacturerName: t[i].manufacturerName,
      //         specification: t[i].specification,
      //         whAreaCategory: t[i].whAreaCategoryCode,
      //       })
      //     } else {
      //       ab.push({
      //         ...c1,
      //         batchId: t[i].batchId,
      //         batchNum: t[i].batchNum,
      //         whLocatorId: t[i].whLocatorId,
      //         whLocatorName: t[i].whLocatorName,
      //         pieceQuantity: t[i].availableQuantity,
      //         pieceQty: t[i].availableQuantity,
      //         quantity: t[i].quantity,
      //         storeTemperature: t[i].storeTemperature,
      //         manufacturerName: t[i].manufacturerName,
      //         specification: t[i].specification,
      //         whAreaCategory: t[i].whAreaCategoryCode,
      //         // prePieceQuantity: t[i].availableQuantity,
      //       })
      //     }
      //     c1.prePieceQuantity -= t[i].availableQuantity
      //     //生成id
      //     ab[i].id = Math.floor(Math.random() * 1000)
      //     i++
      //   }
      //   let index = rowData._row_count - 1
      //   this.detailData.splice(index, 0, ...ab)
      //   console.log(ab, this.detailData, 'ababababaab')
      //   this.$emit('change', false)
      //   return
      // } else {
      //   this.$message.warning('当前所选数量总和小于任务总数，请重新选择！')
      // }
    },
    handleCancel(e) {
      this.$emit('change', false)
    },
    selectChange(selectedRowKeys, selectedRows) {
      this.selectedRows = selectedRows
    },
  },
}
</script>

<style scoped>
.box-wrapper {
  margin-bottom: 20px;
  background: #fff;
}
.btn {
  color: #2b80ff;
  margin-right: 10px;
  cursor: pointer;
}
.btn:hover {
  text-decoration: underline;
}
</style>
