<template>
	<view>
		<ujp-navbar title="充电订单"></ujp-navbar>
		
		<view class="record">
			<view class="recordTime oldTextjp2" oldstyle="font-size: 18px;" @click="show = true">
				<u-picker mode="time" v-model="show" :params="params" :default-time="endDate" @confirm="confirmTime" @cancel="cancelTime"></u-picker>
				<p>{{month}}月</p>
				<u-icon name="arrow-down-s-fill" custom-prefix="custom-icon" color="#B3B3B3" size="32"></u-icon>
			</view>
			<view class="carNone" v-if="chargeList.length == 0">
				<img src="@/assets/static/img/暂无数据-缺省页.png" alt="">
				<p class="oldTextjp2" oldstyle="font-size: 18px;">暂无充电订单</p>
			</view>
			<view class="recordList" v-if="chargeList.length > 0">
				<view class="recordList-item" v-for="(item,index) in chargeList" :key="item.id" @click="toUrl(item)">
					<view class="recordList-name">
						<h4 class="oldTextjp" oldstyle="font-size: 20px;">{{item.stationName}}/{{item.deviceName}}</h4>
						<span class="state1" :style="{color : textColor(item),fontSize: textFontSize()}">{{item.statusText}}</span>
					</view>
					<view class="recordList-row">
						<p :style="elderStatus ? 'font-size: 14px' : ''">结束时间：{{item.status == '0' ? '暂未开始' : item.endTime}}</p>
					</view>
					<view class="recordList-row">
						<p :style="elderStatus ? 'font-size: 14px' : ''">充电电量：{{item.electricQuantity != null ? item.electricQuantity/10000 : '0.000'}}度</p>
					</view>
					<view class="recordList-row">
						<p :style="elderStatus ? 'font-size: 14px' : ''">充电时长：{{item.chargingMinute != null ? getPercent(item.chargingMinute): '0秒'}}</p>
						<h4 class="oldTextjp2" oldstyle="font-size: 28px;">{{item.actualFee != null ? item.actualFee.toFixed(2) : '0.00'}}元</h4>
					</view>
				</view>
			</view>
		</view>
		<u-divider  v-if="chargeList.length == recordsTotal && recordsTotal != 0" style="margin-top: 10px;background-color: #F2F4F4;">已经到底了</u-divider>
	</view>
</template>

<script>
	import * as API from '@/apis/index.js'
	
	export default {
		data() {
			return {
				params: {
					year: true,
					month: true,
					day: false,
					hour: false,
					minute: false,
					second: false,
					timestamp: true,
				},
				show: false,
				month: '',
				startDate: '',
				endDate: '',
				chargeList: [],
				pageIndex: 1,
				recordsTotal: 0,
				elderStatus: false,
			}
		},
		onReady() {
			var date = new Date();
			this.month = date.getMonth() + 1;
			
			var year = date.getFullYear();
			var day = new Date(year, this.month, 0).getDate();
			var monthN = this.month;
			if(monthN >= 1 && monthN <= 9) {
				monthN = "0" + monthN;
			}
			
			this.startDate = year + '-' + monthN + '-' + '01';
			this.endDate = year + '-' + monthN + '-' + day;
					
			this.getChargeList();
			
			if(this.carhelp.get("getElderModeClass") == "长辈模式") {
				this.elderStatus = true;
			} else {
				this.elderStatus = false;
			}
		},
		onReachBottom() {
			if (this.chargeList.length < this.recordsTotal) {			
				this.myLoadmore();
			}
		},
		methods: {
			getPercent(estimateMinute) {
				var value="";
				 
				var ms =estimateMinute
				if (ms > 0) {
					var Hour = parseInt(Math.floor(ms / 60 ));
					var Fen = parseInt(Math.floor(ms % 60 ));	
				//	value = Hour + "小时"  + Fen+"分钟"
					if(Hour){
						value += Hour + "小时" 
					}
						
					if(Fen){
						value += Fen+"分钟"
					}
				}
			
				return value;
			},
			toUrl(item) {
				if(item.status == '1') {
					uni.navigateTo({
						url: '/pages/searchPile/chargeProcess/dcCharging?id=' + item.id
					})
				} else if (item.status == '4') {
					uni.navigateTo({
						url: '/pages/searchPile/chargeProcess/dcCharging?id=' + item.id
					})
				} else {
					uni.navigateTo({
						url: '/pages/record/details?id=' + item.id
					})
				}	
			},
			myLoadmore() {
				this.pageIndex += 1;
				this.getChargeList()
			},
			textColor(item) {
				switch (item.status) {
					case '1':
						return '#00B962'
						break;
					case '2':
						return '#666666'
						break;
					case '4':
						return '#8161FF'
						break;
				}
			},
			textFontSize() {
				if(this.elderStatus) {
						return '40rpx'
				}
			},
			confirmTime(params) {
				if(params.month.slice(0,1) == '0') {
					this.month = params.month.slice(1);
				} else {
					this.month = params.month;
				}
				
				this.startDate = params.year + '-' + params.month + '-' + '01';
				var day = new Date(params.year, params.month, 0).getDate();
				this.endDate = params.year + '-' + params.month + '-' + day;
				this.getChargeList(true);
				this.show = false;
			},
			cancelTime() {
				this.show = false;
			},
			getChargeList(bl) {
				uni.showLoading({
					title: "加载中",
					mask: true,
				})
				if (bl) {
					this.chargeList = [];
					this.pageIndex = 1;
				}
				API.chargingRecordData({
					pageIndex: this.pageIndex,
					startDate: this.startDate,
					endDate: this.endDate
				}).then((res) => {
					uni.hideLoading();
					
					this.chargeList = [
						...this.chargeList,
						...res.data.data
					];
					this.recordsTotal = res.data.recordsTotal;
				}).catch(error => {
					uni.showToast({
						title: error,
						icon: "none"
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.carNone{
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		img{
			width: 100%;
			height: 100%;
		}
		p{
			margin-top: -60px;
		}
	}
	.recordTime{
		height: 44px;
		background-color: #fff;
		border-bottom: 1px solid #F2F2F2;
		display: flex;
		align-items: center;
		padding: 0 16px;
		p{
			margin-right: 4px;
		}
	}
	.recordList{
		background-color: #fff;
	}
	.recordList-item{
		background-color: #fff;
		border-bottom: 1px solid #ededed;
		margin-left: 16px;
		padding: 16px 16px 16px 0;
		.recordList-name{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-bottom: 12px;
			h4{
				font-weight: normal;
				font-size: 15px;
				overflow: hidden;
				width: 75%;
				text-overflow: ellipsis;
				white-space: nowrap;
			}
			.state1{
				color:#8161FF;
			}
			.state2{
				color:#00B962;
			}
			.state3{
				color:#666666;
			}
		}
		.recordList-row{
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 8px;
			p{
				font-size: 12px;
				color:#888;
			}
			h4{
				font-size: 16px;
			}
		}
	}
</style>
