<template>
	<gui-page :custom-header="true" :is-header-sized="false" :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>
			<div 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 @focus="handleInputFocus" placeholder="扫描采购订单" @confirm="handleKeydown" />
						<text class="font-icons" @click="handleMapass">&#xe6b7;</text>
					</view>
				</view>
				<view v-if="cardList?.length > 0" class="list-panel">
					<div v-for="(item, key) in cardList" :key="key" class="panel"
						@click="handleNavigateToPrintLabel(item)">
						<uni-swipe-action>
							<!-- 基础用法 -->
							<uni-swipe-action-item :right-options="options" @click="handleRemoveRecord(item)">
								<template #left>
									<view />
								</template>
								<div class="panel-row">
									<span>采购订单</span>
									<span>{{ item.purchaseOrderNo }}</span>
								</div>
								<div class="panel-row">
									<span>供应商</span>
									<span>{{ item.supplierName }}</span>
								</div>
								<div class="panel-row">
									<span>物料编码</span>
									<span>{{ item.materialNo }}</span>
								</div>
								<div class="panel-row">
									<span>收货数量</span>
									<span>{{ item.receiptQty }}</span>
								</div>
								<div class="panel-row">
									<span>物料批次</span>
									<span>{{ item.materialLots }}</span>
								</div>
							</uni-swipe-action-item>
						</uni-swipe-action>
					</div>
				</view>
				<view v-else>
					<view class="bg-image">
						<image src="@/static/empty.png" mode="heightFix" />
						<text>这里什么都没有...</text>
					</view>
				</view>
			</div>
		</template>
	</gui-page>
</template>
<script>
	import {
		computed,
		defineComponent,
		onBeforeMount,
		ref
	} from 'vue'
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	export default defineComponent({
		setup() {
			const scanNumber = ref('')
			const errorState = ref(0)
			const easyinput = ref()
			const errorTip = ref('')
			const errorTipMessage = ref('')
			const queryParams = ref({
				pageSize: 10,
				pageNo: 1,
				purchaseOrderNo: ''
			})
			const cardList = ref([])

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

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

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

			// 扫描采购订单标签输入框回车事件
			const handleKeydown = function(e) {
				queryParams.value.pageNo = 1;
				queryParams.value.purchaseOrderNo = e;
				search();
			}

			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) {
							queryParams.value.pageNo = 1;
							queryParams.value.purchaseOrderNo = ret.resp_result;
							search();
						}
					})
				// #endif
			}

			const handleRemoveRecord = function(item) {
				uni.$reqDelete('removeRegisterRecord', {
						id: item?.id
					})
					.then(({
						code,
						msg
					}) => {
						if (code === 0) {
							uni.showToast({
								title: '删除成功',
								icon: 'none',
								duration: 2000
							})
							search()
						} else {
							uni.showToast({
								title: msg,
								icon: 'none',
								duration: 2000
							})
						}
					})
				// cardList.value.splice(index, 1)
			}

			// 禁用软键盘
			const handleInputFocus = function() {
				// #ifdef APP-PLUS
				setTimeout(() => {
					uni.hideKeyboard()
				}, 100)
				// #endif
			}

			const handleNavigateToPrintLabel = function(ret) {
				const parentData = JSON.parse(JSON.stringify(ret))
				const params = {
					...parentData,
					detailList: []
				}
				uni.$reqGet('getIncomingLabelPrintDetail', {
						materialLots: ret?.materialLots ?? ''
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (code === 0) {
							params.detailList = data?.list ?? [];
							uni.navigateTo({
								url: `/pages/workbranch/warehouse/materialRegister/Record/recordPrint?params=${JSON.stringify(params)}`
							})
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = msg
							errorTip.value.open()
							errorState.value = 0
						}
					})
			}

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

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

			// uniapp移动端触底事件
			onReachBottom(() => {
				queryParams.value.pageNo += 1
				uni.$reqGet('getIncomingReceiptPage', queryParams.value)
					.then(({
						code,
						data,
						msg
					}) => {
						if (code === 0) {
							Array.prototype.push.call(cardList.value, ...data?.list ?? [])
							setInputFocus();
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = msg
							errorTip.value.open()
							errorState.value = 0
						}
					})
			})

			return {
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				goBack,
				easyinput,
				errorTip,
				errorTipMessage,
				scanNumber,
				handleMapass,
				handleKeydown,
				handleRemoveRecord,
				cardList,
				handleInputFocus,
				handleNavigateToPrintLabel
			}
		}
	})
</script>

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

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

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

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

	.font-icons {
		width: 40px;
		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;
			}
		}
	}
</style>