import React from 'react';
import { Icon, message, Input, Select, Button, Form, DatePicker } from 'antd';
import './userList.less';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import { eduLevelList, auditStatusList, certifyStepList } from '../../dataDic.js'
import { addressInit } from '../../tools.js';

const OrgCertify = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            eduLvlOption: [],
            auditStatusOption: []
        };
    },
    loadData(uid) {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/orgDetail",
            data: {
                uid: uid
            },
            success: function (data) {
                let thisData = data.data;
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
                };
                addressInit('licenceProvince', 'licenceCity', 'licenceArea',
                    thisData.licenceProvince, thisData.licenceCity, thisData.licenceArea);
                addressInit('locationProvince', 'locationCity', 'locationArea',
                    thisData.locationProvince, thisData.locationCity, thisData.locationArea);
                this.setState({
                    id: thisData.id,
                    contacts: thisData.contacts,
                    contactMobile: thisData.contactMobile,
                    fixedTel: thisData.fixedTel,
                    qq: thisData.qq,
                    postalAddress: thisData.postalAddress,
                    postcode: thisData.postcode,
                    aftUsername: thisData.aftUsername,
                    unitName: thisData.unitName,
                    identityType: thisData.identityType, //组织性质
                    registeredCapital: thisData.registeredCapital, //注册资金

                    legalPerson: thisData.legalPerson,
                    legalPersonIdCard: thisData.legalPersonIdCard,

                    licenceNumber: thisData.licenceNumber,
                    licenceScanningUrl: thisData.licenceScanningUrl,
                    orgCode: thisData.orgCode,
                    orgCodeUrl: thisData.orgCodeUrl,

                    bankAccount: thisData.bankAccount,
                    banks: thisData.banks,
                    bankBranch: thisData.bankBranch,
                    bankCardNumber: thisData.bankCardNumber,

                    validationAmount: thisData.validationAmount, //打款金额
                    paymentDate: thisData.paymentDate,
                    paymentDateFormattedDate: thisData.paymentDateFormattedDate,
                    lastYearTaxReportUrl: thisData.lastYearTaxReportUrl,
                    auditStatus: thisData.auditStatus,
                    process: thisData.process ? String(thisData.process) : undefined
                });
                this.props.form.resetFields();
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.setState({
                    loading: true
                });
                //
                let licenceA = document.getElementById('licenceArea').value;
                let licenceC = document.getElementById('licenceCity').value;
                let licenceP = document.getElementById('licenceProvince').value;
                let locationA = document.getElementById('locationArea').value;
                let locationC = document.getElementById('locationCity').value;
                let locationP = document.getElementById('locationProvince').value;
                //金额判断
                if (values.amountMoney && values.amountMoney < 0 && values.amountMoney > 100 && /^\d+(\.\d{2})?$/.test(values.amountMoney)) {
                    message.warning('请输入0-100以内的金额');
                    return;
                };
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/admin/updateOrgDetail",
                    data: {
                        uid: this.props.uid,
                        id: this.state.id,
                        contacts: values.contacts,
                        contactMobile: values.contactMobile,
                        postalAddress: values.postalAddress,
                        postcode: values.postcode,
                        aftUsername: values.aftUsername,
                        unitName: values.unitName,
                        identityType: values.identityType, //组织性质
                        registeredCapital: values.registeredCapital, //注册资金

                        legalPerson: values.legalPerson,
                        legalPersonIdCard: values.legalPersonIdCard,

                        licenceNumber: values.licenceNumber,
                        orgCode: values.orgCode,

                        bankAccount: values.bankAccount,
                        banks: values.banks,
                        bankBranch: values.bankBranch,
                        bankCardNumber: values.bankCardNumber,

                        validationAmount: values.validationAmount, //打款金额
                        paymentDateFormattedDate: values.paymentDateFormattedDate ? values.paymentDateFormattedDate.format("YYYY-MM-DD") : undefined,
                        auditStatus: values.auditStatus,
                        process: this.state.process,

                        licenceArea: licenceA,
                        licenceCity: licenceC,
                        licenceProvince: licenceP,
                        locationArea: locationA,
                        locationCity: locationC,
                        locationProvince: locationP
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this)).always(function () {
                    this.setState({
                        visible: false,
                    });
                    this.props.handleOk();
                }.bind(this));
            }
        });
    },
    handleCancel() {
        this.props.closeDesc(false);
    },
    componentWillMount() {
        let _me = this;
        eduLevelList.map(function (item, i) {
            _me.state.eduLvlOption.push(
                <Select.Option key={item.key} value={item.value} >{item.key}</Select.Option>
            );
        });
        auditStatusList.map(function (item, i) {
            _me.state.auditStatusOption.push(
                <Select.Option key={item.key} value={item.value} >{item.key}</Select.Option>
            );
        });
    },
    componentDidMount() {
        addressInit('licenceProvince', 'licenceCity', 'licenceArea');
        addressInit('locationProvince', 'locationCity', 'locationArea');
        this.loadData(this.props.uid);
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.loadData(nextProps.uid);
        };
    },
    render() {
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
        };
        return (
            <Form horizontal onSubmit={this.handleSubmit} className="person-form">
                <FormItem
                    {...formItemLayout}
                    label="公司名称"
                >
                    {getFieldDecorator('unitName', {
                        initialValue: this.state.unitName
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="认证名称"
                >
                    {getFieldDecorator('aftUsername', {
                        initialValue: this.state.aftUsername
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="身份类型"
                >
                    {getFieldDecorator('identityType', {
                        initialValue: this.state.identityType
                    })(
                        <Select placeholder="身份类型" style={{ width: 200 }}
                            onChange={(e) => { this.state.identityType = e }}>
                            {this.state.eduLvlOption}
                        </Select>
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="联系人"
                >
                    {getFieldDecorator('contacts', {
                        initialValue: this.state.contacts
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="联系人电话"
                >
                    {getFieldDecorator('contactMobile', {
                        initialValue: this.state.contactMobile
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="法人"
                >
                    {getFieldDecorator('legalPerson', {
                        initialValue: this.state.legalPerson
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="法人身份证号"
                >
                    {getFieldDecorator('legalPersonIdCard', {
                        initialValue: this.state.legalPersonIdCard
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="注册资金"
                >
                    {getFieldDecorator('registeredCapital', {
                        initialValue: this.state.registeredCapital
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="营业执照编号"
                >
                    {getFieldDecorator('licenceNumber', {
                        initialValue: this.state.licenceNumber
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    label="营业执照所在地"
                >
                    {getFieldDecorator('licenceAddress')(
                        <div>
                            <select id="licenceProvince" name="licenceProvince"></select>
                            <select id="licenceCity" name="licenceCity"></select>
                            <select id="licenceArea" name="licenceArea"></select>
                        </div>
                    )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    label="营业执照图片"
                >
                    <div className="idcard-img clearfix">
                        {this.state.licenceScanningUrl ? <div>
                            <div className="idcard-imgbox">
                                <img src={globalConfig.context + '/open/writeImage?path=' + this.state.licenceScanningUrl} alt="点击查看大图"
                                    onClick={(e) => { window.open(e.target.src) }} />
                            </div>
                        </div> : <div><Icon type="exclamation-circle" style={{ color: '#ffbf00', marginRight: '6px' }} />未上传</div>}
                    </div>
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="组织机构代码"
                >
                    {getFieldDecorator('orgCode', {
                        initialValue: this.state.orgCode
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    label="组织机构代码证图片"
                >
                    <div className="idcard-img clearfix">
                        {this.state.orgCodeUrl ? <div>
                            <div className="idcard-imgbox">
                                <img src={globalConfig.context + '/open/writeImage?path=' + this.state.orgCodeUrl} alt="点击查看大图"
                                    onClick={(e) => { window.open(e.target.src) }} />
                            </div>
                        </div> : <div><Icon type="exclamation-circle" style={{ color: '#ffbf00', marginRight: '6px' }} />未上传</div>}
                    </div>
                </FormItem>
                <FormItem
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    label="上年度纳税报表"
                >
                    <div className="idcard-img clearfix">
                        {this.state.lastYearTaxReportUrl ? <div>
                            <div className="idcard-imgbox">
                                <img src={globalConfig.context + '/open/writeImage?path=' + this.state.lastYearTaxReportUrl} alt="点击查看大图"
                                    onClick={(e) => { window.open(e.target.src) }} />
                            </div>
                        </div> : <div><Icon type="exclamation-circle" style={{ color: '#ffbf00', marginRight: '6px' }} />未上传</div>}
                    </div>
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="开户银行"
                >
                    {getFieldDecorator('banks', {
                        initialValue: this.state.banks
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="开户银行支行"
                >
                    {getFieldDecorator('bankBranch', {
                        initialValue: this.state.bankBranch
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="银行卡号"
                >
                    {getFieldDecorator('bankCardNumber', {
                        initialValue: this.state.bankCardNumber
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    label="开户行所在地"
                >
                    {getFieldDecorator('locationAddress')(
                        <div>
                            <select id="locationProvince" name="locationProvince"></select>
                            <select id="locationCity" name="locationCity"></select>
                            <select id="locationArea" name="locationArea"></select>
                        </div>
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="打款金额"
                >
                    {getFieldDecorator('validationAmount', {
                        initialValue: this.state.validationAmount
                    })(
                        <Input />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="打款日期"
                >
                    {getFieldDecorator('paymentDateFormattedDate', {
                        initialValue: this.state.paymentDate ? moment(this.state.paymentDate) : null
                    })(
                        <DatePicker />
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="认证状态"
                >
                    {getFieldDecorator('auditStatus', {
                        initialValue: this.state.auditStatus
                    })(
                        <Select placeholder="选择认证状态" style={{ width: 200 }}
                            onChange={(e) => { this.state.auditStatus = e }}>
                            {this.state.auditStatusOption}
                        </Select>
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="认证步骤"
                >
                    {getFieldDecorator('process', {
                        initialValue: this.state.process
                    })(
                        <Select placeholder="选择要修改的认证步骤" style={{ width: 200 }}
                            onChange={(e) => { this.state.process = e }}>
                            {
                                certifyStepList.map(function (item, i) {
                                    return <Select.Option key={i} value={item.value} >{item.key}</Select.Option>
                                })
                            }
                        </Select>
                        )}
                </FormItem>
                <FormItem wrapperCol={{ span: 12, offset: 4 }}>
                    <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
                    <Button className="set-submit" type="ghost" onClick={this.handleCancel} >取消</Button>
                </FormItem>
            </Form >
        );
    }
}));

export default OrgCertify;