<template>
	<div class="digital-human-avatar-container">
		<el-card shadow="hover">
			<div class="search-header mb15">
				<el-form :inline="true" :model="queryParams">
					<el-form-item label="形象名称">
						<el-input v-model="queryParams.name" placeholder="请输入形象名称" clearable style="width: 200px" />
					</el-form-item>
					<el-form-item label="状态">
						<el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 150px">
							<el-option label="启用" :value="1" />
							<el-option label="禁用" :value="0" />
						</el-select>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="handleSearch">
							<el-icon><ele-Search /></el-icon>
							搜索
						</el-button>
						<el-button @click="handleReset">
							<el-icon><ele-Refresh /></el-icon>
							重置
						</el-button>
						<el-button type="success" @click="handleAdd">
							<el-icon><ele-Plus /></el-icon>
							新增形象
						</el-button>
					</el-form-item>
				</el-form>
			</div>

			<!-- 数字人形象卡片列表 -->
			<div class="avatar-grid">
				<el-card v-for="item in tableData" :key="item.id" class="avatar-card" shadow="hover">
					<div class="avatar-image">
						<img :src="item.avatar" :alt="item.name" />
						<div class="avatar-overlay">
							<el-button type="primary" size="small" circle @click="handlePreview(item)">
								<el-icon><ele-View /></el-icon>
							</el-button>
						</div>
					</div>
					<div class="avatar-info">
						<h4>{{ item.name }}</h4>
						<p class="avatar-desc">{{ item.description }}</p>
						<div class="avatar-meta">
							<el-tag :type="item.status === 1 ? 'success' : 'info'" size="small">
								{{ item.status === 1 ? '启用' : '禁用' }}
							</el-tag>
							<span class="avatar-type">{{ item.type }}</span>
						</div>
						<div class="avatar-actions">
							<el-button type="primary" text size="small" @click="handleEdit(item)">编辑</el-button>
							<el-button type="danger" text size="small" @click="handleDelete(item)">删除</el-button>
						</div>
					</div>
				</el-card>
			</div>

			<!-- 分页 -->
			<div class="pagination-container mt15">
				<el-pagination
					v-model:current-page="queryParams.pageNum"
					v-model:page-size="queryParams.pageSize"
					:page-sizes="[12, 24, 36, 48]"
					:total="total"
					layout="total, sizes, prev, pager, next, jumper"
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
				/>
			</div>
		</el-card>
	</div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';

interface AvatarItem {
	id: number;
	name: string;
	avatar: string;
	description: string;
	type: string;
	status: number;
	createdAt: string;
}

const queryParams = reactive({
	name: '',
	status: undefined as number | undefined,
	pageNum: 1,
	pageSize: 12,
});

const tableData = ref<AvatarItem[]>([]);
const total = ref(0);
const loading = ref(false);

// 模拟数据
const mockData: AvatarItem[] = [
	{
		id: 1,
		name: '商务男性形象',
		avatar: 'https://img.icons8.com/3d-fluency/94/businessman.png',
		description: '专业商务风格的男性数字人形象，适合企业宣传',
		type: '真人形象',
		status: 1,
		createdAt: '2024-01-15 10:30:00',
	},
	{
		id: 2,
		name: '甜美女性形象',
		avatar: 'https://img.icons8.com/3d-fluency/94/businesswoman.png',
		description: '甜美可爱的女性数字人形象，适合直播带货',
		type: '真人形象',
		status: 1,
		createdAt: '2024-01-14 14:20:00',
	},
	{
		id: 3,
		name: '卡通男孩形象',
		avatar: 'https://img.icons8.com/3d-fluency/94/person-male.png',
		description: '活泼可爱的卡通男孩形象，适合儿童教育',
		type: '卡通形象',
		status: 1,
		createdAt: '2024-01-13 09:15:00',
	},
	{
		id: 4,
		name: '知性女性形象',
		avatar: 'https://img.icons8.com/3d-fluency/94/woman-profile.png',
		description: '知性优雅的女性数字人形象，适合知识讲解',
		type: '真人形象',
		status: 0,
		createdAt: '2024-01-12 16:45:00',
	},
	{
		id: 5,
		name: '科技机器人形象',
		avatar: 'https://img.icons8.com/3d-fluency/94/robot-2.png',
		description: '未来科技风格的机器人形象，适合科技产品',
		type: '3D形象',
		status: 1,
		createdAt: '2024-01-11 11:00:00',
	},
	{
		id: 6,
		name: '客服助手形象',
		avatar: 'https://img.icons8.com/3d-fluency/94/technical-support.png',
		description: '专业友好的客服助手形象，适合在线客服场景',
		type: '3D形象',
		status: 1,
		createdAt: '2024-01-10 08:30:00',
	},
];

