import React, { Component } from 'react';
import { View, Text, StyleSheet, RefreshControl, FlatList, Pressable } from 'react-native';
import VoucherType from './VoucherType';
import apiVoucher from '../../api/apiVoucher';
import { MyRefreshProps } from '../../components/ThemesConfig';
import TextView from '../../components/TextView';
import BadgeSelectItem from '../../components/BadgeSelectItem';
import Button from '../../components/Button';
import PagerUtil from '../chargeV2/PagerUtil';
export default class VoucherSelect extends Component {
constructor(props) {
super(props);
this.state = {
dataList: [],
voucherType: "",
hasMore: true,
refreshing: false,
selectedVoucher: {}
};
this.params = {
chargeBoxId: "",
connectorId: ""
}
}
componentDidMount() {
const params = this.props.route.params;
if (params.chargeBoxId) {
this.params.chargeBoxId = params.chargeBoxId
}
if (params.connectorId) {
this.params.connectorId = params.connectorId
}
console.log("选择优惠券", this.params);
this.getDataList();
this.props.navigation.addListener('beforeRemove', (e) => {
PagerUtil.setSelectedVoucher(this.state.selectedVoucher);
});
}
onRefresh() {
this.setState({
refreshing: true
})
this.getDataList();
}
getDataList(lastId) {
apiVoucher.getSelectionVoucher({
...this.params,
voucherType: this.state.voucherType
}).then(res => {
if (res.data) {
if (lastId) {
if (res.data.length > 0) {
const list = this.state.dataList;
this.setState({
dataList: list.concat(res.data),
hasMore: true
});
} else {
this.setState({
hasMore: false
})
}
} else {
this.setState({
dataList: res.data,
hasMore: res.data.length == 0
});
}
} else {
this.setState({
dataList: [],
hasMore: true
});
}
}).catch(err => {
toastShort(err)
this.setState({
dataList: [],
hasMore: true
});
}).finally(() => {
this.setState({
refreshing: false,
selectedVoucher: {}
});
});
}
getDataListPage() {
if (this.state.dataList.length > 0 && this.state.hasMore) {
const last = this.state.dataList[this.state.dataList.length-1]
this.getDataList(last.voucherId);
}
}
getColorByStatus(status) {
let color = colorAccent;
switch (status) {
case "Used":
case "Expired":
color = "#434343";
break;
case "Expiring":
color = "#ED3F3F"
break;
}
return color;
}
changeType(type) {
this.setState({
voucherType: type
});
this.getDataList();
}
onSetectVoucher(item) {
if (item.userVoucherId != this.state.selectedVoucher.userVoucherId) {
this.setState({
selectedVoucher: item
})
} else {
this.setState({
selectedVoucher: {}
})
}
}
listItem = ({item, index, separators}) => {
return (
this.onSetectVoucher(item)}>
{item.voucherName}
{item.voucherDesc}
{$t("voucher.expiresOn") + item.expiresOn}
{item.userVoucherStatus}
)
}
topView = (props) => {
return (
this.changeType(type)}
/>
);
}
bottomView = () => {
if (!this.state.hasMore) {
return ({$t('voucher.noMore')})
} else {
return null
}
}
render() {
return (
item.userVoucherId}
onEndReached={() => this.getDataListPage()}
onEndReachedThreshold={0.3}
ListEmptyComponent={{$t('voucher.noData')}}
ListFooterComponent={this.bottomView}
refreshControl={
this.onRefresh()}
/>
}/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingLeft: 16,
paddingRight: 16,
paddingBottom: 16
},
itemView: {
padding: 16,
marginTop: 16,
borderWidth: 1,
borderColor: '#DADADA',
borderRadius: 4,
alignItems: 'center',
flexDirection: 'row',
backgroundColor: colorLight
},
itemContent: {
flex: 1,
paddingRight: 16
},
voucherTitle: {
color: textPrimary,
fontSize: 16,
fontWeight: 'bold'
},
voucherDesc: {
color: textPrimary,
fontSize: 14,
paddingTop: 2,
paddingBottom: 4
},
expireDate: {
color: textCancel,
fontSize: 12
},
statusButton: {
color: colorAccent,
padding: 8,
minWidth: 73,
fontSize: 12,
textAlign: 'center',
borderWidth: 1,
borderColor: colorAccent,
borderRadius: 6,
textTransform: 'uppercase',
backgroundColor: colorLight
},
noData: {
color: textPlacehoder,
fontSize: 14,
padding: 20,
marginTop: 16,
textAlign: 'center'
},
noMore: {
color: textPlacehoder,
fontSize: 14,
padding: 16,
textAlign: 'center'
}
})