import React from 'react';
import { Radio, Icon, Button, AutoComplete, Cascader, layout, Input, Select, Tabs, Spin, Popconfirm, Popover, Table, Switch, message, DatePicker, Modal, Upload, Form, Row, Col, TimePicker } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import moment from 'moment';
import { citySelect, provinceList, areaSelect } from '@/NewDicProvinceList';
import { socialAttribute, industry, newFollow, auditStatusL, lvl, currentMember, cityArr, statuslist, customerStatus, intentionalService, sex } from '@/dataDic.js';
import { getCompanyIntention, splitUrl, getIndustry, getStatuslist, getAuditStatus, getContactType, getSocialAttribute, getfllowSituation, beforeUploadFile, getWhether, getcityArr, getcustomerStatue, getfllowSituationOn, getCertification, getcustomerTyp, getLvl, getCurrentMember, getprovince } from '@/tools.js';
const FormItem = Form.Item;
//图片组件
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 = (
			<div>
                <Icon type="plus" />
                <div className="ant-upload-text">点击上传</div>
            </div>
		);
		return(
			<div style={{display:"inline-block"}}>
                <Upload
                    action={globalConfig.context + "/api/admin/customer/uploadCustomerImg"}
                    data={{ 'sign': '' }}
                    listType="picture-card"
                    fileList={fileList}
                    onPreview={this.handlePreview}
                    onChange={this.handleChange} >
                    {fileList.length >= 1 ? null : uploadButton}
                </Upload>
                <Modal maskClosable={false} visible={previewVisible} footer={null} onCancel={this.handleCancel}>
                    <img alt="example" style={{ width: '100%' }} src={previewImage} />
                </Modal>
            </div>
		);
	}
});

