<template>
  <view>
  <!--  <view class="goodsNumber">
      <span class="goodsNumberCode">商品条码</span>
      <u-input v-model="barCode" border placeholder="请输入或扫描商品条码" @input="queryFun"></u-input>
    </view> -->
    <view v-if="result.businInReceivingDForPdaVo">
      <view class="goodsInfo">
        <view class="goodsInfoItem">
          <span class="goodsItemLabel">收货单号</span>
          <span class="goodsItemValue">{{ result.orderNo}}</span>
        </view>
      </view>
      <view class="titleValue global-font">
        <span class="title">商品编号：</span>
        <span class="value" v-if="result.businInReceivingDForPdaVo">{{result.businInReceivingDForPdaVo.bgGoodNo}}</span>
      </view>
      <view class="titleValue global-font">
        <span class="title">商品名称：</span>
        <span class="value"
          v-if="result.businInReceivingDForPdaVo">{{result.businInReceivingDForPdaVo.bgGoodName}}</span>
      </view>
      <view class="titleValue global-font">
        <span class="title" >规 格：</span>
        <span class="value"
          v-if="result.businInReceivingDForPdaVo">{{result.businInReceivingDForPdaVo.bgSpecifications}}</span>
      </view>
      <view class="titleValue global-font">
        <span class="title">件装量：</span>
        <span v-if="result.businInReceivingDForPdaVo"> {{result.businInReceivingDForPdaVo.bpPackageQty}}</span>
      </view>
	  
      <view class="titleValue global-font">
        <span class="title">单 位：</span>
        <span class="value" v-if="result.businInReceivingDForPdaVo">{{result.businInReceivingDForPdaVo.unitName}}</span>
      </view>
  
      <view class="titleValue global-font">
        <span class="title">未验数量：</span>
        <span class="value" v-if="result.businInReceivingDForPdaVo">{{result.businInReceivingDForPdaVo.ableQty}}</span>
      </view>
	  
      <view class="titleValue global-font">
        <span class="title">验收数量：</span>
        <span class="value" v-if="result.businInReceivingDForPdaVo">{{dataForm.actualQty}}</span>
      </view>
	  
      <view class="titleValue global-font">
        <span class="title">件 数：</span>
        <u-input v-model="dataForm.actualCaseQty" border placeholder="请输入件数" @input="changeCase"></u-input>
      </view>

      <view class="titleValue global-font">
        <span class="title">库 别：</span>
        <u-input v-model="kubevalue" type="select" placeholder="请选择库别" class="u-input" placeholder-style="color:#000000"
          @click="kube = true" />
        <u-select v-model="kube" :list="storageList" @confirm="confirmKube"></u-select>
      </view>

      <view class="titleValue global-font">
        <span class="title">容 器：</span>
        <u-input v-model="dataForm.containerCode" border placeholder="请输入或扫描容器条码"></u-input>
      </view>
      	<view class="titleValue global-font">
			<span class="title">抽检件数：</span>
			<u-input v-model="dataForm.checkCaseQty" :disabled="true" border placeholder="请输入数量"></u-input>
		</view>
      <view class="titleValue global-font">
			<span class="title">抽检零散数：</span>
			<u-input v-model="dataForm.checkPieceQty" :disabled="true" border placeholder="请输入数量"></u-input>
		</view>
    </view>

	<view class="enter-button">
		<button class="smell-button" @click="confirmFinish">确认验收</button>
	</view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        barCode: '',
        result: {},
        dataForm: {
          billingStatus: '',
          checkCaseQty: 0,
          checkPieceQty: 0,
          result: '',
          actualQty: '',
          // 件数
          actualCaseQty: 0,
          // 零散数
          actualPieceQty: 0,
          batchNo: '',
          productionDate: '',
          expiredDate: '',
          storageId: '',
          billingStatus: '',
          reason: '',
          containerCode: ''
        },
        resust: false,
        billingStatusValue: '',
        resustValue: '',
        kubevalue: '',
        receonclusion: false,
        kube: false,
        startTimeValue: '请选择日期',
        starttime: false,
        endtime: false,
        endTimeValue: '请选择日期',
        storageList: [],
        recelist: [],
        resustlist: [],
        sid: '',
        detailId: '',
        recet: '',
        queryFlag: '',
        
      };
    },
    onLoad: function(options) {
      this.sid = options.sid
      this.detailId = options.did
      this.getList(options)
      // 收货结论
      this.getByDicReceivingStatus()
      // 处理原因
      this.getByDicAcceptanceReason()
      // 库别列表
      this.loadStorageList()
      //获取比例
      this.getCaseCheckRatio()
    },
    methods: {
      // 件数更改
      changeCase(e) {
        this.dataForm.checkCaseQty = Math.ceil(e * this.recet)
        this.dataForm.actualQty = e * this.result.businInReceivingDForPdaVo.bpPackageQty + this.dataForm.actualPieceQty
      },
      // 零散数的更改
      changePiece(e) {
        this.dataForm.checkPieceQty = e
        this.dataForm.actualQty = this.result.businInReceivingDForPdaVo.bpPackageQty * this.dataForm.actualCaseQty +
          Number(this.dataForm.actualPieceQty)
      },
      //获取比例
      getCaseCheckRatio() {
        this.$u.api.getCaseCheckRatio().then(res => {
          if (res.code === 200) {
            this.recet = res.msg
          } else {
            this.$u.toast(res.msg);
          }
        });
      },
      //收货结论
      getByDicReceivingStatus() {
        this.$u.api.getAcceptanceConclusion().then(res => {
          if (res.code === 200) {
            this.recelist = res.data.map((item) => ({
              label: item.dictLabel,
              value: item.dictValue
            }))
          } else {
            this.$u.toast(res.msg);
          }
        });
      },
      //处理原因
      getByDicAcceptanceReason() {
        this.$u.api.getByDicAcceptanceReason().then(res => {
          if (res.code === 200) {
            this.resustlist = res.data.map((item) => ({
              label: item.dictLabel,
              value: item.dictValue
            }))
          } else {
            this.$u.toast(res.msg);
          }
        });
      },
      loadStorageList() {
        this.storageList = [];
        this.$u.api.storageList().then(res => {
          let dataArry = res.data;
          for (var i = 0; i < dataArry.length; i++) {
            var obj = {
              value: dataArry[i].id,
              label: dataArry[i].storageName,
            };
            this.storageList.push(obj);
          }
        });
      },
      // 提报异常
      confirmError() {
        this.$u.api.acceptance.confirmError({
          id: this.sid
        }).then((res) => {
          if (res.code === 200) {
            this.$u.toast('提报异常成功');
            this.dataForm = {
              billingStatus: '',
              checkCaseQty: '',
              checkPieceQty: '',
              result: '',
              actualQty: '',
              actualCaseQty: '',
              actualPieceQty: '',
              batchNo: '',
              productionDate: '',
              expiredDate: '',
              storageId: '',
              billingStatus: '',
              reason: '',
              containerCode: '',
              checkCaseQty: ''
            }
          } else {
            this.$u.toast(res.msg)
          }
        })
      },
      // 验收：
      confirmFinish() {
        if (!this.result.businInReceivingDForPdaVo) {
          this.$u.toast('请输入或扫描要验收的商品');
          return
        }
        // // 0整散分开 1整散合一
        // if (this.result.businInReceivingDForPdaVo.bgStorageClassify === 0 && this.dataForm.actualPieceQty && this
        //   .dataForm.actualCaseQty) {
        //   this.$u.toast('整件和零散数不能同时录入');
        //   return
        // }
        // if (this.result.businInReceivingDForPdaVo.bgStorageClassify === 1 && this.dataForm.actualPieceQty === 0 && this
        //   .dataForm.actualCaseQty === 0) {
        //   this.$u.toast('整件或零散数不能同时为0');
        //   return
        // }
        // // 如果为不合格要填写处理原因
        // if (this.billingStatusValue === '不合格' && !this.dataForm.reason) {
        //   this.$u.toast('请选择处理原因');
        //   return
        // }
        // if (!this.dataForm.billingStatus) {
        //   this.$u.toast('请选择收货结论');
        //   return
        // }
        // if(this.billingStatusValue==='合格'&&this.dataForm.reason) {
        // 	this.$u.toast('收货结论为');
        // 	return
        // }
        // 批号 有效期 生产日期 容器编号  抽检数量信息
        if (!this.dataForm.containerCode) {
          this.$u.toast('请扫码或输入容器号');
          return
        }
        // if (!this.dataForm.batchNo) {
        //   this.$u.toast('请输入批号');
        //   return
        // }
        // if (!this.dataForm.productionDate) {
        //   this.$u.toast('请选择生产日期');
        //   return
        // }
        // if (!this.dataForm.expiredDate) {
        //   this.$u.toast('请选择有效期至');
        //   return
        // }
        this.$u.api.acceptance.confirmBilling({
          id: this.sid,
          businInReceivingDetailTransBo: {
            ...this.dataForm,
            id: this.detailId
          }
        }).then(res => {
          if (res.code === 200) {
            this.$u.toast('验收成功');
            this.result = {}
          } else {
            this.$u.toast(res.msg);
          }
        });
      },
      queryFun() {
        if (this.queryFlag !== '') {
          clearTimeout(this.queryFlag);
        }
        if (this.barCode == '' || this.barCode == null || this.barCode ==
          undefined) {
          return
        }
        this.queryFlag = setTimeout(() => {
          this.getList({
            barCode: this.barCode,
            // did: this.detailId,
            sid: this.sid
          })
        }, 500)
      },
      getList(params) {
        this.$u.api.acceptance.getReceiving(params).then(res => {
          if (res.code === 200) {
            this.result = res.data
            if (!this.result.businInReceivingDForPdaVo) {
              this.$u.toast('当前商品待验收信息')
            }
            //生产日期
            this.startTimeValue = res.data.businInReceivingDForPdaVo.productionDate
            this.dataForm.productionDate = res.data.businInReceivingDForPdaVo.productionDate
            // 有限期至
            this.endTimeValue = res.data.businInReceivingDForPdaVo.expiredDate
            this.dataForm.expiredDate = res.data.businInReceivingDForPdaVo.expiredDate
            //库别
            this.dataForm.storageId = res.data.businInReceivingDForPdaVo.storageId
            this.dataForm.containerCode = res.data.businInReceivingDForPdaVo.containerCode
            this.dataForm.batchNo = res.data.businInReceivingDForPdaVo.batchNo
            const storageInfo = this.storageList.find((item) => Number(item.value) === Number(res.data
              .businInReceivingDForPdaVo.storageId))
            if (storageInfo) {
              this.kubevalue = storageInfo.label
            }
          }
        });
      },
      choosedStartDate() {
        this.starttime = true
      },
      changedate(e) {
        this.startTimeValue = e.result
        this.dataForm.productionDate = e.result
        this.starttime = false
      },
      choosedDate(e) {
        this.endtime = true
      },
      change(e) {
        this.endTimeValue = e.result
        this.endtime = false
        this.dataForm.expiredDate = e.result
      },
      // 确定库别
      confirmKube(e) {
        this.kube = false
        this.kubevalue = e[0].label
        this.dataForm.storageId = e[0].value
      },
      // 验收结果
      confirmClusion(e) {
        this.receonclusion = false
        this.billingStatusValue = e[0].label
        this.dataForm.billingStatus = e[0].value
      },
      // 处理原因
      confirmResult(e) {
        this.resust = false
        this.resustValue = e[0].label
        this.dataForm.reason = e[0].value
      }
    },
    onUnload() {
      var pages = getCurrentPages(); //当前页面栈
      if (pages.length > 1) {
        var beforePage = pages[pages.length - 1]; //获取上一个页面实例对象
        beforePage._data.refreshIfNeeded = true;
      }
    }

  }
