<template>
	<view class="container">
		<uni-section title="基础信息" type="line">
			<uni-forms ref="infoForm" :rules="rules" :modelValue="formData" :label-width="90">
				<uni-forms-item required label="姓名" name="name">
					<uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item required label="性别" name="sex">
					<uni-data-checkbox v-model="formData.sex" :localdata="sexs" />
				</uni-forms-item>
				<uni-forms-item name="birthday" label="出生日期">
					<uni-datetime-picker type="date" v-model="formData.birthday" />
				</uni-forms-item>
				<uni-forms-item required label="联系电话" name="phone">
					<uni-easyinput type="text" v-model="formData.phone" placeholder="请输入联系电话" />
				</uni-forms-item>
			</uni-forms>
		</uni-section>
		<uni-section title="地址信息" type="line">
			<uni-forms ref="addrForm" :modelValue="formData" :label-width="90">
				<uni-forms-item label="小区选择">
					<uni-data-picker v-model="formData.estateId" :map="pickerMap" :localdata="items" placeholder="请选择地址" popup-title="请选择地址" @change="onchange" @nodeclick="onnodeclick"></uni-data-picker>
				</uni-forms-item>
				<uni-forms-item label="楼栋选择">
					<uni-data-select :localdata="range"  @change="selectChange" v-model="formData.buildingId"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="单元选择" v-if="house.unit && house.unit !== null">
					<uni-data-select :localdata="house.unit" @change="unitChange" v-model="formData.unit"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="楼层选择" v-if="house.floor && house.floor !== null">
					<uni-data-select :localdata="house.floor" @change="floorChange" v-model="formData.floor"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="门牌选择" v-if="house.number && house.number !== null">
					<uni-data-select :localdata="house.number" v-model="formData.houseId" @change="houseChange"></uni-data-select>
				</uni-forms-item>
			</uni-forms>
		</uni-section>
		<button class="btn" type="primary" @click="submit('infoForm')">提交</button>
	</view>
</template>

<script>
	import request from '../../api/user.js';
	import requestFamily from '../../api/family.js';
	
	export default {
		data() {
			return {
				formData: {},
				sexs: [
					{ text: '男', value: 0 },
					{ text: '女', value: 1},
				],
				items: [],
				pickerMap: {
					text: 'label',
					value: 'interId'
				},
				range: [],
				house: {},
				building: [],
				unit: '',
				residentReqId: '',
				// 校验规则
				rules: {
					name: {
						rules: [{
							required: true,
							errorMessage: '姓名不能为空'
						}]
					},
					sex: {
						rules: [{
							required: true,
							errorMessage: '性别不能为空'
						}]
					},
					phone: {
						rules: [{
							required: true,
							errorMessage: '电话不能为空'
						}]
					},
				},
			}
		},
		async onLoad(option) {
			this.residentId = option.residentId;
			if (option.residentId) this.queryInfo();
		},
		async mounted() {
			const res = await request.addrTreeSelect();
			this.items = res.data;
		},
		methods: {
			async queryInfo() {
				const userinfo = await requestFamily.getFamilyInfoDetails({ residentId: this.residentId });
				console.log(userinfo.data);
				this.formData = userinfo.data;
				const { estateId, buildingId, unit, floor, houseId } = userinfo.data;
				// 小区存在
				if (estateId) {
					const res = await request.buildingList({ estateId });
					this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
				}
				// 楼栋存在
				if(buildingId) {
				 	await this.selectChange(buildingId);
				}
				// 单元存在
				if(unit) {
					await this.unitChange(unit);
				}
				// 楼层存在
				if(floor) {
					await this.floorChange(floor);
				}
				// 门牌存在
				if(houseId) {
					console.log(houseId)
					await this.houseChange(houseId);
				}
			},
			async onchange(e) {
				const val = e.detail.value[e.detail.value.length - 1].value;
				const res = await request.buildingList({ estateId: val });
				this.range = res.rows.map(e => ({ ...e, text: `${e.number}栋`, value: e.buildingId }));
			},
			onnodeclick(node) {},
			// 楼栋选择
			async selectChange(e) {
				if (!e || e == '') return;
				const res = await request.houseList({ buildingId: e });
				this.building = res.rows;
				const list = [];
				const unit = res.rows.filter(j => j.unit !== null).map(j => ({ ...j, text: `${j.unit}单元`, value: j.unit }));
				const number = res.rows.filter(j => j.number !== null).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
				if (unit && unit.length > 0) {
					unit.forEach(k => {
						const isunit = list.find(j => j.unit == k.unit);
						if (!isunit) list.push(k);
					});
					this.$set(this.house, 'unit', list);
					return;
				}
				if (number && number.length > 0) {
					number.forEach(k => {
						const isunit = list.find(j => j.number == k.number);
						if (!isunit) list.push(k);
					});
					this.$set(this.house, 'number', list);
					return;
				}
				
			},
			// 单元选择
			unitChange(e) {
				this.unit = e;
				const list = [];
				const floor = this.building.filter(j => j.unit == e).map(j => ({ ...j, text: `${j.floor}层`, value: j.floor }));
				floor.forEach(k => {
					const isunit = list.find(j => j.floor == k.floor);
					if (!isunit) list.push(k);
				});
				this.$set(this.house, 'floor', list);
			},
			// 楼层选择
			floorChange(e) {
				const list = [];
				const number = this.building.filter(j => j.floor == e && j.unit == this.unit).map(j => ({ ...j, text: `${j.number}号`, value: j.houseId }));
				number.forEach(k => {
					const isunit = list.find(j => j.number == k.number);
					if (!isunit) list.push(k);
				});
				this.$set(this.house, 'number', list);
			},
			// 门牌选择
			houseChange(e) {
				if (!e || e == '') return;
				const dept = this.house.number.find(j => j.houseId == e);
				this.formData.deptId = dept.deptId;
			},
			// 提交
			async submit(ref) {
				this.$refs[ref].validate(async (err, formdata) => {
					if (!err) {
						if (!this.formData.houseId || this.formData.houseId == '') {
							uni.showToast({
								title: '请选择地址信息',
								duration: 2000,
							});
							return;
						}
						this.formData.reqType = this.formData.residentReqId ? 1 : 0;
						const res = await requestFamily.familyInfo(this.formData);
						if (res.code == 200) {
							uni.showToast({
								title: this.formData.residentReqId ? '修改成功' : '添加成功',
								duration: 2000,
							});
							uni.navigateBack()
						}
					}
				})
			},
		}
	}
</script>

<style>
	.uni-section {
		padding-bottom: 10px;
		margin-bottom: 10px;
	}
	.uni-select__selector {
		z-index: 999 !important;
	}
	.uni-forms, .btn {
		width: 90%;
		margin: 10px auto;
	}
</style>
