<template>
	<view class="inquire">
		<view class="orderNumber">
			<view class="form">
				<uni-forms :modelValue="orderNumber" ref="form">
					<uni-forms-item name="number" label="生产单号:">
						<uni-easyinput v-model="orderNumber.number" type="text" :disabled='disabledInp'
							placeholder="请输入生产单号" />
					</uni-forms-item>
					<uni-forms-item name="serial" label="物料编号:">
						<uni-easyinput v-model="orderNumber.serial" maxlength="6" :disabled='disabledInp' type="number"
							placeholder="请输入物料编号" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" class="button" @click="examine" :disabled='disabledInp'>查询</button>
			</view>
		</view>
		<view class="message">
			<view class="form">
				<uni-forms :modelValue="formData" ref="form1">
					<uni-forms-item name="time" label="推送日期:">
						<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.time" />
					</uni-forms-item>
					<uni-forms-item name="num" label="数量(箱/件):">
						<uni-easyinput v-model="formData.num" type="number" placeholder="请输入数量" />
					</uni-forms-item>
				</uni-forms>
				<button type="primary" class="button" :disabled="disabled" @click="pushMessage">推送</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getWO,
		pushWO
	} from "../../api/inquire.js"
	export default {
		data() {
			return {
				orderNumber: {
					number: null,
					serial: null
				},
				formData: {
					time: null,
					num: null,
				},
				disabled: true,
				disabledInp: false,
				ReferbillID: null,
				ReferbillItemID: null,
				bottleNum: null,
				rules: {
					number: {
						rules: [{
							required: true,
							errorMessage: '请输入生产单号',
						}]
					},
					serial: {
						rules: [{
							required: true,
							errorMessage: '请输入物料编号',
						}]
					},
					time: {
						rules: [{
							required: true,
							errorMessage: '请选择日期',
						}]
					},
					num: {
						rules: [{
							required: true,
							errorMessage: '请输入数量',
						}]
					},
				}
			};
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.form.setRules(this.rules)
			this.$refs.form1.setRules(this.rules)
		},
		methods: {
			// 查
			examine() {
				this.$refs.form.validate().then(res => {
					uni.showLoading({
						title: '查询中...'
					});
					this.orderNumber.number = res.number.replace(/\s+/g, "").toUpperCase()
					getWO({
						DocNum: this.orderNumber.number,
						Material: this.orderNumber.serial,
						accessToken: uni.getStorageSync('tokenInfo'),
						account: uni.getStorageSync('account')
					}).then(res1 => {
						uni.hideLoading();
						if (res1.data.success == 0) {
							uni.showToast({
								title: res1.data.message,
								icon: 'success',
								duration: 2000
							})
							this.ReferbillID = res1.data.data.ReferbillID
							this.ReferbillItemID = res1.data.data.ReferbillItemID
							this.bottleNum = res1.data.data.bottleNum
							this.disabled = false
							this.disabledInp = true
						} else {
							uni.showToast({
								title: res1.data.message,
								icon: 'none',
								duration: 2000
							})
						}
					}).catch(err1 => {
						uni.hideLoading();
					})

				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			},
			// 推
			pushMessage() {

				this.$refs.form1.validate().then(res => {

					uni.showLoading({
						title: '推送中...'
					});

					pushWO({
						boxNum: res.num,
						bottleNum: this.bottleNum,
						DocNum: this.orderNumber.number,
						ReferbillItemID: this.ReferbillItemID,
						ReferbillID: this.ReferbillID,
						Material: this.orderNumber.serial,
						SourceBillDate: res.time.replace(/\-/g, ""),
						accessToken: uni.getStorageSync('tokenInfo'),
						account: uni.getStorageSync('account')
					}).then(res1 => {
						if (res1.data.success == 0) {
							uni.hideLoading();

							uni.showToast({
								title: res1.data.message,
								icon: 'success',
								duration: 2000
							})
							this.orderNumber = {
								number: null,
								serial: null
							}
							this.formData = {
								time: null,
								num: null,
							}
							this.disabled = true
							this.disabledInp = false
						} else {
							uni.showToast({
								title: res1.data.message,
								icon: 'none',
								duration: 2000
							})
							this.disabledInp = false
						}
					}).catch(err1 => {
						uni.hideLoading();
					})
				}).catch(err => {
					uni.hideLoading()
					console.log('表单错误信息：', err);
				})
			},
		}
	}
</script>

<style lang="scss" lang="scss">
	.inquire {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;

		.orderNumber {
			width: 100%;
			height: 40%;
			position: relative;

			.form {
				width: 90%;
				position: absolute;
				top: 50%;
				left: 50%;
				transform: translate(-50%, -50%);

				.uni-forms ::v-deep {
					.uni-forms-item__label {
						width: 30% !important;
						text-align: right;
					}
				}
			}
		}

		.message {
			flex: 1;
			padding-top: 100rpx;
			box-sizing: border-box;

			.form {
				width: 90%;
				margin: 0 auto;

				.uni-forms ::v-deep {
					.uni-forms-item__label {
						width: 30% !important;
						text-align: right;
					}
				}

			}
		}

		p {
			span {
				font-size: 30rpx;
			}
		}

		.orderNumber {
			border-bottom: 1px solid #ccc;
		}
	}
</style>