<template>
	<view class="content">
		<scroll-view class="list-scroll-content" scroll-y @scrolltolower="loadData">
			<!-- 空白页 -->
			<!-- #ifdef H5 -->
			<empty src="../../../static/error/emptyMyCart.png" v-if="navList[0].loaded === true && navList[0].orderList.length === 0"></empty>
			<!-- #endif -->
			<!-- #ifndef H5 -->
			<empty src="../static/error/emptyMyCart.png" v-if="navList[0].loaded === true && navList[0].orderList.length === 0"></empty>
			<!-- #endif -->
			<!-- 订单列表 -->
			<view class="listItem" v-for="(item, index) in navList[0].orderList" :key='index'>
				<view v-if="item.day>0" class="topRightTip" @click="navTo('./Prepayment?id='+item.car_number)">
					<image src="../../../static/image/moneyPay.png" mode="widthFix"></image>
				</view>
				<view class="flex">
					<view class="imgbox">
						<image :src="item.image" mode="widthFix"></image>
					</view>
					<view class="contentDetail flex">
						<view class="goodsName">
							<view class="topName">
								{{item.car_name}}
							</view>
							<view class="goodsType">
								{{item.car_sku}}
							</view>
							<view class="goodsTime margin-t-10">
								激活时间：{{item.bind_time}}
							</view>
						</view>
						<view class="goodsRight">
							<view class="status">
								已授权
							</view>
							<navigator :url="'./authorizationList?id='+item.car_number">
								<view class="numR">
									{{item.auth_num}}
								</view>
							</navigator>
						</view>
					</view>
				</view>
				<view class="address">
					当前位置：{{item.address}}
				</view>
				<map @click="openMap(item.latitude,item.longitude)"  class="map" :enable-zoom='false' :enable-scroll='false' :latitude="item.latitude" :longitude="item.longitude" :markers="[{
						latitude: item.latitude,
						longitude:item.longitude,
						iconPath: '../../../static/image/location.png',
						width:35,
						height:35
					}]"></map>
				<view class="detail flex">
					<view class="item">
						<view class="type">
							剩余电量
						</view>
						<view class="num">
							{{item.residue}}%
						</view>
					</view>
					<view class="item">
						<view class="type">
							剩余天数
						</view>
						<view class="num">
							{{item.day}}
						</view>
					</view>
					<!-- <view class="item">
						<view class="type">
							总里程数
						</view>
						<view class="num">
							200
						</view>
					</view> -->
				</view>
				<cartConfing @tab="changetab($event,item)" :item='item'></cartConfing>
			</view>

			<uni-load-more :status=" navList[0].loadingType"></uni-load-more>
		</scroll-view>
	</view>
</template>

<script>
	import cartConfing from './cartConfing.vue';
	import {
		carList,
	} from '@/api/user.js';
	export default {
		components: {
			cartConfing
		},
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
					state: 0,
					text: '待付款',
					loadingType: 'more',
					orderList: [],
					page: 1, //当前页数
					limit: 10 //每次信息条数
				}]
			};
		},

		onLoad(options) {
			this.loadData();
		},
		methods: {
			// tab改变事件
			changetab(tab,item){
				item.status = tab
			},
			// 打开地图导航
			openMap(lat, long) {
				uni.openLocation({
					latitude: +lat,
					longitude: +long,
					complete(e){
						console.log(e,'地图打开');
					}
				})
			},
			navTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			//获取列表
			loadData(source) {
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex;
				let navItem = this.navList[index];
				if (source === 'tabChange' && navItem.loaded === true) {
					//tab切换只有第一次需要加载数据
					return;
				}
				if (navItem.loadingType === 'loading') {
					//防止重复加载
					return; 
				}
				if (navItem.loadingType === 'noMore') {
					//防止重复加载
					return;
				}
				// 修改当前对象状态为加载中
				navItem.loadingType = 'loading';

				carList({
						page: navItem.page,
						limit: navItem.limit
					})
					.then(({
						data
					}) => {
						let arr = data.data.map(e => {
							return e;
						});
						navItem.orderList = navItem.orderList.concat(arr);
						navItem.page++;
						if (navItem.limit == arr.length) {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'more';
							return;
						} else {
							//判断是否还有数据， 有改为 more， 没有改为noMore
							navItem.loadingType = 'noMore';
						}
						uni.hideLoading();
						this.$set(navItem, 'loaded', true);
					})
					.catch(e => {
						console.log(e);
					});
			},
		}
	};
</script>

<style lang="scss">
	page,
	.content {
		height: 100%;
	}

	.list-scroll-content {
		padding-top: 30rpx;
		height: 100%;
	}

	.listItem {
		margin: 0rpx 20rpx 30rpx;
		border-radius: 20rpx;
		padding: 30rpx;
		background-color: #FFFFFF;
		position: relative;
		overflow: hidden;

		.topRightTip {
			position: absolute;
			top: 0;
			right: 0;

			image {
				width: 80rpx;
			}
		}

		.imgbox {
			width: 160rpx;
			height: 160rpx;
			border-radius: 20rpx;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.contentDetail {
			padding-left: 30rpx;
			flex-grow: 1;

			.goodsName {
				.topName {
					color: $font-color-dark;
					font-size: 32rpx;
					font-weight: bold;
					margin-bottom: 30rpx;
				}

				.goodsType,
				.goodsTime {
					color: $font-color-light;
					font-size: 24rpx;
				}
			}
		}

		.map {
			width: 630rpx;
			height: 200rpx;
			font-size: 35rpx;
		}

		.goodsRight {
			flex-shrink: 0;
			.status {
				color: $font-color-light;
				font-size: 24rpx;
			}

			.numR {
				height: 60rpx;
				width: 60rpx;
				border-radius: 10rpx;
				border: 1px solid $color-green;
				line-height: 60rpx;
				font-size: 32rpx;
				color: $color-green;
				text-align: center;
				margin: 0 auto;
				margin-top: 20rpx;
			}
		}

		.address {
			color: $font-color-light;
			font-size: 24rpx;
			line-height: 1;
			padding: 30rpx 0;
		}

		.detail {
			margin-top: 30rpx;
			padding: 0 20rpx;
			justify-content: space-around;
			.item {
				color: $font-color-light;
				text-align: center;
				line-height: 1.5;

				.type {
					font-size: 24rpx;
				}

				.num {
					font-size: 32rpx;

				}
			}
		}
	}
</style>
