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/userPro",
            success: function (data) {
                if (data.data) {
                    this.setState({
                        bankAccount: data.data.bankAccount,
                        bankCardNumber: data.data.bankCardNumber,
                        auditStatus: data.data.auditStatus
                    });
                };
            }.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/userNextPro",
                    data: {
                        amountMoney: values.amountMoney,
                        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.bankAccount}银行卡{this.state.bankCardNumber}收到的认证打款，
                            务必在收到打款5日内完成，否则将无法完成
                        </span>
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="开户银行"
                    >
                        <span>{this.state.bankAccount}</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('amountMoney', {
                            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;