import React from 'react';
import {Form,Button,Spin,message,Table,Popconfirm,Modal} from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import {getCompanyIntention,getfllowSituation,getcustomerStatue,getContactType } from '@/tools.js';
const FormItem =Form.Item;
import BusinFollow from './businFollow.jsx'

const BusinessDetail =React.createClass({
	getInitialState(){
		return {
			loading:false,
			visitArrList: [],
			busData:{},
			paginationst: {
				defaultCurrent: 1,
				defaultPageSize: 10,
				showQuickJumper: true,
				pageSize: 10,
				onChange: function(page) {
					this.loadVisit(page);
				}.bind(this),
				showTotal: function(total) {
					return '共' + total + '条数据';
				}
			},
			businessFollowList: [{
					title: '跟进时间',
					dataIndex: 'followTime',
					key: 'followTime',
				}, {
					title: '营销员',
					dataIndex: 'adminName',
					key: 'adminName',
				}, {
					title: '业务意向进度',
					dataIndex: 'followSituation',
					key: 'followSituation',
					render: text => { return getfllowSituation(text) }
				}, {
					title: '客户状态',
					dataIndex: 'customerStatus',
					key: 'customerStatus',
					render: text => { return getcustomerStatue(text) }
				},
				{
					title: '拜访方式',
					dataIndex: 'contactType',
					key: 'contactType',
					render: text => { return getContactType(text) }
				}, {
					title: '联系人',
					dataIndex: 'contacts',
					key: 'contacts',
				},
				{
					title: '联系电话',
					dataIndex: 'contactMobile',
					key: 'contactMobile',
				}, {
					title: '操作',
					dataIndex: 'ooo',
					key: 'ooo',
					render: (text, record) => { return(<Button onClick={(e)=>{this.businessFollowDetails(record)}}>查看</Button>) }
				}
			],
		}
	},
	//业务跟进查看跟多
	businessFollowDetails(e) {
		this.setState({
			busData:e,
			businessFollowModul: true
		})
	},
	listFollowUp(e) {
		$.ajax({
			method: "get",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + "/api/admin/customer/toUpdateFollowOneBusiness",
			data: {
				ufbId: 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({
					businessGlossoryIds: thedata.businessGlossoryId,
					followSituations: thedata.followSituation,
					customerStatuss: thedata.customerStatus,
					followTimes: thedata.followTime,
					createTimes: thedata.createTime,
					identifyNames: thedata.identifyName,
					contactTypes: parseInt(thedata.contactType),
					results: thedata.result,
					remarkss: thedata.remarks,
					adminNames: thedata.adminName,
					contactss: thedata.contacts,
					contactMobiles: thedata.contactMobile,
					ufbId: thedata.ufbId,
					followIds: thedata.followId,
				});
			}.bind(this),
		}).always(function() {
			this.setState({
				loading: false
			});
		}.bind(this));
	},
	//查看跟进记录列表
	loadVisit(pageNo) {
		this.setState({
			loading: true
		});
		$.ajax({
			method: "get",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + '/api/admin/customer/listFollowHistory',
			data: {
				pageNo: pageNo || 1,
				pageSize: this.state.paginationst.pageSize,
				uid: this.props.ids, //名称1
				businessGlossoryId: this.state.businessGlossoryId,
			},
			success: function(data) {
				let theArr = [];
				if(data.error.length || data.data.list == "") {
					if(data.error && data.error.length) {
						message.warning(data.error[0].message);
					};
				} else {
					for(let i = 0; i < data.data.list.length; i++) {
						let thisdata = data.data.list[i];
						theArr.push({
							followId: thisdata.followId,
							followTime: thisdata.followTime,
							identifyName: thisdata.identifyName,
							contacts: thisdata.contacts,
							contactMobile: thisdata.contactMobile,
							result: thisdata.result,
							ufbId: thisdata.ufbId,
							adminName: thisdata.adminName,
							followSituation: thisdata.followSituation,
							customerStatus: thisdata.customerStatus,
							contactType: thisdata.contactType
						});
					};
					this.state.paginationst.current = data.data.pageNo;
					this.state.paginationst.total = data.data.totalCount;
				};
				if(data.data.list&&!data.data.list.length){
					this.state.paginationst.current = 0;
					this.state.paginationst.total = 0;
				};
				this.setState({
					visitArrList: theArr,
					paginationst: this.state.paginationst
				});
			}.bind(this),
		}).always(function() {
			this.setState({
				loading: false
			});
		}.bind(this));
	},
	businessIntentionDetails(e) {
		$.ajax({
			method: "get",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + '/api/admin/customer/toUpdateBusiness',
			data: {
				businessId: e.businessId
			},
			success: function(data) {
				let thisData = data.data;
				if(!thisData) {
					if(data.error && data.error.length) {
						message.warning(data.error[0].message);
					};
					thisData = {};
				};
				this.setState({
					businessGlossoryId: thisData.businessGlossoryId,
					identifyName: thisData.identifyName,
					uid: thisData.uid,
					businessId: thisData.businessId,
					followSituation: thisData.followSituation,
					customerStatus: thisData.customerStatus,
					createTime: thisData.createTime,
					followTime: thisData.followTime,
					adminName: thisData.adminName,
					remarks: thisData.remarks,
					result: thisData.result,
				});
			}.bind(this),
		}).always(function() {
			this.loadVisit();
			this.setState({
				loading: false
			});
		}.bind(this));
		this.setState({
			businessModul: true
		})
	},
	businessIntentionCancel(){
		this.setState({
			businessFollowModul:false,
			businessModul:false
		})
	},
	componentWillReceiveProps(nextProps) {
		if(nextProps.data.businessId&&nextProps.businessModul&&nextProps.ids){
			this.setState({
				businessModul:true
			})
			this.businessIntentionDetails(nextProps.data);
		}
		this.setState({
			businessFollowModul:false
		})
	},
	render(){
		const formItemLayout = {
			labelCol: { span: 8 },
			wrapperCol: { span: 14 },
		};
		return(
			<div>
				<Modal	
				      footer=''
			          title="业务意向详情"
			          width='1000px'
			          visible={this.state.businessModul}
			          onOk={this.businessIntentionCancel}
					  onCancel={this.businessIntentionCancel}							          							          
			        >				    
					    <div className="clearfix">
			    			<Form layout="horizontal" id="demand-form" onSubmit={this.passworldSubmit}>
			    				<Spin spinning={this.state.loading}>
			    					<div className="clearfix">
				    					<FormItem className="half-item"
					                            {...formItemLayout}
					                            label="意向业务名称" >
					                        <span>{getCompanyIntention(this.state.businessGlossoryId)}</span>
					                    </FormItem>
					                    <FormItem className="half-item"
					                            {...formItemLayout}
					                            label="意向时间" >
					                        <span>{this.state.createTime}</span>
					                    </FormItem>
					                    <FormItem className="half-item"
					                            {...formItemLayout}
					                            label="客户名称" >
					                        <span>{this.state.identifyName}</span>
					                    </FormItem>
					                    <FormItem className="half-item"
					                            {...formItemLayout}
					                            label="营销员" >
					                        <span>{this.state.adminName}</span>
					                    </FormItem>
					                    <FormItem className="half-item"
					                            {...formItemLayout}
					                            label="意向业务进度" >
					                        <span>{getfllowSituation(this.state.followSituation)}</span>
					                    </FormItem>
					                    <FormItem className="half-item"
					                            {...formItemLayout}
					                            label="业务客户状态" >
					                        <span>{getcustomerStatue(this.state.customerStatus)}</span>
					                    </FormItem>
					                </div>
				                    <div className="clearfix">
					                    <FormItem className="half-item"
					                            {...formItemLayout}
					                            label="业务意向说明" >
					                        <span>{this.state.remarks}</span>
					                    </FormItem>
			                        </div>
			                        <div className="clearfix">
			                        	<div style={{fontSize:'18px',lineHeight:'24px',marginBottom:'10px'}}>业务跟进</div>
			                        	<div className="clearfix">
		                    			    <Spin spinning={this.state.loading}>
										      <Table 
								            	 pagination={this.state.paginationst}
								            	 columns={this.state.businessFollowList} 
								            	 dataSource={this.state.visitArrList}
								            	 />
										    </Spin> 
										</div>	
			                        </div>
				                </Spin>
							</Form>                  
						</div>	                    
    			</Modal>
    			<BusinFollow 
    				data={this.state.busData}
    				businessFollowModul={this.state.businessFollowModul}
    			/>
			</div>
		)
	}
});
export default BusinessDetail;