<template>
	<div class="home-entrance-container">
		<el-dialog v-model="state.dialogVisible" draggable title="常用入口设置">
			<div class="entrance-list">
				<p class="entrance-list-title">首页入口</p>
				<div class="mb20" v-loading="state.loading">
					<div>
						<draggable
							:list="state.entranceList"
							:force-fallback="true"
							animation="300"
							@end="onEnd"
							itemKey="id"
							class="entrance-list-box"
							v-show="state.entranceList.length"
						>
							<template #item="item">
								<div class="entrance-list-box-item" :title="item.element.pageName">
<!--									<img v-lazy="getImageUrl(item.element.fastIcon)" alt="" class="my-handle"  src=""/>-->
                  <SvgIcon :name="item.element.icon" class="my-handle" size="42px"/>
									<p class="entrance-list-box-item-name text-no-wrap">{{ item.element.pageName }}</p>
									<SvgIcon name="ele-RemoveFilled" class="plus-icon location" color="#ccc" size="24px" @click="removeOne(item.element)" />
								</div>
							</template>
						</draggable>
					</div>
					<Empty v-show="!state.entranceList.length" />
				</div>
			</div>
			<div class="entrance-select">
				<p class="entrance-select-title">
					<span>系统功能</span>
					<el-input v-model="state.searchKey" :prefix-icon="Search" placeholder="请输入关键字" class="input" @keyup.enter="getEntranceSelect">
						<template #suffix>
							<el-button type="primary" size="small" round @click="getEntranceSelect">搜索</el-button>
						</template>
					</el-input>
				</p>
				<div class="mb20" v-loading="state.selectLoading">
					<template v-if="state.entranceSelect.length">
						<ul class="entrance-list-box">
							<li class="entrance-list-box-item" v-for="(item, index) in state.entranceSelect" :key="item.id" :title="item.pageName">
<!--								<img v-lazy="getImageUrl(item.fastIcon)" alt="" class="mb10"  src=""/>-->
                <SvgIcon :name="item.icon" size="42px" class="entrance-list-box-item-icon"/>
								<p class="entrance-list-box-item-name text-no-wrap">{{ item.pageName }}</p>
								<SvgIcon
									name="ele-CirclePlusFilled"
									class="remove-icon location"
									color="var(--el-color-primary)"
									size="24px"
									@click="plusOne(item)"
								/>
							</li>
						</ul>
					</template>
					<Empty v-else />
				</div>
			</div>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="state.dialogVisible = false" class="default-button">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts" setup name="homeEntrance">
import { reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { Search } from '@element-plus/icons-vue';
import { getImageUrl } from '/@/utils/tools';
import { geFastMenu, fastMenu, setFastMenu } from '/@/api/home';
import Draggable from 'vuedraggable';
const state = reactive<any>({
	dialogVisible: false,
	searchKey: '',
	entranceList: [],
	sortEntranceList: [],
	entranceSelect: [],
	loading: false,
	selectLoading: false,
});
const emit = defineEmits(['updateEntrance', 'openDialog', 'closeDialog']);
// 打开弹窗
const openDialog = () => {
	state.dialogVisible = true;
	getEntrance();
	getEntranceSelect();
	emit('openDialog');
};
// 关闭弹窗
const closeDialog = () => {
	state.dialogVisible = false;
	emit('closeDialog');
};
// 获取我的入口
const getEntrance = () => {
	state.loading = true;
	geFastMenu()
		.then((res: any) => {
			state.entranceList = res?.result ?? [];
			state.loading = false;
		})
		.catch(() => {
			state.loading = false;
		});
};
// 获取可选入口
const getEntranceSelect = () => {
	state.selectLoading = true;
	fastMenu({})
		.then((res: any) => {
			state.entranceSelect = res?.result ?? [];
      if(state.searchKey){
        state.entranceSelect = state.entranceSelect.filter((i: any) => i.pageName.includes(state.searchKey));
        if(state.entranceList.length){
          state.entranceSelect = state.entranceSelect.filter((i: any) => !state.entranceList.some((j: any) => j.id === i.id));
        }
      }else{
        if(state.entranceList.length){
          state.entranceSelect = state.entranceSelect.filter((i: any) => !state.entranceList.some((j: any) => j.id === i.id));
        }
      }
			setTimeout(() => {
				state.selectLoading = false;
			}, 300);
		})
		.catch(() => {
			state.selectLoading = false;
		});
};
// 移除
const removeOne = (val: any) => {
	state.entranceList = state.entranceList.filter((i: any) => i.id !== val.id);
	state.entranceSelect.push(val);
};
// 新增
const plusOne = (val: any) => {
	state.entranceList.push(val);
	state.entranceSelect = state.entranceSelect.filter((i: any) => i.id !== val.id);
};
//结束拖拽事件
const onEnd = () => {
	console.log(state.entranceList);
};
// 保存
const onSubmit = async () => {
	try {
		let req = state.sortEntranceList.length
			? state.sortEntranceList.map((item: any) => item.permissionCode)
			: state.entranceList.map((item: any) => item.permissionCode);
		await setFastMenu({ fastMenuArr: req });
		ElMessage.success('操作成功');
		emit('updateEntrance');
	} catch {
		// 处理错误
		state.dialogVisible = false;
	} finally {
		state.dialogVisible = false;
	}
};
defineExpose({ closeDialog, openDialog }); //暴漏方法
</script>
<style lang="scss" scoped>
.home-entrance-container {
	.entrance-list {
		color: var(--hotline-color-text-main);

		&-title {
			padding-bottom: 20px;
			font-size: var(--el-font-size-medium);
		}

		&-box {
			display: grid;
			justify-content: space-between;
			grid-template-columns: repeat(auto-fill, 110px);
			grid-gap: 15px;
			margin-bottom: 20px;

			&-item {
				background: #f0f4ff;
				border-radius: 8px;
				text-align: center;
				padding: 10px 20px;
				width: 110px;
				height: 110px;
				color: var(--el-text-color-regular);
				position: relative;
				user-select: none;
        &-icon{
          display: inline-block;
          width: 40px;
          height: 50px;
          margin-bottom: 10px;
          margin-top: 5px;
        }
				.my-handle {
          display: inline-block;
					width: 40px;
					height: 50px;
					margin-bottom: 10px;
					cursor: grab;
          margin-top: 5px;
				}

				.location {
					position: absolute;
					top: -5px;
					right: -5px;
					cursor: pointer;
				}

				&-name {
					width: 70px;
				}
			}
		}
	}

	.entrance-select {
		color: var(--hotline-color-text-main);

		&-title {
			padding-bottom: 20px;
			font-size: var(--el-font-size-medium);

			span {
				display: inline-block;
				min-width: 80px;
				margin-right: 40px;
			}

			.input {
				max-width: 350px;

				:deep(.el-input__wrapper) {
					border-radius: 40px;
				}
			}
		}
	}
}
</style>
