Comment.js 9.41 KB
/**
 * Created by DEV005 on 2017/11/13.
 */

/**
 * Created by tdzl2003 on 12/18/16.
 */
import React, {Component} from "react";
import {StyleSheet, Text, TextInput, TouchableOpacity, View} from "react-native";
import dismissKeyboard from 'react-native/Libraries/Utilities/dismissKeyboard';
import {NavigationActions, StackActions} from 'react-navigation'
import {xnToast} from "../utils/utils";
import AppService from "../service/AppService";
import WfQuickCommentView from "../components/WfQuickCommentView";
import {onResponseError} from "../utils/Common";


export default class Detail extends Component {
    status = this.props.navigation.state.params.status || "";
    params = this.props.navigation.state.params;
    isRequired = false;
    ajaxLoading = false;

    constructor(props) {
        super(props);
        this.state = {
            content: "",
            placeholder: ""
        };
    }

    static navigationOptions = ({navigation, screenProps}) => ({});

    componentDidMount() {
        let _this = this;
        //设置头部

        this.props.navigation.setParams({
            title: _this.params.title || "审批意见",
            isBack: true
        });

        _this.init();
    }

    init = () => {
        let _this = this;
        switch (_this.status) {
            //通过
            case "approve":

                _this.setState({
                    placeholder: "请输入同意理由(非必填)"
                })
                _this.isRequired = false;
                break;
            // 拒绝
            case "reject":
                _this.setState({
                    placeholder: "请输入拒绝理由(必填)"
                })
                _this.isRequired = true;
                break;
            case "transfer":
                _this.setState({
                    placeholder: "请输入转交理由(非必填)"
                })
                _this.isRequired = false;
                break;

            //回退
            case "untread":
                _this.setState({
                    placeholder: "请输入回退理由(必填)"
                })
                _this.isRequired = true;
                break;
            //评论
            case "comment":
                _this.setState({
                    placeholder: "说点什么呗..."
                })
                _this.isRequired = true;
                break;
        }


    }

    doSave = () => {
        let _this = this;

        let vm = {
            id: _this.params.id, //stepId
            flowId: _this.params.flowId,
            rowVersion: _this.params.rowVersion,
            tenantId: global.tenantId,
            submitDescription: _this.state.content || ""
        };
        if (_this.ajaxLoading) {
            xnToast("操作进行中,请稍后再试!");
            return;
        }
        switch (_this.status) {
            //通过
            case "approve":
                //同意
                _this.ajaxLoading = true;
                AppService.approveFlowStep(vm).then(data => {
                    _this.ajaxLoading = false;
                    if (!onResponseError(data, true)) {
                        xnToast("已同意!");
                        dismissKeyboard();
                        _this.toPage()
                    }
                });

                break;
            // 拒绝
            case "reject":
                //拒绝
                if (_this.isRequired && !vm.submitDescription) {
                    xnToast("拒绝理由不能为空!")
                    return;
                }
                _this.ajaxLoading = true;
                AppService.rejectFlowStep(vm).then(data => {
                    _this.ajaxLoading = false;
                    if (!onResponseError(data, true)) {
                        xnToast("已拒绝!");
                        dismissKeyboard();
                        _this.toPage()
                    }
                });

                break;
            case "transfer":
                if (!_this.params.submitUser) {
                    xnToast("提交人不能为空!");
                    return
                }
                vm.assignUserName = _this.params.submitUser.name
                vm.assignUserId = _this.params.submitUser.id,
                    vm.assignReason = _this.state.content || "",

                    console.log(vm);
                _this.ajaxLoading = true;
                AppService.transferFlowStep(vm).then(data => {
                    _this.ajaxLoading = false;
                    if (!onResponseError(data, true)) {
                        xnToast("已转交!");
                        dismissKeyboard();
                        _this.toPage();
                    }
                });

                break;


            //回退
            case "untread":
                vm.assignReason = _this.state.content || "";

                if (_this.isRequired && !vm.assignReason) {
                    xnToast("回退理由不能为空!")
                    return;
                }

                //回退
                _this.ajaxLoading = true;
                AppService.untreadFlowStep(vm).then(data => {
                    _this.ajaxLoading = false;
                    if (!onResponseError(data, true)) {
                        xnToast("已回退!");
                        dismissKeyboard();
                        _this.toPage()
                    }

                });

                break;

            //评论
            case "comment":
                let comment = {
                    content: _this.state.content || "",
                    businessType: "WORKFLOW",
                    anonymous: false,
                    userId: global.userId,
                    businessId: _this.params.flowId,
                }

                if (_this.isRequired && !comment.content) {
                    xnToast("评论内容不能为空!")
                    return;
                }

                //评论
                _this.ajaxLoading = true;
                AppService.createComment(comment).then(data => {
                    _this.ajaxLoading = false;
                    if (!onResponseError(data, true)) {
                        xnToast("已评论!");
                        _this.params.callback();
                        dismissKeyboard();
                        _this.props.navigation.goBack();
                    }

                });

                break;
        }

    }

    toPage = () => {
        if (!!global.initParam && global.initParam.page) {
            global.initParam.page = undefined
        }
        const resetAction = StackActions.reset({
            index: 0,
            actions: [
                NavigationActions.navigate({routeName: 'TabNav'})
            ]
        })
        this.props.navigation.dispatch(resetAction)

    };

    render() {
        let text = '';
        switch (this.status) {
            case "approve":
                text = '同意';
                break;
            case "reject":
                //拒绝
                text = '拒绝';
                break;
            case "transfer":
                text = '转交';
                break;
            //回退
            case "untread":
                text = '回退';
                break;
        }
        return (
            <View style={styles.body}>
                <View style={styles.input}>
                    <TextInput
                        placeholder={this.state.placeholder}
                        style={styles.inputArea}
                        multiline={true}
                        underlineColorAndroid="transparent"
                        onChangeText={(text) => {
                            this.setState({
                                content: text,
                            })
                        }}
                        value={this.state.content}
                    />
                </View>
                {(this.status == 'approve' || this.status == 'reject') &&
                <WfQuickCommentView
                    style={{flex: 1}}
                    category={'approve'}

                    userId={global.userId}
                    chooseCallback={(text) => {
                        let current = this.state.content;
                        current += text;
                        this.setState({
                            content: current
                        })
                    }}
                />
                }
                <TouchableOpacity onPress={() => this.doSave()} style={{
                    width: '70%',
                    height: 44,
                    marginTop: 10,
                    backgroundColor: global.homeColor,
                    justifyContent: 'center',
                    alignItems: "center",
                    borderRadius: 4
                }}>
                    <Text style={{color: '#fff'}}>确认{text}</Text>
                </TouchableOpacity>
            </View>
        );
    }

}

const styles = StyleSheet.create({
    body: {
        flex: 1,
        flexDirection: "column",
        backgroundColor: "#ecf0f3",
        alignItems: 'center'
    },
    input: {
        paddingLeft: 0,
        paddingTop: 15,
        paddingRight: 0,
        marginBottom: 15,
        width: '100%'
    },
    inputArea: {
        fontSize: 14,
        height: 200,
        lineHeight: 24,
        textAlignVertical: "top",
        backgroundColor: "#fff",
        padding: 15
    },

});