import React from 'react';
import { Input, Button, Form, Select, Upload, Icon, Spin, message } from 'antd';
import './edu.less';

import EduPfsSelect from './eduPfsSelect.jsx';
import YearSelect from '../../yearSelect.jsx';

const FormItem = Form.Item;
const Option = Select.Option;

const edulvlArr = ["高中", "大专", "本科", "硕士研究生", "博士研究生"];

function getBase64(img, callback) {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result));
    reader.readAsDataURL(img);
};

function beforeUpload(file) {
    const isJPG = file.type === 'image/jpeg';
    if (!isJPG) {
        message.error('You can only upload JPG file!');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('Image must smaller than 2MB!');
    }
    return isJPG && isLt2M;
};

class Avatar extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            imageUrl: ''
        }
    }

    handleChange(info) {
        if (info.file.status === 'done') {
            // Get this url from response in real world.
            getBase64(info.file.originFileObj, imageUrl => this.setState({ imageUrl }));
        }
    }

    render() {
        const imageUrl = this.state.imageUrl;
        return (
            <Upload
                className="avatar-uploader"
                name={this.props.name}
                showUploadList={false}
                action="/upload.do"
                beforeUpload={beforeUpload}
                onChange={this.handleChange}
                >
                {
                    imageUrl ?
                        <img src={imageUrl} role="presentation" id={this.props.name} /> :
                        <Icon type="plus" className="avatar-uploader-trigger" />
                }
            </Upload>
        );
    }
};

const EduFrom = Form.create()(React.createClass({
    getInitialState() {
        return {
            loading: false,
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                console.log('Received values of form: ', values);
            }
        });
    },
    handleyear(e){
        console.log(e);
    },
    normFile(e) {
        if (Array.isArray(e)) {
            return e;
        }
        return e && e.fileList;
    },
    introChange(e) {
        console.log(e);
    },
    professional(e1,e2){
        console.log(e1,e2);
    },
    render() {
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 12 },
        };
        const _me = this;
        return (
            <Form horizontal onSubmit={this.handleSubmit} className="edu-form">
                <FormItem
                    labelCol={{ span: 4 }}
                    wrapperCol={{ span: 6 }}
                    label="学历"
                    >
                    {getFieldDecorator('edulvl')(
                        <Select className="acc-edu-lvl">
                            {
                                edulvlArr.map(function (item, i) {
                                    return <Option key={i} value={item} >{item}</Option>
                                })
                            }
                        </Select>
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="专业"
                    >
                    {getFieldDecorator('edupfs')(
                        <EduPfsSelect Professional={this.professional}/>
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="毕业学校"
                    >
                    {getFieldDecorator('schoolname')(
                        <Input />
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    labelCol={{ span: 4 }}
                    wrapperCol={{ span: 4 }}
                    label="毕业时间"
                    >
                    {getFieldDecorator('schooltime')(
                        <YearSelect handleyear={this.handleyear}/>
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="学历证书"
                    hasFeedback
                    >
                    {getFieldDecorator('eduimg')(
                        <Avatar name="eduimg"/>
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="学历证书编号"
                    >
                    {getFieldDecorator('edunum')(
                        <Input />
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="学位证书"
                    hasFeedback
                    >
                    {getFieldDecorator('degreeimg')(
                        <Avatar name="degreeimg"/>
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="学历证书编号"
                    >
                    {getFieldDecorator('degreenum')(
                        <Input />
                    )}
                </FormItem>
                <div className="set-explain">
                    <p>要求2M 以下的jpg、jpeg格式的图片。该资料不会公开展示，仅作为审核材料使用。</p>
                </div>
                <FormItem wrapperCol={{ span: 12, offset: 3 }}>
                    <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
                </FormItem>
            </Form >
        );
    },
}));

const Person = React.createClass({
    getInitialState() {
        return {
            loading: false
        };
    },
    render() {
        return (
            <Spin spinning={this.state.loading} className='spin-box'>
                <div className="set-edu">
                    <div className="acc-detail">
                        <p className="acc-title">最高学历</p>
                        <EduFrom />
                    </div>
                </div>
            </Spin>
        )
    }
});

export default Person;