const Basic = React.createClass({
	getInitialState() {
		return {
			loading: false,
			orgCodeUrl: [],
			companyLogoUrl: [],
		}
	},
	loadInformation(record) {
		$.ajax({
			method: "get",
			dataType: "json",
			url: globalConfig.context + '/api/admin/customer/findOrganizationCustomerDetail',
			data: {
				uid: record
			},
			success: function(data) {
				let thisData = data.data;
				if(!thisData) {
					if(data.error && data.error.length) {
						message.warning(data.error[0].message);
					};
					thisData = {};
				};
				let ProvinceCityArr = [];
				let ProvinceS = thisData.locationProvince; //getprovince
				let citys = thisData.locationCity;
				let Areas = thisData.locationArea;
				ProvinceCityArr.push(ProvinceS, citys, Areas);
				this.setState({
					InformationId: thisData.id,
					InformationUid: thisData.uid,
					identifyName: thisData.identifyName,
					listed: thisData.listed,
					highTechZone: thisData.highTechZone,
					orgCodeUrl: thisData.orgCodeUrl ? splitUrl(thisData.orgCodeUrl, ',', globalConfig.avatarHost + '/upload') : [],
					companyLogoUrl: thisData.companyLogoUrl ? splitUrl(thisData.companyLogoUrl, ',', globalConfig.avatarHost + '/upload') : [],
					dataInformation: thisData,
					ProvinceCity: ProvinceCityArr[0]!=null?ProvinceCityArr:undefined,
					contacts: thisData.contacts,
					contactMobile: thisData.contactMobile,
					industry: String(thisData.industry) == 'null' ? undefined : String(thisData.industry),
					international: thisData.international,
					societyTag: thisData.societyTag||undefined,
					introduction: thisData.introduction,
					postalAddress: thisData.postalAddress,
					contactsFixedTel: thisData.contactsFixedTel,
					contactsFax: thisData.contactsFax,
					registeredCapital: thisData.registeredCapital,
					enterpriseScale: thisData.enterpriseScale,
					legalPerson: thisData.legalPerson,
					legalPersonIdCard: thisData.legalPersonIdCard,
					legalPersonTel: thisData.legalPersonTel,
					legalPersonEmail: thisData.legalPersonEmail,
					businessScope: thisData.businessScope,
					orgCode: thisData.orgCode,
					investment: thisData.investment,
					auditStatus: thisData.auditStatus ? String(thisData.auditStatus) : undefined,
					businessAudit: thisData.businessAudit
				});
			}.bind(this),
		}).always(function() {
			this.setState({
				loading: false
			});
		}.bind(this));
	},
	//图片
	getOrgCodeUrl(e) {
		this.setState({
			orgCodeUrl: e
		});
	},
	getCompanyLogoUrl(e) {
		this.setState({
			companyLogoUrl: e
		});
	},
	//基本信息提交
	newSubmit(e) {
		e.preventDefault();
		if(!this.state.industry) {
			message.warning('请选择行业');
			return false;
		};
		if(!this.state.societyTag) {
			message.warning('请选择社会性质');
			return false;
		};
		if(!this.state.ProvinceCity[1]) {
			message.warning('请选择地区');
			return false;
		};
		if(isNaN(this.state.registeredCapital)) {
			message.warning('注册资本只能输入数字')
			return false;
		};
		if(isNaN(this.state.enterpriseScale)) {
			message.warning('单位规模只能输入数字')
			return false;
		}
		this.setState({
			selectedRowKeys: [],
		});

		let theorgCodeUrl = [];
		if(this.state.orgCodeUrl.length) {
			let picArr = [];
			this.state.orgCodeUrl.map(function(item) {
				if(item.response && item.response.data && item.response.data.length) {
					picArr.push(item.response.data);
				}
			});
			theorgCodeUrl = picArr.join(",");
		};
		let thecompanyLogoUrl = [];
		if(this.state.companyLogoUrl.length) {
			let picArr = [];
			this.state.companyLogoUrl.map(function(item) {
				if(item.response && item.response.data && item.response.data.length) {
					picArr.push(item.response.data);
				}
			});
			thecompanyLogoUrl = picArr.join(",");
		};

		this.setState({
			loading: true
		});
		$.ajax({
			method: "post",
			dataType: "json",
			url: globalConfig.context + '/api/admin/customer/updateOrganizationCustomer',
			data: {
				id: this.state.InformationId,
				uid: this.state.InformationUid,
				societyTag: this.state.societyTag,
				introduction: this.state.introduction,
				unitName: this.state.unitName,
				industry: this.state.industry,
				locationProvince: (this.state.ProvinceCity)[0], //省-
				locationCity: (this.state.ProvinceCity)[1], //市
				locationArea: (this.state.ProvinceCity)[2], //区
				postalAddress: this.state.postalAddress,
				contactsFixedTel: this.state.contactsFixedTel,
				contactsFax: this.state.contactsFax,
				registeredCapital: this.state.registeredCapital ? this.state.registeredCapital : 0,
				enterpriseScale: this.state.enterpriseScale ? this.state.enterpriseScale : 0,
				legalPerson: this.state.legalPerson,
				legalPersonIdCard: this.state.legalPersonIdCard,
				legalPersonTel: this.state.legalPersonTel,
				legalPersonEmail: this.state.legalPersonEmail,
				highTechZone: this.state.highTechZone,
				listed: this.state.listed,
				contacts: this.state.contacts,
				contactMobile: this.state.contactMobile,
				international: this.state.international,
				orgCode: this.state.orgCode,
				businessScope: this.state.businessScope,
				introduction: this.state.introduction,
				companyLogoUrl: thecompanyLogoUrl != 0 ? thecompanyLogoUrl : '',
				orgCodeUrl: theorgCodeUrl != 0 ? theorgCodeUrl : '',
				businessAudit: this.state.businessAudit,
				auditStatus: this.state.auditStatus
			}
		}).done(function(data) {
			this.setState({
				loading: false
			});
			if(!data.error.length) {
				message.success('保存成功！');
				this.props.closeDetail(false, true)
			} else {
				message.warning(data.error[0].message);
			}
		}.bind(this));
	},
	//取消
	detailsModal() {
		this.props.closeDetail(false, false)
	},
	componentWillMount(){
		this.loadInformation(this.props.data.id)
	},
	componentWillReceiveProps(nextProps) {
		if(nextProps.data&&nextProps.basicState){
			this.loadInformation(nextProps.data.id)
		}
	},
	render() {
		return(
			<div>
				 <Form layout="horizontal" onSubmit={this.newSubmit} id="demand-form">
	                <Spin spinning={this.state.loading}>
		               	<div className="clearfix" style={{paddingLeft:'60px'}}>
	                    	<FormItem className="half-item"
			                    labelCol={{ span: 4 }}
			                    wrapperCol={{ span: 18 }}
			                    label="客户名称"
		                        >
		                        <span>{this.state.identifyName}</span>
		                    </FormItem>
	                    </div>
	                    <div className="clearfix" style={{paddingLeft:'60px'}}>
	                    	<FormItem className="half-item" 
	                         labelCol={{ span: 4 }}
	                         wrapperCol={{ span: 18 }}
	                         label="行业">
							    <Select placeholder="选择行业" value={this.state.industry}  style={{width:'300px'}}
							    				onChange={(e)=>{this.setState({industry:e})}}>
                                      {
                                        industry.map(function (item) {
                                            return <Select.Option key={item.value} >{item.key}</Select.Option>
                                        })
                                      }
                                </Select>
                                <span style={{color:'red',marginLeft:'15px'}}>*</span>
	                   		 </FormItem>
	                   		 <FormItem className="half-item" 
	                           labelCol={{ span: 4 }}
	                            wrapperCol={{ span: 18 }}
	                           label="社会属性"
	                         > 
								    <Select placeholder="客户社会属性(必填项)" value={this.state.societyTag}  style={{width:'300px'}}
							    				onChange={(e)=>{this.setState({societyTag:e})}}>
                                    {
                                        socialAttribute.map(function (item) {
                                            return <Select.Option key={item.value} >{item.key}</Select.Option>
                                        })
                                    }
                                </Select>
                                <span style={{color:'red',marginLeft:'15px'}}>*</span>
	                   		 </FormItem>
	                         <FormItem className="half-item" 
	                           labelCol={{ span: 4 }}
	                           wrapperCol={{ span: 18 }}
	                           label="省-市-区"
	                          > 
	                            <Cascader options={areaSelect()}  value={this.state.ProvinceCity} placeholder="选择城市" style={{width:'300px'}}
									onChange={(e,pre) => { this.setState({ ProvinceCity: e }) }} />
									<span style={{color:'red',marginLeft:'15px'}}>*</span>
		                   </FormItem>
		                   <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                         label="通信地址"
		                            >
		                                <Input placeholder="客户通信地址" value={this.state.postalAddress} 
							    				onChange={(e)=>{this.setState({postalAddress:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                         label="固定电话"
		                            >
		                                <Input placeholder="客户固定电话" value={this.state.contactsFixedTel} 
							    				onChange={(e)=>{this.setState({contactsFixedTel:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="客户传真"
		                            >
		                                <Input placeholder="客户传真" value={this.state.contactsFax} 
							    				onChange={(e)=>{this.setState({contactsFax:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="注册资本"
		                            >
		                                <Input placeholder="注册资本" value={this.state.registeredCapital}  maxLength='6'
							    				onChange={(e)=>{this.setState({registeredCapital:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="单位规模"
		                            >
		                                <Input placeholder="单位规模" value={this.state.enterpriseScale} maxLength='1'
							    				onChange={(e)=>{this.setState({enterpriseScale:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="法人名称"
		                            >
		                                <Input placeholder="法人名称" value={this.state.legalPerson} 
							    				onChange={(e)=>{this.setState({legalPerson:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="身份证号码"
		                            >
		                                <Input placeholder="法人身份证号码" value={this.state.legalPersonIdCard} 
							    				onChange={(e)=>{this.setState({legalPersonIdCard:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item" 
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="主要联系人"
	                           	 >
		                            <Input placeholder="主要联系人" value={this.state.contacts} onChange={(e) => {
				                        this.setState({ contacts:  e.target.value })}}/>
		                     </FormItem>
		                     <FormItem className="half-item" 
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="主要联系人号码"
	                           	 >
		                            <Input placeholder="主要联系人号码" value={this.state.contactMobile} onChange={(e) => {
				                        this.setState({ contactMobile:  e.target.value })}}/>
		                     </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="法人电话"
		                            > 
		                                <Input placeholder="法人电话" value={this.state.legalPersonTel} 
							    				onChange={(e)=>{this.setState({legalPersonTel:e.target.value})}}/>
		                    </FormItem>
		                    <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="电子邮箱"
		                            >
		                                <Input placeholder="法人电子邮箱" value={this.state.legalPersonEmail} 
							    				onChange={(e)=>{this.setState({legalPersonEmail:e.target.value})}}/>
		                    </FormItem>
				            <FormItem className="half-item"
			                    	labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="机构代码"
		                            >
		                                <Input placeholder="社会同意机构代码" value={this.state.orgCode} 
							    				onChange={(e)=>{this.setState({orgCode:e.target.value})}}/>
		                    </FormItem>
				           <div className="clearfix">
	                   		<FormItem className="half-item"
                   		   		labelCol={{ span: 4 }}
	                            wrapperCol={{ span: 18 }}
	                            label="高新"
	                   		    >
		                   		 <Radio.Group value={this.state.highTechZone} onChange={(e) => {
				                        this.setState({highTechZone:e.target.value})
				                    }}>
				                        <Radio value={0}>否</Radio>
				                        <Radio value={1}>是</Radio>
				                  </Radio.Group>
				            </FormItem>
	                   	     <FormItem className="half-item"
                   		   		labelCol={{ span: 4 }}
	                            wrapperCol={{ span: 18 }}
	                            label="上市"
	                   		    >
		                   		 <Radio.Group value={this.state.listed} onChange={(e) => {
				                        this.setState({listed:e.target.value })
				                    }}>
				                        <Radio value={0}>否</Radio>
				                        <Radio value={1}>是</Radio>
				                  </Radio.Group>
						     </FormItem>
						     <FormItem className="half-item"
                   		   		labelCol={{ span: 4 }}
	                            wrapperCol={{ span: 18 }}
	                            label="国际化"
	                   		    >
		                   		 <Radio.Group value={this.state.international} onChange={(e) => {
				                        this.setState({international:e.target.value })
				                    }}>
				                        <Radio value={0}>否</Radio>
				                        <Radio value={1}>是</Radio>
				                  </Radio.Group>
						     </FormItem>
						     </div>
		                    <div className="clearfix pictures">
		                        <FormItem
			                        labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="机构证(PIC)" >
			                        <PicturesWall
			                            pictureSign="PIC"
			                            fileList={this.getOrgCodeUrl}
			                            pictureUrl={this.state.orgCodeUrl} />
			                            <p>图片建议：图片要清晰。</p>
		                        </FormItem>
		                        <FormItem
			                        labelCol={{ span: 4 }}
			                        wrapperCol={{ span: 18 }}
			                        label="单位logo" > 
			                        <PicturesWall
			                            pictureSign="logo"
			                            fileList={this.getCompanyLogoUrl}
			                            pictureUrl={this.state.companyLogoUrl} />
			                            <p>图片建议：图片要清晰。</p>
			                    </FormItem>
			                </div> 
			                <div className="clearfix">
			                    <FormItem
		                        labelCol={{ span: 2 }}
		                        wrapperCol={{ span: 18 }}
		                        label="业务范围" >
		                            <Input type="textarea" rows={4} value={this.state.businessScope} 
							    				onChange={(e)=>{this.setState({businessScope:e.target.value})}}/>
			                    </FormItem>
			                    <FormItem
			                        labelCol={{ span: 2 }}
			                        wrapperCol={{ span: 18 }}
			                        label="单位简介" >
			                            <Input type="textarea" rows={4}  value={this.state.introduction} 
							    				onChange={(e)=>{this.setState({introduction:e.target.value})}}/>			                      
			                    </FormItem>
		                    </div>
		                    <Button className="set-submit" type="primary" htmlType="submit" >保存</Button>  
	                        <Button  type="ghost" onClick={this.detailsModal}>取消</Button>
	                    </div>		                	
				    </Spin>
				</Form>		
			</div>
		)
	}
})
export default Basic;