<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" required>
					<uni-easyinput v-model="formData.longitude" placeholder="请录入经度" />
				</uni-forms-item>
				<uni-forms-item name='latitude' label="纬度" label-width="100px" required>
					<uni-easyinput v-model="formData.latitude" placeholder="请录入纬度" />
				</uni-forms-item>
				<view class="item-button-group" style="padding-right: 10px;">
					<view class="item-button mid-size" @click="onRestartLocation()">
						<view class="items-line">
							<uni-icons class="input-uni-icon" type="location" size="18" color="coral" />
							<text class="button-text">重新定位</text>
						</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: '',
				formData: {
					id: '',
					latitude: '',
					longitude: '',
				},
				rules: {
					latitude: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}]
					},
					longitude: {
						rules: [{
							required: true,
							errorMessage: '必填项',
						}, ]
					},
				},
			}
		},
		onLoad(option) {
			let that = this;
			this.baseURL = process.env.NODE_ENV === 'development' ? devUrl : prodUrl;

			this.formData.id = option.id;
			that.formData.latitude = option.lat;
			that.formData.longitude = option.lng;
			
			// #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;
					uni.showModal({
						content: '定位成功',
						showCancel: false
					});
				}
			});
			// #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;
					uni.showModal({
						content: '定位成功',
						showCancel: false
					});
				}
			});
			// #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
			},
			onRestartLocation() {
				// #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
			},
			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;
				console.log('toSubmit事件:', JSON.stringify(formdata));
				let that = this;
				http.request({
					url: '/galaxy-business/yj/resident/location/add',
					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;
	}
</style>