import React from 'react';
import { Icon, Modal, message, Spin, Steps, Input, Select, Upload, DatePicker, Button } from 'antd';
import './comprehensive.less';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import { patentTypeList, patentFieldList, patentStateList } from '../../../dataDic.js';
import { getBase64, beforeUpload } from '../../../tools.js';

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 }));
            this.props.urlData(info.file.response.data);
        }
    }
    render() {
        const imageUrl = this.state.imageUrl;
        return (
            <Upload
                className="avatar-uploader"
                name="avatar"
                showUploadList={false}
                action={globalConfig.context + "/api/user/patent/upload"}
                data={{ 'sign': this.props.name }}
                beforeUpload={beforeUpload}
                onChange={this.handleChange.bind(this)}
                >
                {
                    imageUrl ?
                        <img src={imageUrl} role="presentation" className="avatar" /> :
                        <Icon type="plus" className="avatar-uploader-trigger" />
                }
            </Upload>
        );
    }
};

const PatentDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            patentTypeOption: [],
            patentFieldOption: [],
            patentStateOption: []
        };
    },
    componentWillMount() {
        let _me = this;
        patentTypeList.map(function (item) {
            _me.state.patentTypeOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        patentFieldList.map(function (item) {
            _me.state.patentFieldOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        patentStateList.map(function (item) {
            _me.state.patentStateOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
    },
    downloadPic(type) {
        window.open(globalConfig.context + "/open/downLoadPicture?path=" + type)
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showDesc
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    getPatentWritingUrl(e){
        this.state.patentWritingUrl = e;
    },
    getAuthorizationNoticeUrl(e){
        this.state.authorizationNoticeUrl = e;
    },
    getPatentCertificateUrl(e){
        this.state.patentCertificateUrl = e;
    },
    render() {
        const Step = Steps.Step;
        const theData = this.props.data;
        if (this.props.data) {
            return (
                <div className="patent-desc">
                    <Spin spinning={this.state.loading} className='spin-box'>
                        <Modal title="专利详情" visible={this.state.visible}
                            onOk={this.handleOk} onCancel={this.handleCancel}
                            width='800px'
                            footer={[
                                <Button key="submit" type="primary" size="large" onClick={this.handleOk}>保存</Button>,
                                <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>,
                            ]}
                            className="patent-desc-content">
                            <div>
                                <p className="widthHalf"><span>公司名称：</span>{theData.companyName}</p>
                                <p className="widthHalf"><span>公司组织机构代码：</span>{theData.companyNum}</p>
                            </div>
                            <div>
                                <p className="widthHalf"><span>公司地址：</span>{theData.companyAddress}</p>
                                <p className="widthHalf"><span className="three2eight">联系人</span>：{theData.companyContacts}</p>
                            </div>
                            <div>
                                <p className="widthHalf"><span className="two2four">编号</span>：{theData.number}</p>
                                <p className="widthHalf"><span className="seven2eight">申请号/专利号</span>：<Input value={this.state.patentNum} onChange={(e) => { this.setState({ patentNum: e.target.value }); } } /></p>
                            </div>
                            <div>
                                <p className="widthHalf"><span>专利名称：</span><Input value={this.state.patentName} onChange={(e) => { this.setState({ patentName: e.target.value }); } } /></p>
                                <div className="widthHalf"><span className="four2eight">专利类型</span>：
                                    <Select value={this.state.patentType} style={{ width: 200 }} onChange={(e) => { this.setState({ patentType: e }); } }>{this.state.patentTypeOption}</Select>
                                </div>
                            </div>
                            <div>
                                <p className="widthHalf"><span>专利状态：</span>{theData.patentState}</p>
                                <div className="widthHalf"><span className="four2eight">专利领域</span>：
                                    <Select value={this.state.patentField} style={{ width: 200 }} onChange={(e) => { this.setState({ patentField: e }); } }>{this.state.patentFieldOption}</Select>
                                </div>
                            </div>
                            <div>
                                <span>专利简介：</span>
                                <Input type="textarea"
                                    placeholder="多行输入"
                                    value={this.state.patentDes}
                                    onChange={(e) => { this.setState({ patentDes: e.target.value }); } }
                                    rows={6} />
                            </div>
                            <div>
                                <p>第一发明人：</p>
                                <p className="widthThird"><span>姓名：</span>{theData.firstInventorName}</p>
                                <p className="widthThird"><span>身份证：</span>{theData.firstInventorIdNumber}</p>
                                <p className="widthThird"><span>国籍：</span>中国</p>
                            </div>
                            <div style={theData.secondInventorName === '' ? { display: 'none' } : {}}>
                                <p>第二发明人：</p>
                                <p className="widthThird"><span>姓名：</span>{theData.secondInventorName}</p>
                                <p className="widthThird"><span>国籍：</span>中国</p>
                            </div>
                            <div style={theData.thirdInventorName === '' ? { display: 'none' } : {}}>
                                <p>第三发明人：</p>
                                <p className="widthThird"><span>姓名：</span>{theData.thirdInventorName}</p>
                                <p className="widthThird"><span>国籍：</span>中国</p>
                            </div>
                            <div className="clearfix">
                                <p>材料管理：
                                    <span>{theData.patentProryStatementUrl !== null ? <a onClick={this.downloadPic.bind(this, theData.patentProryStatementUrl)}>专利代理委托书</a> : ""}</span>
                                    <span>{theData.lastYearTaxReportUrl !== null ? <a onClick={this.downloadPic.bind(this, theData.lastYearTaxReportUrl)}>上年度纳税表</a> : ""}</span>
                                </p>
                                <Avatar urlData={this.getPatentWritingUrl} name='patentWritingUrl' />
                                <Avatar urlData={this.getAuthorizationNoticeUrl} name='authorizationNoticeUrl' />
                                <Avatar urlData={this.getPatentCertificateUrl} name='patentCertificateUrl' />
                            </div>
                            <div className="avatar-text"><span>专利稿件上传</span><span>授权通知书上传</span><span>专利证书上传</span></div>
                            <div>
                                <p>专利状态流转记录：</p>
                                <div className="widthThird">
                                    <span>状态流转：</span>
                                    <Select value={this.state.patentStateAdd} style={{ width: 150 }} onChange={(e) => { this.setState({ patentStateAdd: e }); } }>{this.state.patentStateOption}</Select>
                                </div>
                                <p className="widthThird">
                                    <span>处理时间：</span>
                                    <DatePicker />
                                </p>
                                <div className="widthThird">
                                    <span>负责人：</span>
                                    <Select value={this.state.patentStateAdd} style={{ width: 150 }} onChange={(e) => { this.setState({ patentStateAdd: e }); } }>{this.state.patentStateOption}</Select>
                                </div>
                            </div>
                        </Modal>
                    </Spin>
                </div>
            );
        } else {
            return <div></div>
        }
    },
});
export default PatentDesc;