<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="tg001" :label="$t('入库单别：')">
				<input
					v-model="form.tg001"
					type="text"
				>
				</input>
			</u-form-item>
			<!-- <u-form-item prop="tg010" :label="$t('类型：')">
				<uni-data-select
				v-model="form.tg010"
				:clear="false"
				:localdata="typeData"
				></uni-data-select>
			</u-form-item> -->
			<u-form-item prop="tg014" :label="$t('工单单别：')">
				<input
					v-model="form.tg014"
					type="text"
					@blur="inputCC"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tg015" :label="$t('工单单号：')">
				<input
					v-model="form.tg015"
					focus
					type="text"
					@blur="inputCC"
				>
				</input>
			</u-form-item>
			<!-- <u-form-item prop="tc006" :label="$t('移出加工顺序:')">
				<input
					v-model="form.tc006"
					
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tb005" :label="$t('移出地：')">
				<input
					v-model="form.tb005"
					
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item> -->
			<u-form-item prop="tg010" :label="$t('仓库：')">
				<input
					v-model="form.tg010"
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tg036" :label="$t('库位：')">
				<input
					v-model="form.tg036"
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<!-- <u-form-item prop="tb008" :label="$t('移入地：')">
				<input
					v-model.number="form.tb008"
					
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item> -->
			<u-form-item prop="batchNo" :label="$t('批号：')">
				<input
					v-model.number="form.batchNo"
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tg011" :label="$t('数量：')">
				<input
					v-model="form.tg011"
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<!-- <u-form-item prop="tc020" :label="$t('使用人时：')">
				<input
					v-model="form.tc020"
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tc021" :label="$t('使用机时：')">
				<input
					v-model="form.tc021"
					type="text"
					@confirm="submit"
				>
				</input>
			</u-form-item> -->
			<u-form-item style="width:100%;">
			<button
				size="medium"
				type="primary"
				style="width:95%;margin-top: 50rpx"
				@click="submit()"
			>
				<span>{{$t('确定')}}</span>
			</button>
			</u-form-item>
		</u-form>
	</view>
    </view>
  </template>
  
  <script>
