<script setup lang="ts">
import { ref, onMounted, onUnmounted, reactive, toRefs, watch } from 'vue'
import Tree from './tree/index.vue'
// import { versionsList } from './tools/versionsData'
import { findAndAddChild, findAndRemoveChild } from '../../../../../utils/treeDiagram'
import { useProjectStore } from '@/stores'
import { versionTree } from '@/api/versitonTree'
import { downloadImage } from '@/components/Canvas/tools/tools'

const props = defineProps({
	versionTreeData: {
		type: Object,
		default: () => {
			return {
				drawer: false,
				// list: []
			}
		}
	}
})

const emit = defineEmits([
	'restore','update:versionTreeData','selectNode'
])

const versionsList = ref([])

const projectStore = useProjectStore()
let oldProjectId:any = ''
watch(()=>props.versionTreeData?.drawer, (newVal, oldVal) => {
	console.log(newVal,oldProjectId,projectStore.state.id)
	if(newVal || (oldProjectId !== projectStore.state.id && projectStore.state.id)){
		getVersionTree()
		oldProjectId = JSON.parse(JSON.stringify(projectStore.state.id))
	}
})

const getVersionTree = ()=>{
	versionTree({
		projectId: projectStore.state.id
	}).then(res => {
		if(!res)return
		setVersionsList([res])
		treeKey.value++
	})
}

const setVersionsList = (res)=>{
	//设置versionId
	function traverseArray(items,father, callback) {
		for (let i = 0; i < items.length; i++) {
			const item = items[i]
			// if(!item?.url)continue
			callback(item, i,father)
			if (item.children && Array.isArray(item.children) && item.children.length > 0) {
				traverseArray(item.children, item, callback)
			}
		}
	}
	traverseArray(res,'',(item,i,father)=>{
		item.versionId = father?`${father.versionId}-${i+1}`:'1'
		if(item.id == projectStore.state.nodeId){
			selectItem.value = {...item}
		}
	})
	versionsList.value = res
}

const treeRef = ref(null)
const treeKey = ref(0)
const treeState = ref(true)//

const selectItem:any = ref({})

const openTree = (state)=>{
	treeState.value = state
}

const versionRestore = () => {
	// let id = ''
	// if(selectItem.value?.children?.length > 0){
	// 	function findMaxForYourFormat(items) {
	// 		let max = 0
			
	// 		for (const item of items) {
	// 			// 直接分割并取最后一部分
	// 			const parts = item?.versionId.split('-')
	// 			const lastNumber = parseInt(parts[parts.length - 1], 10)
				
	// 			if (lastNumber > max) {
	// 			max = lastNumber
	// 			}
	// 		}
			
	// 		return max
	// 	}
	// 	id = `${selectItem.value?.versionId}-${findMaxForYourFormat(selectItem.value?.children) + 1}`
	// }else{
	// 	id = `${selectItem.value?.versionId}-1`
	// }
	// let addObj = {
	// 	id,
	// 	name:`V${id}`
	// }
	// findAndAddChild(versionsList.value, selectItem.value?.versionId, addObj)
	// selectItem.value = {...addObj}
	// treeKey.value++
	emit('restore')
	emit('update:versionTreeData', {...props.versionTreeData, drawer: false})
}
const versionDelete = (versionDetail)=>{
	if(!selectItem.value?.versionId)return
	findAndRemoveChild(versionsList.value, selectItem.value?.versionId)
	treeKey.value++
}


let data = reactive({})
// onMounted(() => {setVersionsList('')})
onUnmounted(() => {})
defineExpose({getVersionTree})
const {} = toRefs(data)
</script>
<template>
	<div class="versionTree">
		<el-drawer
			v-model="versionTreeData.drawer"
			:close-on-press-escape="false"
			:size="treeState ? '73.5rem' : '73.5rem'"
			body-class="versionTreeBody"
			:with-header="false"
		>
			<div class="versionTreeTitle">
				<span>Version Tree</span>
				<div class="closeBtn" @click="versionTreeData.drawer = false">
					<div class="closeIcon">
						<SvgIcon name="close" />
					</div>
				</div>
			</div>
			<div class="expandBtnBox" v-if="versionsList.length > 0">
				<div class="btn" v-if="selectItem.url" @click="downloadImage(selectItem.url,'image.png')">
					<div class="versionExport">
						<SvgIcon name="versionExport" size="17" color="rgba(0, 0, 0, 0.6)" />
					</div>
					<span>{{ $t('VersionTree.export') }}</span>	
				</div>
			</div>
			<!-- <div class="expandBtnBox">
				<div class="btn" @click="openTree(true)">
					<div class="bg left" :class="{'active':treeState}"></div>
					<span>{{ $t('VersionTree.linearNodeTree') }}</span>	
				</div>
				<div class="btn" @click="openTree(false)">
					<div class="bg right" :class="{'active':!treeState}"></div>
					<span>{{ $t('VersionTree.branchingNodeTree') }}</span>	
				</div>
			</div> -->
			<div class="versionTreeBox" v-if="versionsList.length > 0">
				<div class="tree">
					<Tree 
						v-if="versionsList.length > 0"
						ref="treeRef"
						:versionsList="versionsList"
						:treeState="treeState" 
						v-model:selectItem="selectItem"
						@versionRestore="versionRestore"
						@versionDelete="versionDelete"
						@selectNode="$emit('selectNode')"
						:key="treeKey"
					></Tree>
				</div>
			</div>
		</el-drawer>
	</div>
</template>
<style lang="less" scoped>
	.versionTree{
		--border-radius: 1rem;
		--treeItem-width: 5.4rem;
		--treeItem-height: 5.4rem;
		--treeItem-raduis: 50%;
		--treeItem-border: 2px solid #C1C1C1;
		--treeItem-background: #ffffff;
		--treeItem-active-background: #e6e6e6;

	:deep(.versionTreeBody) {
		--el-drawer-padding-primary: 0rem;
		display: flex;
		flex-direction: column;
	}
	.versionTreeTitle{
		width: 100%;
		height: 8rem;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 .8rem 0 2.4rem;
		border-bottom: 1px solid #C9C9C9;
		> span{
			font-size: 2rem;
			font-weight: 600;
			font-family: 'SemiBold';
		}
		> .closeBtn{
			width: 2.4rem;
			height: 2.4rem;
			cursor: pointer;
		}
	}
	.expandBtnBox{
		margin-left: auto;
		margin-top: 2.2rem;
		margin-right: 3rem;
		height: 3.3rem;
		> .btn{
			padding: 0 1.8rem;
			line-height: 3rem;
			cursor: pointer;
			display: flex;
			align-items: center;
			justify-content: center;
			border-radius: .8rem;
			border: 1.5px solid #D9D9D9;
			color: rgba(0, 0, 0, 0.6);
			> .versionExport{
				margin-right: .6rem;
			}
			> span{
				font-weight: 500;
				font-size: 1.4rem;
				letter-spacing: Letter Spacing;
			}
		}
	}
	.versionTreeBox {
		flex: 1;
		display: flex;
		overflow: hidden;
		> .tree {
			flex: 1;
			height: 100%;
			overflow: hidden;
			padding: 2.5rem 4rem 5.4rem 4rem;
		}
	}
}
</style>
