/**
* @Description: 入库登账-标准物料单位
* @author wx
* @date 2022/12/13
*/
<template>
  <div class="warehousingAccounting-com">
    <dee-up-table
      table-height="auto"
      :index-row="indexRow"
      :columns="tableColums"
      :data="tableData"
      selection-row
      @selection-change="selectionChange"
    >
      <div slot="header" class="table-title-wrap">
        <dee-tools :tools="tools" mode="normal" />
        <div>
          <div>
            <span>申请:{{ row.inComeQuantity }}{{ row.dxStResUnitName }}</span>
            <span class="countLabel">已存:<span>{{ totalNums ? totalNums : row.recordQuantity }}</span></span>
          </div>
        </div>
      </div>
    </dee-up-table>
    <span style="display:flex;justify-content: center;">
      <el-button
        type="primary"
        class="searchBtn"
        @click="handleSubmit"
      >确定</el-button>

      <el-button
        class="searchBtn"
        @click="handleClose"
      >取消</el-button>
    </span>
  </div>
</template>

<script>
import view from './view.js'
export default {
  componentName: '入库登账-标准物料单位',
  name: 'WarehousingAccounting',
  // import引入的组件需要注入到对象中才能使用
  components: { },
  mixins: [view],
  props: {
    row: {
      type: Object,
      default: null
    }
  },
  data() {
    // 这里存放数据
    return {
      dialogVisible: false,
      tools: [{
        name: '新增',
        icon: '/icons/c-add.png',
        handler: {
          click: () => {
            this.handleAdd()
          }
        }
      }],
      indexRow: {
        title: '序号',
        align: 'center',
        width: '70'
      },
      tableData: [],
      dxStAreaOptions: [],
      totalNums: 0,
      selectionRow: []
    }
  },
  // 监听属性 类似于data概念
  computed: {

  },
  // 监控data中的数据变化
  watch: {

  },
  // 生命周期 - 创建完成(可以访问当前this实例)
  created() {
  },
  // 生命周期 - 挂载完成(可以访问DOM元素)
  mounted() {

  },
  activated() {
  },
  // 方法集合
  methods: {
    selectionChange(val) {
      this.selectionRow = val
    },
    handleAdd() {
      console.log('this.row', this.row)
      const obj = {
        show: true,
        afdId: this.row.afdId,
        appDetailId: this.row.afdId,
        dxResInstId: this.row.dxResInstId,
        batchNumber: this.row.batchNumber,
        // 原始单位code
        dxStResUnitCode: this.row.dxStResUnitCode,
        dxStResUnitName: this.row.dxStResUnitName,
        dxStHouseId: this.row.dxStHouseId,
        dxStHouseName: this.row.dxStHouseName,
        dxStAreaId: '',
        dxStAreaName: '',
        dxStLocationId: '',
        dxStLocationName: '',
        // dxStQuantity: '',
        dxRealQuantity: '',
        dxStLocationOptions: []
      }
      this.tableData.unshift(obj)
      // 查库区
      if (this.row.dxStAreaOptions.length) {
        this.dxStAreaOptions = this.row.dxStAreaOptions
      } else {
        this.getDxStAreaOptions(this.row.dxStInComeAF.dxStHouse.id)
      }
    },
    findEditIndex(index, row) {
      const findIndex = this.tableData.findIndex(r => r.id && r.id === row.id)
      const deleteArr = this.tableData.filter(r => r.operator === 'REMOVE')
      return findIndex > -1 ? findIndex : index + deleteArr.length
    },
    handleSubmit() {
      if (!this.selectionRow.length) {
        this.$utils.showMessageWarning('请先选择需要添加的数据!')
        return
      }
      if (this.selectionRow.length && this.selectionRow.some(a =>
        a.dxStHouseId === '' || a.dxStAreaId === '' || a.count === '' || a.dxRealQuantity === '' ||
        (a.dxStLocationOptions.length && a.dxStLocationId === '')
      )) {
        this.$utils.showMessageWarning('请完善页面必填信息!')
        return
      }

      // 相同库位的生产单位累加要小于等于同库位的课内可存数量的生产单位
      // const dxStLocationName = this.checkDxStQuantity()
      // if (dxStLocationName) {
      //   this.$utils.showMessageWarning(dxStLocationName + '库位超过可入库数量!')
      //   return
      // }

      if (this.totalNums > this.row.inComeQuantity) {
        this.$utils.showMessageError('超出待入库数量!')
        return
      }
      this.$emit('handleAdd', this.selectionRow)
      this.handleClose()
    },
    // 校验相同库位的申请入库数量的生产单位累加要小于等于同库位的库内可存数量的生产单位
    checkDxStQuantity() {
      const arr = []
      this.selectionRow.forEach(x => {
        if (x.dxStLocationId) {
          const findDxStLocationIdIndex = arr.findIndex(el => el.dxStLocationId === x.dxStLocationId)
          if (findDxStLocationIdIndex > -1) {
            arr[findDxStLocationIdIndex]['totalInCount'] = Number(arr[findDxStLocationIdIndex]['totalInCount']) + Number(x.dxRealQuantity)
          } else {
            arr.push({
              dxStLocationName: x.dxStLocationName,
              dxStLocationId: x.dxStLocationId,
              dxStQuantity: x.dxStQuantity,
              totalInCount: x.dxRealQuantity
            })
          }
        }
      })
      let dxStLocationName = ''
      arr.forEach(y => {
        if (y.totalInCount > y.dxStQuantity) {
          dxStLocationName = y.dxStLocationName
        }
      })
      return dxStLocationName
    },
    handleClose() {
      this.$emit('handleClose')
    }
  }
}
</script>
<style lang='scss'>
.warehousingAccounting-com{
  height: calc(100% - 46px);
  .table-title-wrap{
    width: 100%;
    display: flex;
    justify-content: space-between;
  }
  .countLabel{
    margin-left: 10px;
  }
  .btn-wrap{
    display: flex;
    justify-content: center;
    margin-top:20px;
  }
  .tableCellFormCheck-com{
    .el-form-item--small.el-form-item{
      margin-bottom:0;
    }
  }
}
</style>