<template>
	<div class="sidebar">
		<div class="sidebar-header">
			<el-button class="new-chat-btn" @click="handleNewChat">
				<el-icon><Plus /></el-icon>
				新增对话
			</el-button>
		</div>

		<div v-if="historyList.length" class="history-section">
			<div class="history-title">历史对话</div>
			<div class="history-list">
				<div
					v-for="item in historyList"
					:key="item.id"
					class="history-item"
					:class="{ active: activeHistoryId === item.id }"
					@click="handleSelectHistory(item.id)"
				>
					<div class="history-main">
						<div class="history-name">{{ item.title }}</div>
						<div class="history-time">{{ item.time }}</div>
					</div>
					<el-button text class="delete-btn" @click.stop="handleDeleteHistory(item.id)">
						<el-icon><Delete /></el-icon>
					</el-button>
				</div>
			</div>
		</div>

		<!-- 工作空间 -->
		<div class="workspace-section">
			<div class="workspace-title">工作空间</div>
			<div class="workspace-list" ref="workspaceListRef">
				<div
					v-for="item in displayWorkspaceItems"
					:key="item.id"
					class="workspace-item"
					@click="handlePreview(item)"
				>
					<div class="workspace-item-icon">
						<img v-if="item.type === 'image'" :src="item.url" />
						<el-icon v-else-if="item.type === 'video'" ><VideoCamera /></el-icon>
						<el-icon v-else-if="item.type === 'text'" ><Document /></el-icon>
						<el-icon v-else><Document /></el-icon>
					</div>
					<div class="workspace-item-info">
						<div class="workspace-item-name">{{ item.name }}</div>
					</div>
					<el-button
						type="primary"
						size="small"
						link
						@click.stop="handleDownload(item)"
					>
						<el-icon><Download /></el-icon>
					</el-button>
				</div>
			</div>
		</div>
	</div>

	<!-- 预览弹窗 -->
	<el-dialog
		v-model="previewDialogVisible"
		:title="currentPreviewItem?.name"
		width="auto"
		max-width="80%"
		top="10vh"
		align-center
	>
		<div class="preview-dialog-content">
			<img v-if="currentPreviewItem?.type === 'image'" :src="currentPreviewItem?.url" class="dialog-image" />
			<video
				v-else-if="currentPreviewItem?.type === 'video'"
				:src="currentPreviewItem?.url"
				controls
				autoplay
				class="dialog-video"
			/>
			<pre v-else-if="currentPreviewItem?.type === 'text'" class="dialog-text">{{ currentPreviewItem?.content }}</pre>
		</div>
		<template #footer>
			<el-button @click="previewDialogVisible = false">关闭</el-button>
			<el-button type="primary" @click="handleDownload(currentPreviewItem!)">下载</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { Plus, Delete, Document, VideoCamera, Download } from '@element-plus/icons-vue';

interface HistoryItem {
	id: number;
	title: string;
	time: string;
}

interface WorkspaceItem {
	id: number;
	name: string;
	type: 'text' | 'image' | 'video' | 'file';
	url?: string;
	content?: string;
	createTime: string;
}

interface Props {
	activeMenu: string;
	activeHistoryId: number;
	historyList: HistoryItem[];
	workspaceItems?: WorkspaceItem[];
}

interface Emits {
	(e: 'menu-change', key: string): void;
	(e: 'new-chat'): void;
	(e: 'select-history', id: number): void;
	(e: 'delete-history', id: number): void;
}

const props = withDefaults(defineProps<Props>(), {
	workspaceItems: () => [],
});
const emit = defineEmits<Emits>();

const workspaceListRef = ref<HTMLElement | null>(null);
const previewDialogVisible = ref(false);
const currentPreviewItem = ref<WorkspaceItem | null>(null);

