<template>
    <view class="page">
	<NavBar :title="$t('成品入库')"></NavBar>
	<view class="infoedit">
		<u-form ref="form" :modelValue="form" class="login-form" :labelWidth="190">
			<u-form-item prop="boxCode" :label="$t('箱标签：')">
				<input
					v-model="form.boxCode"
					type="text"
					:placeholder="$t('请输入或扫描箱标签')"
					@blur="queryByBoxCode"
					@confirm="queryByBoxCode"
				>
				</input>
			</u-form-item>
		<u-form-item prop="workOrderType" :label="$t('工单单别：')">
			<input
				v-model="form.workOrderType"
				type="text"
				:placeholder="$t('请输入工单单别')"
			>
			</input>
		</u-form-item>
		<u-form-item prop="workOrderNo" :label="$t('工单单号：')">
			<input
				v-model="form.workOrderNo"
				type="text"
				:placeholder="$t('请输入工单单号')"
			>
			</input>
		</u-form-item>
			<u-form-item prop="quantity" :label="$t('数量：')">
				<input
					v-model="form.quantity"
					type="text"
					disabled
				>
				</input>
			</u-form-item>
		<u-form-item style="width:100%;">
		<button
			size="medium"
			type="primary"
			style="width:95%;margin-top: 50rpx"
			@click="submit()"
			:disabled="!form.boxCode || !form.workOrderType || !form.workOrderNo || !form.quantity"
		>
			<span>{{$t('确定入库')}}</span>
		</button>
		</u-form-item>
		</u-form>
	</view>
    </view>
  </template>
  
  <script>
import i18n from '../../lang/index'

  export default {
    data() {
      return {
	form: {
		boxCode: null,
		workOrderType: '5101',
		workOrderNo: null,
		quantity: null,
	},
	queryDetails: [],  // 保存查询到的明细数据
	loading: false
      };
    },
    methods: {
		// 根据箱标签查询入库数量
		queryByBoxCode(e) {
			const boxCode = e && e.detail ? e.detail.value : this.form.boxCode
			if (!boxCode || !boxCode.trim()) {
				return
			}
			
			if (this.loading) {
				return
			}
			
		this.loading = true
		this.$u.api.queryByBoxCode({ boxCode: boxCode.trim() }).then(res => {
			this.loading = false
			if (res.success && res.data && res.data.length > 0) {
				// 保存查询到的明细数据
				this.queryDetails = res.data
				
				// 数量：统计数组长度（行数）或累加 snqty
				// 方式1：统计行数
				this.form.quantity = res.data.length
				
			} else {
				this.$u.toast(res.errorMessage || i18n.t('查询箱标签信息失败，未找到数据'))
				this.clearForm()
			}
		}).catch(err => {
			this.loading = false
			this.$u.toast(i18n.t('查询失败'))
			this.clearForm()
		})
		},
		
	clearForm() {
		this.form.workOrderType = null
		this.form.workOrderNo = null
		this.form.quantity = null
		this.queryDetails = []
	},
		
	submit() {
		if (!this.form.boxCode) {
			return this.$u.toast(i18n.t('请输入箱标签'))
		}
		if (!this.form.workOrderType) {
			return this.$u.toast(i18n.t('请输入工单单别'))
		}
		if (!this.form.workOrderNo) {
			return this.$u.toast(i18n.t('请输入工单单号'))
		}
		if (!this.form.quantity) {
			return this.$u.toast(i18n.t('请先查询箱标签信息'))
		}
		if (!this.queryDetails || this.queryDetails.length === 0) {
			return this.$u.toast(i18n.t('未找到查询明细数据'))
		}
		
		if (this.loading) {
			return
		}
		
		// 构建标签明细：从查询结果中提取数据
		const storageDetails = this.queryDetails.map(item => ({
			workOrderType: this.form.workOrderType,  // 工单单别（表单录入）
			workOrderNo: this.form.workOrderNo,      // 工单单号（表单录入）
			boxCode: item.psnno || '',               // 箱码（查询明细）
			labelNo: item.snNo || ''                 // 小标签号（查询明细）
		}))
		
		// 构建请求参数
        //批号截取最后两位：JA00000021-175352500F-250905-000001
        var boxCodes = this.form.boxCode.split("-")
        var batchNo = boxCodes[2] + boxCodes[3];
		const params = {
			details: [{
				tg014: this.form.workOrderType,  // 工单单别
				tg015: this.form.workOrderNo,    // 工单单号
				tg013: this.form.quantity,       // 验收数量
				tg011: this.form.quantity,       // 入库数量
				tg017: batchNo                   // 批号（箱标签）
			}],
			storageDetails: storageDetails       // 标签明细（从查询结果生成）
		}
		
		this.loading = true
		this.$u.api.moctfcreate(params).then(res => {
			this.loading = false
			if(res.success) {
				this.$u.toast(i18n.t('入库成功'))
				// 清空表单
				this.form = {
					boxCode: null,
					workOrderType: null,
					workOrderNo: null,
					quantity: null,
				}
				this.queryDetails = []
			} else {
				this.$u.toast(res.errorMessage || i18n.t('入库失败'))
			}
		}).catch(err => {
			this.loading = false
			this.$u.toast(i18n.t('入库失败'))
		})
	}
    },
  };
  </script>
  
  <style lang="scss" scoped>
  .infoedit {
	padding: 20rpx 20rpx;
	.u-border-bottom:after {
		display: none;
	}
	::v-deep .uni-select{
		border: none !important;
	}
  }
  </style>


