<template>
	<div class="input-shell">
		<div class="input-card" :class="{ 'is-focused': isFocused }">
			<!-- 已选中状态标签栏 -->
			<div v-if="selectedWorkflowId !== null || selectedSkill !== 'default'" class="selected-tags">
				<div v-if="selectedWorkflowId !== null" class="selected-tag workflow-tag">
					<el-icon><Promotion /></el-icon>
					<span>{{ commonWorkflows.find((w) => w.id === selectedWorkflowId)?.name }}</span>
					<el-icon class="tag-close" @click="selectedWorkflowId = null"><Close /></el-icon>
				</div>
				<div v-if="selectedSkill !== 'default'" class="selected-tag skill-tag">
					<el-icon><MagicStick /></el-icon>
					<span>{{ skillLabels[selectedSkill] }}</span>
					<el-icon class="tag-close" @click="selectedSkill = 'default'"><Close /></el-icon>
				</div>
			</div>

			<!-- 文本输入区 -->
			<el-input
				v-model="message"
				type="textarea"
				:autosize="{ minRows: 1, maxRows: 6 }"
				placeholder="有什么想聊的？按 Enter 发送，Shift+Enter 换行"
				class="message-input"
				@focus="isFocused = true"
				@blur="isFocused = false"
				@keydown.enter.exact.prevent="handleSend"
				@keydown.enter.shift.exact="() => {}"
			/>

			<!-- 底部工具栏 -->
			<div class="input-toolbar">
				<div class="toolbar-left">
					<!-- 上传附件 -->
					<button class="tool-icon-btn" title="上传文件" @click="handleAttachment">
						<el-icon><Paperclip /></el-icon>
					</button>

					<!-- 选择技能 -->
					<el-dropdown trigger="click" placement="top-start" @command="handleSkillSelect">
						<button class="tool-icon-btn" :class="{ active: selectedSkill !== 'default' }" title="选择技能">
							<el-icon><MagicStick /></el-icon>
							<span class="tool-label">技能</span>
						</button>
						<template #dropdown>
							<el-dropdown-menu>
								<el-dropdown-item command="default">
									<el-icon><CircleClose /></el-icon>不使用技能
								</el-dropdown-item>
								<el-dropdown-item divided command="code-review">代码审查</el-dropdown-item>
								<el-dropdown-item command="refactor">代码重构</el-dropdown-item>
								<el-dropdown-item command="writing">内容写作</el-dropdown-item>
							</el-dropdown-menu>
						</template>
					</el-dropdown>
				</div>

				<div class="toolbar-right">
					<span class="hint-text">Shift+Enter 换行</span>
					<button class="send-btn" :disabled="!message.trim()" @click="handleSend">
						<el-icon><Top /></el-icon>
					</button>
				</div>
			</div>
		</div>

		<!-- 快捷工作流胶囊（输入框下方） -->
		<div v-if="commonWorkflows.length > 0" class="workflow-shortcuts">
			<button
				v-for="wf in commonWorkflows"
				:key="wf.id"
				class="shortcut-pill"
				:class="{ active: selectedWorkflowId === wf.id }"
				@click="toggleWorkflow(wf.id)"
			>
				<el-icon><Promotion /></el-icon>
				{{ wf.name }}
			</button>
		</div>
	</div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Top, MagicStick, Promotion, Close, CircleClose, Paperclip } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';

interface Emits {
	(e: 'send', message: string): void;
}

interface Workflow {
	id: number;
	name: string;
	prefix: string;
}

const emit = defineEmits<Emits>();
const message = ref('');
const isFocused = ref(false);
const selectedSkill = ref('default');
const selectedWorkflowId = ref<number | null>(null);

const skillLabels: Record<string, string> = {
	default: '选择技能',
	'code-review': '代码审查',
	refactor: '代码重构',
	writing: '内容写作',
};

const skillPrefixes: Record<string, string> = {
	default: '',
	'code-review': '[技能：代码审查] 请帮我审查下面这段代码，找出潜在问题并给出改进建议：\n',
	refactor: '[技能：代码重构] 请帮我重构下面这段代码，提升可读性和可维护性：\n',
	writing: '[技能：内容写作] 请根据下面的需求帮我创作内容：\n',
};

const commonWorkflows: Workflow[] = [
	{ id: 1, name: '审查', prefix: '[工作流：审查] 完成后请自动生成单元测试：\n' },
	{ id: 2, name: '文档再编码', prefix: '[工作流：文档再编码] 请先编写接口文档，再实现代码：\n' },
	{ id: 3, name: '代码重构', prefix: '[工作流：代码重构] 请帮我重构这段代码：\n' },
	{ id: 4, name: '需求分析', prefix: '[工作流：需求分析] 请帮我分析这个需求并拆解任务：\n' },
];

