<template>
	<div class="hui-left-tree">
		<div class="hui-left-tree-title">
			<span class="hui-left-tree-sub">资产列表</span>
		</div>
		<div class="hui-left-tree-content">
			<el-tree :data="treeData" :props="defaultProps">
				<div :class="nowData.id === data.id ? 'custom-tree-node active':'custom-tree-node'"
					@click.stop="filterDevice(data)" slot-scope="{ node, data }">
					<i :class="'iconfont ' + (data.deviceId ? 'huifont-shebeiguanli' : 'huifont-xiangmuguanli')">
					</i>
					<div class="label">{{node.label}}</div>
				</div>
			</el-tree>
		</div>
	</div>
</template>

<script>
	import {
		initDevicePartList,
		getDevicePartList
	} from '@/api/property'
	export default {
		props: ['type'],
		data() {
			return {
				treeData: [],
				nowData: {},
				defaultProps: {
					children: 'children',
					label: 'name'
				}
			}
		},
		mounted() {
			this.initDeviceList();
		},
		methods: {
			initDeviceList() {
				getDevicePartList(this.$store.getters.organization.id, this.$store.getters.project.id).then(res => {
					if (res.state) {
						if (res.data.length === 0) {
							initDevicePartList(this.$store.getters.organization.id, this.$store.getters.project.id)
								.then(res => {
									if (res.state) this.initDeviceList();
								})
						} else {
							this.treeData = res.data;
							this.returnChildren(this.treeData, []);
						}
					}
				})
			},
			returnChildren(data, parentData) {
				if (this.type === 1) {
					data.forEach(item => {
						item['parentData'] = [...parentData, item.name];
						if (item.children && item.deviceList) {
							let obj = item.deviceList.map(res => {
								res['deviceId'] = res.id;
								res['id'] = -res.id;
								return res;
							})
							item.children = item.children.concat(obj);
						}
						if (item.children && item.children.length > 0) this.returnChildren(item.children,
							[...parentData, item.name]);
					});
				} else {
					data.forEach(item => {
						item['parentData'] = [...parentData, item.name];
						if (item.children && item.children.length > 0) this.returnChildren(item.children, [...
							parentData, item.name
						]);
					});
				}
			},
			filterDevice(data) {
				let option = {};
				if (this.nowData.id === data.id) {
					this.nowData = {};
				} else {
					this.nowData = data;
					if (this.nowData.deviceId) {
						option['deviceId'] = this.nowData.deviceId;
					} else {
						option['deviceLevelIds'] = this.getStorageIds([data]);
					}
				}
				this.$emit('filterDevice', option, data.parentData);
			},
			getStorageIds(array) {
				//获取档案库id
				let ids = [];

				function getIds(array, ids) {
					array.forEach(item => {
						if (item.id) {
							ids.push(item.id);
						}
						if (item.children) {
							ids = getIds(item.children, ids);
						}
					})
					return ids;
				}
				if (Array.isArray(array)) {
					if (array.length == 0) {
						return [];
					}
				} else {
					return [];
				}
				ids = getIds(array, ids);
				return ids;
			},
		},
	}
</script>

<style>
</style>