/**
* 新版充电预定页面
* @邠心vbe on 2023/02/06
*/
import React, { Component } from 'react'
import { Image, StyleSheet, Text, View } from 'react-native'
import Button from '../../components/Button';
import { ChargeStyle, EnterStationDialog, TypeImage } from './Charging';
import apiCharge from '../../api/apiCharge';
import Dialog from '../../components/Dialog';
import { PageList } from '../Router';
import { CancelReserveDialog } from './InfoDialog';
import PagerUtil from './PagerUtil';
import BadgeSelectItem from '../../components/BadgeSelectItem';
export default class TabReserve extends Component {
constructor(props) {
super(props);
this.state = {
total: 0,
leftId: 0,
refreshId: 0,
checkIndex: -1,
available: false,
showReserve: false,
stationInfo: {},
checkConnector: {},
userReserve: {},
timeLeft: '',
showCancelDialog: false,
showStationDialog: false
};
}
componentDidMount() {
PagerUtil.addOnRefresh(this);
this.onRefresh();
}
onRefresh() {
console.log("Reserve刷新", this.props.route.name);
this.setState({
stationInfo: PagerUtil.getStationInfo()
}, () => this.init());
}
init() {
this.stopCountdown(true);
if (this.state.stationInfo.rateList && this.state.stationInfo.rateList.length > 0) {
for (var i = 0; i < this.state.stationInfo.rateList.length; i++) {
const item = this.state.stationInfo.rateList[i]
if (item.available) {
this.setState({
checkIndex: i,
checkConnector: item,
})
break;
}
}
this.setState({
total: this.state.stationInfo.rateList.length,
showReserve: this.state.stationInfo.enableReservation ? true : false
})
this.getReserve();
//this.refreshAvailable();
} else {
this.setState({
showReserve: false
})
}
}
//刷新可用充电接口
refreshAvailable() {
const info = this.state.stationInfo
const all = info?.allConnector;
/*if (info.siteType == 'Private') {
this.setState({
isPrivate: true
})
}*/
if (all) {
this.setState({
available: !all.available > 0
});
}
}
checkChange(index) {
if (this.state.checkIndex !== index) {
this.setState({
checkIndex: index,
checkConnector: this.state.stationInfo.rateList[index],
})
}
}
getAvailable(type) {
let count = type;
if (typeof type === 'string') {
count = type == "AC" ? this.state.stationInfo.acConnector : this.state.stationInfo.dcConnector;
}
if (count) {
return count.available + '/' + count.all;
} else {
return '0/0';
}
}
getReserve() {
apiCharge.getUserReserve(this.state.stationInfo.id).then(res => {
if (res.data.reservePk && res.data.reserveEndTimeTimestamp > 0) {
this.setState({
userReserve: res.data
}, () => this.startCountdown());
} else {
this.stopCountdown();
}
}).catch(err => {
this.stopCountdown();
});
}
onReserve() {
if (this.state.checkConnector?.chargeTypePk) {
Dialog.showProgressDialog();
apiCharge.reserveCharge({
sitePk: this.state.stationInfo.id,
chargeTypePk: this.state.checkConnector.chargeTypePk
}).then(res => {
Dialog.dismissLoading();
toastShort('Reserved successfully!');
PagerUtil.setBackRefreshing();
this.getReserve();
}).catch(err => {
Dialog.dismissLoading();
toastShort(err)
});
} else {
toastShort("Please select a connnector")
}
}
onCancel() {
if (this.state.userReserve.reservePk) {
Dialog.showProgressDialog();
apiCharge.cancelReserve(this.state.userReserve.reservePk).then(res => {
Dialog.dismissLoading();
PagerUtil.setBackRefreshing();
toastShort('Cancel successfully!');
this.getReserve();
}).catch(err => {
Dialog.dismissLoading();
toastShort(err)
});
}
}
cancelReserve() {
// this.setState({
// showCancelDialog: true
// });
Dialog.showDialog({
title: 'Cancle Reservation',
message: 'Are you sure you want to cancle reservation?',
ok: 'YES',
cancel: 'NO',
callback: (btn => {
if (btn == "ok") {
this.onCancel();
}
})
})
}
startCountdown() {
if (this.state.userReserve.reserveEndTimeTimestamp > 0) {
PagerUtil.onReserve();
const leftId = this.state.leftId;
this.countdown(leftId);
} else {
this.stopCountdown(false, true);
}
}
countdown(leftId) {
if (leftId != this.state.leftId) {
console.log(leftId, this.state.leftId);
return;
}
const now = new Date().getTime();
let left = this.state.userReserve.reserveEndTimeTimestamp - now;
let s = 0, m = 0, h = 0;
if (left > 1000) {
s = left / 1000;
if (s > 60) {
m = s / 60;
s = s % 60;
if (m > 60) {
h = m / 60;
m = m % 60;
}
}
} else {
this.stopCountdown(false, true)
}
this.setState({
timeLeft: this.formatNumber(h) + ' : ' + this.formatNumber(m) + ' : ' + this.formatNumber(s)
});
setTimeout(() => {
this.countdown(leftId);
}, 1000);
}
formatNumber(ins) {
const num = parseInt(ins)
if (num > 0) {
if (num < 10) {
return '0' + num;
} else {
return num;
}
} else {
return '00';
}
}
stopCountdown(just, refresh) {
if (just) {
this.setState({
leftId: this.state.leftId + 1
});
} else {
this.setState({
leftId: this.state.leftId + 1,
userReserve: {}
});
}
if (refresh) {
PagerUtil.setBackRefreshing();
}
}
enterStatioinId() {
PagerUtil.onEnterStation();
}
render() {
return (
{ this.state.showReserve
? (
this.state.userReserve.reservePk
? this.countdownView()
: this.reserveView()
)
:
Reservation is not available for this site.
}
{
this.setState({
showCancelDialog: false
});
if (confirm) {
this.onCancel();
}
}}/>
this.enterStatioinId()}
onClose={() => {
this.setState({
showStationDialog: false
});
}}
/>
);
}
//预定页面
reserveView() {
return (
<>
Choose Connector
{ this.state.total > 0
? this.state.stationInfo.rateList.map((item, index) => {
const _type = item.type?.indexOf('AC') >= 0 ? 'AC' : 'DC';
return (
{
if (item.available) {
this.checkChange(index)
}
}
}
checked={index == this.state.checkIndex}>
{/*
*/}
{item.type}
Type
{item.power}
Power
{this.getAvailable(_type)}
Available/Total
{ item?.connectorCount?.available > 0
? Available
: Unavailable
}
Status
{/* index == this.state.checkIndex
? Selected
: (item.available
? Available
: Unavailable
)
*/}
)
}) : null
}
{ this.state.checkConnector.available
? <>
Choose Rate
Rate
{this.state.checkConnector.rates}
{/* Selected */}
>
:
}
{/* Choose Payment Method
*/}