import React from 'react';
import $ from 'jquery/src/ajax';
import {
	Modal,
	message,
	Spin,
	Upload,
	Input,
	Button,
	Form
} from 'antd';

import {  getIndustryCategory } from '@/industryList.js';
import {  splitUrl, getDemandType,getReleaseStateList,getReleasePositionList } from '@/tools.js';


const DemandDetail = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            auditStatus: 0,
            textFileList: [],
            videoFileList: [],
            pictureUrl: [],
        };
    },
    loadData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +  '/api/user/demand/demandDetail' ,
            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 = {};
                };
                let publishPagesArr = [];
                thisData.publishPages.map(item=>{
					publishPagesArr.push(item.publishPage)
                })
				// let ProvinceCityArr = [];
				// let ProvinceS = thisData.locationProvince; //getprovince
				// let citys = thisData.locationCity;
				// let Areas = thisData.locationArea;
				// ProvinceCityArr.push(ProvinceS, citys, Areas);
                this.setState({
					//ProvinceCity:ProvinceCityArr,
                    id:thisData.id,
                    publishPagesArr:publishPagesArr,
					data: thisData,
					isHot:thisData.isHot,
					isUrgent:thisData.isUrgent,
					status:thisData.status,
					isHot:thisData.isHot,
                    pictureUrl: thisData.pictureUrl ? splitUrl(thisData.pictureUrl, ',', globalConfig.avatarHost + '/upload') : []
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getPictureUrl(e) {
        this.setState({ pictureUrl: e });
    },
	cancelFun(){
		this.props.closeDesc();
    },
    //审核通过/拒绝
    updateFun(index){
        this.setState({
            loading:true
        })
        $.ajax({
            method: "get",
            dataType: "JSON",
            crossDomain: false,
            url: globalConfig.context +  '/api/admin/demand/auditDemand',
            data: {
                id: this.state.id,
                auditResult:index,
                auditInfo:this.state.refuseValue
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('操作成功！');
                this.setState({
                    loading: false,
                });
                this.props.handOk()
            } else {
                message.warning(data.error[0].message);
            };
        }.bind(this));
    },
    //审核拒绝
    refuse(){
        this.setState({
            refuseValue:'',
            refuseVisible:true
        })
    },
    refuseOk(){
        if(!this.state.refuseValue){
            message.warning('请填写拒绝原因。')
            return;
        }
        this.setState({
            refuseVisible:false
        })
        this.updateFun(0)
    },
    refuseCancel(){
        this.setState({
            refuseValue:'',
            refuseVisible:false
        })
    },
    componentWillMount() {
        if (this.props.data.id) {
            this.loadData(this.props.data.id);
        } else {
            this.state.data = {};
        };
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.textFileList = [];
            this.state.videoFileList = [];
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
				this.state.data = {};
				this.state.demandType=undefined; //需求类型
				this.state.urgent=undefined;//加急需求
				this.state.offer=undefined;//是否悬赏
				this.state.hotDemand=undefined;
				this.state.solve=undefined;
                this.state.pictureUrl = [];
            };
            this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.state.data || {};
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
        };
        return (
			<div className='login'>
			<Form layout="horizontal" id="demand-form">
				<Spin spinning={this.state.loading}>
                <div className="clearfix">
						<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="发个需求吧">
                            <span>{theData.problemDes}</span>
                        </FormItem>
						<FormItem className="half-item" {...formItemLayout} label="标题">
                            <span>{theData.name}</span>
						</FormItem>
					</div>
                    <div className>
                        <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="关键字">
                            <span>{theData.keyword}</span>
                        </FormItem>
                    </div>
					<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="行业领域">
                        <span>{getIndustryCategory(theData.industryCategoryA,theData.industryCategoryB)}</span>
                    </FormItem>
					<div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="需求类型">
                                <span>{getDemandType(theData.demandType)}</span>
						</FormItem>
						{theData.demandType=='5'&&
							<FormItem className="half-item" {...formItemLayout} label="人才需求酬劳">
							     <span>{theData.budgetCost} 万元</span>
						    </FormItem>}
						{theData.demandType=='3'&&<div className="clearfix"><FormItem className="half-item" {...formItemLayout} label="资金需求额度">
                            <span>{theData.budgetCost}万元</span>
						</FormItem>
						<FormItem className="half-item" {...formItemLayout} label="资金用途">
							<span>{theData.researchType=='1'&&'扩大再生产'}</span>
                            <span>{theData.researchType=='0'&&'研发'}</span>
						</FormItem>
						</div>
						}
					</div>
					<p style={{marginLeft:40,fontSize:18,marginBottom:15}}>您还可以说明</p>
					<div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="加急需求">
                            <span>{theData.isUrgent?'加急':'非加急'}</span>
						</FormItem>
						{theData.isUrgent?<div><FormItem className="half-item" {...formItemLayout} label="日期">
							<span>{theData.urgentDays}天</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="悬赏金额">
							<span>{theData.urgentMoney}万元</span>
						</FormItem>
                        </div>:''}
					</div>
					<div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="热点需求">
                            <span>{theData.isHot?'热点':'非热点'}</span>
						</FormItem>
						<FormItem className="half-item" {...formItemLayout} label="解决">
                            <span>
                                {theData.status=='0'&&'进行中'}
                                {theData.status=='1'&&'未解决'}
                                {theData.status=='2'&&'已解决'}
                            </span>
						</FormItem>
					</div>
					{/* <div className="clearfix">
						<FormItem
							className="half-item"
							{...formItemLayout}
							label="地区"
						>
							{this.state.ProvinceCity}
						</FormItem>  
					</div> */}
					<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="需求文件(图片)">
                        <div className="clearfix">
                                <Upload className="demandDetailShow-upload"
                                    listType="picture-card"
                                    fileList={this.state.pictureUrl}                               
                                    onPreview={(file) => {
                                        this.setState({
                                            previewImage: file.url || file.thumbUrl,
                                            previewVisible: true,
                                        });
                                    }} >
                                </Upload>
                                <Modal maskClosable={false} footer={null}
                                    visible={this.state.previewVisible}
                                    onCancel={() => { this.setState({ previewVisible: false }) }}>
                                    <img alt="" style={{ width: '100%' }} src={this.state.previewImage || ''} />
                                </Modal>
                            </div> 
					</FormItem>
					<div>
						<FormItem className="half-item" {...formItemLayout} label="发布状态">
							<span>{getReleaseStateList(theData.auditStatus)}</span>
						</FormItem>
					</div>
                    <div>
                        <FormItem className="half-item" labelCol={{ span: 6 }}
							wrapperCol={{ span: 18 }} label="发布位置">
                                 <span>{getReleasePositionList(this.state.publishPagesArr)}</span>
						</FormItem>
                    </div>
					<div className="clearfix">
						{this.props.examineState&&<FormItem wrapperCol={{ span: 12, offset: 3 }}>
							<Button className="set-submit" type="primary"  onClick={(e)=>{this.updateFun(1)}} >
								审核通过
							</Button>
							<Button type="danger" style={{marginRight:20}} onClick={(e)=>{this.refuse()}}>
								审核拒绝
							</Button>
							<Button className="set-submit" type="ghost" onClick={this.cancelFun}>
								取消
							</Button>
						</FormItem>}
					</div>
				</Spin>
			</Form>   
            <Modal
                title="审核拒绝"
                visible={this.state.refuseVisible}
                onOk={this.refuseOk}
                onCancel={this.refuseCancel}
                >   
                    <div>
                        <FormItem labelCol={{ span: 4 }} wrapperCol={{ span: 18 }} label="拒绝原因">
                            <Input type="textarea" rows={4} value={this.state.refuseValue} placeholder="输入拒绝原因" onChange={(e)=>{this.setState({refuseValue:e.target.value})}}/>
                        </FormItem>
                    </div>
                </Modal> 
		</div>
        )
    }
}));
export default DemandDetail;




