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 + "/techservice/patent/patentFile"}
                data={{ 'sign': this.props.name, 'oid': this.props.oid }}
                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: [],
            patentProcessList: []
        };
    },
    loadData() {
        this.setState({
            loading: true
        });
        $.when($.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/patentProcess",
            data: {
                pid: this.state.xid
            }
        }), $.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.patentProcessList.push(
                    <p className="patent-process-list" key={item.id}>
                        <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));
    },
    getOption() {
        let _me = this;
        _me.state.patentTypeOption = [];
        _me.state.patentFieldOption = [];
        _me.state.patentStateOption = [];
        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>
            )
        });
    },
    downloadFile(url, type) {
        window.open(globalConfig.context + "/techservice/patent/downloadFile?path=" + url + "&sign=" + type)
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            visible: true,
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/managePatentInfo",
            data: {
                "xid": this.state.xid,
                "patentNumber": this.state.patentNumber,
                "patentName": this.state.patentName,
                "patentCatagory": this.state.patentType,
                "patentField": this.state.patentField,
                "patentDes": this.state.patentDes,
                "patentWritingUrl": this.state.patentWritingUrl,
                "authorizationNoticeUrl": this.state.authorizationNoticeUrl,
                "patentCertificateUrl": this.state.patentCertificateUrl,
                "state": this.state.patentStateAdd,
                "recordTimeFormattedDate": this.state.recordTime,
                "principal": this.state.adminName
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功！');
            } else {
                message.warning(data.error[0].message);
            }
        }.bind(this)).always(function () {
            this.setState({
                visible: false,
            });
            this.props.closeDesc(false);
        }.bind(this));
    },
    checkPatentProcess() {
        if (this.state.patentStateAdd || this.state.adminName || this.state.recordTime) {
            if (this.state.patentStateAdd && this.state.adminName && this.state.recordTime)
            { this.handleOk() } else {
                message.warning("请填写完整的状态流转信息！");
                return;
            }
        } else {
            this.handleOk()
        };
    },
    componentWillReceiveProps(nextProps) {
        let _me = this;
        this.state.patentStateAdd = '';
        this.state.adminName = '';
        this.state.recordTime = '';
        this.state.adminOption = [];
        this.state.patentProcessList = [];
        this.state.visible = nextProps.showDesc;
        if (nextProps.data) {
            this.state.xid = nextProps.data.pid;
            this.state.patentDes = nextProps.data.patentDes;
            this.state.patentName = nextProps.data.patentName;
            this.state.patentNumber = nextProps.data.patentNumber;
            this.state.patentType = String(nextProps.data.patentType);
            this.state.patentState = String(nextProps.data.patentState);
            this.state.patentField = String(nextProps.data.patentField);
            this.loadData();
            this.getOption();
        }
    },
    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.checkPatentProcess} onCancel={this.handleCancel}
                            width='800px'
                            footer={[
                                <Button key="submit" type="primary" size="large" onClick={this.checkPatentProcess}>保存</Button>,
                                <Button key="back" type="ghost" size="large" onClick={this.handleCancel}>取消</Button>,
                            ]}
                            className="patent-desc-content">
                            <div>
                                <p className="widthHalf"><span>公司名称：</span>{theData.unitName}</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.patentNumber} onChange={(e) => { this.setState({ patentNumber: 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>{getPatentState(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">
                                <div>材料管理：
                                    {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>
                                    }
                                    {theData.patentProryStatementUrl !== null && theData.patentProryStatementUrl !== undefined && theData.patentProryStatementUrl !== '' ?
                                        <p className="download-file">
                                            <a href="" onClick={this.downloadFile.bind(this, theData.patentProryStatementUrl, "patent_prory_statement")}>专利代理委托书</a>
                                        </p> :
                                        <span></span>
                                    }
                                    {theData.lastYearTaxReportUrl !== null && theData.lastYearTaxReportUrl !== undefined && theData.lastYearTaxReportUrl !== '' ?
                                        <p className="download-file">
                                            <a href="" onClick={this.downloadFile.bind(this, theData.lastYearTaxReportUrl, "last_year_taxReport")}>专利代理委托书</a>
                                        </p> :
                                        <span></span>
                                    }
                                </div>
                                <Avatar urlData={this.getPatentWritingUrl} name='patent_writing' oid={theData.oid}/>
                                <Avatar urlData={this.getAuthorizationNoticeUrl} name='authorization_notice' oid={theData.oid}/>
                                <Avatar urlData={this.getPatentCertificateUrl} name='patent_certificate' oid={theData.oid}/>
                            </div>
                            <div className="avatar-text"><span>专利稿件上传</span><span>授权通知书上传</span><span>专利证书上传</span></div>
                            <div>
                                <p>专利状态流转记录：</p>
                                {this.state.patentProcessList}
                                <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 defaultValue={null} onChange={(data,dataString)=>{this.state.recordTime = dataString}}/>
                                </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;