const handleSend = () => {
	const msg = message.value.trim();
	if (!msg) return;
	const skillPrefix = skillPrefixes[selectedSkill.value] || '';
	const workflowPrefix = selectedWorkflowId.value !== null ? commonWorkflows.find((w) => w.id === selectedWorkflowId.value)?.prefix || '' : '';
	const finalMessage = (skillPrefix + workflowPrefix + msg).trim();
	emit('send', finalMessage);
	message.value = '';
	selectedWorkflowId.value = null;
};

const handleAttachment = () => {
	ElMessage.info('附件上传功能开发中...');
};

const handleSkillSelect = (key: string | number | object | null) => {
	selectedSkill.value = String(key ?? 'default');
};

const toggleWorkflow = (id: number) => {
	selectedWorkflowId.value = selectedWorkflowId.value === id ? null : id;
};
</script>

<style scoped lang="scss">
.input-shell {
	padding: 0 24px 24px;
	background: transparent;
}

.input-card {
	max-width: 860px;
	margin: 0 auto;
	background: #fff;
	border: 1.5px solid #e2e8f0;
	border-radius: 16px;
	box-shadow: 0 2px 12px rgba(15, 23, 42, 0.07);
	transition:
		border-color 0.2s,
		box-shadow 0.2s;
	overflow: hidden;

	&.is-focused {
		border-color: #93c5fd;
		box-shadow:
			0 0 0 3px rgba(59, 130, 246, 0.12),
			0 2px 12px rgba(15, 23, 42, 0.07);
	}
}

/* 已选标签栏 */
.selected-tags {
	display: flex;
	flex-wrap: wrap;
	gap: 6px;
	padding: 10px 14px 0;
}

.selected-tag {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	padding: 3px 8px 3px 8px;
	border-radius: 6px;
	font-size: 12px;
	font-weight: 500;
	line-height: 1.4;

	&.workflow-tag {
		background: #eff6ff;
		color: #2563eb;
		border: 1px solid #bfdbfe;
	}

	&.skill-tag {
		background: #f5f3ff;
		color: #7c3aed;
		border: 1px solid #ddd6fe;
	}

	.tag-close {
		margin-left: 2px;
		cursor: pointer;
		opacity: 0.6;
		transition: opacity 0.15s;

		&:hover {
			opacity: 1;
		}
	}
}

/* 文本域 */
.message-input {
	:deep(.el-textarea__inner) {
		border: none;
		box-shadow: none;
		resize: none;
		padding: 14px 16px 8px;
		font-size: 15px;
		line-height: 1.6;
		color: #0f172a;
		background: transparent;
		min-height: 28px !important;

		&::placeholder {
			color: #94a3b8;
			font-weight: 400;
		}
	}
}

/* 底部工具栏 */
.input-toolbar {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 6px 10px 10px 12px;
}

.toolbar-left,
.toolbar-right {
	display: flex;
	align-items: center;
	gap: 4px;
}

.tool-icon-btn {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	height: 30px;
	padding: 0 10px;
	border: none;
	border-radius: 8px;
	background: transparent;
	color: #64748b;
	font-size: 13px;
	font-weight: 500;
	cursor: pointer;
	transition:
		background 0.15s,
		color 0.15s;
	outline: none;

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

	&:hover {
		background: #f1f5f9;
		color: #334155;
	}

	&.active {
		background: #eff6ff;
		color: #2563eb;
	}
}

.tool-label {
	font-size: 12px;
}

.hint-text {
	font-size: 11px;
	color: #cbd5e1;
	margin-right: 6px;
	user-select: none;
}

.send-btn {
	display: inline-flex;
	align-items: center;
	justify-content: center;
	width: 32px;
	height: 32px;
	border: none;
	border-radius: 8px;
	background: #2563eb;
	color: #fff;
	cursor: pointer;
	transition:
		background 0.15s,
		transform 0.1s,
		opacity 0.15s;
	outline: none;

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

	&:hover:not(:disabled) {
		background: #1d4ed8;
		transform: translateY(-1px);
	}

	&:disabled {
		background: #e2e8f0;
		color: #94a3b8;
		cursor: not-allowed;
		transform: none;
	}
}

/* 快捷工作流胶囊 */
.workflow-shortcuts {
	max-width: 860px;
	margin: 10px auto 0;
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
}

.shortcut-pill {
	display: inline-flex;
	align-items: center;
	gap: 5px;
	height: 28px;
	padding: 0 12px;
	border: 1px solid #e2e8f0;
	border-radius: 999px;
	background: rgba(255, 255, 255, 0.7);
	color: #475569;
	font-size: 12px;
	font-weight: 500;
	cursor: pointer;
	backdrop-filter: blur(4px);
	transition: all 0.15s;
	outline: none;

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

	&:hover {
		border-color: #93c5fd;
		color: #2563eb;
		background: rgba(239, 246, 255, 0.9);
	}

	&.active {
		border-color: #3b82f6;
		background: #2563eb;
		color: #fff;
		box-shadow: 0 2px 8px rgba(37, 99, 235, 0.3);
	}
}
</style>
