<template>
	<div class="property-flow-form">
		<project-item ref="projectItem" :form="propertyForm"></project-item>
		<el-form :model="propertyForm" label-position="top">
			<el-form-item label="资产名称" required>
				<el-cascader v-model="departId" :options="departList" :props="defaultProps" @change="changePart"
					placeholder="请选择资产名称">
				</el-cascader>
			</el-form-item>
			<el-form-item label="变更内容" required>
				<el-select v-model="propertyForm.content" placeholder="请选择变更内容">
					<el-option label="位置变更" value="位置变更"></el-option>
					<el-option label="属性变更" value="属性变更"></el-option>
					<el-option label="业务变更" value="业务变更"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="变更类型" required>
				<el-select v-model="propertyForm.type" placeholder="请选择变更类型">
					<el-option label="资产购买" value="资产购买"></el-option>
					<el-option label="资产处置" value="资产处置"></el-option>
					<el-option label="资产重估" value="资产重估"></el-option>
					<el-option label="资产租赁" value="资产租赁"></el-option>
					<el-option label="资产改良" value="资产改良"></el-option>
					<el-option label="资产合并与分割" value="资产合并与分割"></el-option>
					<el-option label="资产抵押与解抵押" value="资产抵押与解抵押"></el-option>
					<el-option label="资产减值" value="资产减值"></el-option>
					<el-option label="资产捐赠" value="资产捐赠"></el-option>
					<el-option label="资产的法律变更" value="资产的法律变更"></el-option>
				</el-select>
			</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: {
					remark: '',
					content: '',
					type: ''
				},
				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() {
				let obj = this.$refs.projectItem.returnItem();
				this.propertyForm['projectId'] = this.$store.getters.project.id;
				this.propertyForm['projectItemId'] = obj.projectItem.id;
				this.propertyForm['projectItemTargetId'] = obj.projectItemTarget.id;
				return {
					formData: this.propertyForm,
					commonForm: {
						projectId: this.$store.getters.project.id,
						projectName: this.$store.getters.project.projectName,
						projectItemId: obj.projectItem.id,
						projectItemName: obj.projectItem.name,
						projectItemTargetId: obj.projectItemTarget.id,
						projectItemTargetName: obj.projectItemTarget.name,
						deviceId: this.propertyForm.deviceId
					}
				}
			}
		},
		watch: {
			flowForm() {
				if (JSON.stringify(this.flowForm) === "{}") return;
				this.propertyForm = this.flowForm;
				this.getPartList();
			}
		},
		components: {
			projectItem
		},
	}
</script>

<style>
</style>