<template>
	<view class="page-list">
		<!-- 操作按钮区域 -->
		<view class="action-bar">
			<view class="action-btn" @click="handleAutoPoint">自动点口</view>
			<view class="action-btn" @click="handleForceClose">强制关闭批</view>
			<view class="action-btn" @click="handleAdjust">点口调整</view>
		</view>

		<!-- 批号信息 -->
		<view class="batch-info">
				<text class="batch-label">批号</text>
				<text class="batch-value">{{ PointBatchCode || '--' }}</text>
				<view class="batch-refresh" @click="handleRefresh">
					<u-icon name="reload" :size="36" :class="{ rotating: loading }"></u-icon>
				</view>
			</view>

		<!-- 列表内容 -->
		<view class="list-scroll">
			<!-- 加载中 -->
			<view v-if="loading" class="loading-box">
				<u-loading mode="circle" size="40"></u-loading>
				<text class="loading-text">加载中...</text>
			</view>
			<!-- 数据列表 -->
			<view v-else class="list-wrap">
				<view v-for="(item, index) in listData" :key="item.id || index" class="card-item">
					<view class="card-row">
						<text class="card-label">管线号</text>
						<text class="card-value">{{ item.PipelineCode || '--' }}</text>
					</view>
					<view class="card-row">
						<text class="card-label">焊口代号</text>
						<text class="card-value">{{ item.WeldJointCode || '--' }}</text>
					</view>
					<view class="card-row">
						<text class="card-label">状态</text>
						<view class="card-tag" :class="{ done: item.PointState == '1' }">
							{{ item.PointState == '1' ? '已点' : '未点' }}
						</view>
					</view>
				</view>

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

<script setup>
	import {
		ref
	} from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import { reqGetPointBatchDetail } from '@/api/hj.js'

	// ===== 参数 =====
	const pointBatchId = ref('')
	const PointBatchCode = ref('')

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

	// 加载数据
	const fetchData = async () => {
		if (loading.value) return
		loading.value = true

		try {
			const res = await reqGetPointBatchDetail(pointBatchId.value)
			listData.value = res.data || []
		} catch (error) {
			console.error('加载数据失败:', error)
			uni.showToast({ title: '加载失败，请重试', icon: 'none' })
		} finally {
			loading.value = false
		}
	}

	const handleRefresh = () => {
		fetchData()
	}

	// 自动点口
	const handleAutoPoint = async () => {
		// TODO: 调用自动点口 API
		uni.showToast({ title: '自动点口成功', icon: 'none' })
		fetchData()
	}

	// 强制关闭批
	const handleForceClose = async () => {
		// TODO: 调用强制关闭批 API
		uni.showToast({ title: '强制关闭批成功', icon: 'none' })
		fetchData()
	}

	// 点口调整
	const handleAdjust = () => {
		// TODO: 跳转到点口调整页面
		uni.showToast({ title: '功能开发中', icon: 'none' })
	}

	// ===== 生命周期 =====
	onLoad((opt) => {
		pointBatchId.value = opt.id
		PointBatchCode.value = opt.code
		fetchData()
	})
</script>
