<template>
	<div class="hui-flex hui-dialog">
		<div class="hui-flex-box hui-dialog-content">
			<el-form ref="projectItemForm" label-position="top" :model="projectItemForm">
				<el-form-item label="楼宇名称" prop="name" :rules="[{required: true, message: '请输入楼宇名称'}]">
					<el-input type="text" v-model="projectItemForm.name" placeholder="请输入楼宇名称"></el-input>
				</el-form-item>
				<el-form-item label="建筑总面积(m²)" prop="area" :rules="[{required: true, message: '请输入建筑总面积'}]">
					<el-input type="number" v-model="projectItemForm.area" placeholder="请输入建筑总面积m²"></el-input>
				</el-form-item>
				<el-form-item label="具体位置">
					<el-input type="text" v-model="projectItemForm.address" placeholder="请输入具体位置">
					</el-input>
				</el-form-item>
				<el-form-item label="产权证书/不动产权证号">
					<el-input type="text" v-model="projectItemForm.propertyCertificateNumber"
						placeholder="请输入产权证书/不动产权证号">
					</el-input>
				</el-form-item>
				<el-form-item label="楼宇图片" class="hui-textarea">
					<upload ref="upload" :list="responsibility" type="insert"></upload>
				</el-form-item>
				<el-form-item label="楼宇标签" class="hui-textarea">
					<tag ref="tag" type="insert" :tagType="2"
						:tagActive="projectItemForm.tagIds ? projectItemForm.tagIds.split(',') : []">
					</tag>
				</el-form-item>
			</el-form>
		</div>
		<div class="hui-dialog-submit">
			<el-button size="medium" @click="$emit('callback')">取 消</el-button>
			<el-button size="medium" type="primary" @click="submit">保 存</el-button>
		</div>
	</div>
</template>

<script>
	import {
		insertProjectItem,
		getProjectItemDetailById,
		updateProjectItem
	} from '@/httpApi/work'
	import upload from '@/components/common/upload'
	import tag from '@/components/common/tag'
	export default {
		props: ['isUpdate', 'detailId'],
		data() {
			return {
				projectItemForm: {
					name: '', //楼宇名称
					area: '',
					address: '',
					propertyCertificateNumber: '',
					picture: '',
					tagIds: ''
				},
				specific: '',
				responsibility: []
			}
		},
		created() {
			this.projectItemForm['projectId'] = this.$store.getters.project.id;
			if (this.isUpdate) {
				getProjectItemDetailById(this.detailId).then(res => {
					if (res.state) {
						this.projectItemForm = res.data;
						if (this.projectItemForm.picture) this.responsibility = JSON.parse(this.projectItemForm
							.picture);
					}
				})
			}
		},
		methods: {
			submit() {
				this.$refs.projectItemForm.validate((valid) => {
					if (valid) {
						let postData = JSON.parse(JSON.stringify(this.projectItemForm));
						postData.data = JSON.stringify({
							specific: this.specific
						});
						postData['picture'] = JSON.stringify(this.$refs.upload.fileList);
						postData['tagIds'] = this.$refs.tag.tagIds();
						if (this.isUpdate) {
							updateProjectItem(postData).then(this.successFunc);
						} else {
							insertProjectItem(postData).then(this.successFunc);
						}
					} else {
						return false;
					}
				});
			},
			successFunc(res) {
				if (res.state) {
					this.$message.success('操作成功');
					this.$emit('callback', 'init');
				}
			}
		},
		components: {
			upload,
			tag
		},
	}
</script>

<style lang="scss"></style>