/** * 新版支付方式选择器 * @邠心vbe on 2021/04/13 */ import React, { Component } from 'react'; import { View, StyleSheet, Pressable, ScrollView } from 'react-native'; import BottomModal from '../../components/BottomModal'; import BadgeSelectItem from '../../components/BadgeSelectItem'; import { ChargeStyle } from './Charging'; import { PAYTYPE, PaymentIcon } from '../payment/PaymentConfig'; import Skeleton from "react-native-reanimated-skeleton"; import TextView from '../../components/TextView'; import apiCharge from '../../api/apiCharge'; import Button from '../../components/Button'; import Dialog from '../../components/Dialog'; import app from '../../../app.json'; import utils from '../../utils/utils'; export default class PaymentListV2 extends Component { constructor(props) { super(props); this.state = { visible: false, isSelect: true, options: [], selectIndex: 0, selectOptions: {}, isloading: true }; } componentDidMount() { this.getPaymentOptions(); this.setState({ isSelect: (this.props.isSelect || true) }) } componentDidUpdate() { if (this.props.isSelect != undefined && this.state.isSelect !== this.props.isSelect) { this.setState({ isSelect: this.props.isSelect }) } } getPaymentOptions() { if (utils.isEmpty(this.props.chargeBoxId)) { setTimeout(() => { this.getPaymentOptions(); }, 1000); return; } apiCharge.getPaymentTypeOptions({ chargeBoxId: this.props.chargeBoxId }).then(res => { if (res.data) { this.setState({ options: res.data, isloading: false }) if (this.props.payType && this.props.payType.code) { for (let i = 0; i < res.data.length; i++) { let type = res.data[i]; if (type.code == this.props.payType.code) { this.changeMethod(i); break; } } } else { let index = 0; for (let i = 0; i < res.data.length; i++) { let type = res.data[i]; if (type.def) { index = i; break; } } this.changeMethod(index); } } }).catch(err => { }) } showDialog(visible) { if (this.state.isSelect) { this.setState({ visible: visible }) } } changeMethod(index) { const type = this.state.options[index]; this.setState({ selectIndex: index, selectOptions: type }, () => { if (this.props.onMethodChange) { this.props.onMethodChange(this.state.selectOptions); } }) } setDefault() { const code = this.state.selectOptions?.code; if (code && !this.state.selectOptions.def) { Dialog.showProgressDialog(); apiCharge.setDefaultPaymentType({ defaultPaymentMethod: code }).then(res => { toastShort("success"); this.getPaymentOptions(); }).catch(err => { toastShort(err) }).finally(() => { Dialog.dismissLoading(); }) } } render() { return ( this.showDialog(true)}> { app.charge.version >= 4 ? : } { (this.state.isSelect && this.state.options.length > 1) && {this.state.options.length} } {this.state.selectOptions?.name} {this.state.selectOptions?.desc} { this.state.selectOptions?.def && Default } { this.state.isSelect && } this.showDialog(false)}> Payment Methods { this.state.options.map((item, index) => ( this.changeMethod(index)}> {item.name} {item.desc} { item.def && Default } )) }