<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='personName' label="姓名" label-width="100px" required>
					<uni-easyinput v-model="formData.personName" placeholder="请录入姓名" @input="nameInput"/>
				</uni-forms-item>
				<uni-forms-item name='personId' label="手机号" label-width="100px" required>
					<uni-easyinput v-model="formData.personId" placeholder="请录入手机号" @input="phoneInput"/>
				</uni-forms-item>
<!-- 				<uni-forms-item name="familyMembersTransferStatus" label="登记家庭户其他人员" label-width="100px" required>
					<uni-data-checkbox v-model="formData.familyMembersTransferStatus" :localdata="checkItems.items">
					</uni-data-checkbox>
				</uni-forms-item> -->
				<uni-forms-item name="familyMembers" label="家庭成员" label-width="100px" required>
					<uni-data-checkbox multiple v-model="formData.familyMembers"
						:localdata="familyMembersItems">
					</uni-data-checkbox>
				</uni-forms-item>
		<!-- 		<uni-forms-item name='remark' label="补充说明" label-width="100px">
					<uni-easyinput v-model="formData.remark" placeholder="请录入其他需要说明的情况" />
				</uni-forms-item> -->
			</uni-card>
			<uni-card title="补充说明" :is-shadow="false">
				<uni-forms-item name='remark' >
					<uni-easyinput type="textarea"  v-model="formData.remark" placeholder="请录入其他需要说明的情况" />
				</uni-forms-item>
			</uni-card>
			<uni-card title="留存图片" extra="点击下方按钮开始拍摄" :is-shadow="false">
				<view class="view-flex-rs-flex-wrap">
					<view v-for="(item, index) in keepPhotos" :key="index" class="view-flex-cc" style="width: 100px;">
						<view class="img-container">
							<view class="note-image-box">
								<view class="note-image-item">
									<view class="close-icon" @click="onCheckPhotoDel(index)">
										<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
									</view>
									<view class="image-box">
										<image :src="toOss(item)" mode="widthFix">
										</image>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view class="view-flex-cc" style="width: 80px;">
						<view class="img-container">
							<view class="note-image-box">
								<view class="note-image-item" @click="onCheckPhotoAdd()">
									<view class="image-box">
										<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</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>
	</view>
</template>

