<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"
					style="font-size: 1rem;">备料单列表</text>
				<!-- 此处加一个右侧展位元素与左侧同宽，实现标题居中 -->
				<!-- 实际宽度请根据自己情况设置 -->
				<view style="width:40px;" />
				<!-- 如果右侧有其他内容可以利用条件编译和定位来实现-->
			</view>
		</template>
		<template #gBody>
			<view class="list-content">
				<view class="tabs">
					<div class="tabs-list">
						<text :class="[checkedTabs===-1?'tabs-item-active':'tabs-item']" @click="handleCheckTabs(-1)">多单备料</text>
						<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>
					</div>
				</view>
				<view v-show="checkedTabs===0" 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;width: 40px;text-align: right;"
							@click="handleMapass">&#xe6b7;</text>
					</view>
				</view>
				<view v-show="checkedTabs===1" class="scan">
					<view class="scan-card">
						<uni-easyinput ref="easyinput2" v-model="queryParams.keyWord" :input-border="false"
							:clearable="false" type="text" focus @focus="handleInputFocus"
							placeholder="生产订单号/领料单号/产品编码/物料编码搜索" @confirm="confirmKeyWord" />
						<text class="font-icons" style="font-size: 20px;width: 20px;text-align: right;"
							@click="search(1, 'out')">&#xe6b5;</text>
					</view>
				</view>
				<view v-show="checkedTabs===0" style="margin-top: 90px;">
					<view v-if="cardList?.length > 0" class="list-panel">
						<div v-for="(item, key) in cardList" :key="key" class="panel"
							@click="handleToScanMaterial(item)">
							<div class="panel-row">
								<span>{{ item.erpOrderNo }}</span>
								<span class="font-icons">前往备料&#xe66b;</span>
							</div>
							<div class="panel-row">
								<span>产品名称</span>
								<span>{{ item.materialName }}</span>
							</div>
							<div class="panel-row">
								<span>备料状态</span>
								<span>{{ computedMaterialPreparationState(item.prepareStatus) }}</span>
							</div>
						</div>
					</view>
					<view v-else>
						<view class="bg-image">
							<image src="@/static/empty.png" mode="heightFix" />
							<text>这里什么都没有...</text>
						</view>
					</view>
				</view>
				<view v-show="checkedTabs===1" style="margin-top: 90px;">
					<view v-if="cardList?.length > 0" class="list-panel">
						<div v-for="(item, key) in cardList" :key="key" class="panel">
							<div class="panel-row">
								<span>{{ item.workOrderBomNo }}</span>
							</div>
							<div class="panel-row">
								<span>产品编码</span>
								<span>{{ item.erpMaterialId }}</span>
							</div>
							<div class="panel-row">
								<span>产品名称</span>
								<span>{{ item.materialName }}</span>
							</div>
							<div class="panel-row">
								<span>发料数量</span>
								<span style="color: orange;font-weight: bold;">{{ item.batchQty * 1 }}</span>
							</div>
						</div>
					</view>
					<view v-else>
						<view class="bg-image">
							<image src="@/static/empty.png" mode="heightFix" />
							<text>这里什么都没有...</text>
						</view>
					</view>
				</view>
				<view v-if="checkedTabs===-1" style="margin-top: 45px;">
					<view v-if="cardList?.length > 0" class="list-panel">
						<div v-for="(item, key) in cardList" :key="key" class="panel"
							@click="handleToScanMaterials(item)">
							<div class="panel-row">
								<span>{{ item.prepareOrderNo }}</span>
								<span class="font-icons">前往备料&#xe66b;</span>
							</div>
							<div class="panel-row">
								<span>备料状态</span>
								<span>{{ computedState(item.syntheticalStatus) }}</span>
							</div>
							<div class="panel-row">
								<span>生产订单</span>
								<span>{{ item.workOrderNo }}</span>
							</div>
						</div>
					</view>
					<view v-else>
						<view class="bg-image">
							<image src="@/static/empty.png" mode="heightFix" />
							<text>这里什么都没有...</text>
						</view>
					</view>
				</view>
			</view>
		</template>
	</gui-page>
</template>