// 获取列表数据
const getList = () => {
	loading.value = true;
	// 模拟接口请求
	setTimeout(() => {
		let filteredData = [...mockData];
		if (queryParams.name) {
			filteredData = filteredData.filter((item) => item.name.includes(queryParams.name));
		}
		if (queryParams.status !== undefined) {
			filteredData = filteredData.filter((item) => item.status === queryParams.status);
		}
		tableData.value = filteredData;
		total.value = filteredData.length;
		loading.value = false;
	}, 300);
};

const handleSearch = () => {
	queryParams.pageNum = 1;
	getList();
};

const handleReset = () => {
	queryParams.name = '';
	queryParams.status = undefined;
	queryParams.pageNum = 1;
	getList();
};

const handleAdd = () => {
	ElMessage.info('新增数字人形象功能开发中...');
};

const handleEdit = (row: AvatarItem) => {
	ElMessage.info(`编辑形象: ${row.name}`);
};

const handleDelete = (row: AvatarItem) => {
	ElMessageBox.confirm(`确定要删除形象 "${row.name}" 吗？`, '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(() => {
		ElMessage.success('删除成功');
		getList();
	});
};

const handlePreview = (row: AvatarItem) => {
	ElMessage.info(`预览形象: ${row.name}`);
};

const handleSizeChange = (size: number) => {
	queryParams.pageSize = size;
	queryParams.pageNum = 1;
	getList();
};

const handleCurrentChange = (page: number) => {
	queryParams.pageNum = page;
	getList();
};

onMounted(() => {
	getList();
});
</script>

<style scoped lang="scss">
.digital-human-avatar-container {
	padding: 15px;

	.avatar-grid {
		display: grid;
		grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
		gap: 20px;
	}

	.avatar-card {
		overflow: hidden;
		transition: transform 0.3s;

		&:hover {
			transform: translateY(-5px);
		}

		:deep(.el-card__body) {
			padding: 0;
		}

		.avatar-image {
			position: relative;
			width: 100%;
			height: 220px;
			overflow: hidden;
			background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
			display: flex;
			align-items: center;
			justify-content: center;

			img {
				width: 160px;
				height: 160px;
				object-fit: contain;
				filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
				transition: transform 0.3s ease;
			}

			&:hover img {
				transform: scale(1.1);
			}

			.avatar-overlay {
				position: absolute;
				top: 0;
				left: 0;
				right: 0;
				bottom: 0;
				background: rgba(0, 0, 0, 0.5);
				display: flex;
				align-items: center;
				justify-content: center;
				opacity: 0;
				transition: opacity 0.3s;
			}

			&:hover .avatar-overlay {
				opacity: 1;
			}
		}

		.avatar-info {
			padding: 15px;

			h4 {
				margin: 0 0 8px;
				font-size: 16px;
				color: #303133;
			}

			.avatar-desc {
				margin: 0 0 10px;
				font-size: 13px;
				color: #909399;
				line-height: 1.5;
				height: 40px;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				line-clamp: 2;
				-webkit-box-orient: vertical;
			}

			.avatar-meta {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 10px;

				.avatar-type {
					font-size: 12px;
					color: #909399;
				}
			}

			.avatar-actions {
				display: flex;
				justify-content: flex-end;
				border-top: 1px solid #ebeef5;
				padding-top: 10px;
			}
		}
	}

	.pagination-container {
		display: flex;
		justify-content: flex-end;
	}
}
</style>
