<template>
	<wd-tabbar fixed v-model="tabbar" shape="round">
	  <wd-tabbar-item title="指标考核主表" icon="cart"></wd-tabbar-item>
	  <wd-tabbar-item title="指标考核子表" icon="user"></wd-tabbar-item>
	</wd-tabbar>
	<wd-cell-group border v-if="tabbar == '0'">
		<wd-cell size="large" title="年月" :value="zbInfo.year + '年' + zbInfo.month + '月'" />
		<wd-cell size="large" title="考核指标" :value="zbInfo.assessmentIndicators" />
		<wd-cell size="large" title="指标类别" :value="zbInfo.indicatorCategory_dictText" />
		<wd-cell size="large" title="考核对象" :value="zbInfo.templetName_dictText" />
		<wd-cell size="large" title="考核周期" :value="zbInfo.assessmentCycle_dictText" />
		<wd-cell size="large" title="考核部门" :value="zbInfo.assessmentDepart_dictText" />
		<wd-collapse v-model="zbCollapse">
			<wd-collapse-item title="评价方法" name="评价方法">{{zbInfo.evaluationMethod}}</wd-collapse-item>
		</wd-collapse>
		<wd-cell size="large">
			<template #title>
				<view>
					<view style="display: inline-block">考核附件</view>
				</view>
			</template>
			<view style="color: blue;" @click="onlinePreview(zbInfo.khfj)">
				{{zbInfo.khfj}}
			</view>
		</wd-cell>
	</wd-cell-group>
	<wd-collapse v-model="collapse" v-if="tabbar == '1'">
		<wd-collapse-item v-for="(fbItem, fbIndex) in fbInfo" :key="fbIndex" :title="fbItem.depart_dictText"
			:name="'item' + (fbIndex + 1)">
			<wd-cell v-for="(btItem, btIndex) in btInfo" :key="btIndex" :title="btItem.name"
				:value="getDwkhValue(fbItem, btItem.id)" />
			<wd-cell>
				<template #title>
					<view>
						<view style="display: inline-block">附件</view>
					</view>
				</template>
				<view style="color: blue;" @click="onlinePreview(fbItem.annex)">
					{{getFilename(fbItem.annex)}}
				</view>
			</wd-cell>
		</wd-collapse-item>
	</wd-collapse>
</template>

<script setup>
	import {
		queryZbkhZbByIdApi,
		queryZbkhFbByIdApi,
		queryZbkhBtByIdApi
	} from '@/api/bpm'
	import {
		getFilename
	} from '@/utils'
	const tabbar = ref(0)
	const zbInfo = ref({})
	const fbInfo = ref([])
	const btInfo = ref([])
	const btDataInfo = ref([])
	const zbCollapse = ref(['评价方法'])
	const collapse = ref(['item1'])

	const props = defineProps({
		formData: {
			type: Object,
			default: () => {},
		}
	})

	const queryById = async () => {
		// 1. 获取主表数据
		const zbRes = await queryZbkhZbByIdApi(props.formData.dataId);
		if (zbRes.success) {
			zbInfo.value = zbRes.result.records[0];
			// 2. 获取指标模板数据（btInfo）
			if (zbInfo.value.dwkhBtId) {
				const btRes = await queryZbkhBtByIdApi(zbInfo.value.dwkhBtId);
				btInfo.value = btRes.result;
			}
		}
		// 3. 获取子表数据（fbInfo）
		const fbRes = await queryZbkhFbByIdApi(props.formData.dataId);
		fbInfo.value = fbRes.result.records;
	};

	const getDwkhValue = (fbItem, btId) => {
		if (!fbItem.dwkhSaves) return ''; // 如果没有 dwkhSaves，返回空
		const found = fbItem.dwkhSaves.find(item => item.dwkhBtId === btId);
		return found ? found.value : '';
	};

	const onlinePreview = (e) => {
		uni.navigateTo({
			url: `/pages/onlinePreview/detail?data=${e}`
		})
	}

	onMounted(() => {
		queryById()
	})
</script>

<style lang="scss">
/* 手动引入 wd-segmented 样式 */
@import 'wot-design-uni/components/wd-segmented/index.scss';

/* 或直接覆盖关键样式 */
.wd-segmented {
  /* 确保样式优先级 */
  opacity: 1 !important;
  visibility: visible !important;
}
</style>