import React from 'react';
import { Icon,  Modal, message,  AutoComplete,Spin, Input, Select, Button, Form,Upload,Popconfirm } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './userMangagement.less';
import {socialAttribute,cityArr,station,post} from '../../../dataDic.js';
const Option = AutoComplete.Option;
//图片组件
const PicturesWall = React.createClass({
    getInitialState() {
        return {
            previewVisible: false,
            previewImage: '',
            fileList: [],
        }
    },
    handleCancel() {
        this.setState({ previewVisible: false })
    },
    handlePreview(file) {
        this.setState({
            previewImage: file.url || file.thumbUrl,
            previewVisible: true,
        });
    },
    handleChange(info) {
        let fileList = info.fileList;
        this.setState({ fileList });
        this.props.fileList(fileList);
    },
    componentWillReceiveProps(nextProps) {
        this.state.fileList = nextProps.pictureUrl; 
    },
    render() {
        const { previewVisible, previewImage, fileList } = this.state;
        const uploadButton = (
            
        );
        return (
            
                
                    {fileList.length >= 1 ? null : uploadButton}
                
                
                     
                
             
        );
    }
});
const Newuser = Form.create()(React.createClass({
    getInitialState() {
        return {
        	orgCodeUrl:[],
            loading: false,
            loaduser: {}, 
            visible:false
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        if(!this.state.role){
        	message.warning('亲!请选择角色');
        	return false;
        };
        if(!this.state.roleState){
        	message.warning('亲!请选择角色状态');
        	return false;
        }
        let theorgCodeUrl = [];
        if (this.state.orgCodeUrl.length) {
            let picArr = [];
            this.state.orgCodeUrl.map(function (item) {
                picArr.push(item.response.data);
            });
            theorgCodeUrl = picArr.join(",");
        };
        this.setState({
            loading: true
        });
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/customer/addCustomer',
            data: {
            	id:this.props.datauser.id,
                uesrName:this.state.uesrName,
				role:this.state.role,
				roleState:this.state.roleState,
				customerName:this.state.customerName,
				department:this.state.department,
				post:this.state.post,
				station:this.state.station,
				phone:this.state.phone,
				email:this.state.email,
				director:this.state.theTypes,
				companyIntention:JSON.stringify(this.state.companyIntention),
            }
        }).done(function (data) { 
            this.setState({
                loading: false
            });
            if (!data.error.length) {
                message.success('保存成功!');
                this.handleOk();
            } else {
                message.warning(data.error[0].message);
            }
        }.bind(this));
    },
    //主管初始加载(自动补全)
	supervisor(e){ 
		$.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/customer/findCustomerByName",
            data:{
            	name:e
            },
            success: function (data) {                	  
				       let thedata=data.data;
				    if (!thedata) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };	
                    thedata = {}; 
              }; 
					this.setState({
						customerArr:thedata,	
                    });
				}.bind(this),
			}).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));  
	},
	//上级主管输入框失去焦点是判断客户是否存在
	selectAuto(value,options){
		this.setState({
			auto:value
		})
	},
	blurChange(e){
		console.log(e)
		let theType='';
		let contactLists=this.state.customerArr||[];
			if (e) {
            contactLists.map(function (item) {
                if (item.name == e.toString()) {
                    theType = item.id;
                }
           });     
   	    }
		this.setState({
			theTypes:theType
		})
	},
	//值改变时请求客户名称
	httpChange(e){
		if(e.length>=2){
			this.supervisor(e); 
		}	
		this.setState({
			auto:e
		})
	},
	//查看基本详情基本信息
    loaduser(record){
    	if(record){
    	$.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + '/api/admin/customer/toUpdateBusiness',
            data: {
              businessId: record.name
            },
            success: function (data) {
                let thisData = data.data;                
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
                }; 
                this.setState({
                	orgCodeUrl: thisData.orgCodeUrl ? splitUrl(thisData.orgCodeUrl, ',', globalConfig.avatarHost + '/upload') : [],
                	uids:thisData.uid,
                	followSituation:thisData.followSituation,
                	businessGlossoryId:thisData.businessGlossoryId,
                	customerStatus:thisData.customerStatus,
                	remarks:thisData.remarks,	
                });                  
            }.bind(this),
       }).always(function () {
            this.setState({            	
                loading: false
            });
        }.bind(this));  
       }
    },
    //重置密码
    resetPwd(e) {
        this.setState({
            loading: true
        })
        $.ajax({
            type: 'post',
            url: globalConfig.context + "/api/admin/supervise/resetPwd",
            dataType: "json",
            data: {
                id: this.state.id
            }
        }).done((res) => {
            if (res.error && res.error.length) {
                message.error(res.error[0].message);
            } else {
                message.success("密码重置成功");
            }
        }).always(() => {
            this.setState({
                loading: false
            })
        });
    },
    handleOk(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false, true);
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    getOrgCodeUrl(e) {
        this.setState({ orgCodeUrl: e });
    },
    componentWillMount(e){
    	const cityArrs=[];
        cityArr.map(function (item) {
            cityArrs.push(
                {item.key}
            )                    
        });
        this.state.cityOption=cityArrs;
    },
    componentWillReceiveProps(nextProps) {
    	this.state.visible = nextProps.showDesc;
    	this.state.Detaile=nextProps.userDetaile;
		if(!nextProps.userDetaile){
			this.state.uesrName='';
			this.state.role=undefined;
			this.state.roleState=undefined;
			this.state.customerName='';
			this.state.department=undefined;
			this.state.post=undefined;
			this.state.station=undefined;
			this.state.phone='';
			this.state.email='';
			this.state.auto='';
			this.state.companyIntention=[];
			this.state.orgCodeUrl=[];
   		}else{
    		this.loaduser(nextProps.datauser)
    	   }     
      },
    render() {
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 8 },
            wrapperCol: { span: 14 },
        };  
        const dataSources=this.state.customerArr || [];
        const options = dataSources.map((group,index) =>
				      {group.name}
				     )
        return (
        
        )
    }
}));
export default Newuser;