<!-- TreeItem.vue -->
<template>
	<view>
		<view class="item_tree" @click="toggleNode">
			<view class="tree_title">
				<view v-if="item.children.length > 0">
					<u-icon name="arrow-right" v-if="!expanded"></u-icon>
					<u-icon name="arrow-down" v-else></u-icon>
				</view>
				<view class="name_tree_item">{{ item.name }}</view>
			</view>
			<view>
				<u-button type="primary" size="small" text="确定" @click="choice(item)"></u-button>
			</view>
		</view>
		<view v-if="item.children && item.children.length && expanded">
			<tree-item @confirm="confirm" v-for="child in item.children" :key="child.id" :item="child"></tree-item>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'TreeItem',
		props: ['item'],
		data() {
			return {
				expanded: false // 初始状态为折叠
			};
		},
		methods: {
			toggleNode() {
				this.expanded = !this.expanded; // 切换展开和折叠状态
			},
			choice(value) {
				this.$emit('confirm', value.cmpCode)
			},
			confirm(cmpCode) {
				this.$emit('confirm', cmpCode)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item_tree {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx 20rpx;
		border-bottom: 1rpx solid #dfdfdf;
	}

	.tree_title {
		display: flex;
		align-items: center;
		padding-left: 20rpx;
	}

	.name_tree_item {
		margin-left: 10rpx;
		font-size: 30rpx;
	}
</style>