<template>
	<view class="page-list pressure-filter">
		<!-- 筛选区域 -->
		<view class="filter-section">
			<!-- 单位工程选择 -->
			<view class="filter-item">
				<text class="filter-label">单位工程</text>
				<u-input
					type="select"
					v-model="unitWorkName"
					readonly
					:clearable="false"
					@click="handleShowUnitPopup"
					placeholder="请选择单位工程"
				></u-input>
			</view>

			<!-- 试压条件选择 -->
			<view class="filter-item">
				<text class="filter-label">试压条件</text>
				<u-radio-group v-model="addInfo.isFinish" @change="onFinishChange">
					<u-radio :name="true" label="具备" active-color="#3577FF" shape="square"></u-radio>
					<u-radio :name="false" label="不具备" active-color="#3577FF" shape="square"></u-radio>
				</u-radio-group>
			</view>

			<!-- 搜索框 -->
			<view class="filter-item search-item">
				<u-search
					v-model="addInfo.testPackageNo"
					placeholder="搜索试压包号..."
					@search="getTestPackageNo"
					:show-action="false"
					shape="square"
				></u-search>
			</view>
		</view>

		<!-- 试压包列表 -->
		<scroll-view class="list-scroll" scroll-y>
			<view class="list-wrap">
				<view v-for="(item, index) in itemList" :key="index" class="card-item" @click="goDetail(item)">
					<view class="card-header">
						<text class="card-title">{{ item.BaseInfoCode }}</text>
					</view>
					<view class="card-body">
						<view class="card-row">
							<text class="row-label">单位工程</text>
							<text class="row-value">{{ addInfo.unit_name || '--' }}</text>
						</view>
						<view class="card-row">
							<text class="row-label">试压条件</text>
							<text class="row-value">{{ addInfo.isFinish_name || '--' }}</text>
						</view>
					</view>
				</view>

				<!-- 空状态 -->
				<u-empty v-if="!loading && itemList.length === 0" mode="list" text="暂无数据"></u-empty>
			</view>
		</scroll-view>

		<!-- 单位工程选择弹窗 -->
		<nbd-select v-model="showUnitPopup" :show-search="true" title="请选择单位工程" :list="unitList"
			label-key="BaseInfoName" v-model:model="selectedUnit" value-key="BaseInfoId" @confirm="handleUnitConfirm" />


	</view>
</template>

<script setup>
	import { ref, reactive, computed } from 'vue'
	import { onShow } from '@dcloudio/uni-app'
	import { useUserStore } from '@/store'
	import { storeToRefs } from 'pinia'
	import { reqProjectWorkArea } from '@/api/base.js'
	import { reqTestPackageNoList } from '@/api/hj.js'

	const userStore = useUserStore()
	const { currentProject } = storeToRefs(userStore)

	// 表单数据
	const addInfo = reactive({
		unit_name: '',
		unitWorkId: '',
		BaseInfoCode: '',
		isFinish: false,
		isFinish_name: '否',
		testPackageNo: ''
	})

	// 弹窗状态
	const showUnitPopup = ref(false)

	// 选中值
	const selectedUnit = ref(null)

	// 单位工程名称（计算属性）
	const unitWorkName = computed(() => selectedUnit.value?.BaseInfoName || '')

	// 列表数据
	const itemList = ref([])
	const unitList = ref([])
	const loading = ref(false)

	// 点击单位工程下拉
	const handleShowUnitPopup = () => {
		reqProjectWorkArea(currentProject.value.ProjectId).then(res => {
			unitList.value = res.data || []
			showUnitPopup.value = true
		})
	}

	// 选择单位工程
	const handleUnitConfirm = (item) => {
		selectedUnit.value = item
		addInfo.unit_name = item.BaseInfoName
		addInfo.unitWorkId = item.BaseInfoId
		addInfo.BaseInfoCode = item.BaseInfoCode
		getTestPackageNo()
	}

	// 试压条件变更
	const onFinishChange = (value) => {
		addInfo.isFinish_name = value ? '是' : '否'
		getTestPackageNo()
	}

	// 跳转详情页
	const goDetail = (item) => {
		const params = {
			value: item
		}
		uni.navigateTo({
			url: `/pipe/pressure/pressure_before/detail?params=${JSON.stringify(params)}`
		})
	}

	// 获取单位工程列表
	const getUnitList = async () => {
		try {
			const projectId = currentProject.value.ProjectId || ''
			const res = await reqProjectWorkArea(projectId)
			if (res.code === 1) {
				unitList.value = res.data
			}
		} catch (error) {
			console.error('获取单位工程失败:', error)
		}
	}

	// 获取试压包批号列表
	const getTestPackageNo = async () => {
		if (!addInfo.unitWorkId) return
		
		loading.value = true
		try {
			const res = await reqTestPackageNoList(addInfo.unitWorkId, addInfo.isFinish, addInfo.testPackageNo)
			if (res.code === 1) {
				itemList.value = res.data
			}
		} catch (error) {
			console.error('获取试压包批号失败:', error)
		} finally {
			loading.value = false
		}
	}

	// 页面显示时加载数据
	onShow(() => {
		getUnitList()
	})
</script>