<template>
  <view class="lists page">
    <StickyNavBar class="listTitle">
      <NavBar title="委外发料"></NavBar>
      <!-- <view class="selectSheet">
				<u-tabs :list="list" :is-scroll="false" :current="current" @change="changeTab"></u-tabs>
			</view> -->
      <view class="headerTitle">
        <view class="items">交货单号:{{ goodsList[0].VBELN }}</view>
        <view class="items">8|李XXXXXXX体</view>
        <view class="items">计划日期:9999/99/99</view>
      </view>
      <view class="">
        <view class="btn">
          <!-- <uni-easyinput prefixIcon="search" class="topSearch" v-model="nlpla" focus placeholder="请扫描库位" @change="handleInputNlpla"></uni-easyinput> -->
          <uni-easyinput
            prefixIcon="search"
            :disabled="isDisabled"
            v-model="wxparams"
            :focus="isFocus"
            placeholder="请扫描产品包装条码/外箱码"
            @input="handleInputPlnr"
          ></uni-easyinput>
        </view>
        <view class="total">
          <view class="left">明细</view>
          <view class="right">
            <!-- 物料数量： -->
            <text class="blue">{{ totalNum }}</text>
            <text class="line">/</text>
            <text class="">{{ goodsList.length }}</text>
          </view>
        </view>
      </view>
    </StickyNavBar>
    <ContentLoadingMore class="contentMore" :list="goodsList">
      <view class="cardbox">
        <view
          class="cardContent"
          v-for="(item, index) in goodsList"
          :key="item.PLNR"
        >
          <scanningList :item="item" :list="item.ZPLDT" />
        </view>
      </view>
    </ContentLoadingMore>
    <BottomBtn :btnArr="btnArr" @getBtnHandle="getBtnHandle"></BottomBtn>
  </view>
</template>

