import React from 'react';
import { Input, Button, Form, Icon, Spin, message, Cascader } from 'antd';
import { areaSelect } from '../NewDicProvinceList';
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({
                        firstContacts: data.data.firstContacts,
                        firstMobile: data.data.firstMobile,
                        bankAccount: data.data.bankAccount,
                        address: [data.data.locationProvince, data.data.locationCity, data.data.locationArea],
                        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: {
                        bankAccount: values.bankAccount,
                        banks: values.banks,
                        bankBranch: values.bankBranch,
                        bankCardNumber: values.bankCardNumber,
                        locationProvince: values.address[0],
                        locationCity: values.address[1],
                        locationArea: values.address[2],
                        process: 2
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                        this.props.changeStep(3);
                    } else {
                        message.warning(data.error[0].message);
                        this.setState({
                            loading: false
                        });
                    }
                }.bind(this));
            }
        });
    },
    checkBankCardNum(rule, value, callback) {
        if (!/(^\d{15,}$)/.test(value)) {
            callback('请输入正确的银行卡号！');
        } else {
            callback();
        }
    },
    componentWillMount() {
        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" /> 必须使用以该名称为开户名的银行卡进行认证</span>}
                    >
                        {getFieldDecorator('bankAccount', {
                            initialValue: this.state.bankAccount || null,
                            rules: [{ required: true, message: '请输入开户银行开户名!' }]
                        })(
                            <Input placeholder="请输入开户银行开户名" />
                            )}
                    </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', {
                            initialValue: this.state.address || null,
                            rules: [{ type: 'array', required: true, message: '请选择地址!' }]
                        })(
                            <Cascader options={areaSelect()} placeholder="请选择地址" />
                            )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="银行开户支行名称"
                    >
                        {getFieldDecorator('bankBranch', {
                            initialValue: this.state.bankBranch || null,
                            rules: [{ required: true, message: '请输入银行开户支行名称!' }]
                        })(
                            <Input placeholder="请输入银行开户支行名称" />
                            )}
                    </FormItem>
                    <FormItem
                        {...formItemLayout}
                        label="银行卡号" >
                        {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.firstContacts || '') + ' ' + (this.state.firstMobile || '')}</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;