<template>
	<el-dialog v-model="visible" title="选择技能" width="900px" :close-on-click-modal="false" @close="handleClose">
		<div class="search-bar">
			<el-input v-model="searchParams.keyword" placeholder="搜索技能名称或描述" clearable @clear="handleSearch">
				<template #prefix
					><el-icon><Search /></el-icon
				></template>
			</el-input>
			<el-button type="primary" @click="handleSearch">搜索</el-button>
		</div>
		<div class="skill-list" v-loading="loading">
			<el-empty v-if="!loading && skillList.length === 0" description="暂无技能数据" :image-size="100" />
			<div v-else class="skill-grid">
				<div
					v-for="skill in skillList"
					:key="skill.id"
					class="skill-card"
					:class="{ selected: selectedSkill?.id === skill.id }"
					@click="handleSelectSkill(skill)"
				>
					<div class="skill-card-header">
						<div class="skill-category">{{ skill.category }}</div>
						<el-icon v-if="selectedSkill?.id === skill.id" class="check-icon" color="#67c23a"><CircleCheck /></el-icon>
					</div>
					<div class="skill-card-body">
						<h3 class="skill-name">{{ skill.name }}</h3>
						<p class="skill-desc">{{ skill.description || '暂无描述' }}</p>
					</div>
				</div>
			</div>
		</div>
		<div v-if="pagination.total > 0" class="pagination-wrap">
			<el-pagination
				v-model:current-page="pagination.pageNum"
				v-model:page-size="pagination.pageSize"
				:total="pagination.total"
				:page-sizes="[10, 20, 50]"
				layout="total, prev, pager, next"
				small
				@current-change="handlePageChange"
			/>
		</div>
		<template #footer>
			<el-button @click="handleClose">取消</el-button>
			<el-button type="primary" @click="handleConfirm" :disabled="!selectedSkill">确定</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import { Search, CircleCheck } from '@element-plus/icons-vue';
import { getUserSkilllistUser, type SkillItem } from '/@/api/digitalHuman/skill';

interface Props {
	modelValue: boolean;
	defaultSkill?: SkillItem | null;
}

interface Emits {
	(e: 'update:modelValue', value: boolean): void;
	(e: 'confirm', skill: SkillItem): void;
}

const props = withDefaults(defineProps<Props>(), {
	modelValue: false,
	defaultSkill: null,
});

const emit = defineEmits<Emits>();

const visible = ref(false);
const searchParams = reactive({ keyword: '' });
const pagination = reactive({ pageNum: 1, pageSize: 10, total: 0 });
const skillList = ref<SkillItem[]>([]);
const loading = ref(false);
const selectedSkill = ref<SkillItem | null>(null);

watch(
	() => props.modelValue,
	(val) => {
		visible.value = val;
		if (val) {
			selectedSkill.value = props.defaultSkill || null;
			fetchSkillList();
		}
	}
);

watch(visible, (val) => {
	if (!val) {
		emit('update:modelValue', false);
	}
});

const fetchSkillList = async () => {
	loading.value = true;
	try {
		const params = { pageNum: pagination.pageNum, pageSize: pagination.pageSize, keyword: searchParams.keyword || undefined };
		const res = await getUserSkilllistUser(params, { errorMode: 'message' });
		skillList.value = res.data?.list || [];
		pagination.total = res.data?.total || 0;
	} catch (error) {
		skillList.value = [];
		pagination.total = 0;
	} finally {
		loading.value = false;
	}
};

const handleSearch = () => {
	pagination.pageNum = 1;
	fetchSkillList();
};

const handlePageChange = () => {
	fetchSkillList();
};

const handleSelectSkill = (skill: SkillItem) => {
	selectedSkill.value = skill;
};

const handleConfirm = () => {
	if (selectedSkill.value) {
		emit('confirm', selectedSkill.value);
		handleClose();
	}
};

const handleClose = () => {
	visible.value = false;
	selectedSkill.value = null;
};
</script>

<style scoped lang="scss">
.search-bar {
	display: flex;
	gap: 12px;
	margin-bottom: 20px;
}
.skill-list {
	min-height: 300px;
	max-height: 400px;
	overflow-y: auto;
}
.skill-grid {
	display: grid;
	grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
	gap: 16px;
}
.skill-card {
	background: #f8fafc;
	border-radius: 8px;
	padding: 16px;
	cursor: pointer;
	transition: all 0.3s ease;
	border: 2px solid transparent;
}
.skill-card:hover {
	box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
	transform: translateY(-2px);
}
.skill-card.selected {
	border-color: #67c23a;
	background: #f0f9ff;
}
.skill-card-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	margin-bottom: 12px;
}
.skill-category {
	display: inline-block;
	padding: 2px 8px;
	background: #eff6ff;
	color: #3b82f6;
	border-radius: 4px;
	font-size: 12px;
	font-weight: 600;
}
.check-icon {
	font-size: 20px;
}
.skill-card-body {
	flex: 1;
}
.skill-name {
	font-size: 16px;
	font-weight: 600;
	color: #1f2937;
	margin: 0 0 8px 0;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}
.skill-desc {
	font-size: 13px;
	color: #64748b;
	line-height: 1.5;
	margin: 0;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
	overflow: hidden;
	min-height: 40px;
}
.pagination-wrap {
	display: flex;
	justify-content: center;
	margin-top: 20px;
}
</style>
