<template>
    <view class="page">
      <StickyNavBar>
        <NavBar :title="$t('录入到货单')"></NavBar>
      </StickyNavBar>
      <view style="padding: 0 10px;">
        <u-form ref="daohuoForm" :modelValue="daohuoForm" :rules="rules" labelWidth="200">
          <u-form-item prop="warehouse" class="under" :label="$t('到货仓')">
            <uni-data-select
            v-model="daohuoForm.warehouse"
            :clear="false"
            :localdata="typeData"
            ></uni-data-select>
          </u-form-item>
          <u-form-item prop="supplierNo" class="under" :label="$t('到货供应商')">
            <input
              v-model="input"
              type="text"
              @confirm="inputCC"
              :placeholder="$t('供应商代码')"
            >
            </input>
          </u-form-item>
        </u-form>
        <view v-if="goodsList.length > 0">{{$t('到货明细')}}</view>
      </view>
      <ContentLoadingMore class="cardbox" :list="goodsList">
        <view class="cardContent" v-for="(item, index) in goodsList">
          <scanningList
            :item="item"
            :idx="index"
            @deleGonds="deleGonds"
          />
        </view>
      </ContentLoadingMore>
      <BottomBtn :btnArr="btnArr" @getBtnHandle="getBtnHandle"></BottomBtn>
    </view>
  </template>
  
  <script>
import i18n from '../../lang/index'
import scanningList from "./scanningList.vue";
import BottomBtn from "@/components/BottomBtn/index.vue";

  import StickyNavBar from "@/components/StickyNavBar/index.vue";
  import ContentLoadingMore from "@/components/ContentLoadingMore/index.vue";
  export default {
    components: {
      StickyNavBar,
      scanningList,
      BottomBtn,
      ContentLoadingMore,
    },
    data() {
      return {
        number: 0,
        totalNum: 0,
        show: true,
        goodsList: [],
        typeData: [],
        btnArr: [
        {
          style: "",
          type: "warning",
          text: i18n.t('返回'),
          way: "goBack",
        },
        {
          style: "",
          type: "primary",
          text: i18n.t('到货确认'),
          way: "goSubmit",
        },
      ],
        wxparams: "",
        input: '',
        loading: false,
        daohuoForm: {
					supplierNo: "",
          warehouse: null
				},
        rules: {
					supplierNo: { required: true,type: 'string', trigger: ['blur'], errorMessage: "11" },
					purchaseType: { required: true, type: 'string',trigger: ['blur'], errorMessage: "11" }
				},
      }
    },
    created() {
      this.warehouseList()
    },
    methods: {
      warehouseList() {
        this.$u.api.warehouseList().then(res => {
          if(res.success) {
            this.typeData = res.data.map(v => ({text: v.warehouseName, value: v.warehouseName}))
          }
        })
      },
      getBtnHandle(row) {
        this[row.way]();
      },
      goBack() {
        uni.navigateBack();
      },
      async inputCC(e){
      if(e && e.detail.value) {
        // 2028-330-20250106001-0019-A1010004-5
        if(e.detail.value.includes("-")){
          const arr = e.detail.value.split("-")
          if (arr.length === 6) {
            if (arr[0] !== this.daohuoForm.supplierNo && this.daohuoForm.supplierNo) return this.$u.toast(i18n.t('一个到货单无法录入两个供应商订单'))
            const data = await this.$u.api.validateArrival({
              "materialNo": arr[4],
              "purchaseNo": arr[2],
              "purchaseSerial": arr[3],
              "purchaseType": arr[1]
            })
            if (!data.success) return
            const index = this.goodsList.findIndex(v => v.joinlabel === arr.slice(0, 5).join('-'))
            if (index > -1) {
              this.goodsList[index]['arrivalQuantity'] = Number(this.goodsList[index]['arrivalQuantity']) + Number(arr[5])
            } else {
              const data1 = await this.$u.api
              .getforinvmbinfo({
                mb001: arr[4]
              })
              this.goodsList.push({
                supplierNo: arr[0],
                purchaseType: arr[1],
                purchaseNo: arr[2],
                purchaseSerial: arr[3],
                materialNo: arr[4],
                arrivalQuantity: arr[5],
                udf07: data1.data['udf07'],
                mb002: data1.data['mb002'],
                joinlabel: arr.slice(0, 5).join('-')
              })
            }
            this.input = ''
            this.daohuoForm.supplierNo = arr[0]
          }
        } else {
        }
      }
    },
    deleGonds(idx) {
      this.goodsList.splice(idx, 1);
    },
    goSubmit(){
      if (!this.goodsList.length) return;
        if(!this.daohuoForm.warehouse) return this.$u.toast(i18n.t('请选择到货仓'));
        this.loading = true;
        this.$u.api.confirmArrival({
          warehouse: this.daohuoForm.warehouse,
          details: this.goodsList.map(v => ({arrivalQuantity: v.arrivalQuantity, materialNo: v.materialNo, purchaseNo: v.purchaseNo, purchaseSerial: v.purchaseSerial, purchaseType: v.purchaseType, supplierNo: v.supplierNo}))
        })
        .then(res => {
          this.loading = false;
          if (res.success) {
            this.$u.toast(i18n.t('确认成功'));
            this.goodsList = []
            this.daohuoForm.supplierNo = ''
          } else {
          this.$u.toast(res.errorMessage);
        }
        });
      }
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .page {
    background: #fff;
  }
  .btn {
    padding: 20rpx;
    background: #fff;
    .topSearch {
      margin-bottom: 20rpx;
    }
  }
  .bottomBtn {
    position: sticky;
    padding: 20rpx;
    display: flex;
    justify-content: space-between;
    background: #fff;
    bottom: 0rpx;
    margin-top: 20rpx;
    .u-btn {
      width: 300rpx;
    }
  }
  .blue {
    color: #3f9dcc;
  }
  .total {
    background: #fff;
    color: #000;
    padding: 0rpx 40rpx;
    display: flex;
    justify-content: space-between;
    font-size: 32rpx;
    .line {
      margin: 0px 10rpx;
    }
  }
  .cardbox {
    border: 1px solid #dadde2;
    border-radius: 5px;
    background: #f3f3f5;
    margin: 0rpx 20rpx;
    margin-top: 20rpx;
  }
  .cardContent {
    border-radius: 8px;
    background: #fff;
    margin-top: 20rpx;
  }
  /deep/ .u-sticky {
    top: 0 !important;
    background: #fff;
  }
  page {
    position: relative;
    width: 100%;  height: 100%;
  }

  .cardbox {
  min-height: calc(100vh - 520rpx);
  padding: 0rpx 20rpx;
  margin-top: 20rpx;
}
  </style>
  