<template>
	<div class="menu-panel">
		<pi-icon icon="el-icon-zoom-in" size="18" title="放大" @click="lf.zoom(true)"/>
		<pi-icon icon="el-icon-zoom-out" size="18" title="缩小" @click="lf.zoom(false)"/>
		<pi-icon icon="el-icon-refresh-left" size="18" title="大小还原" @click="lf.resetZoom()"/>
		<pi-icon icon="el-icon-aim" size="18" title="居中" @click="lf.translateCenter()"/>
		<pi-icon icon="el-icon-position" size="18" title="位置还原" @click="lf.resetTranslate()"/>
		<pi-icon icon="pi-icon-auto-layout" size="18" title="自动布线" @click="layout"/>
		<pi-icon :class="{disabled: !canUndo}" icon="el-icon-back" size="18" title="上一步"
				 @click="canUndo && lf.undo()"/>
		<pi-icon :class="{disabled: !canRedo}" icon="el-icon-right" size="18" title="下一步"
				 @click="canRedo && lf.redo()"/>
		<pi-icon icon="el-icon-delete" size="18" title="删除" @click="remove"/>
		<pi-icon icon="el-icon-map-location" size="18" title="小地图" @click="map"/>
		<pi-icon icon="el-icon-download" size="18" title="下载图片" @click="download"/>
	</div>
</template>

<script setup lang="ts">
import {onMounted, onUnmounted, ref} from "vue";
import LogicFlow from "@logicflow/core";
import {ElMessageBox} from "element-plus";
import {useI18n} from "vue-i18n";

const props = defineProps({
	lf: {type: LogicFlow}
})
const {t} = useI18n()

let canUndo = ref(false)
let canRedo = ref(false)

const updateHistoryButtons = () => {
	// 直接从 lf.history 读取最新长度
	canUndo.value = props.lf.history.undos.length > 0;
	canRedo.value = props.lf.history.redos.length > 0;
};

onMounted(() => {
	if (props.lf) {
		props.lf.on('history:change', updateHistoryButtons)
		// 初始化一次状态
		updateHistoryButtons()
	}
})

onUnmounted(() => {
	// 记得移除监听器
	props.lf?.off('history:change', updateHistoryButtons)
})

function remove() {
	// 在你的Vue组件或事件回调中
	const selectedElements = props.lf.getSelectElements()
	// 提取选中的节点
	const selectedNodes = selectedElements.nodes
	if (selectedNodes.length > 0) {
		ElMessageBox.confirm(t('flow.deleteNode'), t('system.warning'), {
			confirmButtonText: t('system.ok'), cancelButtonText: t('system.cancel'), type: 'warning'
		}).then(() => {
			props.lf.deleteNode(selectedNodes[0].id)
		}).catch(() => {
		})
		return
	}
	// 提取选中的连线
	const selectedEdges = selectedElements.edges
	if (selectedEdges.length > 0) {
		ElMessageBox.confirm(t('flow.deleteLine'), t('system.warning'), {
			confirmButtonText: t('system.ok'), cancelButtonText: t('system.cancel'), type: 'warning'
		}).then(() => {
			props.lf.deleteEdge(selectedEdges[0].id)
		}).catch(() => {
		})
		return
	}
}

function map() {
	const miniMap = props.lf.extension.miniMap as any
	if (miniMap.isShow) {
		miniMap.hide()
	} else {
		miniMap.show()
	}
}

function download() {
	props.lf.getSnapshot("下载-" + (Date.now()), {backgroundColor: "#FFFFFF"})
}

function layout() {
	const dagre = props.lf.extension.dagre as any
	// 先应用布局
	dagre.layout({
		nodesep: 50,
		ranksep: 80,
		isDefaultAnchor: true
	})
	// 然后适配视图
	props.lf.fitView()
}
</script>

<style lang="scss" scoped>
.menu-panel {
	background: rgb(255, 255, 255);
	top: 10px;
	right: 330px;
	border-radius: 12px;
	display: flex;
	align-items: center;
	position: absolute;
	z-index: 999;
	padding: 10px;
	box-shadow: 3px 0 10px 1px #e4e0db;
	gap: 10px;

	i {
		cursor: pointer;
	}

	.disabled {
		color: #909399;
	}
}
</style>
