import React from 'react';
import { Icon, Modal, message, Spin, Timeline, Button } from 'antd';
import { getPatentType, getPatentState, getTime, getPatentField } from '../../tools.js';
import './patent.less';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';

const PatentDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            stepList: []
        };
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false, true);
    },
    getStepList(thepid) {
        this.state.stepList = [];
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/patentProcess",
            data: {
                pid: thepid
            },
            success: function (data) {
                if (data.error && data.error.length && !data.data) {
                    return;
                };
                let _me = this;
                for (var item in data.data) {
                    _me.state.stepList.push(
                        <Timeline.Item key={item}> <span>{getPatentState(data.data[item].state)}</span> {getTime(data.data[item].recordTime)}</Timeline.Item>
                    )
                };
                this.setState({
                    stepList: this.state.stepList
                });
            }.bind(this),
        });
    },
    getPatentInfo(thepid) {
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/clientPatentInfo",
            data: {
                patentId: thepid
            },
            success: function (data) {
                let thisData = data.data;
                if (data.error && data.error.length && !data.data) {
                    return;
                };
                this.setState({
                    serialNumber: thisData.serialNumber,
                    patentNumber: thisData.patentNumber,
                    patentName: thisData.patentName,
                    patentCatagory: thisData.patentCatagory,
                    patentState: thisData.patentState,
                    patentField: thisData.patentField,
                    patentDes: thisData.patentDes,
                    patentProryStatementUrl: thisData.patentProryStatementUrl,
                    patentWritingUrl: thisData.patentWritingUrl,
                    authorizationNoticeUrl: thisData.authorizationNoticeUrl,
                    patentCertificateUrl: thisData.patentCertificateUrl,
                    patentApplicationDate: thisData.patentApplicationDate,
                    firstInventorName: thisData.firstInventorName,
                    firstInventorNationality: thisData.firstInventorNationality,
                    firstInventorIdNumber: thisData.firstInventorIdNumber,
                    firstInventorIsPublish: thisData.firstInventorIsPublish,
                    secondInventorName: thisData.secondInventorName,
                    secondInventorNationality: thisData.secondInventorNationality,
                    secondInventorIsPublish: thisData.secondInventorIsPublish,
                    thirdInventorName: thisData.thirdInventorName,
                    thirdInventorNationality: thisData.thirdInventorNationality,
                    thirdInventorIsPublish: thisData.thirdInventorIsPublish,
                    createTime: thisData.createTime,
                    author: thisData.author,
                    office: thisData.office,
                    principal: thisData.principal,
                });
            }.bind(this),
        });
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showDesc
        if (nextProps.data && nextProps.data.pid !== null) {
            this.getStepList(nextProps.data.pid);
            this.getPatentInfo(nextProps.data.pid);
        }
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    downloadFile(url, type) {
        window.open(globalConfig.context + "/techservice/patent/downloadFile?path=" + url + "&sign=" + type)
    },
    confirmButton() {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/clientConfirm",
            data: {
                pid: this.props.data.pid
            }
        }).done(function (data) {
            if (data.error && data.error.length) {
                message.warning(data.error[0].message);
            }
        }.bind(this)).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    render() {
        let theData = this.state;
        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=''
                            className="patent-desc-content">
                            <div>
                                <p className="widthHalf"><span>编号：</span>{theData.serialNumber}</p>
                                <p className="widthHalf"><span>申请号/专利号：</span>{theData.patentNumber}</p>
                            </div>
                            <div>
                                <p className="widthHalf"><span>专利名称：</span>{theData.patentName}</p>
                                <p className="widthHalf"><span>专利类型：</span>{getPatentType(theData.patentCatagory)}</p>
                            </div>
                            <div>
                                <p className="widthHalf"><span>专利状态：</span>{getPatentState(theData.patentState)}</p>
                                <p className="widthHalf"><span>专利领域：</span>{getPatentField(theData.patentField)}</p>
                            </div>
                            <div>
                                <span>简介：</span>{theData.patentDes}
                            </div>
                            <div>
                                <span>专利相关材料：</span>
                                {theData.patentWritingUrl !== null && theData.patentWritingUrl !== undefined && theData.patentWritingUrl !== '' ?
                                    <p className="download-file">
                                        <a href="" onClick={this.downloadFile.bind(this, theData.patentWritingUrl, "patent_writing")}>专利稿件</a>
                                        {theData.confirmState == '0' ?
                                            <Button onClick={this.confirmButton}>确认稿件</Button> : <span></span>
                                        }
                                    </p> :
                                    <span>专利稿件未上传</span>
                                }
                                {theData.authorizationNoticeUrl !== null && theData.authorizationNoticeUrl !== undefined && theData.authorizationNoticeUrl !== '' ?
                                    <p className="download-file">
                                        <a href="" onClick={this.downloadFile.bind(this, theData.authorizationNoticeUrl, "authorization_notice")}>授权通知书</a>
                                    </p> :
                                    <span>授权通知书未上传</span>
                                }
                                {theData.patentCertificateUrl !== null && theData.patentCertificateUrl !== undefined && theData.patentCertificateUrl !== '' ?
                                    <p className="download-file">
                                        <a href="" onClick={this.downloadFile.bind(this, theData.patentCertificateUrl, "patent_certificate")}>专利证书</a>
                                    </p> :
                                    <span>专利证书未上传</span>
                                }
                            </div>
                            <div>
                                <span>专利申请日：</span>{theData.startTime}
                            </div>
                            <div>
                                <span>专利授权日：</span>{theData.authorizedTime}
                            </div>
                            <div>
                                <span>每年年费缴费截止日：</span>{theData.endTime}
                            </div>
                            <div>
                                <p>第一发明人：</p>
                                <p><span>姓名：</span>{theData.firstInventorName}</p>
                                <p><span>身份证：</span>{theData.firstInventorIdNumber}</p>
                            </div>
                            <div style={theData.secondInventorName === '' ? { display: 'none' } : {}}>
                                <p>第二发明人：</p>
                                <p><span>姓名：</span>{theData.secondInventorName}</p>
                            </div>
                            <div style={theData.thirdInventorName === '' ? { display: 'none' } : {}}>
                                <p>第三发明人：</p>
                                <p><span>姓名：</span>{theData.thirdInventorName}</p>
                            </div>
                            <Timeline className="time-line">
                                {this.state.stepList}
                            </Timeline>
                        </Modal>
                    </Spin>
                </div>
            );
        } else {
            return <div></div>
        }
    },
});
export default PatentDesc;