addApprovePeople.js 7.51 KB
/**
 * Created by Cassie on 2018/05/14
 * 添加审批人员
 */
import React, {Component} from "react";
import {StyleSheet, View, Text, TouchableOpacity, Image, StatusBar, DeviceEventEmitter, ScrollView,NativeModules} from "react-native";
import {width,zoomW, zoomH} from "../../utils/getSize";
import {NoDoublePress} from "../../utils/utils";

const back = require('../../img/returnB.png');
const circleAdd = require('../../img/circleAdd.png');
const circleDel = require('../../img/circleDel.png');
const avatar = require('../../img/avatar.png');
const remove = require('../../img/remove.png');
const arrowH = require('../../img/arrow_h.png');

export default class AddApprovePeople extends Component {
    constructor(props) {
        super(props);
        this.state = {
            title:"添加审批人",
        };
    };

    componentWillMount() {

        this.setState({
            approveEmployee: global.approveEmployee,
        });
        global.readApproveEmployee = global.approveEmployee;
        if ( global.approveEmployee.length == 0) {
            this.props.navigation.setParams({
                color: 'rgba(0,190,60,.2)',
                opacity: 1
            });
            this.setState({
                canAdd: false
            })
        } else {
            this.props.navigation.setParams({
                color: 'rgba(0,190,60,1)',
                opacity: 0.8
            });
            this.setState({
                canAdd: true
            })
        }

        this.props.navigation.setParams({
            title: '添加审批人'
        });

    }

    componentDidMount() {
        this.props.navigation.setParams({
            title:'添加审批人',
            backgroundColor:'white',
            titleColor:'black',
            isBack:true,
        });
        let _this = this;
        this.reloaEmployee = DeviceEventEmitter.addListener('reloaEmployee', function () {
            _this.setState({
                approveEmployee: global.readApproveEmployee,
            });
        });
    };

    /*渲染人员*/
    renderPeople(item, index) {
        return (
            <View key={index} style={{flexDirection:'row',alignItems:'center'}}>
                <View style = {[styles.avatarBox]}>
                    <View style={styles.avatarPic}>
                        <Image source={item.avatar ? {uri: item.avatar + '?x-oss-process=image/resize,w_500'} : avatar}
                               style={{width: (50 / zoomH), height: (50 / zoomH), borderRadius: (25 / zoomH)}}
                               resizeMode="cover"/>
                        <TouchableOpacity activeOpacity={0.8} style={{
                            paddingLeft: (10 / zoomH),
                            paddingBottom: (10 / zoomH),
                            position: 'absolute',
                            top: 0,
                            right: 0
                        }} onPress={() => NoDoublePress.onPress(() => {
                            this.delPerson(index)
                        })}>
                            <Image source={remove} style={{width: (18 / zoomH), height: (18 / zoomH)}}
                                   resizeMode="contain"/>
                        </TouchableOpacity>
                    </View>
                    <Text style={{fontSize: 12, color: '#666', marginTop: (6 / zoomH)}}>{item.name}</Text>
                </View>
                {index!=4&&<Image style = {{width:30/zoomW}} source={arrowH} resizeMode={'contain'}></Image>}
            </View>
        )
    };

    finish(){
        NoDoublePress.onPress(() => {

            global.approveEmployee = this.state.approveEmployee;
            DeviceEventEmitter.emit('reloaAddEmployee');
            this.props.navigation.goBack();
        })
    }

    /*跳转到通讯录*/
    toPeopleList(){
        NoDoublePress.onPress(() => {
            NativeModules.security.getTenantName().then((result)=>{
                global.tenantShortName = result;
                //multiChoice通讯录是否支持多选
                this.props.navigation.navigate('AddEmployee', {from: 'approveEmployee',multiChoice:false});
            });
        });
    };

    /*删除人员*/
    delPerson(index) {
        let approveEmployee = [];
        if(this.state.approveEmployee.length == 0){
            approveEmployee = JSON.parse(JSON.stringify(global.approveEmployee));
        }else{
            approveEmployee = JSON.parse(JSON.stringify(this.state.approveEmployee))
        }
        approveEmployee.splice(index, 1);
        global.readApproveEmployee = approveEmployee;
        this.setState({
            approveEmployee: approveEmployee
        });
    };

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

    render() {
        return (
            <View style={styles.background}>
                <StatusBar
                    backgroundColor={'#FFFFFF'}
                    barStyle={"dark-content"}
                    networkActivityIndicatorVisible
                />
                <ScrollView style={{width:'100%',flex: 1,marginBottom:48/zoomH}}>
                    <View style={styles.addContent}>
                        <Text style={{fontSize: 17, color: '#333', paddingTop: (15 / zoomW)}}>审批人</Text>
                        <View style={styles.addItem}>
                            {!!this.state.approveEmployee && this.state.approveEmployee.map((item, index) => this.renderPeople(item, index))}
                            {this.state.approveEmployee.length<5&&<TouchableOpacity style={styles.avatarBox} activeOpacity={0.8}
                                              onPress={() => this.toPeopleList()}>
                                <Image source={circleAdd} style={{width: (50 / zoomH), height: (50 / zoomH)}}
                                       resizeMode="contain"/>
                            </TouchableOpacity>}
                        </View>
                    </View>
                </ScrollView>
                <TouchableOpacity activeOpacity={0.6} style={{flex: 1,display:'flex',flexDirection:'row',alignItems:'center',justifyContent:'center',position:"absolute",bottom:0,width:"100%",backgroundColor:"#28C35A",height:(48/zoomH)}}
                                  onPress={()=>this.finish()}>
                    <View>
                        <Text style={{fontSize:16,color:'#FFFFFF'}}>完成</Text>
                    </View>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    topIcon: {
        paddingLeft: (10 / zoomW),
        paddingRight: (10 / zoomW),
        height: '100%',
        display: 'flex',
        justifyContent: 'center'
    },
    background: {
        flex: 1,
        backgroundColor: '#ececf1',
        display: 'flex',
        alignItems: 'center'
    },
    addContent: {
        width: '100%',
        backgroundColor: '#fff',
        marginTop: (15 / zoomH),
        padding: (15 / zoomW)
    },
    addItem: {
        width: '100%',
        display: 'flex',
        flexDirection: 'row',
        flexWrap: 'wrap'
    },
    avatarBox: {
        marginTop: (5/ zoomW),
        marginBottom: (5 / zoomW),
        marginLeft:(width-30/zoomW-250/zoomH)/10,
        marginRight:(width-30/zoomW-250/zoomH)/10,
        borderRadius: 4,
        display: 'flex',
        alignItems: 'center',
        paddingTop: (8 / zoomH)
    },
    avatarPic: {
        width: (50 / zoomH),
        height: (50 / zoomH),
        borderRadius: (25 / zoomH)
    }
});