import React from 'react';
import { Icon,  Modal, message,  Spin, Input, Select, Button, Form } from 'antd';
import './myClient.less';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import {socialAttribute} from '../../../dataDic.js';
const AchievementDetailForm = Form.create()(React.createClass({
    getInitialState() {
        return {
            loading: false,
            data: {},           
        };
    },

    handleSubmit(e) {
    	console.log(this.state.societyTag)
    	if(this.state.societyTagt==undefined){
    		message.warning('请填写社会属性！')
    		return false;
    	}
        e.preventDefault();
        this.props.form.validateFields((err, values) => {                                 
            if (!err) {
                this.setState({
                    loading: true
                });
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + '/api/admin/customer/addCustomer',
                    data: {
                        name: this.state.companyNamet, 
                        contacts:this.state.content,
                        contactMobile: this.state.telnum,
                        societyTag:this.state.societyTagt,
                        type:'1'
                    }
                }).done(function (data) { 
                    this.setState({
                        loading: false
                    });
                    if (!data.error.length) {
                        message.success('保存成功！');
                        this.props.handleOk();
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this));
            }
        });
    },

	//去完善
    addContent(){
    	let telnum=this.state.telnum;
    	let companyName=this.state.companyNamet;
    	let content=this.state.content;
    	if(!telnum||!companyName||!content){
    		message.warning('此3项都要填');
    	}else{
    		this.props.newmodal(true);
    		this.props.closeDesc(false);
    	}	
    },
    componentWillMount() {
        this.state.companyNamet='';
    	this.state.content='';
    	this.state.telnum='';
    	this.state.societyTagt=undefined;
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.companyNamet='';
	    	this.state.content='';
	    	this.state.telnum='';
	    	this.state.societyTagt=undefined;
            this.props.form.resetFields(); 
        };
    },
    
    render() {
        const theData = this.state.data || {};
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 8 },
            wrapperCol: { span: 14 },
        };  

        return (
        <div>
            <Form horizontal onSubmit={this.handleSubmit} id="demand-form">
                <Spin spinning={this.state.loading}>
                    <div className="clearfix">
                    	<FormItem className="half-item"
	                            {...formItemLayout}
	                            label="公司名称" >
	                         <Input value={this.state.companyNamet} placeholder="公司名称" 
	                              onChange={(e) => { this.setState({ companyNamet: e.target.value }); }} required="required"/>
	                    </FormItem>
                    </div>
                    <div className="clearfix">
                    	<FormItem className="half-item"
	                            {...formItemLayout}
	                            label="联系人" >
	                              <Input value={this.state.content}  placeholder="联系人"  
	                                 onChange={(e) => { this.setState({ content: e.target.value }); }} required="required"/>
	                    </FormItem>
                    </div>
                    <div className="clearfix">
                    	<FormItem className="half-item"
	                            {...formItemLayout}
	                            label="联系电话" >
	                              <Input value={this.state.telnum} placeholder="联系电话"  
	                                    onChange={(e) => { this.setState({ telnum: e.target.value }); }} required="required"/>
	                    </FormItem>
                    </div>
                    <div className="clearfix">
                    	<FormItem className="half-item" 
                          {...formItemLayout}
                           label="社会属性"
                         > 
	                    
							    <Select placeholder="客户社会属性"  value={this.state.societyTagt} onChange={(e) => { this.setState({ societyTagt: e}); }}> 
                                {
                                    socialAttribute.map(function (item) {
                                        return <Select.Option key={item.value} >{item.key}</Select.Option>
                                    })
                                }
                              </Select>
                        
                   		 </FormItem>
                    </div>
                    <FormItem wrapperCol={{ span: 12, offset: 4 }}>
                        <Button className="set-submit" type="primary" htmlType="submit">保存</Button>  
                        {/*<Button className="set-submit" type="primary"  onClick={(e)=>{this.addContent();this.props.closeDesc}}>去完善</Button>*/}
                        <Button className="set-submit" type="ghost" onClick={this.props.closeDesc}>取消</Button>
                    </FormItem> 
                </Spin>
            </Form >
    	</div>
        )
    }
}));

const CustomerDetail = React.createClass({
    getInitialState() {
        return {
            visible: false,
            tabsKey: 1,
            loading: false,           
            dataSource: [],
        };
    },    
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    handleOk(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false, true);
    },
    
    componentWillReceiveProps(nextProps) {
        if (!this.state.visible && nextProps.showDesc) {
            this.state.tabsKey = "1";
        };
        this.state.visible = nextProps.showDesc; 
    },
    render() {
        
            return (
                <div className="patent-desc">
                    <Modal maskClosable={false} visible={this.state.visible}
                        onOk={this.checkPatentProcess} onCancel={this.handleCancel}
                        width='800px'
                        title='新建客户'                       
                        footer=''
                        className="admin-desc-content">
                        <Spin spinning={this.state.loading}>               
                            <AchievementDetailForm
                                data={this.props.data} 
                                newmodal={this.props.newmodal}
                                closeDesc={this.handleCancel}
                                visible={this.state.visible}
                                handleOk={this.handleOk} />
                        </Spin>
                    </Modal>
                </div>
            );        
    },
});


export default CustomerDetail;