<template>
	<view class="container">
		<uni-nav-bar dark :fixed="true" backgroundColor="#3F9EFF" statusBar="false" left-icon="left" left-text="返回"
			@clickLeft="toBack">
			<view class="nav-title">
				<text>{{title}}</text>
			</view>
		</uni-nav-bar>
		<uni-forms ref="baseForm" :model="formData">
			<uni-card title="住址定位信息" :is-shadow="false">
				<uni-forms-item name='longitude' label="经度" label-width="100px">
					<uni-easyinput v-model="formData.longitude" placeholder="请录入经度" />
				</uni-forms-item>
				<uni-forms-item name='latitude' label="纬度" label-width="100px">
					<uni-easyinput v-model="formData.latitude" placeholder="请录入纬度" />
				</uni-forms-item>
			</uni-card>
			<uni-card title="群众家庭信息" :is-shadow="false">
				<uni-forms-item name='personName' label="户主姓名" label-width="100px" required>
					<uni-easyinput v-model="formData.personName" placeholder="请录入户主姓名" />
				</uni-forms-item>
				<uni-forms-item name='personId' label="身份证号" label-width="100px" required>
					<uni-easyinput v-model="formData.personId" placeholder="请录入身份证号" />
				</uni-forms-item>
				<uni-forms-item name='dangerAreaPid' label="危险区编码" label-width="100px" required>
					<uni-row>
						<uni-col :span="17">
							<uni-easyinput v-model="formData.dangerAreaPid" placeholder="请录入危险区编码" />
						</uni-col>
						<uni-col :span="7" class="view-flex-inline-end">
							<image class="scan-icon" mode="widthFix" src="/static/images/scan.png"
								@click="onQrCodeClick">
							</image>
						</uni-col>
					</uni-row>
				</uni-forms-item>
				<!-- 				<uni-forms-item name="isHouseholder" label="是否户主" label-width="100px" required>
					<uni-data-checkbox v-model="formData.isHouseholder" :localdata="checkItems.items">
					</uni-data-checkbox>
				</uni-forms-item> -->
			</uni-card>
			<uni-card title="家庭成员" :is-shadow="false">
				<view class="item-button-group">
					<view class="item-button" @click="onResidentAddClick()">
						<view class="items-line">
							<uni-icons class="input-uni-icon" type="personadd" size="18" color="coral" />
							<text class="button-text">添加成员</text>
						</view>
					</view>
				</view>
				<uni-list style="min-height: 40px;">
					<uni-list-item v-for="item in familyMembersList" :key="item.id">
						<template v-slot:body>
							<view class="list-item-block">
								<view class="line">
									<text class="text text-ellipsis" style="color: gray;">姓名：{{item.personName}}</text>
								</view>
								<view class="line">
									<text class="text text-ellipsis" style="color: gray;">身份证号：{{item.personId}}</text>
								</view>
							</view>
						</template>
					</uni-list-item>
				</uni-list>
			</uni-card>
		</uni-forms>
		<view class="footer">
			<view class="control">
				<view class="view-flex-rc">
					<view class="block"
						style="width: 120px;height:28px;margin-left: 10px;margin-right: 10px;border-radius:7px;background-color:lightpink"
						@click="toSubmit()">
						<view class="view-flex-cc">
							<uni-icons class="input-uni-icon" type="checkmarkempty" size="18" color="#999" />
						</view>
						<view class="view-flex-cc" style="padding-left:5px;">
							<text style="color:black;font-size:0.7rem">立即提交</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-drawer ref="showRight" mode="right" :mask-click="false" :width="drawerWidth">
			<view>
				<view class="view-flex-inline" style="background-color: #3F9EFF;height: 40px;padding-left: 10px;">
					<uni-icons class="input-uni-icon" type="personadd" size="30" color="coral" />
					<text style="color: white;font-size: 1rem;margin-left: 10px;">添加家庭成员</text>
				</view>
				<view style="padding-top: 20px;padding-left: 5px;padding-right: 5px;">
					<uni-forms ref="familyMembersForm" :model="familyMembersFormData">
						<uni-forms-item name='personName' label="姓名" label-width="90px" required>
							<uni-easyinput v-model="familyMembersFormData.personName" placeholder="请录入姓名" />
						</uni-forms-item>
						<uni-forms-item name='personId' label="身份证号" label-width="90px" required>
							<uni-easyinput v-model="familyMembersFormData.personId" placeholder="请录入身份证号" />
						</uni-forms-item>
					</uni-forms>
				</view>
				<view class="item-button-group" style="padding-right: 10px;">
					<view class="item-button" @click="onResidentEditCloseClick()">
						<view class="items-line">
							<uni-icons class="input-uni-icon" type="closeempty" size="18" color="coral" />
							<text class="button-text">取消</text>
						</view>
					</view>
					<view class="item-button" @click="onResidentEditSubmitClick()">
						<view class="items-line">
							<uni-icons class="input-uni-icon" type="checkmarkempty" size="18" color="coral" />
							<text class="button-text">确定</text>
						</view>
					</view>
				</view>
			</view>
		</uni-drawer>
	</view>
</template>

