<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="goHome">&#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="scanBatchNumber" :input-border="false"
							:clearable="false" type="text" focus @focus="handleInputFocus" placeholder="请扫描来源批次号"
							@confirm="confirmBatchNumber" />
						<text class="font-icons" style="font-size: 20px;" @click="handleMapass">&#xe6b7;</text>
					</view>
				</view>
				<view class="tabs">
					<div class="tabs-list">
						<text :class="[checkedTabs===0?'tabs-item-active':'tabs-item']"
							@click="handleCheckTabs(0)">待分切</text>
						<text :class="[checkedTabs===1?'tabs-item-active':'tabs-item']"
							@click="handleCheckTabs(1)">任务中</text>
						<text :class="[checkedTabs===2?'tabs-item-active':'tabs-item']"
							@click="handleCheckTabs(2)">已完成</text>
					</div>
				</view>
				<view v-if="cardList?.length > 0" class="list-panel">
					<view v-for="item in cardList" :key="item.id" class="panel" @click="handleToScanMaterials(item)">
						<view class="panel-row">
							<text>{{ item.ebillNo }}</text>
							<text v-if="checkedTabs===0 || checkedTabs===2" class="text-2 font-icons">分切&#xe66b;</text>
						</view>
						<view class="panel-row">
							<text>创建人</text>
							<text>{{ item.creator }}</text>
						</view>
						<view class="panel-row">
							<text>创建时间</text>
							<text>{{ $parseTime(item.createTime) }}</text>
						</view>
					</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 {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		ref,
		defineComponent,
		computed,
		onMounted
	} from 'vue'
	export default defineComponent({
		setup() {
			const errorState = ref(0)
			const easyinput = ref()
			const errorTip = ref('')
			const errorTipMessage = ref('')
			const scanBatchNumber = ref('')
			const checkedTabs = ref(0)
			const queryParams = ref({
				pageSize: 8,
				pageNo: 1,
				slitStatus: 0
			})
			const cardList = ref([])

			onMounted(() => {
				search()
			})

			const search = function() {
				uni.$reqGet('getCuttingList', queryParams.value)
					.then(({
						data,
						msg
					}) => {
						cardList.value = data?.list ?? []
					})
			}

			const goHome = function() {
				uni.$goHome()
			}
			const handleToScanMaterials = function(params) {
				uni.navigateTo({
					url: `/pages/workbranch/production/cutting/cuttingCheckout?params=${JSON.stringify(params)}`
				})
			}
			const handleCheckTabs = function(item) {
				switch (item) {
					case 0:
						queryParams.value.slitStatus = 0
						break
					case 1:
						queryParams.value.slitStatus = 1
						break
					case 2:
						queryParams.value.slitStatus = 2
						break
				}
				queryParams.value.pageNo = 1
				checkedTabs.value = item
				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) {
							uni.$reqGet('scanOrderNo', {
									qrCode: ret.resp_result
								})
								.then(({
									data,
									msg
								}) => {
									if (data != null && Object.keys(data).length > 0) {
										uni.navigateTo({
											url: `/pages/workbranch/production/cutting/cuttingCheckout?params=${JSON.stringify({ ...data, qrCode: ret.resp_result })}`
										})
									} else {
										// #ifdef APP-PLUS
										plus.device.beep(2)
										// #endif
										errorTipMessage.value = '此来源批次号不正确！'
										errorTip.value.open()
										errorState.value = 0
									}
								})
						}
					})
				// #endif
				// #ifdef H5
				uni.$reqGet('scanOrderNo', {
						qrCode: '20230630000013-0001-001'
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (data != null && Object.keys(data).length > 0) {
							uni.navigateTo({
								url: `/pages/workbranch/production/cutting/cuttingCheckout?params=${JSON.stringify({ ...data, qrCode: '20230630000013-0001-001' })}`
							})
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = '此来源批次号不正确！'
							errorTip.value.open()
							errorState.value = 0
						}
					})
				// #endif
			}

			const confirmBatchNumber = function(e) {
				uni.$reqGet('scanOrderNo', {
						qrCode: e
					})
					.then(({
						code,
						data,
						msg
					}) => {
						if (data != null && Object.keys(data).length > 0) {
							uni.navigateTo({
								url: `/pages/workbranch/production/cutting/cuttingCheckout?params=${JSON.stringify({ ...data, qrCode: e })}`
							})
						} else {
							// #ifdef APP-PLUS
							plus.device.beep(2)
							// #endif
							errorTipMessage.value = '此来源批次号不正确！'
							errorTip.value.open()
							errorState.value = 0
						}
					})
			}

			const setInputFocus = function() {
				scanBatchNumber.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)
			}

			// uniapp移动端触底事件
			onReachBottom(() => {
				queryParams.value.pageNo += 1
				uni.$reqGet('getCuttingList', queryParams.value)
					.then(({
						data
					}) => {
						Array.prototype.push.call(cardList.value, ...data?.list ?? [])
					})
			})
			return {
				goHome,
				cardList,
				checkedTabs,
				easyinput,
				errorState,
				errorTip,
				errorTipMessage,
				scanBatchNumber,
				handleInputFocus,
				handleMapass,
				handleCheckTabs,
				confirmBatchNumber,
				handleToScanMaterials,
				handleCloseErrorTipsModal
			}
		}
	})
</script>

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

	.list-content {
		margin-top: 80px;
		background-color: #edeeee;
	}

	.tabs {
		position: fixed;
		top: 72px;
		left: 0;
		width: 100%;
		height: 45px;
		display: flex;
		align-items: flex-end;
		padding: 0 2px;
		z-index: 9;
		background-color: white;
		
		.tabs-list {
			border-radius: 3px;
			overflow: hidden;
		}

		.tabs-item {
			display: inline-block;
			width: 60px;
			height: 30px;
			line-height: 30px;
			padding: 0 8px;
			font-size: 14px;
			font-weight: bold;
			text-align: center;
			color: black;
			border-bottom: 1.5px dashed #00a0e9;
			transition: all .5s ease-in-out;
		}

		.tabs-item-active {
			position: relative;
			display: inline-block;
			width: 60px;
			height: 30px;
			line-height: 30px;
			padding: 0 8px;
			font-size: 14px;
			font-weight: bold;
			text-align: center;
			color: white;
			border-left: 1px solid #00a0e9;
			border-top: 1px solid #00a0e9;
			border-right: 1px solid #00a0e9;
			border-bottom: 1.5px solid #00a0e9;
			border-radius: 5px 5px 0 0;
			animation: .3s linear show;
			background-color: #00a0e9;
		}

		.tabs-item-active::before {
			content: '';
			position: absolute;
			left: -10px;
			bottom: 0;
			width: 10px;
			height: 10px;
			background: radial-gradient(circle at 0% 25%, transparent 10px, #00a0e9 0)
		}

		.tabs-item-active::after {
			content: '';
			position: absolute;
			right: -10px;
			bottom: 0;
			width: 10px;
			height: 10px;
			background: radial-gradient(circle at 100% 25%, transparent 10px, #00a0e9 0)
		}
	}

	@keyframes show {
		from {
			transform: translateY(5%);
		}

		to {
			transform: translateY(0%);
		}
	}

	.list-panel {
		padding: 5px 8px;
		margin-top: 92px;
		overflow-y: scroll;
	}

	.scan {
		position: fixed;
		top: 116px;
		left: 0;
		height: 45px;
		width: calc(100% - 24px);
		padding: 0 12px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border-top: 1px solid #edeeee;
		border-bottom: 1px solid #edeeee;
		z-index: 9;
		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>