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,getContactType,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 (
							
								{!record.id?:
                               {record.businessVarietiesName}}
							
						)
					}
				},
				{
					title: '项目名称',
					dataIndex: 'businessName',
					key:'businessName',
					width:200,
					render:(text,record,index)=>{
						return(
							
								{!record.id?
									
								
:
								
{record.businessProjectName}
								}
							
 
							)
						}
				},
				{
					title: '最新进度',
					width:120,
					dataIndex: 'followSituation',
					key: 'followSituation',
					render: (text, record, index) => {
						return 
									
                             			 {getfllowSituation(text)}
                              		 
								
					}
				}, {
					title: '最新状态',
					width:120,
					dataIndex: 'customerStatus',
					key: 'customerStatus',
					render: (text, record, index) => {
						return 
								{getcustomerStatue(text)}
							
					}
				}, {
					title: '跟进说明',
					width:180,
					dataIndex: 'remarks',
					key: 'remarks',
					render: (text, record, index) => {
						return 
									
	                           		  {text&&text.length>8?text.substr(0,8)+'…':text}
		                             
	                            
					}
				}, {
					title: '操作',
					dataIndex: 'rrr',
					key: 'rrr',
					render: (text, record, index) => {
						return 
                            {!record.readOnly&&
{this.intentionDelet(record,index)}} okText="删除" cancelText="不删除">
							    
							}
                        
 
					}
				}
			],
		}
	},
	//选择子项目
	businessFn(record,index){
		if(!record.businessGlossoryId){
			message.warn('请先选择业务名称!');
			return;
		}
		this.setState({
			businessId:record.businessGlossoryId,
			busModul:true,
			index:index,
			addcontactModul:false
		})
	},
	closeBus(datas,e,s){
		this.state.addcontactModul=e;
		this.state.busModul=false;
		if(s){
			this.state.data[this.state.index].businessName=datas.id;
			this.state.data[this.state.index].projectName=datas.name;
			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 = {};
				};
				if(thedata&&thedata.userBusinessList){
					for(let i = 0; i < data.data.userBusinessList.length; i++) {
						let thisdata = data.data.userBusinessList[i];
						theArr.push({
							id: thisdata.ufbId,
							readOnly:this.props.followData.readOnly,
							businessName:thisdata.businessProjectId,
							businessGlossoryId:i,
							businessVarietiesName:thisdata.businessVarietiesName,
							businessProjectName:thisdata.businessProjectName,
							followSituation: thisdata.followSituation==null?undefined : String(thisdata.followSituation),
						    customerStatus: thisdata.customerStatus==null?undefined : String(thisdata.customerStatus),
							remarks: thisdata.remarks,
						});
					}
				};
				this.setState({
					followIds: thedata.followId,
					contacts: thedata.contacts,
					contactMobile:thedata.contactMobile,
					uid: thedata.uid,
					data: theArr,
					result: thedata.result,
					contactType: parseInt(thedata.contactType),
					followTime:thedata.followTime
				});
			}.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();
		let GlossoryId=false;
		this.state.data.map(function(item) {
			if(!item.followSituation||!item.customerStatus||!item.businessName) {
				GlossoryId = true
			}
		})
		if(this.state.data.length&&GlossoryId) {
			message.warning("业务名称/项目名称/最新进度/最新状态不能为空!")
			return false;
		};
	   let dataList=this.state.data,
			idsList=[];
		dataList.map((item,index) => {
			idsList.push({
				ufbId:item.id||'',
				businessProjectId:item.businessName,
				customerStatus:item.customerStatus,
				followSituation:item.followSituation,
				remarks:item.remarks||''
			})
		});
		this.setState({
			loading: true
		});
		//新增
		console.log(idsList)
		$.ajax({
			method: "post",
			dataType: "json",
			url: globalConfig.context + '/api/admin/customer/updateFollow',
			data: {
				userBusinessList: JSON.stringify(idsList),
				followId: this.state.followIds,
				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));
	},
	//添加新业务
	addNew(){
		this.state.data.push({
			projectName:'选择',
			businessName:'',
			customerStatus: undefined,
			businessGlossoryId: undefined,
			followSituation: undefined,
			remarks: '',
		});
		this.setState({
			addcontactModul:false,
			data: this.state.data
		})
	},
	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 },
		};
		const followData= this.props.followData || []
		return(