<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="cc001" class="under" :label="$t('到货供应商')">
            <input
              v-model="input"
              type="text"
              @confirm="inputCC"
              :placeholder="$t('供应商代码')"
            >
            </input>
          </u-form-item>
        </u-form>
        <view>{{$t('到货明细')}}</view>
      </view>
      <ContentLoadingMore class="cardbox" :list="goodsList">
        <view class="cardContent" v-for="(item, index) in goodsList">
          <scanningList
            :item="item"
            :idx="index"
            @deleGonds="deleGonds"
            :list="list"
          />
        </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: [],
        btnArr: [
        {
          style: "",
          type: "warning",
          text: "返回",
          way: "goBack",
        },
        {
          style: "",
          type: "primary",
          text: "到货确认",
          way: "goSubmit",
        },
      ],
        wxparams: "",
        input: '',
        loading: false,
        daohuoForm: {
					cc001: "3701"
				},
        rules: {
					cc001: { required: true,type: 'string', trigger: ['blur'], errorMessage: "11" },
					cc002: { required: true, type: 'string',trigger: ['blur'], errorMessage: "11" }
				},
      }
    },
    created() {
    },
    methods: {
      getBtnHandle(row) {
        this[row.way]();
      },
      goBack() {
        uni.navigateBack();
      },
      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.cc001) return this.$u.toast(i18n.t('一个到货单无法录入两个供应商订单'))
            const index = this.goodsList.findIndex(v => v.joinlabel = arr.slice(0, 5).join('-'))
            if (index > -1) {
              this.goodsList[index]['cc006'] = Number(this.goodsList[index]['cc006']) + Number(arr[5])
            } else {
              this.goodsList.push({
                cc001: arr[0],
                cc002: arr[1],
                cc003: arr[2],
                cc004: arr[3],
                cc005: arr[4],
                cc006: arr[5],
                joinlabel: arr.slice(0, 5).join('-')
              })
            }
            this.input = ''
            this.daohuoForm.cc001 = arr[0]
          }
        } else {
        }
      }
    },
    deleGonds(idx) {
	  this.list = this.list.filter((item) => {
	  	return item.qrcode !== this.goodsList[idx].qrcode;
	  })
      this.goodsList.splice(idx, 1);
    },
    goSubmit(){
      if (!this.goodsList.length) return;
        if(!this.daohuoForm.cc001) return this.$u.toast(i18n.t('请输入到货单别'));
        if(!this.daohuoForm.cc002) return this.$u.toast(i18n.t('请输入到货单号'));
        this.loading = true;
        this.$u.api.getforcreate({
          cc001: this.daohuoForm.cc001,
          cc002: this.daohuoForm.cc002
        })
        .then(res => {
        this.loading = false;
          if (res.success) {
            if (res.data) {
              if(!res.data.purths || res.data.purths.length === 0) return this.$u.toast(i18n.t('到货单已经完成入库'));
              uni.navigateTo({
                url: '/pages/forcreate/Info?info=' + encodeURIComponent(JSON.stringify(res.data))
              });
            } else {
              this.$u.toast(i18n.t('到货单不存在或未检验'));
            }
          } else {
          this.$u.toast(res.errorMessage);
        }
        });
      }
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .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 {
    min-height: calc(100vh - 540rpx);
    padding: 0rpx 20rpx;
    margin-top: 20rpx;
  }
  .cardContent {
    border-radius: 8px;
    background: #fff;
    margin-bottom: 20rpx;
  }
  /deep/ .u-sticky {
    top: 0 !important;
    background: #fff;
  }
  page {
    position: relative;
    width: 100%;  height: 100%;
  }

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