import React from 'react'
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import {Modal,Form,layout,Radio,Button,Input,Spin,Table,Select,Popconfirm,message} from 'antd';
import {intentionalService,newFollow,customerStatus} from '@/dataDic.js';
import {getCompanyIntention,getfllowSituation,getcustomerStatue} from '@/tools.js';
const FormItem =Form.Item;
import BusName from '../../followDetail/businessName.jsx';

const VisitDetail = React.createClass({
	getInitialState(){
		return{
			visitModul:false,
			loading:false,
			data:[],
			//业务意向列表
			intentionList:[{
					title: '业务名称',
					dataIndex: 'businessGlossoryId',
					width:120,
					key: 'businessGlossoryId',
					render: (text, record, index) => {
						return (
							<div>
                               <span>{getCompanyIntention(text)}</span>
							</div>
						)
					}
				},
				{
					title: '项目名称',
					dataIndex: 'businessName',
					key:'businessName',
					width:200,
					render:(text,record,index)=>{
						return(
							<div>
								<span title={text}>{text&&text.length>14?(text.substr(0,14)+'…'):text}</span>
							</div>
							)
						}
				},
				{
					title: '最新进度',
					width:120,
					dataIndex: 'followSituation',
					key: 'followSituation',
					render: (text, record, index) => {
						return  <span>{getfllowSituation(text)}</span>
					}
				}, {
					title: '最新状态',
					width:120,
					dataIndex: 'customerStatus',
					key: 'customerStatus',
					render: (text, record, index) => {
						return <span>{getcustomerStatue(text)}</span>
					}
				}, {
					title: '跟进说明',
					width:120,
					dataIndex: 'remarks',
					key: 'remarks',
					render: (text, record, index) => {
						return(
							<div>
								<span title={text}>{text&&text.length>14?(text.substr(0,14)+'…'):text}</span>
							</div>
							)
					}
				}, {
					title: '操作',
					width:120,
					dataIndex: 'rrr',
					key: 'rrr',
					render: (text, record, index) => {
						return <div>
                            <Popconfirm title="是否删除?" onConfirm={(e)=>{this.intentionDelet(record,index)}} okText="删除" cancelText="不删除">
							    <Button style={{marginRight:'10px',color:'#ffffff',background:'#f00',border:'none'}}>删除</Button>
							</Popconfirm>
                        </div>
					}
				}
			],
		}
	},
	//选择子项目
	businessFn(record,index){
		if(!record.businessGlossoryId){
			message.warn('请先选择业务名称!');
			return;
		}
		this.setState({
			businessId:record.businessGlossoryId,
			busModul:true,
			index:index,
			addcontactModul:false
		})
	},
	closeBus(data,e,s){
		this.state.addcontactModul=e;
		this.state.busModul=false;
		if(s){
			this.state.data[this.state.index].businessName=data;
			this.setState({
				busModul:false,
				data:this.state.data
			})
		}
	},
	//进入修改拜访记录  
	visitModify(e) {
		this.setState({
			visitModul: true,
			loading: true
		});
		$.ajax({
			method: "get",
			dataType: "json",
			url: globalConfig.context + '/api/admin/customer/toUpdateFollow',
			data: {
				followId: e,
			},
			success: function(data) {
				let theArr = [];
				let thedata = data.data;
				if(!thedata) {
					if(data.error && data.error.length) {
						message.warning(data.error[0].message);
					};
					thedata = {};
				};

				for(let i = 0; i < data.data.userBusinessList.length; i++) {
					let thisdata = data.data.userBusinessList[i];
					theArr.push({
						id: thisdata.id,
						businessGlossoryId: String(thisdata.businessGlossoryId),
						followSituation: String(thisdata.followSituation),
						customerStatus: String(thisdata.customerStatus),
						remarks: thisdata.remarks,
					});
				};
				this.setState({
					followIds: thedata.followId,
					contacts: thedata.contacts,
					uid: thedata.uid,
					data: theArr,
					nub: thedata.contactMobile,
					result: thedata.result,
					contactType: parseInt(thedata.contactType),
					identifyName: thedata.identifyName,
					followTime: thedata.followTime,
					businessGlossoryId: thedata.businessGlossoryId,
					followSituation: thedata.followSituation,
					customerStatus: thedata.customerStatus,
					adminName: thedata.adminName,
				});
			}.bind(this),
		}).always(function() {
			this.setState({
				loading: false
			});
		}.bind(this));
	},
	//拜访意向服务列表单个删除
	intentionDelet(e, index) {
		let detId = this.state.followIds;
		if(e.id) {
			this.setState({
				selectedRowKeys: [],
			});
			$.ajax({
				method: "get",
				dataType: "json",
				crossDomain: false,
				url: globalConfig.context + "/api/admin/customer/deleteFollowOneBusiness",
				data: {
					ufbId: e.id
				}
			}).done(function(data) {
				if(!data.error.length) {
					message.success('删除成功');
					this.setState({
						loading: false,
					});
				} else {
					message.warning(data.error[0].message);
				};
				this.visitModify(detId);
			}.bind(this));
		} else {
			this.visitModify(detId);
		}
	},
	//拜访modul函数
	visitOk(e) {
		this.setState({
			addcontactModul:false,
			visitModul: false
		});
	},
	visitCancel(e) {
		this.setState({
			addcontactModul:false,
			visitModul: false
		});
	},
	
	//详情保存
	visitSubmit(e){
		e.preventDefault();
		this.setState({
			loading: true
		});
		let contactsId = '';
		if(this.state.keys) {
			let conts = this.state.lastName;
			contactsId = this.state.contactsIdArr[conts].id;
		}
		//新增
		$.ajax({
			method: "post",
			dataType: "json",
			url: globalConfig.context + '/api/admin/customer/updateFollow',
			data: {
				followId: this.state.followIds,
				userBusinessList: JSON.stringify(this.state.data),
				uid: this.state.uid,
				contactType: this.state.contactType,
				result: this.state.result,
				followTime: this.state.followTime,
			}
		}).done(function(data) {
			this.setState({
				loading: false
			});
			if(!data.error.length) {
				message.success('保存成功！');
				this.props.closeDesc(false,true);
				this.visitCancel();
			} else {
				message.warning(data.error[0].message);
			}
		}.bind(this));

	},
	componentWillReceiveProps(nextProps) {
        if (nextProps.visitModul && nextProps.followData.followId) {
            this.setState({
				visitModul: true,
				loading: true,
			})
			this.visitModify(nextProps.followData.followId)
        };
    },
	render() {
		const formItemLayout = {
			labelCol: { span: 8 },
			wrapperCol: { span: 14 },
		};
		return(
			<div>
				<Modal	
				      className="customeDetails"
				      footer=''
			          title="客户跟进详情"
			          width='1000px'
			          visible={this.state.visitModul}
			          onOk={this.visitOk}
					  onCancel={this.visitCancel}							          							          
			        >				    
					    <Form layout="horizontal" id="demand-form" onSubmit={this.visitSubmit}>
			    			<Spin spinning={this.state.loading}>
			    			   	<div className="clearfix">
			    			   		<FormItem 
			                            labelCol={{ span:4 }}
										wrapperCol={{ span: 20 }}
			                            label="拜访方式" >
			    			   			<Radio.Group value={this.state.contactType} onChange={(e) => {
										        this.setState({ contactType: e.target.value }) 
						                    }}>
						                        <Radio value={0}>外出</Radio>
						                        <Radio value={1}>电话</Radio>
						                        <Radio value={2}>QQ</Radio>
						                        <Radio value={3}>微信</Radio>
						                        <Radio value={4}>邮箱</Radio>
						                </Radio.Group>
					                </FormItem>
					                <div className="clearfix">
						                <FormItem className="half-item"
				                            {...formItemLayout}
				                             label="当前联系人"
				                             >
						                    <span>{this.state.contacts}</span>
						                </FormItem>
						                <FormItem className="half-item"
				                            {...formItemLayout}
				                             label="跟进时间"
				                             >
						                	<span>{this.state.followTime}</span>
						                </FormItem>
						                <FormItem className="half-item"
				                            {...formItemLayout}
				                             label="联系电话"
				                             >
						                	<span>{this.state.contactMobiles}</span>
						                </FormItem>
					                </div>
					                <div className="clearfix">
					                	<FormItem
					                        labelCol={{ span: 4 }}
							                wrapperCol={{ span: 16 }}
					                        label="跟进备注" >
					                            <Input type="textarea" rows={4} value={this.state.result} 
					                              onChange={(e)=>{this.setState({result:e.target.value})}}/>
					                    </FormItem>
					                </div>
					                <div className="clearfix">
					                 	<div style={{fontSize:'18px',marginLeft:'100px',marginTop:'15px',marginBottom:'10px'}}>业务意向</div>
					                 	<div className="clearfix" style={{paddingLeft:'50px',paddingRight:'50px'}}>
		                    			    <Spin spinning={this.state.loading}>
										      <Table 
										         rowKey={record => record.id}
								            	 pagination={false}
								            	 columns={this.state.intentionList} 
								            	 dataSource={this.state.data}
								            	 />
										    </Spin> 
										</div>	 
					                </div>
			    			   	</div>
			    			   	<FormItem wrapperCol={{ span: 12, offset: 6 }}>
								    <Button  type="primary" size="large" htmlType="submit" style={{marginRight:'150px',marginTop:'20px'}}>保存</Button>
						           	<Button  size="large" onClick={this.visitCancel}>取消</Button>
					   			</FormItem>
					   		</Spin>
					    </Form>
				</Modal> 
			</div>
		)
	}
})
export default VisitDetail;