<script>
	import {
		role
	} from "@/api/role.js";
	import http from '@/http/api.js';
	import {
		devUrl,
		prodUrl
	} from '@/common/setting';
	export default {
		data() {
			return {
				title: '危险区群众登记',
				baseURL: '',
				warnId: '',
				formData: {
					latitude: '',
					longitude: '',
					personName: '',
					personId: '',
					dangerAreaPid: '',
					isHouseholder: 1,
				},
				familyMembersFormData: {
					personName: '',
					personId: '',
				},
				rules: {
					personName: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}]
					},
					personId: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}, ]
					},
					dangerAreaPid: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}, ]
					},
				},
				keepPhotos: [],
				familyMembersList: [],
				checkItems: {
					items: [{
						'value': 0,
						'text': '否'
					}, {
						'value': 1,
						'text': '是'
					}],
					current: 0,
				},
				drawerWidth: 300,
			}
		},
		onLoad(option) {
			let that = this;
			this.baseURL = process.env.NODE_ENV === 'development' ? devUrl : prodUrl;
			this.drawerWidth = uni.getWindowInfo().windowWidth - 100;
			// #ifdef APP-PLUS
			console.log("app")
			uni.getLocation({
				type: 'gcj02',
				geocode: true,
				success: function(res) {
					console.log('当前位置：' + JSON.stringify(res));
					that.formData.latitude = res.latitude;
					that.formData.longitude = res.longitude;
				}
			});
			// #endif
			// #ifdef MP-WEIXIN
			console.log("weixin")
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					console.log('当前位置：' + JSON.stringify(res));
					that.formData.latitude = res.latitude;
					that.formData.longitude = res.longitude;
				}
			});
			// #endif
		},
		computed: {

		},
		onShow() {},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.baseForm.setRules(this.rules)
		},
		methods: {
			//返回上一页
			toBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},
			onResidentAddClick() {
				this.familyMembersFormData.personId = '';
				this.familyMembersFormData.personName = '';
				this.$refs['showRight'].open();
			},
			onResidentEditCloseClick() {
				this.$refs['showRight'].close();
			},
			onResidentEditSubmitClick() {
				let familyMember = {};
				familyMember['personId'] = this.familyMembersFormData.personId;
				familyMember['personName'] = this.familyMembersFormData.personName;
				this.familyMembersList.push(familyMember);
				this.$refs['showRight'].close();
			},
			onCheckPhotoAdd() {
				let that = this;
				uni.chooseImage({
					sourceType: ['camera'],
					sizeType: ['compressed'],
					success: (res) => {
						const len = res.tempFilePaths.length;
						if (len === 1) {
							res.tempFilePaths.forEach(path => {
								that.uploadCheckPhoto(path);
							})
						} else {
							uni.showModal({
								content: '只能选择一张图片，请确认！',
								showCancel: false
							});
						}
					}
				})
			},
			uploadCheckPhoto(imagePath) {
				let that = this;
				uni.showLoading({
					title: '上传中'
				});
				uni.uploadFile({
					url: this.baseURL + '/galaxy-resource/oss/endpoint/put-file-attach?Blade-Auth=' +
						uni.getStorageSync('accessToken'),
					fileType: 'image',
					filePath: imagePath,
					name: 'file',
					success: (uploadFileRes) => {
						if (uploadFileRes.statusCode == 200) {
							let data = JSON.parse(uploadFileRes.data);
							if (data.success) {
								let path = data.data['name'];
								that.keepPhotos.push(path);
							}
						}
					},
					fail: (err) => {
						console.log(err);
						reject('err')
					},
					complete() {
						uni.hideLoading()
					}
				});
			},
			onCheckPhotoDel(index) {
				this.keepPhotos.splice(index, 1);
			},
			onQrCodeClick() {
				let that = this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						that.formData.dangerAreaPid = res.result;
					}
				});
			},
			toSubmit() {
				let that = this;
				this.$refs.baseForm.validate().then(res => {
					console.log('表单数据信息1：', res);
					that.submit();
				}).catch(err => {
					console.log('表单错误信息：', err);
					uni.showModal({
						content: "内容录入错误，请根据提示信息检查录入内容!",
						showCancel: false,
						success(res) {
							if (res.confirm) {
								//that.$refs.baseForm.clearValidate();
							}
						}
					});
				})
			},
			submit() {
				let formdata = this.formData;
				//if (this.formData.isHouseholder === 1) {
				formdata['familyMembers'] = this.familyMembersList;
				//}
				console.log('toSubmit事件:', JSON.stringify(formdata));
				let that = this;
				http.request({
					url: '/galaxy-business/yj/resident/record',
					method: 'POST',
					data: formdata
				}).then(res => {
					console.log('res ', JSON.stringify(res));
					if (res.success) {
						if (res.code == 200) {
							//that.clearCache();
							uni.showModal({
								content: '填报信息已成功提交',
								showCancel: false,
								success(res) {
									if (res.confirm) {
										that.toBack();
									}
								}
							});
						} else {
							uni.showModal({
								content: '提交失败',
								showCancel: false
							});
						}
					} else {

						uni.showModal({
							content: '提交失败',
							showCancel: false
						});
					}
				}).catch(err => {
					uni.showModal({
						content: '' + err.data.msg,
						showCancel: false
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 0 100rpx;
	}

	.scan-icon {
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
		// margin-right: 10px;
		width: 30px;
		height: 30px;
	}

	.close {
		padding: 10px;
	}
</style>