<template>
	<gui-page :custom-header="true" :header-class="['gui-theme-background-color']">
		<template #gHeader>
			<view style="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-ellipsis gui-color-white gui-primary-text">采购单列表</text>
				<!-- 此处加一个右侧展位元素与左侧同宽，实现标题居中 -->
				<!-- 实际宽度请根据自己情况设置 -->
				<view style="width:40px;" />
				<!-- 如果右侧有其他内容可以利用条件编译和定位来实现-->
			</view>
		</template>
		<template #gBody>
			<view class="list-content">
				<view class="scan">
					<view class="scan-card">
						<uni-easyinput ref="easyinput" v-model="scanNumber" :input-border="false" :clearable="false"
							type="text" focus placeholder="扫描采购单号 / 供应商" @focus="handleInputFocus"
							@confirm="handleKeydown" />
						<text class="font-icons icon-scan" @click="handleMapass">&#xe6b7;</text>
					</view>
				</view>
				<view class="collapsed-panel" v-if="purchaseOrderList?.length > 0">
					<view class="panel" v-for="item in purchaseOrderList">
						<uni-collapse>
							<uni-collapse-item open :show-animation="true">
								<template v-slot:title>
									<div class="top">
										<div class="panel-row">
											<span>采购单</span>
											<span>{{ item.erpOrderNo }}</span>
										</div>
										<div class="panel-row">
											<span>供应商</span>
											<span>{{ item.supplierName }}</span>
										</div>
									</div>
								</template>
								<div class="content" v-for="detail in item?.detail"
									@click="handleNavigateToInfoDetail(item, detail)">
									<div class="panel-row">
										<span>物料编码</span>
										<span>{{ detail.materialNo }}</span>
									</div>
									<div class="panel-row">
										<span>采购数量</span>
										<span>{{ detail.materialQty }}</span>
									</div>
									<span class="font-icons">&#xe66b;</span>
								</div>
							</uni-collapse-item>
						</uni-collapse>
					</view>
				</view>
				<view v-else>
					<view class="bg-image">
						<image src="@/static/empty.png" mode="heightFix" />
						<text>这里什么都没有...</text>
					</view>
				</view>
			</view>
			<uni-popup ref="errorTip" type="dialog">
				<uni-popup-dialog type="error" cancel-text="关闭" confirm-text="确认" title="提示" :content="errorTipMessage"
					@confirm="handleCloseErrorTipsModal" @close="handleCloseErrorTipsModal" />
			</uni-popup>
		</template>
	</gui-page>
</template>

<script>
	import {
		defineComponent,
		onBeforeMount,
		ref
	} from 'vue'
	export default defineComponent({
		setup() {
			const modalForm = ref('')
			const errorState = ref(0)
			const easyinput = ref()
			const errorTip = ref('')
			const errorTipMessage = ref('')
			const purchaseOrderList = ref([])
			const scanNumber = ref('')

			onBeforeMount(() => {
				search()
			})

			const search = function(keyWord) {
				uni.$reqGet("getPurchaseOrderList", {
						keyWord: keyWord ?? ''
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (code === 0) {
							purchaseOrderList.value = data?.list ?? [];
							setInputFocus();
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = msg
							errorTip.value.open()
							errorState.value = 0
						}
					})
			}

			const handleMapass = 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) {
							search(ret.resp_result)
						}
					})
				// #endif
			}

			// 扫描完成品标签输入框回车事件
			const handleKeydown = function(e) {
				search(e)
			}

			const handleNavigateToInfoDetail = function(parent, detail) {
				const parentData = JSON.parse(JSON.stringify(parent))
				Reflect.deleteProperty(parentData, 'detail');
				const params = {
					...parentData,
					detail
				}
				uni.navigateTo({
					url: `/pages/workbranch/warehouse/materialRegister/Input/materialInfoDetails?params=${JSON.stringify(params)}`
				})
			}

			const goBack = function() {
				uni.$goBack('/pages/workbranch/warehouse/materialRegister/registerType')
			}

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

			// 关闭错误信息弹窗
			const handleCloseErrorTipsModal = async function() {
				errorTip.value.close()
				if (errorState.value === 0) {
					await setInputFocus()
				}
			}
			
			// 禁用软键盘
			const handleInputFocus = function() {
				setTimeout(() => {
					uni.hideKeyboard()
				}, 100)
			}

			return {
				goBack,
				easyinput,
				errorTip,
				errorTipMessage,
				handleInputFocus,
				modalForm,
				scanNumber,
				handleMapass,
				handleKeydown,
				purchaseOrderList,
				handleNavigateToInfoDetail,
				handleCloseErrorTipsModal
			}
		}
	})
</script>

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

	.list-content {
		margin-top: 80px;
		min-height: calc(100vh - 80px);
		background-color: #edeeee;
	}

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

	.icon-scan {
		font-size: 20px;
		text-align: right;
	}

	.scan {
		height: 45px;
		width: calc(100% - 48px);
		margin: 12px;
		padding: 0 12px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-radius: 6px;
		background-color: white;

		.scan-card {
			width: 100%;
			display: grid;
			grid-template-rows: 1fr;
			grid-template-columns: 7fr 2fr;
			align-items: center;

			input {
				height: 35px;
				line-height: 35px;
			}

			text {
				width: 100%;
				text-align: right;
			}
		}
	}

	span,
	text {
		font-size: 12px;
	}

	.collapsed-panel {
		// height: calc(100vh - 190px);
		// min-height: 230px;
		margin: 5px 0;
		padding: 0 8px;
		overflow-y: scroll;

		.panel {
			position: relative;
			margin: 8px 0;

			.top {
				font-weight: bold;
				border-top-left-radius: 4px;
				border-top-right-radius: 4px;
				background-color: white;

				.panel-row {
					line-height: 32px;
					padding: 0 12px;
					display: flex;
					justify-content: space-between;

					span:nth-of-type(1) {
						min-width: 30%;
					}

					span:nth-of-type(2) {
						min-width: 30%;
						padding-right: 8px;
						text-align: right;
						word-wrap: break-word;
						overflow-y: scroll;
					}

					span {
						color: rgba(0, 160, 233, 1);
					}
				}
			}

			.content {
				position: relative;
				overflow-y: scroll;
				border-top: 2px dashed rgba(237, 238, 238, 1);

				.panel-row {
					// height: 32px;
					line-height: 32px;
					padding: 0 12px;
					display: flex;
					justify-content: space-between;
					background-color: #fbfbfb;

					span:nth-of-type(1) {
						min-width: 30%;
					}

					span:nth-of-type(2) {
						min-width: 30%;
						padding-right: 8px;
						text-align: right;
						word-wrap: break-word;
						overflow-y: scroll;
					}
				}

				.font-icons {
					position: absolute;
					right: 2px;
					top: 50%;
					transform: translate(0, -50%);
					z-index: 1;
				}
			}
		}
	}
</style>