import React from 'react';
import { Input, Button, Form, Icon, Spin, message, } from 'antd';
import { addressInit } from '../tools.js';
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) {
                    addressInit('cmbProvince', 'cmbCity', 'cmbArea',
                        data.data.locationProvince, data.data.locationCity, data.data.locationArea);
                    this.setState({
                        contactMobile: data.data.contactMobile,
                        username: data.data.username,
                        banks: data.data.banks,
                        banksBranch:data.data.banksBranch,
                        bankCardNumber: data.data.bankCardNumber,
                    });
                } else {
                    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) => {
            let cmbP = document.getElementById('cmbProvince').value;
            let cmbC = document.getElementById('cmbCity').value;
            let cmbA = document.getElementById('cmbArea').value;
            if ( !cmbP || !cmbC || !cmbA) {
                message.warning("请选择所在地！");
                return;
            };
            debugger
            if (!err) {
                this.setState({
                    loading: true
                });
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/user/orgNextPro",
                    data: {
                        bankAccount: data.data.username,
                        banks: values.banks,
                        banksBranch:values.banksBranch,
                        bankCardNumber: values.bankCardNumber,
                        locationProvince:cmbP,
                        locationCity:cmbC,
                        locationArea:cmbA,
                        process: 2
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                        this.props.changeStep(3);
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this)).always(function () {
                    this.setState({
                        loading: false
                    });
                }.bind(this));
            }
        });
    },
    checkBankCardNum(rule, value, callback) {
        if (!/(^\d{15,}$)/.test(value)) {
            callback('请输入正确的银行卡号！');
        } else {
            callback();
        }
    },
    componentDidMount() {
        addressInit('cmbProvince', 'cmbCity', 'cmbArea');
        this.getData();
    },
    changeStep() {
        this.props.changeStep(1);
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 6 },
            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: 20, offset: 6 }}>
                        <p className="certify-title">请填写您的银行卡信息，该银行卡仅用于认证您的身份。</p>
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="银行开户名"
                        extra={<span><Icon type="exclamation-circle" /> 必须使用以“{this.state.username}”为开户名的银行卡进行认证</span>}
                    >
                        {getFieldDecorator('username')(
                            <span>{this.state.username}</span>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="开户银行"
                        extra={<span><Icon type="exclamation-circle" /> 不支持信用卡和存折进行认证</span>}
                    >
                        {getFieldDecorator('banks', {
                            initialValue: this.state.banks || null,
                            rules: [{ required: true, message: '请输入开户银行名称!' }]
                        })(
                            <Input placeholder="请输入开户银行名称" />
                            )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="开户银行所在地"
                    >
                        {getFieldDecorator('address')(
                            <div>
                                <select id="cmbProvince" name="cmbProvince"></select>
                                <select id="cmbCity" name="cmbCity"></select>
                                <select id="cmbArea" name="cmbArea"></select>
                            </div>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="银行开户支行名称"
                    >
                        {getFieldDecorator('banksBranch', {
                            initialValue: this.state.banksBranch || null,
                            rules: [{ required: true, message: '请输入银行开户支行名称!' }]
                        })(
                            <Input placeholder="请输入银行开户支行名称"/>
                        )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="银行卡号"
                        extra={
                            <span style={{ 'color': '#ea0862' }}>
                                <Icon type="exclamation-circle" />
                                认证系统会给该银行卡打入一笔1元以下的确认金额，您需要通过银行柜台并在
                                步骤4输入该银行卡收到的阿凡提打款金额数目
                            </span>}
                    >
                        {getFieldDecorator('bankCardNumber', {
                            initialValue: this.state.bankCardNumber || null,
                            rules: [{
                                validator: this.checkIdcardnum,
                            }],
                        })(
                            <Input placeholder="请输入银行卡号" />
                            )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="联系方式"
                        extra={
                            <span style={{ 'color': '#ea0862' }}>
                                <Icon type="exclamation-circle" />
                                 我们会将重要进度以短信形式通知您
                            </span>}
                    >
                        {getFieldDecorator('contactMobile')(
                            <span>{this.state.contactMobile}</span>
                        )}
                    </FormItem>
                    <FormItem wrapperCol={{ span: 12, offset: 6 }}>
                        <Button className="set-submit" type="primary" htmlType="submit">下一步</Button>
                        <Button className="exit" type="ghost" onClick={_me.changeStep}>返回修改</Button>
                    </FormItem>
                </Form >
            </Spin >
        );
    },
}));

export default Content;