<!-- firmTree.vue -->
<template>
	<view>
		<view class="item_tree" @click="toggleNode">
			<view class="tree_title" :style="{paddingLeft:item.children.length > 0 ? '20rpx' : '0rpx'}">
				<view class="card_icon_arrow" 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="left_title">
					<view class="name_tree_item">{{ item.name }}</view>
					<view class="distance_title" v-if="getDistance(item.provStore.lat,item.provStore.lng)">
						距您{{getDistance(item.provStore.lat,item.provStore.lng)}}</view>
				</view>
			</view>
			<view>
				<u-button type="primary" size="small" text="确定" @click.native="choice(item)"></u-button>
			</view>
		</view>
		<view v-if="item.children && item.children.length && expanded">
			<firm-tree @confirm="confirm" v-for="child in item.children" :key="child.id" :item="child"></firm-tree>
		</view>
	</view>
</template>

<script>
	import calculateDistance from '@/utils/distance.js'
	export default {
		name: 'firmTree',
		props: ['item'],
		data() {
			return {
				expanded: false // 初始状态为折叠
			};
		},
		methods: {
			getDistance(value, value1) {
				const arr = value + ',' + value1
				const arr1 = calculateDistance(arr)
				return arr1
			},
			toggleNode() {
				this.expanded = !this.expanded; // 切换展开和折叠状态
			},
			choice(value) {
				this.$emit('confirm', value.id)
			},
			confirm(cmpCode) {
				this.$emit('confirm', cmpCode)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.item_tree {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20rpx;
		border-bottom: 2rpx solid transparent;
		border-image: linear-gradient(to bottom, transparent 50%, #e7e6e4 50%) 0 0 100% 0;
	}

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

	.card_icon_arrow {
		position: absolute;
		left: 0rpx;
	}

	.left_title {
		margin-left: 20rpx;
	}

	.name_tree_item {
		color: #303133;
		font-size: 30rpx;
		font-weight: 600;
	}

	.distance_title {
		margin-top: 10rpx;
		font-size: 24rpx;
		color: #909399;
	}
</style>