<template>
	<gui-page :custom-header="true" :is-header-sized="false"
		:header-class="['gui-theme-background-color', isLandscapeScreen?'width: 100vmax;':'']"
		:style="[isLandscapeScreen?'width: 100vmax':'']">
		<template #gHeader>
			<view :style="[isLandscapeScreen?'height:44px;width: 100vmax':'height:44px;']"
				class="gui-flex gui-nowrap gui-rows gui-align-items-center">
				<!-- 使用组件实现返回按钮及返回首页按钮 -->
				<text style="font-size:44rpx;" class="gui-header-leader-btns gui-color-white font-icons"
					@tap="goBack">&#xe6c5;</text>
				<!-- 导航文本此处也可以是其他自定义内容 -->
				<text
					class="gui-h4 gui-blod gui-flex1 gui-text-center gui-color-white gui-ellipsis gui-primary-text">来料登记</text>
				<!-- 此处加一个右侧展位元素与左侧同宽，实现标题居中 -->
				<!-- 实际宽度请根据自己情况设置 -->
				<view style="width:40px;" />
			</view>
		</template>
		<template #gBody>
			<view class="form">
				<view class="card-list-flexbox">
					<text class="card-list-title" :style="[isPad?'font-size: 18px':'font-size: 14px']">单据信息</text>
					<view class="card-list-item">
						<text>送货单号</text>
						<text>{{ scanParams.deliveryOrderNo }}</text>
					</view>
					<view class="card-list-item">
						<text>供应商编码</text>
						<text>{{ scanParams.supplierCode }}</text>
					</view>
					<view class="card-list-item">
						<text>供应商名称</text>
						<text>{{ scanParams.supplierName }}</text>
					</view>
					<view class="card-list-item">
						<text>备注</text>
						<text>{{ scanParams.remark }}</text>
					</view>
				</view>
				<view class="row">
					<view class="row-card">
						<uni-easyinput ref="easyinput" v-model="scanParams.deliveryOrderNo" :input-border="false"
							:clearable="false" type="text" focus @focus="handleInputFocus" placeholder="扫描送货单条码"
							@confirm="confirmDeliverOrderNo" />
						<text class="font-icons" @click="handleMapass">&#xe6b7;</text>
					</view>
				</view>
				<view v-if="scanParams.deliveryOrderNo" class="row">
					<view class="row-card">
						<uni-easyinput ref="easyinput2" v-model="scanParams.deliverBatch" :input-border="false"
							:clearable="false" type="text" @focus="handleInputFocus" placeholder="扫描物料条码"
							@confirm="confirmMaterial" />
						<text class="font-icons" @click="handleScanMaterial">&#xe6b7;</text>
					</view>
				</view>
				<view class="table-title">
					<span>需求清单</span>
				</view>
				<view class="custom-table">
					<uni-table border stripe empty-text="暂无更多数据">
						<!-- 表头行 -->
						<uni-tr class="custom-table-head">
							<uni-th align="center" width="80px">数量</uni-th>
							<uni-th align="center" width="140px">采购单号</uni-th>
							<uni-th align="center" width="140px">物料编码</uni-th>
							<uni-th align="center" width="140px">送货批次</uni-th>
							<uni-th align="center" width="140px">生产日期</uni-th>
						</uni-tr>
						<!-- 表格数据行 -->
						<uni-tr v-for="(item, key) in scanParams.detail" :key="key">
							<uni-td align="center"
								:style="item.state? 'background-color: rgba(109, 205, 50, 1);color: white;': ''">{{ item.receiptQty }}</uni-td>
							<uni-td align="center"
								:style="item.state? 'background-color: rgba(109, 205, 50, 1);color: white;': ''">{{ item.purchaseOrderNo }}</uni-td>
							<uni-td align="center"
								:style="item.state? 'background-color: rgba(109, 205, 50, 1);color: white;': ''">{{ item.materialNo }}</uni-td>
							<uni-td align="center"
								:style="item.state? 'background-color: rgba(109, 205, 50, 1);color: white;': ''">{{ item.deliverBatch }}</uni-td>
							<uni-td align="center"
								:style="item.state? 'background-color: rgba(109, 205, 50, 1);color: white;': ''">{{ item.produceDate }}</uni-td>
						</uni-tr>
					</uni-table>
				</view>
				<gui-right-menus>
					<!-- 扩展按钮 -->
					<template #menus-more>
						<view hover-class="gui-tap"
							class="menu-items gui-bg-green gui-flex gui-columns gui-justify-content-center"
							@click="handeleRegister">
							<text class="menu-text gui-block gui-text-center gui-color-white">登记</text>
						</view>
						<view hover-class="gui-tap"
							class="menu-items gui-bg-blue gui-flex gui-columns gui-justify-content-center"
							@click="handleNavigateToRegisterRecord">
							<text class="menu-text gui-block gui-text-center gui-color-white">登记记录</text>
						</view>
					</template>
					<!-- 核心按钮 -->
					<template #menus-primary>
						<view class="menu-items gui-bg-primary gui-flex gui-columns gui-justify-content-center">
							<text class="menu-icon gui-color-white gui-block gui-text-center gui-icons">&#xe614;</text>
							<text class="menu-text gui-color-white gui-block gui-text-center">操作</text>
						</view>
					</template>
				</gui-right-menus>
				<uni-popup ref="modalForm" type="dialog">
					<uni-popup-dialog type="error" cancel-text="不使用" confirm-text="继续使用" title="提示" content="此物料已过保质期!"
						@confirm="handleCompleteDialog" @close="handleCloseDialog" />
				</uni-popup>
				<uni-popup ref="errorTip" type="dialog">
					<uni-popup-dialog type="error" cancel-text="关闭" confirm-text="确认" title="提示"
						:content="errorTipMessage" @confirm="handleCloseErrorTipsModal"
						@close="handleCloseErrorTipsModal" />
				</uni-popup>
			</view>
		</template>
	</gui-page>