// 示例 mock 数据
const mockItems: WorkspaceItem[] = [
	{
		id: 1,
		name: '对话总结.md',
		type: 'text',
		content: `# 项目需求总结

## 功能需求
1. 在首页对话添加工作空间功能
2. 工作空间用于存放对话产出的文本、图片、视频
3. 需要支持预览和下载功能

## 技术要点
- 使用响应式布局，网格卡片展示
- 点击弹出对话框预览完整内容
- 原生 JS 实现文件下载`,
		createTime: '2024-05-29',
	},
	{
		id: 2,
		name: '示例图片.jpg',
		type: 'image',
		url: 'https://placekitten.com/800/600',
		createTime: '2024-05-29',
	},
	{
		id: 3,
		name: '演示视频.mp4',
		type: 'video',
		url: 'https://www.w3schools.com/html/mov_bbb.mp4',
		createTime: '2024-05-29',
	},
];

// 合并 props 和本地 mock（开发时显示示例）
const displayWorkspaceItems = computed(() => {
	return props.workspaceItems && props.workspaceItems.length > 0 ? props.workspaceItems : mockItems;
});

const handleNewChat = () => {
	emit('new-chat');
};

const handleSelectHistory = (id: number) => {
	emit('select-history', id);
};

const handleDeleteHistory = (id: number) => {
	emit('delete-history', id);
};

const handlePreview = (item: WorkspaceItem) => {
	currentPreviewItem.value = item;
	previewDialogVisible.value = true;
};

const handleDownload = (item: WorkspaceItem) => {
	if (!item.url && !item.content) return;

	if (item.type === 'text' && item.content) {
		// 文本内容下载
		const blob = new Blob([item.content], { type: 'text/plain' });
		const url = URL.createObjectURL(blob);
		const a = document.createElement('a');
		a.href = url;
		a.download = item.name;
		document.body.appendChild(a);
		a.click();
		document.body.removeChild(a);
		URL.revokeObjectURL(url);
	} else if (item.url) {
		// 图片、视频、文件下载
		const a = document.createElement('a');
		a.href = item.url;
		a.download = item.name;
		document.body.appendChild(a);
		a.click();
		document.body.removeChild(a);
	}
};
</script>

<style scoped lang="scss">
.sidebar {
	width: 252px;
	background: linear-gradient(180deg, rgba(255, 255, 255, 0.95) 0%, rgba(249, 251, 255, 0.92) 100%);
	border-right: 1px solid #e7edf7;
	display: flex;
	flex-direction: column;
	height: 100%;
	box-shadow: 8px 0 30px rgba(15, 23, 42, 0.06);
	backdrop-filter: blur(10px);
}

.sidebar-header {
	padding: 18px 16px 14px;
	border-bottom: 1px solid #e9eef7;
	position: relative;
}

