import React from 'react';
import { Spin, Button, Tabs, message, Modal } from 'antd';
import $ from 'jquery/src/ajax';
import { Link } from 'react-router';
import '../content.less';
import './content.less';
import Collection from './collection';

import weixin from '../../../../image/weixin-w.png';
import qq from '../../../../image/qq-w.png';
import weibo from '../../../../image/weibo-w.png';

import defaultAvatar from '../../../../image/avatarImg.png'

const Content = React.createClass({
    getInitialState () {
        return {
            loading: false,
            visible: false
        };
    },
    getData () {
        this.setState({
            loading: false
        });
        $.ajax({
            method: "get",
            dataType: "json",
            url: globalConfig.context + "/api/user/homePage"
        }).done(function (data) {
            if (data.error && data.error.length) {
                message.warning(data.error[0].message);
            } else {
                this.setState({
                    achievementNum: data.data.achievementNum,
                    copyrightNum: data.data.copyrightNum,
                    demandNum: data.data.demandNum,
                    gradeName: data.data.gradeName,
                    email: data.data.email,
                    intellectualPropertyNum: data.data.intellectualPropertyNum,
                    logoUrl: data.data.logoUrl,
                    level: data.data.lvl,
                    nickname: data.data.nickname,
                    number: data.data.number,
                    patentNum: data.data.patentNum,
                    personPortraitUrl: data.data.personPortraitUrl,
                    techProjectNum: data.data.techProjectNum,
                    type: data.data.type,
                    uid: data.data.uid,
                    visible: data.data.type == '0'||data.data.type=='1' ? false : true
                })
            }
        }.bind(this)).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    componentWillMount () {
        this.getData();
    },
    render () {
        return (
            <Spin spinning={this.state.loading} className='spin-box'>
                <div className="index-content">
                    <div className="index-userdata clearfix">
                        <p className="acc-title">
                            欢迎,
                            <span>{this.state.nickname || ('No.' + this.state.number)}</span>
                            {(() => {
                                if ( this.state.level ) {
                                    return <img src={globalConfig.avatarUploadHost + '/rank/level_' + this.state.level + '.png'} alt="" />
                                } else {
                                    return ''
                                }
                            })()}
                            {(() => {
                                if (this.state.level > 1 && this.state.level < 2) {
                                    return <span style={{ fontSize: '14px', color: '#333' }}>
                                        <a href={globalConfig.context + '/portal/membership.html'}>升级高级会员</a> ,获取更多会员权益
                                        </span>;
                                }
                            })()}
                        </p>
                        <div className="userdata-img">
                            {this.state.personPortraitUrl || this.state.logoUrl ?
                                <img src={globalConfig.avatarHost + "/upload" + (this.state.type == 0 ? this.state.personPortraitUrl : this.state.logoUrl)} alt="" />
                                : <img src={defaultAvatar} alt="" />}
                        </div>
                        <div className="userdata-content">
                            {(() => {
                                if (this.state.type == '0') {
                                    return <p className="userdata-lvl">已个人认证</p>;
                                } else if(this.state.type == '1'){
                                    return <p className="userdata-lvl"><span>已企业认证</span></p>
                                }
                            })()}
                            <div>
                                {
                                    (()=>{
                                        if (this.state.type=='0'||this.state.type=='1') {
                                            if(this.state.type=='0'){
                                                return <p><Link to="/personal">填写完整资料</Link>，有助于提高您的资信</p>
                                            }
                                            if(this.state.type=='1'){
                                                return <p><Link to="/unit">填写完整资料</Link>，有助于提高您的资信</p>
                                            }
                                            
                                        }else {
                                            return <div>
                                                <p>未认证,请填写相应资料进行认证</p>
                                                <p>
                                                    <Link to='/personal' style={{marginRight:20}}>个人认证</Link>
                                                    <Link to='/unit'>企业认证</Link>
                                                </p>
                                            </div>
                                        }
                                    })()
                                }
                            </div>
                            <p className="userdata-relate" style={{ 'display': 'none' }}>
                                <Button className="relateqq"><img src={qq} alt="" /> QQ账号登录</Button>
                                <Button className="relateweibo"><img src={weibo} alt="" />微博账号登录</Button>
                                <Button className="relateweixin"><img src={weixin} alt="" />微信账号登录</Button>
                            </p>
                        </div>
                    </div>
                    <div className="index-record">
                        <p className="acc-title">我的轨迹</p>
                    </div>
                    <div className="index-activity">
                        <Collection />
                    </div>
                </div>
                <Modal maskClosable={false}
                    title="实名认证提示"
                    visible={this.state.visible}
                    onCancel={() => {
                        this.setState({
                            visible: false,
                        });
                    }}
                    width='400px'
                    footer={[]} >
                    <p style={{ textAlign: 'center' }}>
                        <span>您还未进行认证，请完成认证，为您提供更多服务</span>
                    </p>
                    <div style={{ marginTop: '20px', textAlign: 'center' }}>
                        <Button type="primary" style={{marginRight:15}}><Link to="/personal">个人认证</Link></Button>
                        <Button type="primary"><Link to="/unit">企业认证</Link></Button>
                    </div>
                </Modal>
            </Spin>
        )
    }
});

export default Content;