</template>

<script>
	import {
		defineComponent,
		ref
	} from 'vue'
	export default defineComponent({
		setup() {
			const modalForm = ref('')
			const errorTip = ref('')
			const errorTipMessage = ref('')
			const easyinput = ref('')
			const easyinput2 = ref('')
			const errorState = ref(0)
			const fdIndex = ref(0)
			const scanParams = ref({
				deliveryOrderNo: '',
				supplierCode: '',
				supplierName: '',
				remark: '',
				deliverBatch: '',
				validateFlag: -1,
				detail: []
			})

			const handleMapass = function() {
				// uni.$reqGet("scanDeliveryOrderNo", {
				// 		deliveryOrderNo: "CGSH230907000007"
				// 	})
				// 	.then(({
				// 		code,
				// 		data,
				// 		msg
				// 	}) => {
				// 		if (code === 0) {
				// 			Object.assign(scanParams.value, data);
				// 			setInputFocus2();
				// 		} else {
				// 			// #ifdef APP-PLUS
				// 			plus.device.beep(2);
				// 			// #endif
				// 			errorTipMessage.value = msg;
				// 			errorTip.value.open();
				// 			errorState.value = 0;
				// 		}
				// 	})
				// #ifdef APP-PLUS
				const mpaasScanModule = uni.requireNativePlugin('Mpaas-Scan-Module')
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						// errorTipMessage.value = ret.resp_code + "-" + ret.resp_result;
						// errorTip.value.open();
						// errorState.value = -1;
						if (ret.resp_code === 1000) {
							uni.$reqGet('scanDeliveryOrderNo', {
									deliveryOrderNo: ret.resp_result
								})
								.then(({
									code,
									data,
									msg
								}) => {
									if (code === 0) {
										Object.assign(scanParams.value, data)
										setInputFocus2()
									} else {
										// #ifdef APP-PLUS
										plus.device.beep(2)
										// #endif
										errorTipMessage.value = msg
										errorTip.value.open()
										errorState.value = 0
									}
								})
						}
					})
				// #endif
			}

			// 扫描送货单输入框回车事件
			const confirmDeliverOrderNo = function(e) {
				uni.$reqGet('scanDeliveryOrderNo', {
						deliveryOrderNo: e
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (code === 0) {
							Object.assign(scanParams.value, data)
							setInputFocus2()
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = msg
							errorTip.value.open()
							errorState.value = 0
						}
					})
			}

			const handleScanMaterial = function() {
				// #ifdef APP-PLUS
				const mpaasScanModule = uni.requireNativePlugin('Mpaas-Scan-Module')
				mpaasScanModule.mpaasScan({
						// 扫码识别类型，参数可多选，qrCode、barCode，不设置，默认识别所有
						'scanType': ['qrCode', 'barCode'],
						// 是否隐藏相册，默认false不隐藏
						'hideAlbum': false
					},
					(ret) => {
						if (ret.resp_code === 1000) {
							uni.$reqGet('scanRegisterMaterial', {
									batchNumber: ret.resp_result
								})
								.then(({
									code,
									data,
									msg
								}) => {
									scanParams.value.deliverBatch = data?.deliverBatch
									if (code === 0) {
										fdIndex.value = scanParams.value.detail.findIndex(item => item
											?.deliverBatch ===
											data?.deliverBatch)
										if (fdIndex.value !== -1) {
											if (data?.ifExpired === 1) {
												// 物料超过保质期
												modalForm.value.open()
											} else {
												setInputFocus2()
												scanParams.value.detail[fdIndex.value].state = true
											}
										} else {
											// #ifdef APP-PLUS
											plus.device.beep(2)
											// #endif
											errorTipMessage.value = '请扫描正确的物料'
											errorTip.value.open()
											errorState.value = 1
										}
									} else {
										// #ifdef APP-PLUS
										plus.device.beep(2)
										// #endif
										errorTipMessage.value = msg
										errorTip.value.open()
										errorState.value = 1
									}
								})
						}
					})
				// #endif
			}

			// 扫描物料输入框回车事件
			const confirmMaterial = function(e) {
				uni.$reqGet('scanRegisterMaterial', {
						batchNumber: e
					})
					.then(({
						code,
						data,
						msg
					}) => {
						scanParams.value.deliverBatch = data?.deliverBatch
						if (code === 0) {
							fdIndex.value = scanParams.value.detail.findIndex(item => item
								?.deliverBatch ===
								data?.deliverBatch)
							if (fdIndex.value !== -1) {
								if (data?.ifExpired === 1) {
									// 物料超过保质期
									modalForm.value.open()
								} else {
									setInputFocus2()
									scanParams.value.detail[fdIndex.value].state = true
								}
							} else {
								// #ifdef APP-PLUS
								plus.device.beep(2)
								// #endif
								errorTipMessage.value = '请扫描正确的物料'
								errorTip.value.open()
								errorState.value = 1
							}
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = msg
							errorTip.value.open()
							errorState.value = 1
						}
					})
			}

			const handleNavigateToRegisterRecord = function() {
				uni.navigateTo({
					url: '/pages/workbranch/warehouse/materialRegister/Record/registerRecord'
				})
			}

			// 登记
			const handeleRegister = function() {
				let isTrue = false
				if (scanParams.value.detail.length === 0) {
					// #ifdef APP-PLUS
					plus.device.beep(2)
					// #endif
					errorTipMessage.value = '登记数据不能为空'
					errorTip.value.open()
					errorState.value = 0
					return
				}
				if (scanParams.value.validateFlag === 0) {
					isTrue = true
				} else {
					isTrue = scanParams.value.detail.every(item => item.state === true)
				}

				if (isTrue) {
					uni.showLoading({
						title: '数据请求中',
						duration: 20000,
						mask: true
					})

					uni.$reqPost('incomingReceiptMasterCreate', scanParams.value)
						.then(({
							code,
							data,
							msg
						}) => {
							scanParams.value = {
								deliveryOrderNo: '',
								supplierCode: '',
								supplierName: '',
								remark: '',
								deliverBatch: '',
								validateFlag: -1,
								detail: []
							}
							if (code === 0) {
								uni.showToast({
									title: '来料登记成功',
									icon: 'none',
									duration: 2000
								})
							} else {
								// #ifdef APP-PLUS
								plus.device.beep(2)
								// #endif
								errorTipMessage.value = msg
								errorTip.value.open()
								errorState.value = -1
							}
						})
						.finally(() => {
							uni.hideLoading()
						})
				} else {
					// #ifdef APP-PLUS
					plus.device.beep(2)
					// #endif
					errorTipMessage.value = '请填写完整需求清单'
					errorTip.value.open()
					errorState.value = 1
				}
			}
			const handleCompleteDialog = function() {
				setInputFocus2()
				scanParams.value.detail[fdIndex.value].state = true
				modalForm.value.close()
			}
			const handleCloseDialog = function() {
				setInputFocus2()
				modalForm.value.close()
			}
			const goBack = function() {
				uni.$goBack('/pages/workbranch/warehouse/materialRegister/registerType')
			}

			const setInputFocus = function() {
				scanParams.value.deliveryOrderNo = ''
				easyinput.value.onBlur()
				easyinput.value.onFocus()
			}

			const setInputFocus2 = function() {
				scanParams.value.deliverBatch = ''
				easyinput2.value.onBlur()
				easyinput2.value.onFocus()
			}

			// 关闭错误信息弹窗
			const handleCloseErrorTipsModal = async function() {
				errorTip.value.close()
				if (errorState.value === 0) {
					await setInputFocus()
				} else if (errorState.value === 1) {
					await setInputFocus2()
				}
			}

			// 禁用软键盘
			const handleInputFocus = function() {
				setTimeout(() => {
					uni.hideKeyboard()
				}, 100)
			}

			return {
				goBack,
				modalForm,
				easyinput,
				easyinput2,
				errorState,
				errorTip,
				errorTipMessage,
				scanParams,
				handleMapass,
				handleInputFocus,
				handleCompleteDialog,
				handleCloseDialog,
				handleScanMaterial,
				handeleRegister,
				confirmMaterial,
				confirmDeliverOrderNo,
				handleCloseErrorTipsModal,
				handleNavigateToRegisterRecord
			}
		}
	})