.new-chat-btn {
	margin-top: 0;
	width: 100%;
	height: 40px;
	border-radius: 12px;
	border: 1px solid rgba(59, 130, 246, 0.28);
	background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
	color: #ffffff;
	font-weight: 600;
	letter-spacing: 0.2px;
	box-shadow: 0 4px 12px rgba(37, 99, 235, 0.26);
	transition: all 0.2s ease;

	&:hover {
		transform: translateY(-1px);
		color: #ffffff;
		border-color: rgba(59, 130, 246, 0.4);
		background: linear-gradient(135deg, #60a5fa 0%, #3b82f6 100%);
		box-shadow: 0 6px 16px rgba(37, 99, 235, 0.32);
	}
}

.history-section {
	flex: 1;
	display: flex;
	flex-direction: column;
	margin-top: 8px;
	min-height: 0;
	background: linear-gradient(180deg, rgba(249, 251, 255, 0.5) 0%, rgba(249, 251, 255, 0.9) 100%);
}

.history-title {
	padding: 12px 12px 8px;
	font-size: 11px;
	font-weight: 600;
	color: #64748b;
	text-transform: uppercase;
	letter-spacing: 0.8px;
}

.history-list {
	flex: 1;
	padding: 0 8px 10px;
	overflow-y: auto;
	scrollbar-width: none;
	-ms-overflow-style: none;

	&::-webkit-scrollbar {
		display: none;
	}
}

.history-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
	padding: 10px 10px;
	margin-bottom: 6px;
	border-radius: 10px;
	border: 1px solid transparent;
	cursor: pointer;
	transition: all 0.25s cubic-bezier(0.4, 0, 0.2, 1);
	background: rgba(255, 255, 255, 0.6);

	&:hover {
		border-color: #bfdbfe;
		background: linear-gradient(135deg, rgba(239, 246, 255, 0.9) 0%, rgba(219, 234, 254, 0.7) 100%);
		transform: translateX(2px);
	}

	&.active {
		border-color: #3b82f6;
		background: linear-gradient(135deg, rgba(59, 130, 246, 0.12) 0%, rgba(59, 130, 246, 0.04) 100%);
		box-shadow: 0 2px 8px rgba(59, 130, 246, 0.15);
	}

	.delete-btn {
		opacity: 0;
		color: #94a3b8;
		transition: all 0.2s ease;
		padding: 4px;

		&:hover {
			color: #ef4444;
			background: rgba(239, 68, 68, 0.1);
			border-radius: 4px;
		}
	}

	&:hover .delete-btn {
		opacity: 1;
	}
}

.history-main {
	min-width: 0;
	flex: 1;
}

.history-name {
	font-size: 12px;
	font-weight: 500;
	color: #1e293b;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	margin-bottom: 2px;
}

.history-time {
	font-size: 11px;
	color: #94a3b8;
}

/* 工作空间样式 */
.workspace-section {
	margin-top: 8px;
	padding-top: 8px;
	border-top: 1px solid #e9eef7;
	max-height: 40%;
	display: flex;
	flex-direction: column;
	min-height: 0;
}

.workspace-title {
	padding: 12px 12px 8px;
	font-size: 11px;
	font-weight: 600;
	color: #64748b;
	text-transform: uppercase;
	letter-spacing: 0.8px;
}

.workspace-list {
	flex: 1;
	padding: 0 8px 10px;
	overflow-y: auto;
	scrollbar-width: none;
	-ms-overflow-style: none;

	&::-webkit-scrollbar {
		display: none;
	}
}

.workspace-item {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 8px 10px;
	margin-bottom: 6px;
	border-radius: 8px;
	border: 1px solid transparent;
	background: rgba(255, 255, 255, 0.6);
	cursor: pointer;
	transition: all 0.2s ease;

	&:hover {
		border-color: #bfdbfe;
		background: linear-gradient(135deg, rgba(239, 246, 255, 0.9) 0%, rgba(219, 234, 254, 0.7) 100%);
	}
}

.workspace-item-icon {
	width: 28px;
	height: 28px;
	flex-shrink: 0;
	border-radius: 6px;
	background: #f1f5f9;
	display: flex;
	align-items: center;
	justify-content: center;
	overflow: hidden;
	color: #64748b;

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

	.el-icon {
		font-size: 16px;
	}
}

.workspace-item-info {
	flex: 1;
	min-width: 0;
}

.workspace-item-name {
	font-size: 12px;
	font-weight: 500;
	color: #1e293b;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

/* 预览弹窗样式 */
.preview-dialog-content {
	display: flex;
	justify-content: center;
	align-items: center;
	min-height: 100px;
	max-height: 60vh;
	overflow: auto;
}

.dialog-image {
	max-width: 100%;
	max-height: 60vh;
}

.dialog-video {
	max-width: 100%;
	max-height: 60vh;
}

.dialog-text {
	min-width: 400px;
	max-width: 100%;
	padding: 16px;
	background: #f8fafc;
	border-radius: 8px;
	white-space: pre-wrap;
	word-break: break-word;
	font-size: 14px;
	line-height: 1.6;
}
</style>
