<template>
	<div class="system-user-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input v-model="state.tableData.param.modelName" size="default" placeholder="请输入模型名称" style="max-width: 180px" clearable>
				</el-input>
				<el-select v-model="state.tableData.param.modelType" size="default" placeholder="请选择模型类型" style="max-width: 180px" clearable class="ml10">
					<el-option v-for="type in state.modelTypes" :key="type.id" :label="type.label" :value="type.id" />
				</el-select>
				<el-button size="default" type="primary" class="ml10" @click="getTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" @click="onOpenAddModule('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增模型配置
				</el-button>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column type="index" label="序号" width="60" />
				<el-table-column prop="modelName" label="模型名称" show-overflow-tooltip></el-table-column>
				<el-table-column label="模型类型" width="120" show-overflow-tooltip>
					<template #default="{ row }">
						{{ resolveModelTypeLabel(row.modelsType) }}
					</template>
				</el-table-column>
				<!-- <el-table-column prop="baseUrl" label="模型服务地址" show-overflow-tooltip width="200"></el-table-column> -->
				<el-table-column prop="isPrivate" label="访问类型" width="100">
					<template #default="scope">
						<el-tag :type="scope.row.isPrivate === 1 ? 'primary' : 'info'">{{ scope.row.isPrivate === 1 ? '公共' : '私有' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="httpMethod" label="请求方式" width="100"></el-table-column>
				<el-table-column label="会话模型" width="110" align="center">
					<template #default="{ row }">
						<el-switch
							v-if="isInferenceModel(row.modelsType)"
							size="small"
							:model-value="Number(row.isChatModel) === 1"
							:before-change="() => onChatModelSwitchRequest(row)"
						/>
						<span v-else style="color: #999;">—</span>
					</template>
				</el-table-column>
				<el-table-column prop="enabled" label="状态" width="100">
					<template #default="scope">
						<el-tag :type="scope.row.enabled === 1 ? 'success' : 'danger'">{{ scope.row.enabled === 1 ? '启用' : '禁用' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="maxConcurrency" label="最大并发" width="100"></el-table-column>
				<el-table-column prop="queueLimit" label="队列上限" width="100"></el-table-column>
				<el-table-column prop="remark" label="备注" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createdAt" label="创建时间" show-overflow-tooltip width="160"></el-table-column>
				<el-table-column prop="updatedAt" label="修改时间" show-overflow-tooltip width="160"></el-table-column>
				<el-table-column label="操作" width="150" fixed="right">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditModule('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="danger" @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30, 50]"
				v-model:current-page="state.tableData.param.pageNum"
				background
				v-model:page-size="state.tableData.param.pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<EditModule ref="editModuleRef" :model-types="state.modelTypes" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="digitalHumanModelModule">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import {
	getModelModuleList,
	getModelTypeList,
	deleteModelModule,
	normalizeModelTypeOptions,
	updateChatModel,
} from '/@/api/digitalHuman/modelConfig/modelModule/index';

const EditModule = defineAsyncComponent(() => import('/@/views/digitalHuman/modelConfig/modelModule/component/editModule.vue'));

const editModuleRef = ref();
const state = reactive({
	modelTypes: [] as Array<{ id: number | string; label: string }>,
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			pageNum: 1,
			pageSize: 10,
			modelName: '',
			modelType: undefined as number | string | undefined,
		},
	},
});

// 判断是否为推理模型（只有推理模型才显示会话模型开关）
const isInferenceModel = (modelsType: number | string | undefined | null) => {
	if (modelsType === undefined || modelsType === null || modelsType === '') {
		return false;
	}
	// 查找模型类型标签，判断是否为"推理模型"
	const typeInfo = state.modelTypes.find((t) => String(t.id) === String(modelsType));
	return typeInfo?.label === '推理模型' || String(modelsType) === '1';
};

// 会话模型开关切换
const onChatModelSwitchRequest = async (row: { id?: number | string; isChatModel?: number }) => {
	try {
		const newStatus = Number(row.isChatModel) === 1 ? 0 : 1;
		await updateChatModel({
			id: row.id!,
			isChatModel: newStatus as 0 | 1,
		});
		ElMessage.success(newStatus === 1 ? '已设置为会话模型' : '已取消会话模型');
		// 重新获取列表数据
		await getTableData();
		return true;
	} catch {
		// 接口错误由 request 全局提示后端 message
		return false;
	}
};

const resolveModelTypeLabel = (modelsType: number | string | undefined | null) => {
	if (modelsType === undefined || modelsType === null || modelsType === '') {
		return '—';
	}
	const hit = state.modelTypes.find((t) => String(t.id) === String(modelsType));
	return hit?.label ?? String(modelsType);
};

const loadModelTypes = async () => {
	try {
		const res: any = await getModelTypeList();
		if (res.code === 0) {
			state.modelTypes = normalizeModelTypeOptions(res);
		}
	} catch {
		// 接口错误由 request 全局提示后端 message
	}
};

// 初始化表格数据
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const res: any = await getModelModuleList(state.tableData.param);
		if (res.code === 0) {
			state.tableData.data = res.data.list || [];
			state.tableData.total = res.data.total || 0;
		}
	} catch {
		// 接口错误由 request 全局提示后端 message
	} finally {
		state.tableData.loading = false;
	}
};

// 打开新增模型模块弹窗
const onOpenAddModule = (type: string) => {
	editModuleRef.value.openDialog(type);
};

// 打开修改模型模块弹窗
const onOpenEditModule = (type: string, row: any) => {
	editModuleRef.value.openDialog(type, row);
};

// 删除模型模块
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`确定要删除模型配置：${row.modelName}？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(async () => {
			try {
				await deleteModelModule(row.id);
				ElMessage.success('删除成功');
				getTableData();
			} catch {
				// 接口错误由 request 全局提示后端 message
			}
		})
		.catch(() => {});
};

// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};

// 页面加载时
onMounted(async () => {
	await loadModelTypes();
	getTableData();
});
</script>

<style scoped lang="scss">
.text-muted {
	color: var(--el-text-color-placeholder);
}

.system-user-container {
	:deep(.el-card__body) {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow: auto;
		.el-table {
			flex: 1;
		}
	}
}
</style>
