<template>
	<view>
		<u-navbar
		 title="停车场列表" 
		 title-color="#fff" 
		 :custom-back="customBack" 
		 :border-bottom="false" 
		 back-icon-color="#CCE8FF" 
		 :background="{background: 'linear-gradient(145deg, #41AFF9 0%, #2D8CFB 100%)' }"></u-navbar>
		
		<view class="road-list">
			<view class="road-list-item" v-for="item in Roads" :key="item.id" @click="handleModifyRoad(item.id)">
				<view class="name">{{item.roadName}}</view>
				<view class="content">
					<view class="position">{{item.areaName}}</view>
					<view class="distance">{{item.distance|kmUnit}}</view>
				</view>
				
			</view>
		</view>
		
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default{
		data(){
			return{
				Roads:[],
				latitude:'',
				longitude:'',
			}
		},
		onLoad(page){
			// this.Roads = this.$store.state.vuex_user?.roadList;
			console.log('page', page)
			this.latitude = page.latitude;
			this.longitude = page.longitude;
		},
		onShow(){
			this.$u.api.bindRoads({latitude: this.latitude,longitude: this.longitude})
			.then(res=>{
				this.Roads = res.data.roadList;
				// this.list=[];
				// res.data.roadList.forEach(item=>{
				// 	const obj = {
				// 		roadName: item.roadName,
				// 		areaName: item.areaName,
				// 		distance: item.distance,
				// 	}
				// 	this.list.push(obj)
				// 	console.log('obj',obj)
				// });
			});
		},
		methods:{
			customBack(){
				uni.navigateBack({
				    delta: 1
				});
			},
			handleModifyRoad(id){
				this.$u.api.modifyRoad({roadId:id})
				.then(res=>{
					let userdata = this.$store.state.vuex_user;
					userdata.roadList = res.data?.roadList;
					this.$u.vuex('vuex_user', userdata);
					uni.setStorageSync('payee_roadinfo', JSON.stringify(userdata.roadList?.[0] || []));
					plus.nativeUI.toast('切换路段成功');
					uni.reLaunch({
					    url: '/pages/index/index'
					});
				}).catch(err=>{
					plus.nativeUI.toast('失败' + err.msg);
				});
			}
			
		}
	}
</script>

<style lang="scss">
	@import  './roadlist.scss'
</style>
