<template>
	<view class="task-form-item form-item-media">
		<view class="form-lable">
			<text><text class="form-must" v-if="rule && rule.required">* </text>定位:</text>
			<view :class="['form-lable-content' , Position.length > 0 ? 'lable-content-right' : '']" v-if="!checkPage">
				<view class="lable-content-btn" @click.stop="onLocation()">
					<text class="iconfont_yige">&#xe6a8;</text>
					<text>获取当前位置</text>
				</view>
			</view>
		</view>
		<view class="form-img-video form-value">
			{{Position}}
		</view>
		<mumu-getlocation ref='muLocation'></mumu-getlocation>
	</view>
</template>
<script>
	import {
		getLocation
	} from "@/utils/tool.js"
	import mumuGetlocation from '@/uni_modules/mumu-getlocation/components/mumu-getlocation/mumu-getlocation.vue'
	export default {
		name: 'submintContent',
		components: {
			mumuGetlocation
		},
		model: {
			prop: ['lbsPosition'],
			event: ['onChange']
		},
		props: {
			checkPage: {
				type: Boolean,
				default: false
			},
			rule: {
				type: Object,
				default: null
			},
			lbsPosition: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				Position: '',
				locationRef: null
			}
		},
		methods: {
			onLocation() {
				try {
					// #ifdef APP
					getLocation().then(res => {
						console.log('getLocation = ', res)
						const {
							address
						} = res || {};
						if (address) {
							this.Position =
								`${address.province || ''}${address.city || ''}${address.district || ''}${address.street || ''}${address.poiName || ''}${address.streetNum || ''}`

						}
					}).catch(err => {
						console.log('getLocation err= ', err)
					})
					// #endif
					// #ifdef H5

					uni.showLoading({
						title: '获取定位中...',
						mask: true
					})
					// 初始化组件，并且检测是否开启与授权定位
					this.$refs.muLocation.__init().then(location => {
						// 用户授权了和开启了定位，把定位对象保存到 data 中 
						// this.locationRef = location
						// 调用 getLocation 方法获取精准定位
						location.getLocation().then(res => {
							console.log('location', res);
							try {
								const { province, city, district, addr } = res || {}
								this.Position = `${province}${city}${district}${addr}`;
							} catch (e) {
								//TODO handle the exception
							}
							// res 就是返回的数据
							// uni.showModal({
							// 	content: JSON.stringify(res, null, 4)
							// })
						})
						uni.hideLoading()
					}, err => {
						// 用户拒绝了定位请求，获取系统没有开启定位功能
						uni.hideLoading()
						if (err.code === 1) {
							uni.showModal({
								title: '获取定位权限失败',
								content: '你拒绝了位置授权服务。请允许当前页面获取定位授权，后刷新页面。'
							})
						} else {
							uni.showModal({
								title: '获取定位权限失败',
								content: '请确定手机定位已打开，并且当前浏览器允许获取定位，都开启后请刷新页面。'
							})
						}
					})
					// #endif
				
				} catch (e) {
					console.log('showToast = ', e)
					uni.showToast({
						title: '定位失败',
						icon: 'none'
					})
					//TODO handle the exception
				}
			}
		},
		watch: {
			lbsPosition: {
				handler(newL, oldL) {
					this.Position = newL
				},
				immediate: true
			},
			Position: {
				handler(newL, oldL) {
					if (this.Position !== this.lbsPosition) {
						this.$emit('onChange', this.Position)
					}
				},
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import '~./submint.scss';
</style>