<template>
  <div class="inboundDetails mt20">
    <div class="content-detail">
      <header-title title="基本信息" size="16px"></header-title>
      <div class="mt20">
        <el-form label-width="110px">
          <el-col :span="8">
            <el-form-item label="物品类型">
              <!-- <span>{{ codeList[infoData.extInfo.assetType] }}</span> -->
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="入库类型">
              <span>销售退货入库</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="退货订单">
              <span>{{ infoData.clientName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="送货人">
              <span>{{ infoData.deliveryName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="送货人联系方式">
              <span>{{ infoData.deliveryPhone }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="备注">
              <span>{{ infoData.remark }}</span>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
    </div>
    <div class="content-detail mt20">
      <header-title title="物品明细" size="16px"></header-title>
      <div class="mt20">
        <div class="mt10">
          <el-table
            v-if="showTable"
            ref="multipleTable"
            :data="warehousingMaterialList"
            tooltip-effect="dark"
            style="width: 100%"
            stripe
            :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
          >
            <el-table-column
              label="序号"
              type="index"
              width="50"
              align="center"
            >
            </el-table-column>
            <el-table-column
              label="编码"
              prop="categoryCode"
              align="center"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="名称"
              align="center"
              prop="name"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="型号"
              align="center"
              prop="modelType"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="规格"
              align="center"
              prop="specification"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column
              label="牌号"
              align="center"
              prop="brandNum"
              :show-overflow-tooltip="true"
            ></el-table-column>
            <el-table-column label="批次号" prop="batchNo"></el-table-column>
            <!-- <el-table-column
              label="最小包装单元"
              align="center"
              prop="minPackingCount"
              width="100"
            >
              <template slot-scope="{ row, $index }">
                {{ row.measuringUnit }} /{{ row.minPackUnit }}
              </template>
            </el-table-column>
            <el-table-column
              label="包装数量"
              prop="outInNum"
              width="150"
              align="center"
            >
              <template slot-scope="{ row, $index }">
                <template> {{ row.outInNum }} {{ row.minPackUnit }} </template>
              </template>
            </el-table-column> -->
            <el-table-column label="计量数量" prop="counts">
              <template slot-scope="{ row, $index }">
                {{ row.count }}
              </template>
            </el-table-column>
            <el-table-column
              label="计量单位"
              prop="measuringUnit"
              align="center"
            >
            </el-table-column>
            <el-table-column
              label="重量"
              align="center"
              prop="weight"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column
              label="重量单位"
              align="center"
              prop="weightUnit"
              :show-overflow-tooltip="true"
            >
            </el-table-column>
            <el-table-column
              label="单价"
              prop="univalence"
              width="150"
              align="center"
            >
            </el-table-column>
            <el-table-column
              label="金额"
              align="center"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="{ row, $index }">
                {{
                  calcSumTotal(
                    row.outInNum,
                    row.minPackingCount,
                    row.univalence
                  )
                }}
              </template>
            </el-table-column>
            <el-table-column
              label="货位"
              prop="houseList"
              :show-overflow-tooltip="true"
              width="250"
              required
            >
              <template slot-scope="{ row, $index }">
                <div v-if="isEdit" @click="handleWareHouse(row, $index)">
                  <div style="color: #ff4949" v-if="row.houseList.length === 0">
                    ---（必填）
                  </div>
                  <div v-else>
                    <div
                      class="fontOmitted"
                      v-for="(item, index) in row.houseList"
                      :key="index"
                    >
                      {{ item.warehouseName }}- {{ item.areaName }}-
                      {{ item.shelfCode }}- {{ item.cargoSpaceCode
                      }}<span style="color: #ff4949" v-if="item.num">
                        - {{ item.num }}(数)</span
                      >
                      <br />
                    </div>
                  </div>
                </div>
                <div v-else>{{ row.position }}</div>
              </template>
            </el-table-column>
            <!-- <el-table-column
                label="质检单"
                align="center"
                prop="weightUnit"
                :show-overflow-tooltip="true"
              > -->
            <!-- <el-table-column label="序号" type="index" width="50">
            </el-table-column>
            <el-table-column label="编码" prop="categoryCode"></el-table-column>
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="批次号" prop="batchNo"></el-table-column>
            <el-table-column label="最小包装单元" width="120">
              <template slot-scope="{ row }">
                {{ row.minPackingCount }}{{ row.measuringUnit }}/{{
                  row.packingUnit
                }}
              </template>
            </el-table-column>
            <el-table-column label="包装数量" prop="availableCountBase">
              <template slot-scope="{ row }">
                {{ row.packingCount }}{{ row.packingUnit }}
              </template>
            </el-table-column>
            <el-table-column label="计量数量" prop="count"></el-table-column>
            <el-table-column
              label="计量单位"
              prop="measuringUnit"
            ></el-table-column>
            <el-table-column label="重量" prop="weight"></el-table-column>
            <el-table-column
              label="重量单位"
              prop="weightUnit"
            ></el-table-column>
            <el-table-column label="单价">
              <template slot-scope="{ row }">
                {{ row.price ? row.price : '-' }}元/{{ row.measuringUnit }}
              </template>
            </el-table-column>
            <el-table-column label="金额" prop="totalMoney"></el-table-column>
            <el-table-column label="领料部门" prop="llbm"></el-table-column> -->
          </el-table>
        </div>
      </div>
    </div>
    <div class="mt20">
      <header-title :title="`包装明细`" size="16px"></header-title>
      <el-table
        ref="multipleTable"
        :data="materialCodeReqList"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
      >
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <!-- <el-table-column :label="`编码`" prop="assetCode"></el-table-column> -->
        <el-table-column :label="`编码`" prop="categoryCode"></el-table-column>
        <!-- <el-table-column :label="`名称`" prop="assetName"></el-table-column> -->
        <el-table-column :label="`名称`" prop="name"></el-table-column>
        <el-table-column label="批次号" prop="batchNo"></el-table-column>
        <!-- <el-table-column
          label="包装编码"
          prop="onlyCode"
          width="80"
        ></el-table-column> -->
        <el-table-column
          label="包装编码"
          prop="code"
          width="80"
        ></el-table-column>
        <el-table-column label="包装数量" prop="packingCount" width="80">
          <template slot-scope="{ row }">
            {{ row.packingCount == 0 ? '0' : row.packingCount }}
          </template>
        </el-table-column>
        <el-table-column label="包装单位" prop="packingUnit"></el-table-column>
        <!-- <el-table-column label="计量数量" prop="totalCount"></el-table-column> -->
        <el-table-column label="计量数量" prop="count"></el-table-column>

        <el-table-column
          label="计量单位"
          prop="measuringUnit"
        ></el-table-column>
        <!-- <el-table-column label="领料部门" prop=""></el-table-column> -->
        <el-table-column
          v-if="infoData.bizType == 2 || infoData.bizType == 1"
          label="物料代号"
          prop="materielCode"
        >
          <template slot-scope="{ row }">
            {{ row.materielCode }}
          </template>
        </el-table-column>
        <el-table-column
          v-if="infoData.bizType == 2 || infoData.bizType == 1"
          label="客户代号"
          prop="clientCode"
        >
          <template slot-scope="{ row }">
            {{ row.clientCode }}
          </template>
        </el-table-column>
        <!-- v-if="infoData.bizType == 1" -->
        <el-table-column label="刻码" prop="engrave">
          <template slot-scope="{ row }">
            {{ row.engrave }}
          </template>
        </el-table-column>
        <el-table-column label="重量" prop="weight">
          <template slot-scope="{ row }">
            {{ row.weight }}
          </template>
        </el-table-column>
        <el-table-column label="重量单位" prop="weightUtil">
          <template slot-scope="{ row }">
            {{ row.weightUtil }}
          </template>
        </el-table-column>
        <el-table-column label="质检结果" prop="result" width="120">
          <template slot-scope="{ row }">
            <DictSelection
              v-if="isEdit && metaList?.length <= 0"
              @updateVal="changeWrapStatus($event, row, '包装明细')"
              dictName="质检结果"
              clearable
              v-model="row.result"
            />
            <span v-else>{{ qualityResults[row.result] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="质检状态" prop="status" width="120">
          <template slot-scope="{ row }">
            <span>{{ qualityStatus[row.status] }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="生产日期"
          prop="manufactureTime"
          width="200"
        ></el-table-column>
        <el-table-column
          label="采购日期"
          prop="procurementTime"
          width="200"
        ></el-table-column>
      </el-table>
    </div>
    <div class="mt20">
      <header-title :title="`物料明细`" size="16px"></header-title>
      <el-table
        ref="multipleTable1"
        :data="metaList"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        :header-cell-style="{ background: '#EEEEEE', border: 'none' }"
      >
        <el-table-column label="序号" type="index" width="50">
        </el-table-column>
        <el-table-column label="编码" prop="assetCode"></el-table-column>
        <el-table-column label="名称" prop="assetName"></el-table-column>
        <el-table-column label="批次号" prop="batchNo"></el-table-column>
        <el-table-column label="物料编码" prop="no"></el-table-column>
        <el-table-column label="计量数量" prop="">
          <template slot-scope="{ row }">1</template>
        </el-table-column>
        <el-table-column label="计量单位" prop="measuringUnit">
        </el-table-column>
        <el-table-column label="物料代号" prop="meterielCode">
        </el-table-column>
        <el-table-column label="客户代号" prop="clientCode"> </el-table-column>
        <el-table-column label="刻码" prop="engrave"></el-table-column>
        <el-table-column label="重量" prop="weight"></el-table-column>
        <el-table-column label="重量单位" prop="weightUnit"> </el-table-column>
        <el-table-column label="质检结果" prop="result" width="120">
          <template slot-scope="{ row }">
            <DictSelection
              v-if="isEdit && metaList?.length > 0"
              @updateVal="changeWrapStatus($event, row, '物料明细')"
              dictName="质检结果"
              clearable
              v-model="row.result"
            />
            <span v-else>{{ qualityResults[row.result] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="质检状态" prop="status" width="120">
          <template slot-scope="{ row }">
            <span>{{ qualityStatus[row.status] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <WareHouseDailog
      ref="wareHouseDailogRef"
      @houseData="houseData"
    ></WareHouseDailog>
  </div>
</template>

<script>
  // import { getInfo } from '@/api/stockManagement/outgoingManagement' '@/components/PageHeader'
  // import PageHeader from '@/components/PageHeader';
  // import { tableHeader } from '../common';
  import WareHouseDailog from '@/components/WareHouseDailog/index.vue';
  import { mapGetters, mapActions } from 'vuex';
  import {
    getOutInBySourceBizNoOrError,
    getTreeByGroup
  } from '@/api/classifyManage/index';
  //import warehouseDefinition from '@/api/warehouseManagement/warehouseDefinition';

  // import {
  //   materialType,
  //   warehousingType,
  //   useDict,
  //   useDictLabel,
  //   inputStatus,
  //   outputSceneState,
  //   auditStatus
  // } from '@/utils/dict/index';
  import { getUser } from '@/api/system/user';
  import { getOutInByIdAPI } from '@/api/bpm/components/saleManage/saleorder';
  import { deepClone } from '@/utils';

  export default {
    props: {
      returnDetailsForm: {
        type: Object,
        default: {
          detailList: []
        }
      }
    },
    components: {
      WareHouseDailog
    },
    data() {
      return {
        codeList: {}, // 物品类型键值对
        isEdit: true, // 是否可以编辑(货位和质检状态)
        showTable: true, // 解决set数组Table不刷新问题
        title: '信息',
        qualityStatus: {
          0: '未检',
          1: '已检',
          2: '部分检'
        }, // 质检状态 0未检 1已检
        qualityResults: {
          0: '合格',
          1: '不合格',
          3: '让步接收'
        }, // 质检结果 0合格 1不合格 3让步接收
        infoData: {}, // 上传入库信息
        warehousingMaterialList: [], // 物品列表
        materialCodeReqList: [], // 包装列表
        metaList: [] // 物料列表
      };
    },
    computed: {
      ...mapGetters(['getDictValue'])
    },
    created() {
      this.getCodeList();
    },
    methods: {
      // 仓库选择
      houseData(argum, idx) {
        console.log(argum);
        console.log(idx);
        this.showTable = false;
        this.$set(this.warehousingMaterialList[idx], 'houseList', argum);
        this.$set(this.infoData.outInDetailAddPOList[idx], 'houseList', argum);
        this.$nextTick(() => {
          this.showTable = true;
          // 添加货位字段
          for (let key in this.infoData.outInDetailAddPOList) {
            for (let k in this.infoData.outInDetailAddPOList[key].houseList) {
              let house = this.infoData.outInDetailAddPOList[key].houseList[k];
              (this.infoData.outInDetailAddPOList[
                key
              ].pathName = `${house.warehouseName},${house.areaName},${house.shelfCode},${house.cargoSpaceCode}`),
                (this.infoData.outInDetailAddPOList[
                  key
                ].pathIds = `${house.warehouseId},${house.areaId},${house.shelfId},${house.cargoSpaceId}`);
              this.infoData.outInDetailAddPOList[key].num = 1; // 有问题
              // this.infoData.outInDetailAddPOList[key].warehouseName =
              //   house.warehouseName;
              // 	this.infoData.outInDetailAddPOList[key].areaName = house.areaName;
              // 	this.infoData.outInDetailAddPOList[key].shelfCode = house.shelfCode;
              // 	this.infoData.outInDetailAddPOList[key].cargoSpaceCode =
              //   house.cargoSpaceCode;
              // 	this.infoData.outInDetailAddPOList[key].warehouseId = house.warehouseId;
              // 	this.infoData.outInDetailAddPOList[key].areaId = house.areaId;
              // 	this.infoData.outInDetailAddPOList[key].shelfId = house.shelfId;
              // 	this.infoData.outInDetailAddPOList[key].cargoSpaceId = house.cargoSpaceId;
              // 	this.infoData.outInDetailAddPOList[key].num = house.num;
            }
          }
          console.log(this.infoData.outInDetailAddPOList);
        });
      },
      // 获取物品类型
      async getCodeList() {
        const { data } = await getTreeByGroup({ type: 1 });
        console.log(data);
        let arr = {};
        data.forEach((item) => {
          arr[item.id] = item.name;
        });
        this.codeList = arr;
      },
      // 仓库编辑
      handleWareHouse(row, idx) {
        this.$refs.wareHouseDailogRef.open(row, idx);
      },
      // 包装维度修改质检改变物料维度质检（结果和状态）
      changeMaterialResult() {
        this.metaList.forEach((outItem) => {
          let arr = [];
          this.materialCodeReqList.forEach((inneritem) => {
            if (inneritem.onlyCode == outItem.code) {
              arr.push(inneritem);
            }
          });
          console.log(arr);
          // 首先判断是未填/合格不合格
          let boolen = arr.every((item) => {
            console.log(item.result);
            return item.result === '' || item.result === undefined;
          });
          // 判断只要物料全部质检结果(未填)——>包装质检结果(未填) 状态(未检)
          if (boolen) {
            this.$set(outItem, 'result', '');
            this.$set(outItem, 'status', '0');
          } else {
            // 判断只要物料有一个质检结果(不合格)——>包装质检结果(不合格) 状态(已检)
            let boolen = arr.some((item) => {
              return item.result === '1';
            });
            if (boolen) {
              this.$set(outItem, 'result', '1');
              this.$set(outItem, 'status', '1');
            } else {
              // 判断只要物料有一个质检结果(让步接收)——>包装质检结果(让步接收) 状态(已检)
              let boolen = arr.some((item) => {
                return item.result === '3';
              });
              if (boolen) {
                // 让步接收 已检
                this.$set(outItem, 'result', '3');
                this.$set(outItem, 'status', '1');
              } else {
                // 判断只要物料有全部质检结果(合格)——>包装质检结果(合格) 状态(已检)
                let boolen = arr.every((item) => {
                  return item.result === '0';
                });
                if (boolen) {
                  // 合格 已检
                  this.$set(outItem, 'result', '0');
                  this.$set(outItem, 'status', '1');
                } else {
                  // 合格 部分检
                  this.$set(outItem, 'result', '0');
                  this.$set(outItem, 'status', '2');
                }
              }
            }
          }
        });
      },
      // 物料维度修改质检改变包装维度质检（结果和状态）
      changeWrapResult() {
        this.materialCodeReqList.forEach((outItem) => {
          let arr = [];
          this.metaList.forEach((inneritem) => {
            if (inneritem.onlyCode == outItem.code) {
              arr.push(inneritem);
            }
          });
          console.log('arr--', arr);
          // 首先判断是未填/合格不合格
          let boolen = arr.every((item) => {
            console.log(item.result);
            return (
              item.result === '' ||
              item.result === undefined ||
              item.result === null
            );
          });
          // 判断只要物料全部质检结果(未填)——>包装质检结果(未填) 状态(未检)
          if (boolen) {
            this.$set(outItem, 'result', '');
            this.$set(outItem, 'status', '0');
          } else {
            // 判断只要物料有一个质检结果(不合格)——>包装质检结果(不合格) 状态(已检)
            let boolen = arr.some((item) => {
              return item.result === '1';
            });
            if (boolen) {
              this.$set(outItem, 'result', '1');
              this.$set(outItem, 'status', '1');
            } else {
              // 判断只要物料有一个质检结果(让步接收)——>包装质检结果(让步接收) 状态(已检)
              let boolen = arr.some((item) => {
                return item.result === '3';
              });
              if (boolen) {
                // 让步接收 已检
                this.$set(outItem, 'result', '3');
                this.$set(outItem, 'status', '1');
              } else {
                // 判断只要物料有全部质检结果(合格)——>包装质检结果(合格) 状态(已检)
                let boolen = arr.every((item) => {
                  return item.result === '0';
                });
                if (boolen) {
                  // 合格 已检
                  this.$set(outItem, 'result', '0');
                  this.$set(outItem, 'status', '1');
                } else {
                  // 合格 部分检
                  this.$set(outItem, 'result', '0');
                  this.$set(outItem, 'status', '2');
                }
              }
            }
          }
        });
      },
      // 选择质检结果改变质检状态
      changeWrapStatus(resultValue, row, type) {
        switch (resultValue) {
          case '':
            // 清空质检状态
            row.status = '0';
            break;
          default:
            // 默认已检质检状态
            row.status = '1';
            break;
        }
        if (type === '物料明细') {
          this.$nextTick(() => {
            this.changeWrapResult();
          });
        } else {
          this.$nextTick(() => {
            this.changeMaterialResult();
          });
        }
        // 缺少包装明细让步接收物料如何选择结果
      },
      //计算金额
      calcSumTotal(packingCount, outInNum, univalence) {
        //最小包装单元,包装数量,单价
        const total = {
          PackingCount: Number(packingCount > 0 ? packingCount : 0),
          outInNum: Number(outInNum > 0 ? outInNum : 0),
          univalence: Number(univalence > 0 ? univalence : 0)
        };
        return total.PackingCount * total.outInNum * total.univalence;
      },
      // 获取出库单详情
      async initialize() {
        // returnInReceiptId退货入库单id(有说明已入库)
        if (this.returnDetailsForm.returnInReceiptId) {
          getOutInByIdAPI(this.returnDetailsForm.returnInReceiptId).then(
            (data) => {
              console.log(data);
              console.log('有订单来源');
              this.isEdit = false;
              this.infoData = deepClone(data);
              // 增加是否已经入库字段
              this.infoData.isStorage = true;
              // 填充遍历数据
              if (this.infoData.outInDetailVOList?.length > 0) {
                this.warehousingMaterialList = []; // 物品列表
                this.materialCodeReqList = []; // 包装列表
                this.metaList = []; // 物料列表
                this.infoData.outInDetailVOList.forEach((goodsiItem) => {
                  this.warehousingMaterialList.push(goodsiItem);
                  goodsiItem.outInDetailRecordVOList.forEach((wrapItem) => {
                    wrapItem.count =
                      wrapItem.outInDetailRecordMaterialDetailVOList.length; // 增加包装计量数量
                    this.materialCodeReqList.push(wrapItem);
                    wrapItem.outInDetailRecordMaterialDetailVOList.forEach(
                      (materialItem) => {
                        this.metaList.push(materialItem);
                      }
                    );
                  });
                });
              }
            }
          );
        } else {
          console.log('没有订单来源');
          this.isEdit = true;
          // outboundType 1包装 2物料
          let sendNo = this.returnDetailsForm.detailList[0]?.outInId;
          // 将退货物品明细划分为包装和物料（从物料往上反推形成tree）
          // 包装清单
          let wrapList =
            this.returnDetailsForm.detailList?.length > 0
              ? this.returnDetailsForm.detailList.filter(
                  (item) => item.outboundType === 1
                )
              : [];
          // 物料清单
          let material =
            this.returnDetailsForm.detailList?.length > 0
              ? this.returnDetailsForm.detailList.filter(
                  (item) => item.outboundType === 2
                )
              : [];
          // 获取出库单详情
          const data = await getOutInByIdAPI(sendNo);
          // 复制出库单详情
          this.infoData = deepClone(data);
          // 添加单据来源
          this.infoData.sourceBizNo = this.returnDetailsForm.returnHandleNo;
          // 增加是否已经入库字段
          this.infoData.isStorage = false;
          // 将出库状态改成入库状态 2出库 1入库
          this.infoData.type = 1;
          // 通过物料过滤出库单详情树
          this.infoData.outInDetailVOList.forEach((goodsItem) => {
            // 过滤物料维度
            goodsItem.outInDetailRecordVOList.forEach((wrapItem) => {
              wrapItem.outInDetailRecordMaterialDetailVOList =
                wrapItem.outInDetailRecordMaterialDetailVOList.map(
                  (materialItem) => {
                    let hasMaterialItem = material.some(
                      (item) => item.outboundDetailId === materialItem.id
                    );
                    if (hasMaterialItem) {
                      console.log(materialItem);
                      return materialItem;
                    }
                  }
                );
            });
          });
          // 通过包装过滤出库单详情树
          this.infoData.outInDetailVOList.forEach((goodsItem) => {
            // 过滤包装维度
            goodsItem.outInDetailRecordVOList =
              goodsItem.outInDetailRecordVOList.map((wrapItem) => {
                // 过滤在包装清单内或者存在过滤物料的包装
                if (wrapList.length > 0) {
                  let hasWrapItem = wrapList.some((item) => {
                    return (
                      item.outboundDetailId === wrapItem.id ||
                      wrapItem.outInDetailRecordMaterialDetailVOList?.length > 0
                    );
                  });
                  if (hasWrapItem) {
                    return wrapItem;
                  }
                } else {
                  // 返回存在物料维度的包装维度
                  if (
                    wrapItem.outInDetailRecordMaterialDetailVOList?.length > 0
                  ) {
                    return wrapItem;
                  }
                }
              });
          });
          // 过滤没有包装的物品维度
          this.infoData.outInDetailVOList = this.infoData.outInDetailVOList.map(
            (goodsItem) => {
              if (goodsItem.outInDetailRecordVOList?.length > 0) {
                return goodsItem;
              }
            }
          );
          // 处理过滤map为空的数组
          this.infoData.outInDetailVOList =
            this.infoData.outInDetailVOList.filter((item) => !!item);
          this.infoData.outInDetailVOList.forEach((goodsItem) => {
            goodsItem.outInDetailRecordVOList =
              goodsItem.outInDetailRecordVOList.filter(
                (wrapItem) => !!wrapItem
              );
          });
          this.infoData.outInDetailVOList.forEach((goodsItem) => {
            goodsItem.outInDetailRecordVOList.forEach((wrapItem) => {
              wrapItem.outInDetailRecordMaterialDetailVOList =
                wrapItem.outInDetailRecordMaterialDetailVOList.filter(
                  (metail) => !!metail
                );
            });
          });
          this.initData();
        }
        // returnHandleNo 有单据来源说明已入库（被驳回状态)
        // getOutInBySourceBizNoOrError(this.returnDetailsForm.returnHandleNo).then((data) => {
        //   console.log(data)
        //   console.log('有订单来源')
        //   this.isEdit = false
        // 	this.infoData = deepClone(data)
        // 	// 增加是否已经入库字段
        // 	this.infoData.isStorage = true
        // 	// 填充遍历数据
        // 	if (this.infoData.outInDetailVOList?.length > 0) {
        //     this.warehousingMaterialList = []; // 物品列表
        //     this.materialCodeReqList = []; // 包装列表
        //     this.metaList = []; // 物料列表
        //     this.infoData.outInDetailVOList.forEach((goodsiItem) => {
        //       this.warehousingMaterialList.push(goodsiItem);
        //       goodsiItem.outInDetailRecordVOList.forEach((wrapItem) => {
        //         wrapItem.count =
        //           wrapItem.outInDetailRecordMaterialDetailVOList.length; // 增加包装计量数量
        //         this.materialCodeReqList.push(wrapItem);
        //         wrapItem.outInDetailRecordMaterialDetailVOList.forEach((materialItem) => {
        //           this.metaList.push(materialItem);
        //         });
        //       });
        //     });
        //   }
        // }).catch(async() => {
        //   console.log('没有订单来源')
        //   this.isEdit = true
        // 	// outboundType 1包装 2物料
        // 	let sendNo = this.returnDetailsForm.detailList[0]?.outInId;
        // 	// 将退货物品明细划分为包装和物料（从物料往上反推形成tree）
        // 	// 包装清单
        // 	let wrapList =
        // 		this.returnDetailsForm.detailList?.length > 0
        // 			? this.returnDetailsForm.detailList.filter(
        // 					(item) => item.outboundType === 1
        // 				)
        // 			: [];
        // 	// 物料清单
        // 	let material =
        // 		this.returnDetailsForm.detailList?.length > 0
        // 			? this.returnDetailsForm.detailList.filter(
        // 					(item) => item.outboundType === 2
        // 				)
        // 			: [];
        // 	// 获取出库单详情
        // 	const data = await getOutInByIdAPI(sendNo);
        // 	// 复制出库单详情
        // 	this.infoData = deepClone(data);
        // 	// 添加单据来源
        // 	this.infoData.sourceBizNo = this.returnDetailsForm.returnHandleNo
        // 	// 增加是否已经入库字段
        // 	this.infoData.isStorage = false
        // 	// 将出库状态改成入库状态 2出库 1入库
        // 	this.infoData.type = 1;
        // 	// 通过物料过滤出库单详情树
        // 	this.infoData.outInDetailVOList.forEach((goodsItem) => {
        // 		// 过滤物料维度
        // 		goodsItem.outInDetailRecordVOList.forEach((wrapItem) => {
        // 			wrapItem.outInDetailRecordMaterialDetailVOList =
        // 				wrapItem.outInDetailRecordMaterialDetailVOList.map(
        // 					(materialItem) => {
        // 						let hasMaterialItem = material.some(
        // 							(item) => item.outboundDetailId === materialItem.id
        // 						);
        // 						if (hasMaterialItem) {
        //               console.log(materialItem)
        // 							return materialItem;
        // 						}
        // 					}
        // 				);
        // 		});
        // 	});
        // 	// 通过包装过滤出库单详情树
        // 	this.infoData.outInDetailVOList.forEach((goodsItem) => {
        // 		// 过滤包装维度
        // 		goodsItem.outInDetailRecordVOList =
        // 			goodsItem.outInDetailRecordVOList.map((wrapItem) => {
        // 				// 过滤在包装清单内或者存在过滤物料的包装
        // 				if (wrapList.length > 0) {
        // 					let hasWrapItem = wrapList.some((item) => {
        // 						return (
        // 							item.outboundDetailId === wrapItem.id ||
        // 							wrapItem.outInDetailRecordMaterialDetailVOList?.length > 0
        // 						);
        // 					});
        // 					if (hasWrapItem) {
        // 						return wrapItem;
        // 					}
        // 				} else {
        // 					// 返回存在物料维度的包装维度
        // 					if (
        // 						wrapItem.outInDetailRecordMaterialDetailVOList?.length > 0
        // 					) {
        // 						return wrapItem;
        // 					}
        // 				}
        // 			});
        // 	});
        // 	// 过滤没有包装的物品维度
        // 	this.infoData.outInDetailVOList = this.infoData.outInDetailVOList.map(
        // 		(goodsItem) => {
        // 			if (goodsItem.outInDetailRecordVOList?.length > 0) {
        // 				return goodsItem;
        // 			}
        // 		}
        // 	);
        //   // 处理过滤map为空的数组
        //   this.infoData.outInDetailVOList = this.infoData.outInDetailVOList.filter((item) => !!item)
        //   this.infoData.outInDetailVOList.forEach((goodsItem) => {
        //     goodsItem.outInDetailRecordVOList = goodsItem.outInDetailRecordVOList.filter((wrapItem) => !!wrapItem)
        // 	});
        //   this.infoData.outInDetailVOList.forEach((goodsItem) => {
        //     goodsItem.outInDetailRecordVOList.forEach((wrapItem) => {
        //       wrapItem.outInDetailRecordMaterialDetailVOList = wrapItem.outInDetailRecordMaterialDetailVOList.filter((metail) => !!metail)
        //     })
        //   })
        //   console.log(this.infoData)
        // 	this.initData();
        // })
      },
      // 初始化显示数据
      async initData() {
        this.infoData.outInDetailAddPOList = this.infoData.outInDetailVOList;
        delete this.infoData.outInDetailVOList;
        console.log(this.infoData);
        if (this.infoData.outInDetailAddPOList?.length > 0) {
          this.warehousingMaterialList = []; // 物品列表
          this.materialCodeReqList = []; // 包装列表
          this.metaList = []; // 物料列表
          this.infoData.outInDetailAddPOList.forEach((goodsiItem) => {
            goodsiItem.houseList = [];
            goodsiItem.count = goodsiItem.outInDetailRecordVOList.length;
            goodsiItem.outInDetailRecordAddPOList =
              goodsiItem.outInDetailRecordVOList;
            delete goodsiItem.outInDetailRecordVOList;
            console.log(goodsiItem);

            this.warehousingMaterialList.push(goodsiItem);
            goodsiItem.outInDetailRecordAddPOList.forEach((wrapItem) => {
              wrapItem.minUnit = wrapItem.minPackingCount;
              wrapItem.minPackingCount = wrapItem.minPackingCount;
              wrapItem.minPositionId = wrapItem.cargoSpaceId;
              wrapItem.unit = wrapItem.packingUnit;
              wrapItem.count =
                wrapItem.outInDetailRecordMaterialDetailVOList.length;
              wrapItem.materialDetails =
                wrapItem.outInDetailRecordMaterialDetailVOList;
              delete wrapItem.outInDetailRecordMaterialDetailVOList;
              this.materialCodeReqList.push(wrapItem);
              wrapItem.materialDetails.forEach((materialItem) => {
                this.metaList.push(materialItem);
              });
            });
          });
        }
        console.log(this.warehousingMaterialList);
        console.log(this.materialCodeReqList);
        console.log(this.metaList);
        console.log('------------------');
        console.log(this.infoData);
      },
      ...mapActions('dict', ['requestDict'])
    }
  };
</script>

<style lang="scss" scoped>
  // 货位省略号样式
  .fontOmitted {
    text-overflow: ellipsis;
    overflow: hidden;
    word-break: break-all;
    white-space: nowrap;
  }

  .mt20 {
    margin-top: 20px;
  }

  .content-detail {
    overflow: hidden;
  }
</style>
