<template>
	<div class="property-flow-form">
		<el-form :model="propertyForm" label-position="top">
			<el-form-item label="资产名称">
				<el-cascader v-model="departId" :options="departList" :props="defaultProps" @change="changePart"
					placeholder="请选择资产名称">
				</el-cascader>
			</el-form-item>
			<el-form-item label="盘点内容">
				<el-input type="text" v-model="propertyForm.inventoryContent" placeholder="请输入盘点内容"></el-input>
			</el-form-item>
			<el-form-item label="盘点描述" class="hui-textarea">
				<el-input type="textarea" v-model="propertyForm.remark" placeholder="请输入盘点描述" resize="none">
				</el-input>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	import {
		getDevicePartList
	} from '@/httpApi/property'
	import projectItem from '@/components/common/projectItem'
	import {
		findParentIds
	} from '@/uitls'
	export default {
		props: ['flowForm'],
		data() {
			return {
				propertyForm: {
					inventoryContent: '',
					remark: ''
				},
				departList: [],
				departId: [],
				defaultProps: {
					children: 'children',
					label: 'name',
					value: 'id'
				},
				deviceList: []
			}
		},
		created() {
			if (JSON.stringify(this.flowForm) === "{}") return this.getPartList();
			this.propertyForm = this.flowForm;
			this.getPartList();
		},
		methods: {
			getPartList() {
				getDevicePartList(this.$store.getters.organization.id, this.$store.getters.project.id).then(res => {
					if (res.state) {
						this.departList = res.data;
						this.returnChildren(this.departList);
						if (this.propertyForm.deviceId) this.departId = findParentIds(this.departList,
							-this.propertyForm.deviceId);
					}
				})
			},
			returnChildren(data) {
				data.forEach(item => {
					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);
						this.deviceList = this.deviceList.concat(obj);
					}
					if (item.children && item.children.length > 0) this.returnChildren(item.children);
				});
			},
			changePart() {
				this.propertyForm['deviceId'] = -this.departId[this.departId.length - 1];
				this.propertyForm['name'] = this.deviceList.filter(node => node.id === this.departId[this.departId
					.length - 1])[0].name;
			},
			returnForm() {
				return {
					formData: this.propertyForm,
					commonForm: {
						projectId: this.$store.getters.project.id,
						projectName: this.$store.getters.project.projectName,
						projectItemId: '',
						projectItemName: '',
						projectItemTargetId: '',
						projectItemTargetName: '',
						projectItemTargetRoomId: '',
						projectItemTargetRoomName: '',
						deviceId: this.propertyForm.deviceId
					}
				}
			}
		},
		watch: {
			flowForm() {
				if (JSON.stringify(this.flowForm) === "{}") return;
				this.propertyForm = this.flowForm;
				this.getPartList();
			}
		},
		components: {
			projectItem
		},
	}
</script>

<style>
</style>