import React from 'react';
import { Input, Button, Form, Icon, Spin, message, } from 'antd';
import './certify.less';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';


const Content = Form.create()(React.createClass({
    getData() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            url: globalConfig.context + "/api/user/orgProcess",
            success: function (data) {
                if (data.data) {
                    this.setState({
                        banks:data.data.banks,
                        bankBranch:data.data.bankBranch,
                        bankCardNumber: data.data.bankCardNumber
                    });
                };
            }.bind(this),
        }).done(function (data) {
            if (data.error.length) {
                message.warning(data.error[0].message);
            }
        }.bind(this)).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            loading: false
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.setState({
                    loading: true
                });
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/user/orgNextPro",
                    data: {
                        validationAmount: values.validationAmount,
                        process: 5
                    },
                    success: function (data) {
                        if (!data.error.length) {
                            message.success('审核成功！');
                            this.props.changeStep(6);
                        } else {
                            if (isNaN(Number(data.error[0].message))) {
                                message.warning(data.error[0].message);
                                this.setState({
                                    loading: false
                                });
                            } else {
                                switch (Number(data.error[0].message)) {
                                    case 1:
                                        message.warning("超过打款日期五日，无法确认提交！");
                                        this.props.changeStep(6);
                                        break;
                                    case 2:
                                        message.warning("输入错误金额次数过多！");
                                        this.props.changeStep(6);
                                        break;
                                }
                            };
                        };
                    }.bind(this),
                });
            }
        });
    },
    componentWillMount() {
        this.getData();
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 12 },
        };
        const _me = this;
        return (
            <Spin spinning={this.state.loading} className="certify-spin">
                <Form horizontal onSubmit={this.handleSubmit} className="certify-form">
                    <FormItem wrapperCol={{ span: 12, offset: 4 }}>
                        <p className="certify-title">输入打款金额</p>
                    </FormItem>
                    <FormItem wrapperCol={{ span: 18}}>
                        <span>
                            请在下方输入{this.state.banks}银行卡{this.state.bankCardNumber}收到的认证打款，
                            务必在收到打款5日内完成，否则将无法完成
                        </span>
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="开户银行"
                    >
                        <span>{this.state.banks}</span>
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="开户银行支行"
                    >
                        <span>{this.state.bankBranch}</span>
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="银行卡号"
                    >
                        <span>{this.state.bankCardNumber}</span>
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="打款金额"
                        extra={<span style={{ 'color': '#ea0862' }}><Icon type="exclamation-circle" /> 仅有3次输入机会，如3次输入有误，需重新认证</span>}
                    >
                        {getFieldDecorator('validationAmount', {
                            rules: [{ required: true, message: '请输入金额!' }]
                        })(
                            <Input />
                            )}
                            <span className="fromItem-postfix">元</span>
                    </FormItem>
                    <FormItem wrapperCol={{ span: 12, offset: 4 }}>
                        <Button className="set-submit" type="primary" htmlType="submit">确认</Button>
                    </FormItem>
                </Form >
            </Spin >
        );
    },
}));

export default Content;