chooseUserInfo.js 10.5 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,
            list: this.props.navigation.state.params.itemList,
            orgaCodeList: []
        };

        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(){
        if(this.props.navigation.state.params.type == 'organization') {
            const _this = this;
            let itemNameList = [];
            let itemCodeList = [];
            this.setState({
                loading: true,
            });
            xnService.getOrganization({ pageSize: 0 }).then((data) => {
                _this.setState({
                    loading: false,
                });
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (!!data.errors === true && !!data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                } else {
                    if(data.result.length > 0) {
                        data.result.map(function (ele,idx) {
                            itemNameList.push(ele.name);
                            itemCodeList.push(ele.code);
                            if(idx === data.result.length - 1) {
                                _this.setState({
                                    list: itemNameList,
                                    orgaCodeList: itemCodeList
                                });
                            }
                        })
                    } else {
                        xnToast('暂无数据');
                        _this.setState({
                            list: [],
                            orgaCodeList: []
                        });
                    }
                }
            });
        }
    };

    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 = 1;
            }else if (this.state.select == '群众'){
                type = 2;
            }else if (this.state.select == '共青团员'){
                type = 3;
            }else if (this.state.select == '民主党派'){
                type = 4;
            }
            xnService.registPart({id:global.user.id,politicalStatus:type}).then((data) => {
                this.setState({
                    loading: false
                });
                if(data.message){
                    xnToast(data.message);
                    return;
                }
                if(!!data.errors === true && !!data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                }else {
                    if (this.state.select == '中共党员'){
                        global.user.politicalStatus = 1;
                    }else if (this.state.select == '群众'){
                        global.user.politicalStatus = 2;
                    }else if (this.state.select == '共青团员'){
                        global.user.politicalStatus = 3;
                    }else if (this.state.select == '民主党派'){
                        global.user.politicalStatus = 4;
                    }
                    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.state.orgaCodeList[this.state.list.indexOf(this.state.select)];
            xnService.registPart({id:global.user.id,partyId:id}).then((data) => {
                this.setState({
                    loading: false
                });
                if(data.message){
                    xnToast(data.message);
                    return;
                }
                if(!!data.errors === true && !!data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                }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})} key={i}>
                <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.state.list.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'
    },
    loadingBg: {
        width: '100%',
        height: '100%',
        position: 'absolute',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
    },
});