</script>

<style lang="scss" scoped>
	.gui-header-leader-btns {
		color: black;
		font-size: 24px !important;
	}

	.gui-sbody {
		background-color: rgba(234, 239, 242, 1);
	}

	.form {
		margin-top: 85px;
		font-size: 14px;
	}

	.row {
		height: 55px;
		display: flex;
		justify-content: center;
		padding-left: 20px;
		padding-right: 20px;
		margin: 10px 0;
		background-color: white;

		.row-card {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			width: calc(375px - 30px);
		}
	}

	.input-200 {
		width: 200px;
		padding-left: 10px;
	}

	.font-icons {
		width: 40px;
		font-size: 20px;
		text-align: right;
	}

	.card-list-flexbox {
		width: calc(100vw - 24px);
		height: 195px;
		margin: 0 12px;
		display: flex;
		justify-content: center;
		flex-direction: column;
		align-items: center;
		overflow-y: scroll;

		.card-list-title {
			width: 100%;
			height: 35px;
			line-height: 35px;
			margin: 0 4px 0 4px;
			border-radius: 4px 4px 0 0;
			text-align: center;
			color: white;
			background-color: rgba(0, 160, 233, 1.0);
		}

		.card-list-item,
		.card-list-item-operation {
			width: 100%;
			height: 35px;
			margin: 0 4px 3rpx 4px;
			border-radius: 4px;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			background-color: #fff;

			uni-text {
				font-size: 14px;
				height: 50rpx;
				text-align: left;
				padding: 0 8px;
				display: flex;
				flex-direction: row;
				align-items: center;
			}

			.text-1 {
				flex: 1;
				height: 35px;
				justify-content: flex-start;
			}

			.text-2 {
				flex: 3;
				height: 35px;
				justify-content: flex-end;
				margin-right: 4px;
				padding: 2px 6px;
			}

		}

	}

	.table-title {
		height: 40px;
		line-height: 40px;
		margin: 4px 0 -3px 0;
		padding: 0 12px;
		font-size: 16px;
		font-weight: bold;
		background-color: white;
	}

	.custom-table {
		height: calc(100vh - 405px);
		min-height: 230px;
		margin: 5px 0;
		overflow-y: scroll;
	}
</style>