<script>
	// import {
	// 	role
	// } from "@/api/role.js";
	import http from '@/http/api.js';
	import {
		oss,
		devUrl,
		prodUrl
	} from '@/common/setting';
	export default {
		data() {
			return {
				title: '转移登记',
				baseURL: '',
				baseOSS: '',
				warnId: '',
				formData: {
					personName: '',
					personId: '',
					familyMembersTransferStatus: 0,
					remark: '',
					familyMembers: [],
				},
				keepPhotos: [],
				familyMembersItems:[],
				checkItems: {
					items: [{
						'value': 0,
						'text': '否'
					}, {
						'value': 1,
						'text': '是'
					}],
					current: 0,
				},
				rules: {
					personName: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}]
					},
					personId: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}, ]
					},
					familyMembers: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}, ]
					},
				},
			}
		},
		onLoad(option) {
			let that = this;
			this.baseOSS = oss;
			this.baseURL = process.env.NODE_ENV === 'development' ? devUrl : prodUrl;
			this.warnId = option.id;
		},
		computed: {

		},
		onShow() {},
		onReady(){
			this.$refs.baseForm.setRules(this.rules)
		},
		methods: {
			toOss(path) {
				let url = this.baseOSS + path;
				return url;
			},
			onBackPress() {
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
			},
			//返回上一页
			toBack() {
				uni.navigateBack({
					delta: 1
				})
			},
			nameInput(e){
				console.log(e)
				//this.formData.personName=e;
				if(e!=undefined&&e.length>0){
					if(this.formData.personId!=undefined&&this.formData.personId.length>0){
						console.log("find+++++++++++")
						let p ={
							personId: this.formData.personId,
							personName: e
						}
						this.getFamilyList(p);
					}
				}
			},
			phoneInput(e){
				console.log(e)
				//this.formData.personId=e;
				if(e!=undefined&&e.length>0){
					if(this.formData.personName!=undefined&&this.formData.personName.length>0){
						console.log("find+++++++++++")
						
						let p ={
							personId: e,
							personName:this.formData.personName
						}
						this.getFamilyList(p);
					}
				}
			},
			getFamilyList(params = {}) {
				let postData = Object.assign(params, this.query);
				let that = this;
				http.request({
					url: '/galaxy-business/yj/resident/family/list',
					method: 'GET',
					data: postData,
				}).then(res => {
						console.log("111"+JSON.stringify(res))
					if (res.data != null) {
						//console.log(JSON.stringify(res.data))
						//that.list = res.data;
						that.familyMembersItems = res.data.map(item => {
							//let id = item.personId.replace(/^(\d{6})\d+(\d{4})$/, '$1******$2')
							return {
								'value': item.id,
								'text':item.personName,
							}
						})
						that.formData.familyMembers=[];
						if(res.data.length>0){
							for(let i=0;i<res.data.length;i++){
								if(res.data[i].personId == that.formData.personId && res.data[i].personName == that.formData.personName){
									that.formData.familyMembers.push(res.data[i].id);
									break;
								}
							}
						}
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getAddress(lng, lat) {
				let url = 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + lat + ',' + lng +
					'&key=4HYBZ-EB23D-SLC42-HQ5R3-LP3LQ-OZFU5';
				console.log(url);
				http.request({
					url: url,
					method: 'GET',
				}).then(res => {
					console.log('当前位置：' + JSON.stringify(res));

				}).catch(err => {
					console.log(err)
				})
			},
			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);
			},
			toSubmit() {
				let that = this;
				this.$refs.baseForm.validate().then(res => {
					console.log('表单数据信息1：', JSON.stringify(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;
				formdata['warnId'] = this.warnId;
				if (this.keepPhotos.length > 0) {
					let photos = '';
					for (let i = 0; i < this.keepPhotos.length; i++) {
						if (photos.length > 0) {
							photos = photos + ',';
						}
						photos = photos + this.keepPhotos[i];
					}
					formdata['keepPhotos'] = photos;
				}
				let that = this;
				http.request({
					url: '/galaxy-business/yj/transfer/person/record',
					method: 'POST',
					data: formdata
				}).then(res => {
					//console.log('res ', JSON.stringify(res));
					if (res.success) {
						if (res.code == 200) {
							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;
	}

	.view-flex-rs-flex-wrap {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;
		flex-wrap: wrap;
	}

	.img-container {
		margin-bottom: 0px;
		width: 100px;
		height: 100px;

		.note-image-box {
			margin-top: 0px;
			display: flex;
			flex-wrap: wrap;
			padding: 10px;

			.note-image-item {
				position: relative;
				width: 100%;
				height: 0;
				padding-top: 100%;
				box-sizing: border-box;
				// background-color: #18B566;

				.close-icon {
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					right: 0px;
					top: 0px;
					width: 22px;
					height: 22px;
					border-radius: 50%;
					background-color: #d5d5d5;
					z-index: 2;
				}

				.image-box {
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					top: 0px;
					right: 0px;
					border: 0px;
					left: 0px;
					border: 1px #eee solid;
					border-radius: 5px;
					overflow: hidden;
					width: 99%;
					height: 99%;
				}
			}
		}
	}

	.view-flex-rc {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}

	.view-flex-cc {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		z-index: 1;
		height: 100rpx;
		padding: 20rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		background-color: whitesmoke;

		.ipt {
			width: 380rpx;
			height: 77rpx;
			background: #f7f7f7;
			border-radius: 38px;
			padding: 0 37rpx;
			box-sizing: border-box;
			margin-right: 20rpx;
		}

		.control {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: flex-end;

			.block {
				display: flex;
				align-items: center;
				justify-content: center;
				flex: 1;
			}

			.icon {
				height: auto;
			}

			.c {
				width: 41rpx;
				margin-right: 10rpx;
			}

			.s {
				width: 36rpx;
			}

			.t {
				width: 31rpx;
			}
		}
	}
</style>