<template>
	<view class="page-container uni-column">
		<view class="option-container uni-column">
			<!-- tab-bar -->
			<view class="tab-bars uni-row">
				<view :class="currentTabName === 1 ? 'active' : ''" @click.stop="handleTabBarClick(1)">
					<text>检查指导书</text>
					<view class="line"></view>
				</view>
				<view :class="currentTabName === 2 ? 'active' : ''" @click.stop="handleTabBarClick(2)">
					<text>分选标准</text>
					<view class="line"></view>
				</view>
			</view>
			<!-- 搜索框 -->
			<view class="search-container uni-row">
				<input type="text" v-model="keywords" placeholder="请输入编号/关键字" />
				<view class="btn">搜索</view>
			</view>
			<!-- 选项 -->
			<view class="option-item" v-for="(item, index) in optionList" :key="index"
				@click="handleOptionChecked(item)">
				<view class="uni-row">
					<view class="label">检查项</view>
					<view class="value">{{ item.title }}</view>
				</view>
				<view class="uni-row">
					<view class="label">检查标准</view>
					<view class="value">{{ item.standard }}</view>
				</view>
				<uni-icons class="arrow-right" type="right" size="24" />
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		onMounted,
		getCurrentInstance
	} from 'vue';
	import {
		onLoad,
		onReady,
		onUnload,
		onShow
	} from '@dcloudio/uni-app'
	import {
		getInspectionList
	} from '@/api/business/sortDaywork.js'

	const currentTabName = ref(1)
	const keywords = ref('')
	const optionList = ref([])
	// 检查指导书
	const optionList1 = ref([])
	// 分选标准
	const optionList2 = ref([])
	const dayworkItem = ref({})
	onMounted(() => {
		const instance = getCurrentInstance().proxy
		const eventChannel = instance.getOpenerEventChannel();

		eventChannel.on('acceptDataFromOpenerPage', function(data) {
			// console.log('acceptDataFromOpenerPage', data)
			// 传入当前报工信息 通过当前报工的产品和工序获取检查指导书和分选标准
			if (data && data.data) {
				dayworkItem.value = data.data
				loadInspection({
					technologicalProcessId: dayworkItem.value.technologicalProcessId,
					processId: dayworkItem.value.processId
				})
			}
		})
	})
	// 页面生命周期函数
	onLoad(() => {
		// optionList1.value = [
		// 	{
		// 		title: '001 台阶径',
		// 		standard: '角偏 < 0.05'
		// 	},
		// 	{
		// 		title: '002 台阶径',
		// 		standard: '角偏 < 0.04'
		// 	}
		// ]

		// optionList2.value = [
		// 	{
		// 		title: '划痕',
		// 		standard: '划痕描述'
		// 	},
		// 	{
		// 		title: '划伤',
		// 		standard: '划伤描述'
		// 	}
		// ]

		// optionList.value = optionList1.value
	})

	const loadInspection = (data) => {
		// console.log(data)
		getInspectionList(data).then(res => {
			console.log(res)
			if (res.code === 200) {
				optionList1.value = res.data.instructions
				optionList2.value = res.data.standards
				handleTabBarClick(currentTabName.value)
				// console.log(optionList1.value, res.data.instructions)
				// console.log(optionList2.value, res.data.standards)
			} else {
				uni.showToast({
					icon: 'none',
					title: '分选标准和检查指导项获取失败'
				})
			}
		})
	}

	// tabbar切换
	const handleTabBarClick = (val) => {
		switch (val) {
			case 1:
				optionList.value = optionList1.value
				break
			case 2:
				optionList.value = optionList2.value
		}
		currentTabName.value = val
	}

	const handleOptionChecked = (data) => {
		uni.$emit('addUnfitInfoEvent', {
			id: data.id,
			title: data.title,
			standard: data.standard,
			type: data.type
		})
		uni.navigateBack()
	}
</script>

<style lang="scss">
	.page-container {
		height: 100%;
		background-color: #ececec;
		font-size: 28rpx;
		padding: 24rpx;
		box-sizing: border-box;
	}

	.option-container {
		background-color: #ffffff;
		padding: 24rpx;
		border-radius: 12rpx;
	}

	.tab-bars {
		height: 56rpx;

		>view {
			flex: 1;
			text-align: center;

			.line {
				width: 50%;
				height: 2px;
				margin: 8rpx auto 0 auto;
				background-color: #FFFFFF;
			}

			&.active {
				.line {
					background-color: #1684FC;
				}
			}
		}
	}

	.search-container {
		margin-top: 16rpx;
		align-items: center;

		input {
			flex: 1;
			height: 72rpx;
			border: 1px solid #bbbbbb;
			padding: 0 8rpx;
			box-sizing: border-box;
		}

		.btn {
			display: flex;
			flex-direction: row;
			width: 112rpx;
			height: 72rpx;
			align-items: center;
			justify-content: center;
			background-color: #1684FC;
			color: #ffffff;
		}
	}

	.option-item {
		position: relative;
		margin-top: 24rpx;
		padding-bottom: 8rpx;
		border-bottom: 1px solid #BBBBBB;

		.uni-row {
			padding-bottom: 16rpx;

			.label {
				width: 144rpx;
			}

			.value {
				flex: 1;
			}
		}

		.uni-row:first-child {
			font-size: 32rpx;
			font-weight: 700;
		}

		.arrow-right {
			position: absolute;
			top: 24rpx;
			right: 24rpx;
		}
	}
</style>