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

import { getAdviserType,getIndustry,getSocialAttribute } from '@/tools.js';
import { getProvince } from '@/NewDicProvinceList';
const AuthDetail = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            pictureUrl: [],
            headPortraitUrl: [],
			adviserUrl:[],
			positiveIdUrl: [],
			oppositeIdUrl: [],
        };
    },
    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 provice;
                provice=getProvince(thisData.province,thisData.city,thisData.area);
                this.setState({
                    id:thisData.id,
                    data: thisData,
                    ProvinceCity:provice,
					headPortraitUrl: thisData.headPortraitUrl //专家照片
						? splitUrl(thisData.headPortraitUrl, ',', globalConfig.avatarHost + '/upload')
						: [],
					adviserUrl: thisData.adviserUrl		//顾问照片
						? splitUrl(thisData.adviserUrl, ',', globalConfig.avatarHost + '/upload')
						: [],
					positiveIdUrl: thisData.positiveIdUrl
						? splitUrl(thisData.positiveIdUrl, ',', globalConfig.avatarHost + '/upload')
						: [],
					oppositeIdUrl: thisData.oppositeIdUrl
						? splitUrl(thisData.oppositeIdUrl, ',', globalConfig.avatarHost + '/upload')
						: [],
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
	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) {
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
				this.state.data = {};
            };
            this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.state.data || {};
        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 className="half-item" {...formItemLayout} label="姓名">
                            <span>{theData.identifyName}</span>
                        </FormItem>
						<FormItem className="half-item" {...formItemLayout} label="行业">
                            <span>{getIndustry(theData.industry)}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="社会属性">
                            <span>{getSocialAttribute(theData.societyTag)}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="性别">
                            <span>{theData.sex}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="出生日期">
                            <span>{theData.dateOfBirthYear?theData.dateOfBirthYear + '/' + theData.dateOfBirthMonth:''}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="身份证号码">
                            <span>{theData.idNumber}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="省-市-区">
                            <span>{this.state.ProvinceCity}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="通信地址">
                            <span>{theData.postalAddress}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="手机号码">
                            <span>{theData.mobile}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="固定电话">
                            <span>{theData.fixedTel}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="QQ">
                            <span>{theData.qq}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="电子邮箱">
                            <span>{theData.email}</span>
                        </FormItem>
                    </div>
                    <div className="clearfix">
                        <FormItem 
                            labelCol={{ span: 3 }}
                            wrapperCol={{ span: 18 }} 
                            label="个人简介">
                            <span>{theData.introduction}</span>
                        </FormItem>     
                    </div>
                    <p style={{fontSize:16}}>{theData.authType=='0'?'专家认证':'顾问认证'}</p>
                    {theData.authType=='0'?<div>
                        <FormItem className="half-item" {...formItemLayout} label="毕业院校">
                            <span>{theData.graduateSchool}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="专业类别">
                            <span>{theData.majorCategory}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="职业资格">
                            <span>{theData.qualification}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="就职单位">
                            <span>{theData.workUnit}</span>
                        </FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="职称">
                            <span>{theData.professionalTitle}</span>
                        </FormItem>
                        <div className="clearfix">
                            <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 13 }} label="专家照片">
                                    <Upload className="demandDetailShow-upload"
                                        listType="picture-card"
                                        fileList={this.state.headPortraitUrl}                               
                                        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>
                            </FormItem>
                        </div>
                    </div>:<div>
                        <FormItem className="half-item" {...formItemLayout} label="顾问类型">
                            <span>{getAdviserType(theData.adviserType)}</span>
                        </FormItem>
                        <div className="clearfix">
                            <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 13 }} label="顾问照片">
                                    <Upload className="demandDetailShow-upload"
                                        listType="picture-card"
                                        fileList={this.state.adviserUrl}                               
                                        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>
                            </FormItem>
                        </div>
                    </div>}
                    <div>
                        <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 10 }} label="身份证正面">
                            <div className="clearfix">
                                <Upload className="demandDetailShow-upload"
                                    listType="picture-card"
                                    fileList={this.state.positiveIdUrl}                               
                                    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>
                        <FormItem labelCol={{ span:3 }} wrapperCol={{ span: 10 }} label="身份证反面">
                            <div className="clearfix">
                                <Upload className="demandDetailShow-upload"
                                    listType="picture-card"
                                    fileList={this.state.oppositeIdUrl}                               
                                    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>
					<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 AuthDetail;




