<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"
					disabled
					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: '01',
			tg001: '5801',
			tg014: '5101',
			tg015: null,
			// tc006: null,
			// tb005: null,
			tg036: null,
			// tb008: null,
			// tc032: null,
			tg011: null,
			batchNo: null,
			// tc020: null,
			// tc021: null,
		}
      };
    },
    created() {
    },
    methods: {
		inputCC(e){
			if(e && e.detail.value) {
				if(e.detail.value.includes("-")){
					const arr = e.detail.value.split("-")
					if (arr.length === 2) {
						this.form.tg014 = arr[0]
						this.form.tg015 = arr[1]
						// 查询工单信息并生成批次号
						this.queryMoctaAndGenerateBatch()
					}
				} else {
					// 查询工单信息并生成批次号
					this.queryMoctaAndGenerateBatch()
				}
			}
		},
		// 查询工单信息并生成批次号
		queryMoctaAndGenerateBatch() {
			if (!this.form.tg014 || !this.form.tg015) {
				return
			}
			
			const params = {
				ta001: this.form.tg014, // 工单单别
				ta002: this.form.tg015  // 工单单号
			}
			
			this.$u.api.getMocta(params).then(res => {
				if (res.success && res.data) {
					const moctaData = res.data
					// 生成批次号：品号（ta006）+工厂编号（ta019）+日期yyyyMMdd
					const today = new Date()
					const dateStr = today.getFullYear().toString() + 
						(today.getMonth() + 1).toString().padStart(2, '0') + 
						today.getDate().toString().padStart(2, '0')
					
					this.form.batchNo = (moctaData.ta006 || '').trim() + (moctaData.ta019 || '').trim() + dateStr
				} else {
          this.form.batchNo=""
					this.$u.toast(i18n.t('未找到工单信息'))
				}
			}).catch(err => {
        this.form.batchNo=""
				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: '01',
						tg001: '5801',
						tg014: '5101',
						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>
  