import i18n from '../../lang/index'

  import StickyNavBar from "@/components/StickyNavBar/index.vue";
  import ContentLoadingMore from "@/components/ContentLoadingMore/index.vue";
  export default {
    components: {
      StickyNavBar,
      ContentLoadingMore,
    },
	onLoad(option) {
		if (option.info) {
			let data = JSON.parse(decodeURIComponent(option.info));
			this.index = option.index
			this.info = data
			this.form = data.moctis[this.index]
			this.form['tg014'] = this.form['tg014'] ? this.form['tg014'].trim() : ''
			this.form['tg015'] = this.form['tg015'] ? this.form['tg015'].trim() : ''
			// this.form['tc006'] = this.form['tc006'] ? this.form['tc006'].trim() : ''
			this.form['tg036'] = this.form['tg036'] ? this.form['tg036'].trim() : ''
			this.form['tg010'] = this.form['tg010'] ? this.form['tg010'].trim() : ''
		}
	},
    data() {
      return {
		info: null,
		index: 0,
		typeData: [{
				text: '1:正常完成',
				value: '1'
			},
			{
				text: '2:返工完成',
				value: '2'
			},
			{
				text: '3:退回返工',
				value: '3'
			},
			{
				text: '4:拨转',
				value: '4'
			},
			{
				text: '5:盘盈盘亏',
				value: '5'
			},
			{
				text: '6:投入',
				value: '6'
			}
		],
		form: {
			tg010: null,
			tg001: '5801',
			tg014: '510',
			tg015: null,
			// tc006: null,
			// tb005: null,
			tg036: null,
			// tb008: null,
			// tc032: null,
			tg011: null,
			batchNo: null,
			// tc020: null,
			// tc021: null,
		}
      };
    },
    created() {
    },
    methods: {
		inputCC(e) {
			const val = e && e.detail ? String(e.detail.value || '').trim() : ''
			if (val && val.includes('-')) {
				const arr = val.split('-')
				if (arr.length >= 2) {
					this.form.tg014 = arr[0].trim()
					this.form.tg015 = arr[1].trim()
				}
			}
			this.queryMoctaAndGenerateBatch()
		},
		applyMoctaDefaults(moctaData) {
			if (!moctaData) return
			if (moctaData.batchNo) {
				this.form.batchNo = moctaData.batchNo
			}
			if (moctaData.tg010) {
				this.form.tg010 = String(moctaData.tg010).trim()
			} else if (moctaData.ta020) {
				this.form.tg010 = String(moctaData.ta020).trim()
			}
			if (moctaData.tg036) {
				this.form.tg036 = String(moctaData.tg036).trim()
			}
			if (moctaData.tg011 != null && moctaData.tg011 !== '') {
				this.form.tg011 = moctaData.tg011
			} else {
				const qty = this.calcRemainQty(moctaData)
				if (qty != null) {
					this.form.tg011 = qty
				}
			}
		},
		calcRemainQty(moctaData) {
			const ta015 = Number(moctaData.ta015) || 0
			const ta017 = Number(moctaData.ta017) || 0
			const remain = ta015 - ta017
			if (remain > 0) return remain
			if (ta015 > 0) return ta015
			return null
		},
		clearMoctaDefaults() {
			this.form.batchNo = ''
			this.form.tg036 = ''
			this.form.tg011 = ''
		},
		// 查询工单信息并带出批号、库位、数量
		queryMoctaAndGenerateBatch() {
			const tg014 = this.form.tg014 ? String(this.form.tg014).trim() : ''
			const tg015 = this.form.tg015 ? String(this.form.tg015).trim() : ''
			if (!tg014 || !tg015) {
				return
			}
			this.form.tg014 = tg014
			this.form.tg015 = tg015

			const params = {
				ta001: tg014,
				ta002: tg015
			}

			this.$u.api.getMocta(params).then(res => {
				if (res.success && res.data) {
					this.applyMoctaDefaults(res.data)
				} else {
					this.clearMoctaDefaults()
					this.$u.toast(i18n.t('未找到工单信息'))
				}
			}).catch(err => {
				this.clearMoctaDefaults()
				this.$u.toast(i18n.t('查询工单失败'))
			})
		},
		submit(row) {
			if (!this.form['tg001']) {
				return this.$u.toast(i18n.t('请输入入库单别'))
			}
			if (!this.form['tg014']) {
				return this.$u.toast(i18n.t('请输入工单单别'))
			}
			if (!this.form['tg015']) {
				return this.$u.toast(i18n.t('请输入工单单号'))
			}
			// if (!this.form['tc006']) {
			// 	return this.$u.toast(i18n.t('请输入移出加工顺序'))
			// }
			// if (!this.form['tb005']) {
			// 	return this.$u.toast(i18n.t('请输入移出地'))
			// }
			if (!this.form['tg010']) {
				return this.$u.toast(i18n.t('请输入仓库'))
			}
			if (!this.form['tg036']) {
				return this.$u.toast(i18n.t('请输入库位'))
			}
			// if (!this.form['tb008']) {
			// 	return this.$u.toast(i18n.t('请输入移入地'))
			// }
			if (!this.form['tg011']) {
				return this.$u.toast(i18n.t('请输入入库数量'))
			}
			this.$u.api.createforstorage(this.form).then(res => {
				if(res.success) {
					this.$u.toast(i18n.t('入库成功'))
					this.form =  {
						tg010: null,
						tg001: '5801',
						tg014: '510',
						tg015: null,
						// tc006: null,
						// tb005: null,
						tg036: null,
						// tb008: null,
						// tc032: null,
						tg011: null,
						batchNo: null,
						// tc020: null,
						// tc021: null,
					}
				}
			})
		}
    },
  };
  </script>
  
  <style lang="scss" scoped>
  ::v-deep .u-sticky {
    top: 0 !important;
    background: #fff;
  }
  .infoedit {
	padding: 20rpx 20rpx;
	.underline {
		border-bottom: 1px solid #d5d5d5;
		padding-bottom: 0px;
    	line-height: 40px;
		font-weight: 500;
		margin-left: 20rpx;
		font-size: 19px;
	}
	.u-border-bottom:after {
		display: none;
	}
	::v-deep .uni-select{
		border: none !important;
	}
	
 }
  
  </style>
  