<template>
	<div class="digital-human-video-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.videoType" placeholder="请选择视频类型" clearable style="width: 150px">
							<el-option label="数字人视频" value="avatar" />
							<el-option label="背景视频" value="background" />
							<el-option label="素材视频" value="material" />
						</el-select>
					</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="video-grid">
				<el-card v-for="item in tableData" :key="item.id" class="video-card" shadow="hover">
					<div class="video-thumbnail">
						<img :src="item.thumbnail" :alt="item.name" />
						<div class="video-duration">{{ formatDuration(item.duration) }}</div>
						<div class="video-overlay">
							<el-button type="primary" size="default" circle @click="handlePreview(item)">
								<el-icon size="24"><ele-VideoPlay /></el-icon>
							</el-button>
						</div>
					</div>
					<div class="video-info">
						<h4>{{ item.name }}</h4>
						<div class="video-meta">
							<el-tag :type="getVideoTypeTag(item.videoType)" size="small">
								{{ getVideoTypeLabel(item.videoType) }}
							</el-tag>
							<span class="video-size">{{ formatFileSize(item.fileSize) }}</span>
						</div>
						<div class="video-specs">
							<span>{{ item.resolution }}</span>
							<span>{{ item.fps }}fps</span>
						</div>
						<div class="video-status">
							<el-switch
								v-model="item.status"
								:active-value="1"
								:inactive-value="0"
								size="small"
								@change="handleStatusChange(item)"
							/>
							<span class="status-text">{{ item.status === 1 ? '启用' : '禁用' }}</span>
						</div>
						<div class="video-actions">
							<el-button type="primary" text size="small" @click="handleEdit(item)">编辑</el-button>
							<el-button type="primary" text size="small" @click="handleDownload(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="[8, 16, 24, 32]"
					:total="total"
					layout="total, sizes, prev, pager, next, jumper"
					@size-change="handleSizeChange"
					@current-change="handleCurrentChange"
				/>
			</div>
		</el-card>

		<!-- 视频预览弹窗 -->
		<el-dialog v-model="previewVisible" :title="previewVideo?.name" width="800px" destroy-on-close>
			<div class="video-preview-container">
				<div class="video-placeholder">
					<el-icon size="64" color="#909399"><ele-VideoPlay /></el-icon>
					<p>视频预览区域</p>
					<p class="video-preview-info">{{ previewVideo?.resolution }} | {{ previewVideo?.fps }}fps | {{ formatDuration(previewVideo?.duration || 0) }}</p>
				</div>
			</div>
		</el-dialog>
	</div>
</template>

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

interface VideoItem {
	id: string;
	name: string;
	thumbnail: string;
	videoType: string;
	duration: number;
	fileSize: number;
	resolution: string;
	fps: number;
	videoUrl: string;
	status: number;
	createdAt: string;
}

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

const tableData = ref<VideoItem[]>([]);
const total = ref(0);
const loading = ref(false);
const previewVisible = ref(false);
const previewVideo = ref<VideoItem | null>(null);

// 模拟数据
const mockData: VideoItem[] = [
	{
		id: '1',
		name: '商务男性数字人-产品介绍',
		thumbnail: 'https://picsum.photos/400/225?random=10',
		videoType: 'avatar',
		duration: 125,
		fileSize: 52428800,
		resolution: '1920x1080',
		fps: 30,
		videoUrl: '',
		status: 1,
		createdAt: '2024-01-15 10:30:00',
	},
	{
		id: '2',
		name: '甜美女性数字人-直播带货',
		thumbnail: 'https://picsum.photos/400/225?random=11',
		videoType: 'avatar',
		duration: 245,
		fileSize: 104857600,
		resolution: '1920x1080',
		fps: 60,
		videoUrl: '',
		status: 1,
		createdAt: '2024-01-14 14:20:00',
	},
	{
		id: '3',
		name: '科技感背景-蓝色粒子',
		thumbnail: 'https://picsum.photos/400/225?random=12',
		videoType: 'background',
		duration: 30,
		fileSize: 15728640,
		resolution: '1920x1080',
		fps: 30,
		videoUrl: '',
		status: 1,
		createdAt: '2024-01-13 09:15:00',
	},
	{
		id: '4',
		name: '办公室场景背景',
		thumbnail: 'https://picsum.photos/400/225?random=13',
		videoType: 'background',
		duration: 60,
		fileSize: 31457280,
		resolution: '3840x2160',
		fps: 30,
		videoUrl: '',
		status: 0,
		createdAt: '2024-01-12 16:45:00',
	},
	{
		id: '5',
		name: '转场特效-渐变',
		thumbnail: 'https://picsum.photos/400/225?random=14',
		videoType: 'material',
		duration: 3,
		fileSize: 2097152,
		resolution: '1920x1080',
		fps: 60,
		videoUrl: '',
		status: 1,
		createdAt: '2024-01-11 11:00:00',
	},
	{
		id: '6',
		name: '知性女性数字人-新闻播报',
		thumbnail: 'https://picsum.photos/400/225?random=15',
		videoType: 'avatar',
		duration: 180,
		fileSize: 78643200,
		resolution: '1920x1080',
		fps: 30,
		videoUrl: '',
		status: 1,
		createdAt: '2024-01-10 08:30:00',
	},
];

