<template>
    <view class="page">
	<NavBar :title="$t('修改领料单信息')" right="checkbox" @clickRight="submit"></NavBar>
	<view class="infoedit">
		<u-form ref="form" :modelValue="form" class="login-form" :labelWidth="160">
			<u-form-item prop="tb006" :label="$t('工艺：')" class="disabled">
				<input
					v-model="form.tb006"
					type="text"
					disabled
					
				>
				</input>
			</u-form-item>
			<u-form-item prop="tb003" :label="$t('材料品号：')" class="disabled">
				<input
					v-model="form.tb003"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tb012" :label="$t('材料品名：')" class="disabled">
				<input
					v-model="form.tb012"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tb004" :label="$t('需领数量：')" class="disabled">
				<input
					v-model="form.tb004"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<u-form-item prop="tb005" :label="$t('已领数量：')" class="disabled">
				<input
					v-model="form.tb005"
					disabled
					type="text"
					
					@confirm="submit"
				>
				</input>
			</u-form-item>
			<template v-if="!form.islot">
                <u-form-item prop="quantity" :label="$t('领料数量：')">
                    <input
                        v-model.number="form.quantity"
                        type="text"
                        
                        @confirm="submit"
                    >
                    </input>
                </u-form-item>
				<u-form-item prop="te025" :label="$t('库位：')">
					<input
						v-model="form.te025"
						type="text"
						@confirm="submit"
					>
					</input>
				</u-form-item>
			</template>
			<template v-else>
				<view style="display: flex;">
					<button
						size="medium"
						type="primary"
						style="margin-top: 10rpx;margin-right: 20rpx;"
						@click="addBatch()"
					>
						<span>{{$t('添加批次')}}</span>
					</button>
				</view>
				<view class="batchBox">
					<view v-for="(item, index) in info.moctes" :key="index"  @click="editBatch(item, index)">
						<view class="batchContent" v-if="item.batch && item.tb003 === form.tb003">
							<view class="itemtitle">
								<text>{{calckw(item.te010)}}</text>
								<view>
									<text style="color: #1f731f;">{{item.quantity}}</text>
								</view>
							</view>
							<view>
								<text>{{$t('库位')}}</text>
								<text>{{item.te025}}</text>
							</view>
						</view>
					</view>
				</view>
			</template>
			<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.moctes[this.index]
		}
	},
	computed: {
		chargaArr() {
			return this.info.moctes.filter(v => v.batch && v.tb003 === this.form.tb003)
		}
	},
    data() {
      return {
		info: null,
		index: 0,
		form: {
			cd003: null,
			tb003: null,
			tb012: null,
			tb004: null,
			cd008: null,
			tb005: null,
			cd019: null,
		}
      };
    },
    created() {
    },
    methods: {
        calckw(str){
			if(!str) return i18n.t('批号')
			if(typeof str === 'string' && !str.trim()) {
				return i18n.t('批号')
			}
			return str
		},
		addBatch() {
			const item = {
				tb003: this.form.tb003,
				tb006: this.form.tb006,
				ta001: this.form.ta001,
				ta002: this.form.ta002,
				ta006: this.form.ta006,
                tb004: this.form.tb004,
				batch: true,
				te010: this.form.te010,
				te025: this.form.te025,
				quantity: 0
			}
			const num = this.chargaArr.reduce((pre, cur) => {
				return pre + cur.quantity
			}, 0)
			uni.navigateTo({
              url: '/pages/moctcforcreate/Batch?info=' + encodeURIComponent(JSON.stringify(item)) + '&num=' + num
            });
		},
		editBatch(item, index) {
			const num = this.chargaArr.reduce((pre, cur) => {
				return pre + cur.quantity
			}, 0)
			uni.navigateTo({
              url: '/pages/moctcforcreate/Batch?info=' + encodeURIComponent(JSON.stringify(item))  + '&num=' + num + '&index=' + index
            });
		},
		getValue(item, index, splice = false){
			if (splice) return this.info.moctes.splice(index, 1)
			if(index === null) {
				this.info.moctes.push(item)
			} else {
				this.$set(this.info.moctes, index, item)
			}
		},
		submit(row) {
			if (!this.form.islot) {
				if (this.form['quantity'] && !this.form['te025']) {
					return this.$u.toast(i18n.t('请输入库位'))
				}
                if (!this.form['quantity'] && this.form['quantity'] !== 0) {
					return this.$u.toast(i18n.t('请输入领料数量'))
				}
				if (this.form['tb004'] || this.form['quantity']) {
                    console.log(this.form['tb004'], this.form['quantity'])
					const tb004 = this.form['tb004'] || 0
					const quantity = this.form['quantity'] || 0
					if (tb004 < quantity) return this.$u.toast(i18n.t('超出可领料数量'))
				}
			} else {
			}
			let pages = getCurrentPages() 
			
			// 2. 上一页面实例
				// 注意是length长度，所以要想得到上一页面的实例需要 -2
				// 若要返回上上页面的实例就 -3，以此类推
			let prevPage = pages[pages.length -2] 
			// 3. 给上一页面实例绑定getValue()方法和参数（注意是$vm）
			prevPage.$vm.getValue(this.info) 
			
			// 4. 返回上一页面
			uni.navigateBack({
				delta: 1 // 返回的页面数
			})
		}
    },
  };
  </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;
	}
	
  }
  .batchContent{
	color: #787878;
	font-size: 17px;
	width: 100%;
	padding: 15rpx;
	.itemtitle{
		font-weight: 600;
		font-size: 19px;
	}
	&>view{
		width: 100%;
		display: flex;
		justify-content: space-between;
	}
  }
  </style>
  