</script>

<style lang="scss">
  .goodsNumber {
    padding: 20rpx 50rpx;
    display: flex;
    align-items: center;

    .goodsNumberCode {
      color: #333333;
      font-size: 36rpx;
      margin-right: 20rpx;
    }
  }

  .goodsInfo {
    width: 95%;
    margin: 0 auto;
    background-color: #f2f2f2;
    padding: 20rpx 30rpx;
    line-height: 40rpx;
    margin-bottom: 20rpx;

    .goodsInfoItem {
      display: flex;
      align-items: center;
      color: #333333;

      .goodsItemLabel {
        width: 120rpx;
        font-size: 24rpx;
      }

      .goodsItemValue {
        font-size: 28rpx;
        font-weight: 600;
      }
    }

    .itemsContend {
      display: flex;
      justify-content: space-between;

      .items {
        width: 50%;
        display: flex;
        font-size: 24rpx;
        color: #555555;

        .itemsLabel {
          width: 120rpx;
        }

        .itemsValue {}
      }
    }
  }

  .titleValue {
    display: flex;
    align-items: center;
    font-size: 32rpx;
    padding: 0 50rpx;
    margin-bottom: 20rpx;

    .title {
      color: #555555;
      width: 170rpx;
      text-align: left;
    }

    .value {
      color: #666666;
    }
  }
  .enter-button{
  		display: flex;
  		flex: 2;
  		height: 70px;
  		width: 100%;
  		z-index: 100px;
  		background-color: #fff;
  		position: fixed;
  		bottom: 0px;
  		.smell-button{
  			display: block;
  			color: #fff;
  			margin: 19px auto;
  			height: 35px;
  			line-height: 35px;
  			width: 45%;
  			background-color: #19be6b;
  			
  		}
  	}

  .footer {
    padding: 20rpx 40rpx;
    display: flex;
    justify-content: space-between;
  }
</style>
