<template>
	<view class="container">
		<view class="listBox">
			<view class="list">
				<view class="flex listItem" @click="chooseLng">
					<view class="flex titleBox">
						<text class="title">打开地图选择定位</text>
					</view>
					<view class="right flex">
						<text>{{latitude ? (latitude + ','+longitude): ''}}</text>
						<image class="img" src="../../static/icon/next1.png" mode="widthFix"></image>
					</view>
				</view>
			</view>
		</view>
		<view class="base-buttom" @click="toSub">
			提交
		</view>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex';
	import { addLng } from '@/api/set.js'
	export default {
		data() {
			return {
				latitude: '',
				longitude: ''
			};
		},
		computed: {
			...mapState('user', ['userInfo'])
		},
		methods: {
			chooseLng() {
				let that = this
				uni.chooseLocation({
					success(res) {
						that.latitude = res.latitude
						that.longitude = res.longitude
					},
					fail(err) {          // 失败回调
					    console.error('选择位置失败:', err)
					  },
				})
			},
			toSub() {
				let that = this
				if(!that.latitude) {
					return that.$api.msg('请打开地图选择定位')
				}
				addLng({
					latitude: that.latitude,
					longitude: that.longitude
				}).then(res => {
					this.$api.msg('修改成功');
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/userhome/user'
						});
					}, 1000);
				})
			}
		}
	}
</script>

<style lang='scss'>
	.container {
		padding-top: 30rpx;
	}

	.listBox {
		margin: 0 $page-row-spacing;
		background-color: #FFFFFF;
		border-radius: 20rpx;
		overflow: hidden;
	}

	.list {
		.listItem {
			padding: 35rpx 40rpx;
			border-bottom: 1px solid $page-color-light;
		}

		.listIconImg {
			width: 36rpx;
		}

		.right {
			color: $font-color-light;
			font-size: $font-base;
			flex-grow: 1;
			justify-content: flex-end;

			.img {
				width: 26rpx;
			}
		}

		.titleBox {
			.title {
				color: $font-color-base;
				font-size: $font-base;
			}
		}
	}

	.row {
		display: flex;
		align-items: center;
		padding: 35rpx 40rpx;
		background: #fff;
		border-bottom: 1px solid $page-color-light;

		.background-img {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background: #f2f2f2;
		}

		.tit {
			flex-shrink: 0;
			width: 120upx;
			font-size: $font-base;
			color: $font-color-base;
		}

		.input {
			flex: 1;
			text-align: right;
			font-size: $font-base;
			color: $color-gray;
		}
	}
</style>
