<template>
	<el-dialog title="查看详情" v-model="visible" :close-on-click-modal="false" draggable width="960px">
		<div class="detail-container">
			<!-- 活动主信息（来自列表行） -->
			<el-descriptions v-if="mainInfo.activityTheme" :column="2" border class="mb16">
				<el-descriptions-item label="活动主题" :span="2">
					{{ mainInfo.activityTheme || '-' }}
				</el-descriptions-item>
				<el-descriptions-item label="活动说明" :span="2">
					{{ mainInfo.remarks || '-' }}
				</el-descriptions-item>
				<el-descriptions-item label="活动兼报数">
					{{ mainInfo.maxSub !== undefined && mainInfo.maxSub !== null ? mainInfo.maxSub : '-' }}
				</el-descriptions-item>
				<el-descriptions-item label="开始时间">
					{{ mainInfo.startTime ? parseTime(mainInfo.startTime, '{y}-{m}-{d}') : '-' }}
				</el-descriptions-item>
				<el-descriptions-item label="结束时间">
					{{ mainInfo.endTime ? parseTime(mainInfo.endTime, '{y}-{m}-{d}') : '-' }}
				</el-descriptions-item>
			</el-descriptions>
			<!-- 子项目列表（接口：getActivityInfoSubList） -->
			<div class="sub-title">子项目列表</div>
			<el-table :data="subList" v-loading="loading" border size="small" max-height="400" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" align="center" />
				<el-table-column prop="subTitle" label="子项目名称" min-width="140" show-overflow-tooltip />
				<el-table-column prop="deptName" label="学院" width="110" show-overflow-tooltip />
				<el-table-column prop="classNo" label="班号" width="80" align="center" />
				<el-table-column prop="classMasterName" label="班主任" width="90" show-overflow-tooltip />
				<el-table-column prop="startTime" label="开始时间" width="155" align="center">
					<template #default="scope">
						{{ scope.row.startTime ? parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}') : '-' }}
					</template>
				</el-table-column>
				<el-table-column prop="endTime" label="结束时间" width="155" align="center">
					<template #default="scope">
						{{ scope.row.endTime ? parseTime(scope.row.endTime, '{y}-{m}-{d} {h}:{i}') : '-' }}
					</template>
				</el-table-column>
				<el-table-column prop="maxNum" label="人数限制" width="88" align="center" />
				<el-table-column prop="applyNums" label="已报名" width="78" align="center" />
				<el-table-column prop="position" label="地点" min-width="100" show-overflow-tooltip />
				<el-table-column prop="projectDescription" label="项目描述" min-width="180" show-overflow-tooltip />
			</el-table>
			<el-empty v-if="!loading && subList.length === 0" description="暂无子项目" :image-size="80" />
		</div>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">关 闭</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="ActivityInfoDetailDialog">
import { ref, reactive } from 'vue';
import { getActivityInfoSubList } from '/@/api/stuwork/activityinfosub';
import { parseTime } from '/@/utils/formatTime';

const visible = ref(false);
const loading = ref(false);
const mainInfo = reactive<Record<string, any>>({});
const subList = ref<any[]>([]);

/**
 * 打开弹窗：使用接口 getActivityInfoSubList(activityInfoId) 获取详情子项目列表
 * @param activityInfoId 活动信息ID
 * @param row 列表行数据，用于展示活动主题等主信息
 */
const openDialog = async (activityInfoId: string, row?: any) => {
	visible.value = true;
	loading.value = true;
	subList.value = [];
	Object.keys(mainInfo).forEach((k) => delete mainInfo[k]);
	if (row && typeof row === 'object') {
		Object.assign(mainInfo, row);
	}

	try {
		const res = await getActivityInfoSubList(activityInfoId);
		const data = res.data;
		subList.value = Array.isArray(data) ? data : [];
	} catch (_err) {
		subList.value = [];
	} finally {
		loading.value = false;
	}
};

defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.detail-container {
	padding: 8px 0;
}
.mb16 {
	margin-bottom: 16px;
}
.sub-title {
	margin-bottom: 8px;
	font-weight: 600;
	color: #303133;
}
</style>
