<template>
  <div class="split_modal">
    <div style="text-align: right; margin-bottom: 20px">
      <p-button type="primary" icon="bars" @click="addRow" style="margin-right: 15px">新增拆分</p-button>
      <p-button type="primary" icon="delete" @click="delShow" style="margin-right: 15px">删除拆分</p-button>
      <p-button type="primary" icon="check" @click="handleOk" style="margin-right: 15px">确定拆分</p-button>
    </div>
    <p-table-edit-pro
      ref="table2"
      editor-row-table
      :columns="columns2"
      :data-source.sync="data2"
      rowKey="key"
      dragle
      @checkRow="checkRow"
      :row-select-type="1"
      :loading="loading"
      bordered
    >
    </p-table-edit-pro>
  </div>
</template>

<script>
import { columns2 } from '../config/extractMove.js'
export default {
  props: {
    // 拆分数据
    objData: {
      type: Object,
      default: null,
    },
    // 总数量
    totalNum: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      columns2: columns2(this),
      data2: [],
      selectedRowKeys: [], //选中的当前行 key
      loading: false,
    }
  }, //data()
  methods: {
    checkRow(selectedRowKeys, selectedRows) {
      console.log('selectedRowKeys, selectedRows', selectedRowKeys, selectedRows)
      this.selectedRowKeys = [...selectedRowKeys]
    },
    // 拆分
    addRow() {
      this.data2.push({
        ...this.objData,
        key: 'key' + parseInt(Math.random() * 10000),
        oddPieceQuantity: 0, // 零散数0
        caseQuantity: 0, //整件数为0
      })
      console.log('data2', this.data2)
    },
    handleOk() {
      let tempSum = 0
      let caseQuantity = 0
      // 计算所有零散数量的和
      for (let item in this.data2) {
        tempSum += this.data2[item].oddPieceQuantity
      }
      // 计算所有整件数量的和
      for (let item in this.data2) {
        caseQuantity += this.data2[item].caseQuantity
      }
      // 0、整散合一（整件）  1、整散分离（整散）
      switch (this.objData.isCaseOrPiece) {
        case 0:
          let sumNum = tempSum + caseQuantity * this.objData.caseConvertionRate
          if (sumNum != this.totalNum) {
            this.$message.warning(
              `拆分数量不相等！${this.totalNum - sumNum > 0 ? '还少' : '超出'} ${Math.abs(this.totalNum - sumNum)}`,
              5
            )
            return
          }
          let rows0 = this.data2.filter((item) => item.caseQuantity || item.oddPieceQuantity)
          this.$emit('handleOk', rows0)
          break
        case 1: //1、整散分离（整散）
          if (tempSum && caseQuantity * this.objData.caseConvertionRate) {
            this.$message.warning('整散分离, 整件数和散件数只能输入一种！')
            return
          }
          // let sumNum3 = tempSum + caseQuantity * this.objData.caseConvertionRate
          // if( sumNum3 != this.totalNum  ) {
          // 	this.$message.warning(`拆分数量不相等！${this.totalNum - sumNum3 >0?'还少':'超出' } ${ Math.abs(this.totalNum - sumNum3 ) }`,5)
          // 	return ;
          // }
          let rows3 = this.data2.filter((item) => item.caseQuantity || item.oddPieceQuantity)
          this.$emit('handleOk', rows3)
          break
        default:
          this.$message.warning('存储属性为空！')
          break
      }
      //  判拆分数量是否等于总数量
      // if(tempSum != this.totalNum) {
      // 	this.$message.warning(`拆分数量与总数量不相等！${this.totalNum -tempSum>0?'还少':'超出' } ${ Math.abs(this.totalNum -tempSum) }`,5)
      // } else {
      // 	let temp = this.data2.filter(item=> item.oddPieceQuantity>0 )
      // 	this.$emit("handleOk",temp);
      // }
    },
    // 仅前台显示删除，后台不做删除
    delShow() {
      let _this = this
      if (this.selectedRowKeys.length) {
        this.$confirm({
          title: '删除',
          content: '您确定要删除吗?',
          onOk() {
            _this.$refs.table2.removeRow(_this.selectedRowKeys)
            _this.selectedRowKeys.length = 0
          },
          onCancel() {},
        })
      } else {
        this.$message.warning('请选择数据！')
      }
    },
  }, //methods
  mounted() {
    // this.$nextTick(()=>{
    // this.data2.splice(0,this.data2.length);
    this.$refs.table2.createRow(this.objData)
    // })
  },
}
</script>

<style></style>
