addEmployee.js 7.41 KB
/**
 * Created by Cassie on 18/06/15
 */
import React, {Component} from "react";
import {
    StyleSheet,
    Text,
    TouchableOpacity,
    View,
    Image,
    StatusBar,
    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');
const search2 = require('../../img/search.png');

export default class AddEmployee extends Component {

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

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

    componentDidMount() {
        this.props.navigation.setParams({
            title:'通讯录',
            backgroundColor:'white',
            titleColor:'black',
            isBack:true,
        });
    };
    search(){
        NoDoublePress.onPress(() => {
            this.toSearch = true;
            this.refs.mailList.toSearch();
            this.props.navigation.navigate('SearchPeople',{callback:this.searchBcak});
        })
    }
    //搜索选择回调
    searchBcak=(data)=>{
        if (data != ""){
            this.refs.mailList.selectItem(data);
        }
        this.toSearch = false;
    };
    /*渲染员工*/
    renderEmployee=(data)=>{
        if(data.length ==0 ){
            return;
        }

        if (this.from == 'doEmployee') {
            let selectEmployee = [];
            let alreadyArr = global.readyCcEmployee;
            let alreadyId = [];
            let hash = {};
            if(alreadyArr.length != 0){
                for(let i=0;i<alreadyArr.length;i++){
                    alreadyId.push(alreadyArr[i].userId)
                }
                let resultData = [];
                for(let i=0;i<data.length;i++){
                    data[i].userRole = 'DEAL_USER';
                    if(alreadyId.indexOf(data[i].userId) == -1){
                        resultData.push(data[i])
                    }
                }
                selectEmployee = JSON.parse(JSON.stringify(global.readyDoEmployee)).concat(resultData);
            }else{
                selectEmployee = JSON.parse(JSON.stringify(global.readyDoEmployee)).concat(data);
            }
            global.readyDoEmployee = [];
            for(let i=0;i<selectEmployee.length;i++){
                if(!hash[selectEmployee[i].userId]){
                    hash[selectEmployee[i].userId] = -1;
                    global.readyDoEmployee.push(selectEmployee[i])
                }
            }
        } else if (this.from == 'ccEmployee') {
            for(let i=0;i<data.length;i++){
                if(data[i].userId == global.userId){
                    data.splice(i,1);
                    break;
                }
            }
            let selectEmployee = [];
            let alreadyArr = global.readyDoEmployee;
            let alreadyId = [];
            let hash = {};
            if(alreadyArr.length != 0){
                for(let i=0;i<alreadyArr.length;i++){
                    alreadyId.push(alreadyArr[i].userId)
                }
                let resultData = [];
                for(let i=0;i<data.length;i++){
                    data[i].userRole = 'CC_USER';
                    if(alreadyId.indexOf(data[i].userId) == -1){
                        resultData.push(data[i])
                    }
                }
                selectEmployee = JSON.parse(JSON.stringify(global.readyCcEmployee)).concat(resultData)
            }else{
                selectEmployee = JSON.parse(JSON.stringify(global.readyCcEmployee)).concat(data)
            }
            global.readyCcEmployee = [];
            for(let i=0;i<selectEmployee.length;i++){
                if(!hash[selectEmployee[i].userId]){
                    hash[selectEmployee[i].userId] = -1;
                    global.readyCcEmployee.push(selectEmployee[i])
                }
            }
        }else if (this.from == 'approveEmployee'){
            if (this.toSearch ==true){
                this.toSearch = false;
            }
            for(let i=0;i<data.length;i++){
                if(data[i].userId == global.userId){
                    data.splice(i,1);
                    break;
                }
            }
            let selectEmployee = [];
            let alreadyArr = global.readApproveEmployee;
            let alreadyId = [];
            let hash = {};
            if(alreadyArr.length != 0){
                for(let i=0;i<alreadyArr.length;i++){
                    alreadyId.push(alreadyArr[i].userId)
                }
                let resultData = [];
                for(let i=0;i<data.length;i++){
                    data[i].userRole = 'APPROVE_USER';
                    if(alreadyId.indexOf(data[i].userId) == -1){
                        resultData.push(data[i])
                    }
                }
                selectEmployee = JSON.parse(JSON.stringify(global.readApproveEmployee)).concat(resultData)
            }else{
                selectEmployee = JSON.parse(JSON.stringify(global.readApproveEmployee)).concat(data)
            }
            global.readApproveEmployee = [];
            for(let i=0;i<selectEmployee.length;i++){
                if(!hash[selectEmployee[i].userId]){
                    hash[selectEmployee[i].userId] = -1;
                    global.readApproveEmployee.push(selectEmployee[i])
                }
            }
        }
        DeviceEventEmitter.emit('reloaEmployee');
        if(!this.toSearch){
            this.props.navigation.goBack();
        }
    };

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

    render() {
        return (
            <View style={styles.background}>
                <StatusBar
                    backgroundColor={'#FFFFFF'}
                    barStyle={"dark-content"}
                    networkActivityIndicatorVisible
                />
                <TouchableOpacity activeOpacity={0.8} style={styles.searchBox}
                                  onPress={() => this.search()}>
                    <Image source={search2} style={{width: (13 / zoomH), height: (13 / zoomH)}} resizeMode="contain"/>
                    <View style={{flex: 1, marginLeft: (8 / zoomW)}}>
                        <Text style={{fontSize: 14, color: '#8e8e93'}}>搜索</Text>
                    </View>
                </TouchableOpacity>

                <Maillist ref="mailList" callback={this.renderEmployee} tenantId={global.tenantId} tenantShortName={global.tenantShortName} employeeType="USER" themeColor="#00be3c" multiChoice = {this.multiChoice} />
            </View>
        );
    }
}

const styles = StyleSheet.create({
    topIcon: {
        paddingLeft: (10 / zoomW),
        paddingRight: (10 / zoomW),
        height: 34/zoomH,
        display: 'flex',
        justifyContent: 'center'
    },
    searchBox: {
        width: (345 / zoomW),
        height: (34 / zoomH),
        backgroundColor: '#eeeff3',
        borderRadius: 3,
        display: 'flex',
        flexDirection: 'row',
        alignItems: 'center',
        paddingLeft: (12.5 / zoomW),
        paddingRight: (12.5 / zoomW),
        marginBottom: (12.5 / zoomH),
        marginTop: (7 / zoomH)
    },
    background: {
        flex: 1,
        backgroundColor: '#fff',
        display: 'flex',
        alignItems:"center",
        justifyContent:"center",
    },
});