<script>
import StickyNavBar from "@/components/StickyNavBar/index.vue";
import ContentLoadingMore from "@/components/ContentLoadingMore/index.vue";
import BottomBtn from "@/components/BottomBtn/index.vue";
import scanningList from "./scanningList.vue";
import { parseTime } from "@/utils/ruoyi";
export default {
  components: {
    StickyNavBar,
    ContentLoadingMore,
    BottomBtn,
    scanningList,
  },
  data() {
    return {
      value: "",
      type: "select",
      border: true,
      current: 0,
      status: "loadmore",
      btnArr: [
        {
          style: "",
          type: "warning",
          text: "返回",
          way: "goBack",
        },
        {
          style: "",
          type: "primary",
          text: "提交",
          way: "goSubmit",
        },
      ],
      isFocus: false,
      isDisabled: false,
      nlpla: "",
      wxparams: "",
      goodsList: [],
      orderItem: {},
      catchPLs: [],
      resFlag: false,
    };
  },
  created() {
    // this.getPlnr('10000820240325000001|10000320100');
  },
  onLoad(option) {
    if (option && option.order) {
    console.log('0-------',option.order);
      const arr = [...JSON.parse(option.order)];
      arr.forEach((ele) => {
        this.goodsList.push(
          ...(ele.ZSIZE.length
            ? ele.ZSIZE.map((item) => {
                return {
                  KDAUF: ele.KDAUF,
                  KDPOS: ele.KDPOS,
                  LFIMG: ele.LFIMG,
                  MEINS: ele.MEINS,
                  WERKS: ele.WERKS,
                  ...item,
                  MATNR: ele.MATNR,
                  MAKTX: ele.MAKTX,
				  ZPLDT: []
                };
              })
            : [ele].map(v => {
				v.ZSIZE = ''
				v.ZPLDT = []
				return v
			}))
        );
      });

	  console.log(this.goodsList);
    }
  },
  computed: {
    totalNum() {
      const count = this.goodsList.filter((item) => item.checkedBox).length;
      return count || 0;
    },
  },
  methods: {
    goSubmit() {
      if (!this.goodsList.length) return;
      if (this.resFlag) {
        return this.$u.toast("当前单据已经提交成功！无需继续提交！");
      }
      const flag = this.goodsList.every((ele) => {
        return (
          (ele.ZINSNUM || ele.LFIMG) == ele?.ZPLDT?.reduce((acc, cur) => acc + cur.PACMG, 0)
        );
      });

      if (!flag) {
        return this.$u.toast("请确认所有包装数量是否正确");
      }

      const params = this.goodsList.reduce((acc, cur, index) => {
        if (index == 0) {
          acc.push(JSON.parse(JSON.stringify(cur)));
        } else {
          const item = acc.find((v) => v.POSNR === cur.POSNR);
          if (item) {
            item.ZPLDT.push(...cur.ZPLDT);
          } else {
			const value = JSON.parse(JSON.stringify(cur))
            acc.push(value);
          }
        }
        return acc;
      }, []);

      this.$u.api.sapApi.sapPickingDn(params, 300).then((res) => {
        this.loading = false;
        if (!Array.isArray(res)) {
          return;
        }
        const flag = res.some((item) => {
          const f = item.TYPE == "E";
          if (f) {
            this.$u.toast(item.MESSAGE);
          }
          return f;
        });
        if (!flag) {
          this.$u.toast(res[0].MESSAGE);
          // this.resFlag = true
          setTimeout(() => {
            this.goBack();
          }, 3000);
        }
      });
    },
    getPlnr(plnr) {
      let params = {
        ZCODE: plnr.includes("BL") ? plnr : "",
        plnr: plnr.includes("BL") ? "" : plnr,
      };
      this.$u.api.sapApi.sapBlpl(params, 300).then((res) => {
        if (res && res.MSG[0].TYPE === "S") {
          // 缓存PL
          this.catchPLs.push(plnr);
          // 循环BLPL合并相同的物料号和size
          res.BLPL.forEach((ele) => {
            ele.TOMNG = ele.PACMG;
            const item = this.goodsList.find((v) =>  {
				if (ele.MATNR == v.MATNR && v.ZSIZE == ele.ZSIZE) {
					return true
				} else if (ele.MATNR == v.MATNR) {
					return true
				}
				return false
			});
            if (item) {
              if (Reflect.has(item, "ZPLDT")) {
                item.ZPLDT.push({
                  ...ele,
                  KDAUF: item.KDAUF,
                  KDPOS: item.KDPOS,
                });
              } else {
                this.$set(item, "ZPLDT", [ele]);
              }
            }
          });
		  console.log(this.goodsList, '---=====--')
        } else {
          this.$u.toast(res.MSG[0].MESSAGE);
        }
        setTimeout(() => {
          this.isFocus = true;
          this.isDisabled = false;
          this.wxparams = "";
        }, 1000);
        this.loading = false;
      });
    },
    // 加载更多
    onReachBottom() {
      console.log("加载更多inventoryCounting");
    },
    getBtnHandle(row) {
      console.log("getBtnHandle", row);
      this[row.way]();
    },
    goBack() {
      console.log("进来");
      uni.navigateBack();
    },
    changeTab(index) {
      this.current = index;
    },
    handleInputNlpla(e) {},
    handleInputPlnr(e) {
      this.isFocus = false;
      if (this.catchPLs.includes(e)) {
        return this.$u.toast("请勿重复扫描该条码！");
      }
      e && this.getPlnr(e);
    },
  },
};
</script>

<style lang="scss">
.selectSheet {
  border-bottom: 1px solid #ccc;
}

/deep/ .u-sticky {
  top: 0 !important;
  background: #fff;
}

.tabTwo {
  /deep/ .u-sticky-wrap {
    height: 330rpx !important;
  }
}

.listTitle {
}

.contentMore {
  margin: 20rpx;
}

page {
  background: #f8f8f8;
}

.headerTitle {
  padding: 20rpx;
  font-weight: 600;

  .items {
    line-height: 40rpx;
  }
}

.btn {
  padding: 20rpx;
  background: #fff;

  .topSearch {
    margin-bottom: 20rpx;
  }
}

.total {
  background: #fff;
  color: #000;
  padding: 0rpx 40rpx;
  display: flex;
  justify-content: space-between;
  font-size: 32rpx;

  .line {
    margin: 0px 10rpx;
  }
}

.cardbox {
  min-height: calc(100vh - 410rpx);
  padding: 0rpx 20rpx;
  margin-top: 20rpx;
}

.cardContent {
  border-radius: 8px;
  background: #fff;
  margin-bottom: 20rpx;
}
</style>
