import React from 'react';
import { Icon, Modal, message, Spin, Steps, Input, Select, Upload, DatePicker, Button, Row, Col } 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';

const PatentDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            patentTypeOption: [],
            patentFieldOption: [],
            patentStateOption: [],
            adminOption: [],
            patentProcessList: [],
            companyOption: []
        };
    },
    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"
        }), $.ajax({
           	method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/techservice/patent/getUnitNames",
        })).done((data1, data2, data3) => {
            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>
                )
            };
            if (data3[0].error.length || !data3[0].data) {
             	return;
          	};
        	for (var item in data3[0].data) {
             	_me.state.companyOption.push(
                 	<Select.Option value={item} key={item}>{data3[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">
                        
                        	
                        	<Row className="express-desc-row">
			                	<Col span={4}>公司名称</Col>
			                 	<Col span={4}>xxxxxx</Col>
			                 	<Col span={4}>公司组织机构代码</Col>
			                 	<Col span={4}>xxxxxxxx</Col>
							</Row>
							<Row className="express-desc-row">
			                	<Col span={4}>公司地址</Col>
			                 	<Col span={4}>xxxxxx</Col>
			                 	<Col span={4}>联系人</Col>
			                 	<Col span={4}>xxxxxxxx</Col>
							</Row>
			                <Row className="express-desc-row">
			                	<Col span={4}>编号</Col>
			                 	<Col span={4}>xxxxxx</Col>
			                 	<Col span={4}>外包公司</Col>
			                 	<Col span={4}>
			                 		<Select placeholder="外包公司名"
						                filterOption={(input, option) => { 
						                    this.setState({companyId : option.key});
						                    option.props.value.indexOf(input) >= 0;
						                }}
						                onChange={(e)=>{ this.state.companyName = e;}}
						                showSearch={true} style={{ width: 200 }} >{this.state.companyOption}</Select>
			                 	</Col>
							</Row>
							<Row className="express-desc-row">
			                	<Col span={3}>派单详情</Col>
			                 	<Col span={16}>
			                 		<Input type="textarea"
			                            placeholder="多行输入"
			                            rows={6} />
			                 	</Col>
							</Row>
			              	<Row className="express-desc-row">
			                	<Col span={3}>备注</Col>
			                 	<Col span={16}>
			                 		<Input type="textarea"
			                            placeholder="多行输入"
			                            rows={6} />
			                 	</Col>
							</Row>
							<Row className="express-desc-row">
			                	<Col span={3}>软著名称</Col>
			                 	<Col span={6}>
			                 		<Input type="input" value="名称" />
			                 	</Col>
							</Row>
							<Row className="express-desc-row">
			                	<Col span={3}>下证时间(预计)</Col>
			                 	<Col span={6}>
			                 		<DatePicker />
			                 	</Col>
							</Row>
							<p>状态流转记录</p>
							<Row className="express-desc-row">
			                	<Col span={3}>专利状态</Col>
			                 	<Col span={3}>派单</Col>
			                 	<Col span={3}>处理时间</Col>
			                 	<Col span={3}>2017/01/01</Col>
			                 	<Col span={3}>负责人</Col>
			                 	<Col span={3}>随便</Col>
							</Row>
							<Row className="express-desc-row">
			                	<Col span={3}>专利状态</Col>
			                 	<Col span={3}>受理中</Col>
			                 	<Col span={3}>处理时间</Col>
			                 	<Col span={3}>2017/01/01</Col>
			                 	<Col span={3}>负责人</Col>
			                 	<Col span={3}>随便</Col>
			                 	<Col span={3}>操作人</Col>
			                 	<Col span={3}>随便</Col>
							</Row>
							<Row className="express-desc-row">
			                	<Col span={3}>状态流转</Col>
			                 	<Col span={4}>
			                 		<Select placeholder="请选择" value={this.state.searchKey} style={{ width: 120 }} onChange={(e) => { this.setState({ searchKey: e, searchValue: '' }); }}>
				                        <Select.Option value="1">1</Select.Option>
				                        <Select.Option value="2">2</Select.Option>
				                    </Select>
			                 	</Col>
			                 	<Col span={3}>处理时间</Col>
			                 	<Col span={4}>
			                 		<DatePicker />
			                 	</Col>
			                 	<Col span={3}>负责人</Col>
			                 	<Col span={4}>
			                 		<Select placeholder="请选择" value={this.state.searchKey} style={{ width: 120 }} onChange={(e) => { this.setState({ searchKey: e, searchValue: '' }); }}>
				                        <Select.Option value="1">1</Select.Option>
				                        <Select.Option value="2">2</Select.Option>
				                    </Select>
			                 	</Col>
							</Row>
                        
                        
                        </Modal>
                    </Spin>
                </div>
            );
        } else {
            return <div></div>
        }
    },
});
export default PatentDesc;
