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, getPatentState, getTime } 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: [],
            adminOption: [],
            patentProcess: []
        };
    },
    loadData() {
        this.state.data = [];
        this.setState({
            loading: true
        });
        $.when($.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/patentProcess",
            data: {
                pid: this.state.pid
            }
        }), $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/getAdmin"
        })).done((data1, data2) => {
            if (data1[0].error.length) {
                message.warning(data1[0].error[0].message);
                return;
            };
            let _me = this;
            data1[0].data.map(function (item) {
                _me.state.patentProcess.push(
                    <p>
                        <span>专利状态: {getPatentState(item.state)}</span>
                        <span>处理时间: {getTime(item.recordTime)}</span>
                        <span>负责人:{item.principal}</span>
                        <span>操作人:{item.operator}</span>
                    </p>
                )
            });
            if (data2[0].error.length) {
                message.warning(data2[0].error[0].message);
                return;
            };
            for (var item in data2[0].data) {
                _me.state.adminOption.push(
                    <Select.Option value={item} key={item}>{data2[0].data[item]}</Select.Option>
                )
            };
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    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;
        if (nextProps.data) {
            this.state.pid = nextProps.data.pid;
            this.loadData()
        }
    },
    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.adminName} style={{ width: 150 }} onChange={(e) => { this.setState({ adminName: e }); } }>{this.state.adminOption}</Select>
                                </div>
                            </div>
                        </Modal>
                    </Spin>
                </div>
            );
        } else {
            return <div></div>
        }
    },
});
export default PatentDesc;