import React from 'react';
import {
	Icon,
	Button,
	Input,
	Select,
	Spin,
	Checkbox,
	Cascader,
	Table,
	Form,
	Switch,
	message,
	DatePicker,
	Modal,
	Row,
	Col,
	InputNumber,
} from 'antd';
import { citySelect, provinceList ,areaSelect} from '@/NewDicProvinceList';
import {demandProfile} from '../../../dataDic.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
//import axios from 'axios';
import 'antd/dist/antd.css';
const FormItem = Form.Item;
import './sichuan.css';
const { TextArea } = Input;
const CheckboxGroup = Checkbox.Group;

class Achievement extends React.Component {
	loadData(pageNo) {
		this.setState({
			page: pageNo,
			loading: true
		});
		$.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/open/api/demandList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                type:1,
                orgName:this.state.seachOrgName,//企业名称
                
            },
            success: function (data) {
                let theArr = [];
                if (!data.data || !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({
                            key: i,
                            id: thisdata.id,//需求ID
                            name:thisdata.orgName,//企业名称
                            contacts:thisdata.contacts,//联系人
                            address:thisdata.address,//详细地址
                            telephone:thisdata.telephone,//联系电话
                        });
                    };
                    this.state.pagination.current = data.data.pageNo;
                    this.state.pagination.total = data.data.totalCount;
                };
                this.setState({
                    dataSource: theArr,
                    pagination: this.state.pagination
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
	}
	constructor(props) {
		super(props);
		this.state = {
			loading:false,
			publishPages: [],
			visible: false,
			pagination: {
				defaultCurrent: 1,
				defaultPageSize: 10,
				showQuickJumper: true,
				pageSize: 10,
				onChange: function(page) {
					this.loadData(page);
				}.bind(this),
				showTotal: function(total) {
					return '共' + total + '条数据';
				}
			},
			columns: [
				{
					title: '企业名称',
					dataIndex: 'name',
					key: 'name'
				},{
					title: '联系人',
					dataIndex: 'contacts',
					key: 'contacts'
				},{
					title: '联系方式',
					dataIndex: 'telephone',
					key: 'telephone'
				},{
					title: '详细地址',
					dataIndex: 'address',
					key: 'address'
				},
				{
					title: '操作',
					dataIndex: 'caozuo',
					key: 'caozuo',
					render: (text, recard) => {
						return (
							<div className="btnRight">
								<Button
									type="danger"
									onClick={(e) => {
										e.stopPropagation();
										this.delectRow(recard);
									}}
								>
									删除
								</Button>
							</div>
						);
					}
				}
			]
		};
	}
	
	
	//删除
	delectRow(recard) {
		console.log(recard);
		$.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/open/api/delectDemand",
            data: {
                id:recard.id
            },
            success: function (data) {
	            if (!data.error.length) {
	                message.success('删除成功！');
	                this.setState({
	                    loading: false,
	                });
	                this.loadData();
	            } else {
	                message.warning(data.error[0].message);
	            };
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
	}
	details(recard){
		this.state({
			loading:true
		})
	}
	add() {
		this.setState({
			uid: '',
			visible: true,
			addState: true,
			name: '',
			introduce: '',
			industry: '',
			summary: '',
			ownerName: '',
			reserveButtMode: '',
			patentNumber: '',
			transferPrice: '',
			telephone: '',
			cname: ''
		});
	}
	search() {
		this.loadData(1);
	}
	reset() {
		this.state.seachOrgName = '';
		this.loadData(1);
	}
	tableRowClick(record, index) {
		this.setState({
			visible: true,
			addState: false,
			id:record.id,
		});
		$.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/open/api/demandDetails",
            data: {
                id: record.id,//需求名称
                
            }
        }).done(function (data) {
            if (!data.error.length) {
            	console.log(data.data);
               let thisData=data.data;
               let ProvinceCityArr = [];
               let ProvinceS =thisData.province? parseInt(thisData.province):""; //省
			   let citys = thisData.city?parseInt(thisData.city):"";//市
			   ProvinceCityArr.push(ProvinceS, citys);
               this.setState({
               		id:thisData.id,
               		Pname: thisData.pname,//企业名称
               		ProvinceCity:ProvinceCityArr[0]!=null?ProvinceCityArr:undefined,//地区
               		address: thisData.address,//详细地址
               		publisherName: thisData.publisherName,//联系人
	                telephone: thisData.telephone,//联系方式
               		email: thisData.email,//企业邮箱
               		feature: thisData.feature,//企业特征
               		introduce: thisData.introduce,//需求内容
               		investmentCost: thisData.investmentCost,//经费投入
               		pintroduce:thisData.pintroduce,//企业简介
               		remarks:thisData.remarks,//企业
//					name: thisData.name,//需求名称
//	                industry:(thisData.industry||thisData.industry==0)?thisData.industry.toString():undefined,//行业
//	                reserveButtUnit: thisData.reserveButtUnit,//拟对接单位
//	                reserveButtMode: (thisData.reserveButtMode||thisData.reserveButtMode==0)?thisData.reserveButtMode.toString():undefined,//拟对接方式
//	                reserveSolveMode:(thisData.reserveSolveMode||thisData.reserveSolveMode==0)?thisData.reserveSolveMode.toString():undefined ,//拟解决方式
//	                summary: thisData.summary?thisData.summary.split(","):[],//需求项目概况this.state.shehui?this.state.shehui.join(","):undefined
//	                nature: thisData.nature,//企业特质
//	                zipCode: thisData.zipCode,//邮政编码
//	                legalPerson: thisData.legalPerson,//法人名称
//	                website: thisData.website,//企业网址
	                
				});
            } else {
                message.warning(data.error[0].message);
            };
            this.setState({
                loading: false,
            });
        }.bind(this));
	}
	handleOk() {
		this.handleSubmit();
	}
	handleCancel() {
		this.setState({
			visible: false
		});
		this.resets();
	}
	handleSubmit() {
		this.setState({
			loading: true
		});
		if(!this.state.Pname){
			 message.warning("企业名称必须填写");
			 this.setState({
				loading: false
			});
			 return;
		}
		$.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + ( this.state.addState?"/open/api/addDemand":"/open/api/updateDemand"),
            data: {
            	id:this.state.id,//需求ID
            	Pname: this.state.Pname,//企业名称
            	province: this.state.ProvinceCity?this.state.ProvinceCity[0]:"",//省
                city: this.state.ProvinceCity?this.state.ProvinceCity[1]:"",//市
            	publisherName: this.state.publisherName,//联系人
                telephone: this.state.telephone,//联系方式
            	email: this.state.email,//企业邮箱
            	address: this.state.address,//详细地址
            	Pintroduce: this.state.pintroduce,//企业简介
            	remarks: this.state.remarks,//企业情况
            	feature:this.state.feature,//企业特征
            	introduce: this.state.introduce,//人才需求
                type:1,//需求类型
                investmentCost: this.state.investmentCost,//经费投入
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success(this.state.addState?'新建成功！':"修改成功！");
                this.setState({
	                visible: false,
	            });
                this.loadData();
            } else {
                message.warning(data.error[0].message);
            };
            this.setState({
                loading: false,
            });
        }.bind(this));
	}
	componentWillMount() {
		this.loadData(1);
	}
	resets(){
    	this.state.Pname= '',//企业名称
    	this.state.ProvinceCity= undefined,//省
    	this.state.publisherName= '',//联系人
        this.state.telephone= '',//联系方式
    	this.state.email= '',//企业邮箱
    	this.state.address= '',//详细地址
    	this.state.pintroduce= '',//企业简介
    	this.state.remarks= '',//企业情况
    	this.state.feature= '',//企业特征
    	this.state.introduce= '',//人才需求
        this.state.investmentCost= ''//经费投入
        
	}
	render() {
		const rowSelection = {
			selectedRowKeys: this.state.selectedRowKeys,
			onChange: (selectedRowKeys, selectedRows) => {
				this.setState({
					selectedRows: selectedRows.slice(-1),
					selectedRowKeys: selectedRowKeys.slice(-1)
				});
			}
		};
		const formItemLayout = {
			labelCol: { span: 6 },
			wrapperCol: { span: 12 }
		};
		return (
			<div className="user-content">
				<div className="content-title">
					<span className="title">四川人才需求</span>
					<div className="patent-addNew">
						<Button type="primary" onClick={this.add.bind(this)}>
							发布人才需求<Icon type="plus" />
						</Button>
					</div>
				</div>
				<div className="user-search">
					<Input
						placeholder="企业名称"
						value={this.state.seachOrgName}
						className="searchInp"
						onChange={(e) => {
							this.setState({ seachOrgName: e.target.value });
						}}
						style={{ width: 120 }}
					/>
					<Button type="primary" onClick={this.search.bind(this)} className="interval">
						搜索
					</Button>
					<Button onClick={this.reset.bind(this)} className="interval">
						重置
					</Button>
				</div>
				<div className="patent-table">
					<Spin spinning={this.state.loading}>
						<Table
							columns={this.state.columns}
							dataSource={this.state.dataSource}
							rowSelection={rowSelection}
							pagination={this.state.pagination}
							onRowClick={this.tableRowClick.bind(this)}
						/>
					</Spin>
				</div>
				<Modal
					title={this.state.addState ? '新建人才需求' : '人才需求详情'}
					visible={this.state.visible}
					width="900px"
					onOk={this.handleOk.bind(this)}
					onCancel={this.handleCancel.bind(this)}
					okText="保存"
					cancelText="取消"
				>
					<Form layout="horizontal" id="demand-form">
						<Spin spinning={this.state.loading}>
								<div className="clearfix">
									<FormItem
										className="half-item"
										{...formItemLayout}
										label={
											<span>
												<strong style={{ color: '#f00' }}>*</strong>企业名称
											</span>
										}
									>
										<Input
											placeholder="企业名称"
											value={this.state.Pname}
											onChange={(e) => {
												this.setState({ Pname: e.target.value });
											}}
										/>
									</FormItem>
									<FormItem  className="half-item"
	                            		{...formItemLayout}
			                           label="企业所在地"
			                          > 
										   <Cascader options={citySelect()}   placeholder="选择城市"  value={this.state.ProvinceCity}
											onChange={(e) => {this.setState({ ProvinceCity: e })}}
											/>
				                    </FormItem>
				                    <FormItem className="half-item" {...formItemLayout} label="联系人">
										<Input
											placeholder="联系人"
											value={this.state.publisherName}
											onChange={(e) => {
												this.setState({ publisherName: e.target.value });
											}}
										/>
									</FormItem>
									<FormItem className="half-item" {...formItemLayout} label="联系人电话">
										<Input
											placeholder="联系人电话"
											value={this.state.telephone}
											onChange={(e) => {
												this.setState({ telephone: e.target.value });
											}}
										/>
									</FormItem>
									<FormItem className="half-item" {...formItemLayout} label="电子邮件">
										<Input
											placeholder="电子邮件"
											value={this.state.email}
											onChange={(e) => {
												this.setState({ email: e.target.value });
											}}
										/>
									</FormItem>
									<FormItem className="half-item" {...formItemLayout} label="详细地址">
										<Input
											placeholder="详细地址"
											value={this.state.address}
											onChange={(e) => {
												this.setState({ address: e.target.value });
											}}
										/>
									</FormItem>
									<div className="clearfix">
										<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="企业简介">
											<Input type="textarea" rows={4} placeholder="企业简介" value={this.state.pintroduce}
	                           				 	onChange={(e) => { this.setState({ pintroduce: e.target.value }) }} style={{width:'95%'}}/>
										</FormItem>
									</div>
									<div className="clearfix" style={{marginTop:"10px"}}>
										<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="企业情况">
											<Input type="textarea" rows={4} placeholder="企业在行业中情况" value={this.state.remarks}
	                           				 	onChange={(e) => { this.setState({ remarks: e.target.value }) }} style={{width:'95%'}}/>
										</FormItem>
									</div>
									<div className="clearfix" style={{marginTop:"10px"}}>
										<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="产品概述">
											<Input type="textarea" rows={4} placeholder="产品概述" value={this.state.feature}
	                           				 	onChange={(e) => { this.setState({ feature: e.target.value }) }} style={{width:'95%'}}/>
										</FormItem>
									</div>
									<div className="clearfix" style={{marginTop:"10px"}}>
										<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="人才需求概述">
											<Input type="textarea" rows={4} placeholder="人才需求概述" value={this.state.introduce}
	                           				 	onChange={(e) => { this.setState({ introduce: e.target.value }) }} style={{width:'95%'}}/>
										</FormItem>
									</div>
									<div className="clearfix" style={{marginTop:"10px"}}>
										<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="待遇">
											<Input type="textarea" rows={4} placeholder="待遇" value={this.state.investmentCost}
	                           				 	onChange={(e) => { this.setState({ investmentCost: e.target.value }) }} style={{width:'95%'}}/>
										</FormItem>
									</div>
								</div>
						</Spin>
					</Form>
				</Modal>
			</div>
		);
	}
}

export default Achievement;