<script>
	import {
		onReachBottom
	} from '@dcloudio/uni-app'
	import {
		ref,
		defineComponent,
		computed,
		onMounted
	} from 'vue'
	export default defineComponent({
		setup() {
			const easyinput = ref()
			const easyinput2 = ref()
			const checkedTabs = ref(-1)
			const queryParams = ref({
				pageSize: 10,
				pageNo: 1,
				keyWord: ''
			})
			const scanBatchNumber = ref('')
			const cardList = ref([])

			const computedState = computed(() => {
				return item => {
					switch (item) {
						case 0:
							return '待备料'
						case 1:
							return '已备料'
						case 2:
							return '已发料'
					}
				}
			})

			// 订单备料 备料状态
			const computedMaterialPreparationState = computed(() => {
				return item => {
					switch (item) {
						case 0:
							return '未备料'
						case 1:
							return '已备料'
						case 2:
							return '已发料'
						case -1:
							return '部分备料'
					}
				}
			})

			const search = function(pageNo, state) {
				if (state === 'out') {
					uni.$reqGet('getOutStockMainDetailPage', {
							...queryParams.value,
							pageNo
						})
						.then(({
							data
						}) => {
							cardList.value = data?.list
						})
				} else if (state === 'materials') {
					uni.$reqGet('getPrepareMatersList', {
							...queryParams.value,
							pageNo
						})
						.then(({
							data,
							msg
						}) => {
							cardList.value = data?.list ?? []
						})
				} else {
					uni.$reqGet('getProductionWorkOrderList')
						.then(({
							data,
							msg
						}) => {
							cardList.value = data?.list ?? []
						})
				}
			}

			onMounted(() => {
				search(1, 'materials')
			})

			const goBack = function() {
				uni.removeStorageSync('workOrderId')
				uni.removeStorageSync('masterId')
				uni.$goHome()
			}
			const handleToScanMaterial = function(params) {
				uni.setStorageSync('workOrderId', JSON.stringify({
					id: params?.erpOrderNo
				}))
				uni.navigateTo({
					url: `/pages/workbranch/warehouse/production/scanMaterials`
				})
			}

			const handleToScanMaterials = function(params) {
				uni.setStorageSync('masterId', JSON.stringify({
					id: params?.id
				}))
				uni.navigateTo({
					url: `/pages/workbranch/warehouse/production/scanMixMaterials`
				})
			}

			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.setStorageSync('workOrderId', JSON.stringify({
								id: ret.resp_result
							}))
							uni.navigateTo({
								url: `/pages/workbranch/warehouse/production/scanMaterials`
							})
						}
					})
				// #endif
				// #ifdef H5
				uni.setStorageSync('workOrderId', JSON.stringify({
					id: '60'
				}))
				uni.navigateTo({
					url: `/pages/workbranch/warehouse/production/scanMaterials`
				})
				// #endif
			}

			// 扫描生产订单二维码输入框回车事件
			const confirmBatchNumber = function(e) {
				uni.setStorageSync('workOrderId', JSON.stringify({
					id: e
				}))
				uni.navigateTo({
					url: `/pages/workbranch/warehouse/production/scanMaterials`
				})
			}

			// 生产发料单搜索输入框回车事件
			const confirmKeyWord = function(e) {
				search(1, 'out')
			}

			const handleCheckTabs = function(item) {
				queryParams.value.pageNo = 1
				queryParams.value.keyWord = ''
				cardList.value = []
				checkedTabs.value = item
				if (item === 0) {
					search()
					easyinput.value.onBlur()
					easyinput.value.onFocus()
				} else if (item === 1) {
					search(1, 'out')
					easyinput2.value.onBlur()
					easyinput2.value.onFocus()
				} else {
					search(1, 'materials')
				}
			}

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

			// uniapp移动端触底事件
			onReachBottom(() => {
				queryParams.value.pageNo += 1
				if (checkedTabs.value === 0) {
					uni.$reqGet('getProductionWorkOrderList', queryParams.value)
						.then(({
							data
						}) => {
							Array.prototype.push.call(cardList.value, ...data?.list ?? [])
						})
				} else if (checkedTabs.value === 1) {
					uni.$reqGet('getOutStockMainDetailPage', queryParams.value)
						.then(({
							data
						}) => {
							Array.prototype.push.call(cardList.value, ...data?.list ?? [])
						})
				} else {
					uni.$reqGet('getPrepareMatersList', queryParams.value)
						.then(({
							data,
							msg
						}) => {
							Array.prototype.push.call(cardList.value, ...data?.list ?? [])
						})
				}
			})
			return {
				goBack,
				cardList,
				search,
				easyinput,
				easyinput2,
				computedState,
				computedMaterialPreparationState,
				queryParams,
				checkedTabs,
				scanBatchNumber,
				confirmBatchNumber,
				confirmKeyWord,
				handleMapass,
				handleCheckTabs,
				handleInputFocus,
				handleToScanMaterial,
				handleToScanMaterials
			}
		}
	})
</script>

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

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

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

	.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: flex;
			align-items: center;

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

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

	
	.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: 72px;
			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: 72px;
			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%);
		}
	}
</style>