addEmployee.js 5.06 KB
/**
 * Created by Cassie on 18/06/15
 */
import React, {Component} from "react";
import {
    StyleSheet,
    Text,
    TextInput,
    TouchableOpacity,
    View,
    Image,
    NativeModules,
    DeviceEventEmitter
} from "react-native";
import Maillist from "./maillist";
import {zoomW, zoomH} from "../../utils/getSize";
import {NoDoublePress} from "../../utils/utils";

const back = require('../../img/returnB.png');

export default class AddEmployee extends Component {
    static navigationOptions = ({navigation, screenProps}) => ({
        title:'通讯录',
        headerLeft:(<View style={{flex: 1, display: 'flex', flexDirection: 'row'}}>
                <TouchableOpacity style={styles.topIcon}
                                  onPress={navigation.state.params ? navigation.state.params.back : null}>
                    <Image source={back} style={{width: (16 / zoomH), height: (16 / zoomH)}} resizeMode="contain"/>
                </TouchableOpacity>
            </View>
        ),
        headerRight: (<View style={{flex: 1, display: 'flex', flexDirection: 'row'}} />)
    });

    constructor(props) {
        super(props);
        this.state = {};
    };

    componentWillMount(){
        this.from = this.props.navigation.state.params.from;
        this.target = this.props.navigation.state.params.target;
    };

    componentDidMount() {
        this.props.navigation.setParams({
            back: () => {
                NoDoublePress.onPress(() => {
                    DeviceEventEmitter.emit('reloaEmployee');
                    this.props.navigation.goBack();
                })
            }
        });
    };

    /*渲染员工*/
    renderEmployee=(data)=>{
        for(let i=0;i<data.length;i++){
            if(data[i].userId == global.userId){
                data.splice(i,1);
                break;
            }
        }
        if (this.from == 'doEmployee') {
            let selectEmployee = [];
            let alreadyArr = this.target ? global.linkCcEmployee:global.ccEmployee;
            let alreadyId = [];
            let hash = {};
            if(alreadyArr.length != 0){
                for(let i=0;i<alreadyArr.length;i++){
                    alreadyId.push(alreadyArr[i].id)
                }
                let resultData = [];
                for(let i=0;i<data.length;i++){
                    data[i].userRole = 'DEAL_USER';
                    if(alreadyId.indexOf(data[i].id) == -1){
                        resultData.push(data[i])
                    }
                }
                selectEmployee = this.target ? global.linkDoEmployee.concat(resultData) : global.doEmployee.concat(resultData);
            }else{
                selectEmployee = this.target ? global.linkDoEmployee.concat(data) : global.doEmployee.concat(data);
            }
            global.readyDoEmployee = [];
            for(let i=0;i<selectEmployee.length;i++){
                if(!hash[selectEmployee[i].id]){
                    hash[selectEmployee[i].id] = -1;
                    global.readyDoEmployee.push(selectEmployee[i])
                }
            }
        } else if (this.from == 'ccEmployee') {
            let selectEmployee = [];
            let alreadyArr = this.target ? global.linkDoEmployee:global.doEmployee;
            let alreadyId = [];
            let hash = {};
            if(alreadyArr.length != 0){
                for(let i=0;i<alreadyArr.length;i++){
                    alreadyId.push(alreadyArr[i].id)
                }
                let resultData = [];
                for(let i=0;i<data.length;i++){
                    data[i].userRole = 'CC_USER';
                    if(alreadyId.indexOf(data[i].id) == -1){
                        resultData.push(data[i])
                    }
                }
                selectEmployee = this.target ? global.linkCcEmployee.concat(resultData) : global.ccEmployee.concat(resultData);
            }else{
                selectEmployee = this.target ? global.linkCcEmployee.concat(data) : global.ccEmployee.concat(data);
            }
            global.readyCcEmployee = [];
            for(let i=0;i<selectEmployee.length;i++){
                if(!hash[selectEmployee[i].id]){
                    hash[selectEmployee[i].id] = -1;
                    global.readyCcEmployee.push(selectEmployee[i])
                }
            }
        }
        DeviceEventEmitter.emit('reloaEmployee');
        this.props.navigation.goBack();
    };

    componentWillUnmount() {
        this.setState = (state, callback) => {
            return;
        };
    };

    render() {
        return (
            <View style={styles.background}>
                <Maillist callback={this.renderEmployee} tenantId={global.tenantId} tenantShortName={global.tenantShortName} employeeType="USER" themeColor="#00be3c" />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    topIcon: {
        paddingLeft: (10 / zoomW),
        paddingRight: (10 / zoomW),
        height: '100%',
        display: 'flex',
        justifyContent: 'center'
    },
    background: {
        flex: 1,
        backgroundColor: '#fff',
        display: 'flex'
    },
});