chooseUserInfo.js 8.16 KB
/**
 * Created by Cassie on 2018/03/21
 */
import React, { Component } from 'react';
import {
    StyleSheet,
    View,
    Text,
    TouchableOpacity,
    Image,
    Animated,
    ActivityIndicator,
    Platform,
    ScrollView,
    Button
} from 'react-native';
import moment from 'moment';
import {width,height,zoomW,zoomH} from '../../utils/getSize';
import xnService from "../../service/AppService";

import {clock,xnToast,timeReduce,timePlus,timeEvery,getDay} from '../../utils/utils';


export default class chooseUserInfo extends Component {

    static navigationOptions = ({navigation}) => ({
        headerTitle:navigation.state.params.title,
        headerLeft:(
            <TouchableOpacity style={styles.backWrap} onPress={() => navigation.goBack()}>
                <Image source={require('../../img/back_gray.png')} resizeMode="contain" />
            </TouchableOpacity>
        ),
        headerRight:(
            <Button
                onPress={navigation.state.params?navigation.state.params.save:()=>console.log('onPress')}
                title="保存"
                color="#4b85e0"
            />
        )
    });

    constructor(props){
        super(props);
        this.state = {
            loading:false,
            select:this.props.navigation.state.params.defultValue,
        };

        if (this.props.navigation.state.params.type == 'gender'){
            if (this.props.navigation.state.params.defultValue == 'M'){
                this.state.select = '男'
            }else if (this.props.navigation.state.params.defultValue == 'F'){
                this.state.select = '女'
            }else {
                this.state.select = ''
            }
        }
    };

    componentWillMount(){

    };

    componentDidMount(){
        this.props.navigation.setParams({
            save:this.save,
        })
    }

    save=()=>{

        if (this.props.navigation.state.params.type == 'gender'){

            let value;

            if (this.props.navigation.state.params.defultValue == 'M'){
                value = '男'
            }else if (this.props.navigation.state.params.defultValue == 'F'){
                value = '女'
            }else {
                value = ''
            }

            if (this.state.select == value){
                xnToast('您并未做出修改');
                return;
            }

        }else {
            if (this.state.select == this.props.navigation.state.params.defultValue){
                xnToast('您并未做出修改');
                return;
            }
        }

        this.setState({
            loading:true,
        });

        let params;

        if (this.props.navigation.state.params.type == 'gender'){

            params = {
                id:global.user.id,
                gender:this.state.select =='男'?'M':'F'
            };

            global.user.gender = this.state.select =='男'?'M':'F'

        }else if (this.props.navigation.state.params.type == 'education'){

            params = {
                id:global.user.id,
                education:this.state.select
            };
            global.user.education = this.state.select;

        }else if (this.props.navigation.state.params.type == 'occupation'){

            params = {
                id:global.user.id,
                occupation:this.state.select
            };
            global.user.occupation = this.state.select;

        }else if (this.props.navigation.state.params.type == 'partyMember'){

            let type;
            if (this.state.select == '是'){
                type = true;
                global.user.isPartyMember = true
            }else {
                type = false;
                global.user.isPartyMember = false
            }

            xnService.registPart({id:global.user.id,isPartyMember:type}).then((data) => {
                this.setState({
                    loading: false
                });
                if(data.message){
                    xnToast(data.message);
                    return;
                }
                if(data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    global.user.isPartyMember = undefined
                }else {
                    if (this.props.navigation.state.params.callback){
                        this.props.navigation.state.params.callback();
                        this.props.navigation.goBack();
                    }
                }
            })
            return;
        }else if (this.props.navigation.state.params.type == 'organization'){


            let id = this.props.navigation.state.params.itemList.indexOf(this.state.select) +1

            xnService.registPart({id:global.user.id,partyId:id+1}).then((data) => {
                this.setState({
                    loading: false
                });
                if(data.message){
                    xnToast(data.message);
                    return;
                }
                if(data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    global.user.partyName = undefined;
                }else {

                    global.user.partyName = this.state.select
                    if (this.props.navigation.state.params.callback){
                        this.props.navigation.state.params.callback();
                        this.props.navigation.goBack();
                    }
                }
            })
            return;
        }

        xnService.undateUserInfo(params).then((data) => {
            this.setState({
                loading: false
            });
            if (data.message) {
                xnToast(data.message);
                return;
            }
            if (data.errors.length > 0) {
                xnToast(data.errors[0].message);

                if (this.props.navigation.state.params.type == 'gender'){

                    global.user.gender = this.props.navigation.state.params.defultValue;

                }else if (this.props.navigation.state.params.type == 'education'){

                    global.user.education = this.props.navigation.state.params.defultValue;

                }else if (this.props.navigation.state.params.type == 'occupation'){

                    global.user.occupation = this.props.navigation.state.params.defultValue;
                }

            } else {

                if (this.props.navigation.state.params.callback){
                    this.props.navigation.state.params.callback();
                    this.props.navigation.goBack();
                }
            }
        })
    }

    renderItem=(v,i)=>{
        return (
            <TouchableOpacity onPress={()=>this.setState({select:v})}>
                <View style={{width:width,height:44/zoomH,justifyContent:'center',alignItems:'center',backgroundColor:'white'}}>
                    {this.state.select==v&&<Text style={styles.select}>{v}</Text>}
                    {this.state.select!=v&&<Text style={styles.unSelect}>{v}</Text>}
                </View>
                <View style={{width:'100%',height:1}}></View>
            </TouchableOpacity>
        )
    }


    render(){
        return (
            <View style={styles.background}>
                <ScrollView bounces={false} style={{flex:1}}>
                    <View style={{width:'100%',height:10/zoomH}}></View>
                    {this.props.navigation.state.params.itemList.map((v, i) => this.renderItem(v, i))}
                </ScrollView>
                {this.state.loading && <View style={styles.loadingBg}>
                    <ActivityIndicator size="large" />
                </View>}
            </View>
        );
    }
}

const styles = StyleSheet.create({
    backWrap: {
        justifyContent: 'center',
        paddingLeft: 18.5/zoomW,
        paddingRight: 18.5/zoomW,
        height: 44/zoomH,
    },
    background:{
        flex:1,
        backgroundColor:'#f6f6f6',
        display:'flex',
        alignItems:'center',

    },
    itemLine:{
        height:44/zoomH,
        width:'100%',
        backgroundColor:'white',
        paddingLeft:15/zoomW,
        paddingRight:15/zoomW,
        marginTop:12/zoomH
    },
    select:{
        fontSize:15,
        color:'#3e6db7'
    },
    unSelect:{
        fontSize:15,
        color:'#464646'
    }



});