// 获取视频类型标签
const getVideoTypeTag = (type: string) => {
	const tagMap: Record<string, string> = {
		avatar: 'primary',
		background: 'success',
		material: 'warning',
	};
	return tagMap[type] || 'info';
};

// 获取视频类型文本
const getVideoTypeLabel = (type: string) => {
	const labelMap: Record<string, string> = {
		avatar: '数字人视频',
		background: '背景视频',
		material: '素材视频',
	};
	return labelMap[type] || type;
};

// 格式化时长
const formatDuration = (seconds: number) => {
	const mins = Math.floor(seconds / 60);
	const secs = seconds % 60;
	return `${mins}:${secs.toString().padStart(2, '0')}`;
};

// 格式化文件大小
const formatFileSize = (bytes: number) => {
	if (bytes < 1024) return bytes + 'B';
	if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + 'KB';
	if (bytes < 1024 * 1024 * 1024) return (bytes / 1024 / 1024).toFixed(1) + 'MB';
	return (bytes / 1024 / 1024 / 1024).toFixed(2) + 'GB';
};

// 获取列表数据
const getList = () => {
	loading.value = true;
	setTimeout(() => {
		let filteredData = [...mockData];
		if (queryParams.name) {
			filteredData = filteredData.filter((item) => item.name.includes(queryParams.name));
		}
		if (queryParams.videoType) {
			filteredData = filteredData.filter((item) => item.videoType === queryParams.videoType);
		}
		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.videoType = '';
	queryParams.status = undefined;
	queryParams.pageNum = 1;
	getList();
};

const handleAdd = () => {
	ElMessage.info('上传视频功能开发中...');
};

const handleEdit = (row: VideoItem) => {
	ElMessage.info(`编辑视频: ${row.name}`);
};

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

const handlePreview = (row: VideoItem) => {
	previewVideo.value = row;
	previewVisible.value = true;
};

const handleDownload = (row: VideoItem) => {
	ElMessage.info(`下载视频: ${row.name}`);
};

const handleStatusChange = (row: VideoItem) => {
	ElMessage.success(`状态已${row.status === 1 ? '启用' : '禁用'}`);
};

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-video-container {
	padding: 15px;

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

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

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

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

		.video-thumbnail {
			position: relative;
			width: 100%;
			height: 180px;
			overflow: hidden;
			background: #000;

			img {
				width: 100%;
				height: 100%;
				object-fit: cover;
			}

			.video-duration {
				position: absolute;
				bottom: 8px;
				right: 8px;
				padding: 2px 6px;
				background: rgba(0, 0, 0, 0.7);
				color: #fff;
				font-size: 12px;
				border-radius: 4px;
			}

			.video-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 .video-overlay {
				opacity: 1;
			}
		}

		.video-info {
			padding: 15px;

			h4 {
				margin: 0 0 10px;
				font-size: 15px;
				color: #303133;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

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

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

			.video-specs {
				display: flex;
				gap: 15px;
				margin-bottom: 10px;
				font-size: 12px;
				color: #606266;
			}

			.video-status {
				display: flex;
				align-items: center;
				gap: 8px;
				margin-bottom: 10px;

				.status-text {
					font-size: 12px;
					color: #909399;
				}
			}

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

	.pagination-container {
		display: flex;
		justify-content: flex-end;
	}

	.video-preview-container {
		.video-placeholder {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			height: 400px;
			background: #f5f7fa;
			border-radius: 8px;

			p {
				margin: 10px 0 0;
				color: #909399;
			}

			.video-preview-info {
				font-size: 12px;
			}
		}
	}
}
</style>
