/**
 * 我的会员列表页面
 * @邠心vbe on 2023/07/14
 */
import React, { Component } from 'react';
import { View, Text, StyleSheet, Pressable } from 'react-native';
import apiMember from '../../api/apiMember';
import { ElevationObject } from '../../components/Button';

export default class MembersList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      memberList: []

    };
  }

  componentDidMount() {
    this.getMyMemberList();
    this.props.navigation.addListener('focus', () => {
      this.getMyMemberList();
    })
  }

  getMyMemberList() {
    apiMember.getMyMemberList().then(res => {
      if (res.data) {
        this.setState({
          memberList: res.data
        });
      }
    }).catch(err => {
      this.setState({
        memberList: []
      });
      toastShort(err)
    });
  }

  getMembershipStatus(status) {
    switch (status) {
      case "Pending":
        return $t("members.statusPending");
      case "Approved":
        return $t("members.statusApproved");
      case "Pass":
        return $t("members.statusApproved");
      case "Rejected":
        return $t("members.statusRejected");
      default:
        return $t("charging.statusUnavailable");
    }
  }

  render() {
    return (
      this.state.memberList.length > 0
      ? this.state.memberList.map((item, index) => (
        <Pressable
          key={index}
          style={styles.memberView}
          onPress={() => {
            //startPage(PageList.editVehicle, {id: item.vehiclePk});
          }}>
          <View style={styles.itemBackground}>
            <MaterialIcons
              name="card-membership"
              color={colorAccent}
              size={56}/>
          </View>
          <View style={styles.memberItem}>
            <Text style={styles.textLabel2}>{$t('members.membership')}:</Text>
            <Text style={styles.textValue2}>{item.membership}</Text>
          </View>
          <View style={styles.memberItem}>
            <Text style={styles.textLabel}>{$t('members.membershipNo')}:</Text>
            <Text style={styles.textValue}>{item.membershipNo}</Text>
          </View>
          <View style={styles.memberItem}>
            <Text style={styles.textLabel}>{$t('members.status')}:</Text>
            <Text style={styles.textValue}>{this.getMembershipStatus(item.membershipStatus)}</Text>
          </View>
        </Pressable>
      ))
      : <Text style={ui.noData}>{$t('members.noData')}</Text>
    );
  }
}

const styles = StyleSheet.create({
  memberView: {
    borderRadius: 8,
    overflow: 'hidden',
    ...ElevationObject(5),
    ...$margin(16, 16, 0),
    ...$padding(16),
    backgroundColor: colorLight
  },
  memberItem: {
    paddingTop: 1,
    paddingBottom: 1,
    flexDirection: 'row'
  },
  textLabel: {
    color: textPrimary,
    fontSize: 14,
    paddingRight: 6,
    fontWeight: 'bold'
  },
  textValue: {
    flex: 1,
    color: textPrimary,
    fontSize: 14
  },
  textLabel2: {
    color: textPrimary,
    fontSize: 16,
    paddingRight: 5,
    marginBottom: 2,
    fontWeight: 'bold'
  },
  textValue2: {
    flex: 1,
    color: textPrimary,
    fontSize: 16,
    marginBottom: 2,
  },
  itemBackground: {
    right: 0,
    bottom: 0,
    opacity: 0.06,
    position: 'absolute'
  }
})