<!-- TreeItem.vue -->
<template>
	<view>
		<view class="item_tree frame">
			<view class="tree_title" style="width: 100%;" @click="choice(item)">
				<view v-if="item.children.length > 0"  @click="toggleNode">
					<u-icon name="arrow-right" v-if="!expanded"></u-icon>
					<u-icon name="arrow-down" v-else></u-icon>
				</view>
				<view v-else style="width: 32rpx;"></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 style="padding-left: 20rpx;" @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: true // 初始状态为折叠
			};
		},
		methods: {
			toggleNode() {
				this.expanded = !this.expanded; // 切换展开和折叠状态
			},
			choice(value) {
				this.$emit('confirm', value)
			},
			confirm(cmpCode) {
				this.$emit('confirm', cmpCode)
			}
		}
	}
</script>

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

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

	.name_tree_item {
		width: 60%;
		margin-left: 10rpx;
		font-size: 30rpx;
		white-space: nowrap;
		text-overflow: ellipsis;
		overflow: hidden;
	}
</style>