import React from 'react';
import { Modal, Spin } from 'antd';
import DemandForm from '@/account/demand/demandForm';

class DemandDesc extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			loading: false,
			pictureUrl: []
		};
	}
	loadData(id) {
		this.setState({
			loading: true
		});
		$.ajax({
			method: 'get',
			dataType: 'json',
			crossDomain: false,
			url:
				globalConfig.context +
				(window.userData.type == '1'
					? '/api/user/demand/orgDemandDetail'
					: '/api/user/demand/userDemandDetail'),
			data: {
				id: id
			},
			success: function(data) {
				let thisData = data.data;
				if (!thisData) {
					if (data.error && data.error.length) {
						message.warning(data.error[0].message);
					}
					thisData = {};
				}
				this.setState({
					data: thisData,
					tags: thisData.keyword ? thisData.keyword.split(',') : [],
					pictureUrl: thisData.pictureUrl
						? splitUrl(thisData.pictureUrl, ',', globalConfig.avatarHost + '/upload')
						: []
				});
			}.bind(this)
		}).always(
			function() {
				this.setState({
					loading: false
				});
			}.bind(this)
		);
	}
	handleCancel() {
		this.setState({
			visible: false
		});
	}
	handOk() {
		this.setState({
			visible: false
		});
		this.props.closeDesc(false, true);
	}
	componentWillReceiveProps(nextProps) {
		if (nextProps.showDesc && nextProps.data && nextProps.data.id) {
			this.loadData(nextProps.data.id);
		}
		this.state.visible = nextProps.showDesc;
	}
	render() {
		let data = this.props.data || [];
		return (
			<div className="patent-desc">
				<Modal
					maskClosable={false}
					visible={this.state.visible}
					onOk={this.handOk.bind(this)}
					onCancel={this.handleCancel.bind(this)}
					width="1000px"
					title={!data.id ? '新建需求' : '修改需求'}
					footer=""
					className="admin-desc-content"
				>
					<div>
						{!data.id ? (
							<div>
                                <DemandForm 
                                    closeDesc={this.handleCancel}
                                    visible={this.state.visible}
                                    data={[]}
                                />
							</div>
						) : (
							<div>
                                <DemandForm 
                                    closeDesc={this.handleCancel}
                                    visible={this.state.visible}
                                    data={this.props.data}
                                />
							</div>
						)}
					</div>
				</Modal>
			</div>
		);
	}
}

export default DemandDesc;
