import React from 'react';
import {
	Icon,
	Button,
	Input,
	Select,
	Spin,
	Checkbox,
	Cascader,
	Table,
	Form,
	Switch,
	message,
	DatePicker,
	Modal,
	Row,
	Col,
	InputNumber,
	notification
} from 'antd';
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;

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/achievementList",
            data: {
                pageNo: pageNo || 1,
                pageSize: this.state.pagination.pageSize,
                type:0,
                name:this.state.seachName,//成果名称
                ownerName:this.state.seachOwnerName,//所有权人
                patentNumber:this.state.seachPatentNumber,//专利编号
            },
            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,//成果编号
                            name: thisdata.name,//成果名称
                            industry: thisdata.industry,//行业
                            ownerName:thisdata.ownerName,//所有权人
                            patentNumber:thisdata.patentNumber,//专利编号
                            contacts:thisdata.contacts,//负责人姓名
                            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: 'ownerName',
					key: 'ownerName'
				},
				{
					title: '专利编号',
					dataIndex: 'patentNumber',
					key: 'patentNumber'
				},
				{
					title: '负责人姓名',
					dataIndex: 'contacts',
					key: 'contacts'
				},
				{
					title: '负责人电话',
					dataIndex: 'telephone',
					key: 'telephone'
				},
				{
					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) {
		$.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/open/api/delectAchievement",
            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.seachName = '';
		this.state.seachOwnerName = '';
		this.state.seachPatentNumber = '';
		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/AchievementDetails",
            data: {
                id: record.id,//需求名称
                
            }
        }).done(function (data) {
            if (!data.error.length) {
               let thisData=data.data;
               this.setState({
               		id:thisData.id,//需求ID
	                name: thisData.name,//成果名称
	                type:0,//需求类型
	                industry:thisData.industry?thisData.industry.toString():"",//行业
	                patentNumber: thisData.patentNumber,//专利编号
	                ownerName: thisData.ownerName,//所有权人
	                publisherName: thisData.publisherName,//联系人
	                telephone: thisData.telephone,//联系方式
	                reserveButtMode: (thisData.reserveButtMode||thisData.reserveButtMode==0)?thisData.reserveButtMode.toString():undefined,//拟对接方式
	                introduce: thisData.introduce,//技术成果内容
	                pIntroduce: thisData.pIntroduce,//企业简介
				});
            } else {
                message.warning(data.error[0].message);
            };
            this.setState({
                loading: false,
            });
        }.bind(this));
	}
	handleOk() {
		this.handleSubmit();
	}
	handleCancel() {
		this.resets();
		this.setState({
			visible: false
		});
	}
	handleSubmit() {
		this.setState({
			loading: true
		});
		if(!this.state.name){
			 message.warning("技术简称必须填写");
			 this.setState({
				loading: false
			});
			 return;
		}
		$.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +( this.state.addState?"/open/api/achievements":"/open/api/updateAchievement"),
            data: {
            	id:this.state.id,//需求ID
                name: this.state.name,//成果名称
                type:0,//需求类型
                industry:this.state.industry,//行业
                patentNumber: this.state.patentNumber,//专利编号
                ownerName: this.state.ownerName,//所有权人
                publisherName: this.state.publisherName,//联系人
                telephone: this.state.telephone,//联系方式
                reserveButtMode: this.state.reserveButtMode,//拟对接方式
                introduce: this.state.introduce,//技术成果内容
                pIntroduce: this.state.pIntroduce,//企业简介
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success(this.state.addState?'新建成功！':"修改成功！");
                this.setState({
	                visible: false,
	            });
                this.loadData();
                this.resets();
            } else {
                message.warning(data.error[0].message);
            };
            this.setState({
                loading: false,
            });
        }.bind(this));
	}
	resets(){
		this.state.name = '',//需求名称
        this.state.industry = undefined,//行业
        this.state.investmentCost = '',//经费投入
        this.state.reserveButtUnit = '',//拟对接单位
        this.state.reserveButtMode = '',//拟对接方式
        this.state.reserveSolveMode = '',//拟解决方式
        this.state.publisherName = '',//联系人
        this.state.telephone = '',//联系方式
        this.state.summary = '',
        this.state.introduce = '',//需求内容
        this.state.Pname = '',//企业名称
        this.state.nature = undefined,//企业特质
        this.state.feature = '',//企业特征
        this.state.ProvinceCity =undefined,//省
        this.state.zipCode = '',//邮政编码
        this.state.legalPerson = '',//法人名称
        this.state.website = '',//企业网址
        this.state.email = '',//企业邮箱
        this.state.pIntroduce = ''//企业简介
	}
	componentWillMount() {
		this.loadData(1);
	}
	render() {
		const url="https://api.qrserver.com/v1/create-qr-code/?data=http://"+window.location.host+"/open/achievementDetails?id="+this.state.id+"&size=100x100"
		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.seachName}
						className="searchInp"
						onChange={(e) => {
							this.setState({ seachName: e.target.value });
						}}
						style={{ width: 120 }}
					/>
					<Input
						placeholder="所有权人"
						value={this.state.seachOwnerName}
						onChange={(e) => {
							this.setState({ seachOwnerName: e.target.value });
						}}
						style={{ width: 120 }}
					/>
					<Input
						placeholder="专利编号"
						value={this.state.seachPatentNumber}
						onChange={(e) => {
							this.setState({ seachPatentNumber: 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.name}
										onChange={(e) => {
											this.setState({ name: e.target.value });
										}}
									/>
								</FormItem>
								<FormItem className="half-item" {...formItemLayout} label={
										<span>
											<strong style={{ color: '#f00' }}>*</strong>行业领域
										</span>
									}>
									<Select
										value={this.state.industry}
										onChange={(e) => {
											this.setState({ industry: e });
										}}
										placeholder="请选择行业领域"
									>
										<Select.Option value="1">先进制造与自动化</Select.Option>
										<Select.Option value="2">电子信息技术</Select.Option>
										<Select.Option value="3">新材料技术</Select.Option>
										<Select.Option value="4">生物与新药</Select.Option>
										<Select.Option value="5">资源与环境</Select.Option>
										<Select.Option value="6">新能源</Select.Option>
									</Select>
								</FormItem>
								<FormItem className="half-item" {...formItemLayout} label="专利编号">
									<Input
										placeholder="专利编号"
										value={this.state.patentNumber}
										onChange={(e) => {
											this.setState({ patentNumber: e.target.value });
										}}
									/>
								</FormItem>
								<FormItem className="half-item" {...formItemLayout} label={
										<span>
											<strong style={{ color: '#f00' }}>*</strong>所有权人
										</span>
									}>
									<Input
										placeholder="所有权人"
										value={this.state.ownerName}
										onChange={(e) => {
											this.setState({ ownerName: e.target.value });
										}}
									/>
								</FormItem>
								<FormItem className="half-item" {...formItemLayout} label={
										<span>
											<strong style={{ color: '#f00' }}>*</strong>项目负责人
										</span>
									}>
									<Input
										placeholder="项目负责人"
										value={this.state.publisherName}
										onChange={(e) => {
											this.setState({ publisherName: e.target.value });
										}}
									/>
								</FormItem>
								<FormItem className="half-item" {...formItemLayout} label={
										<span>
											<strong style={{ color: '#f00' }}>*</strong>联系方式
										</span>
									}>
									<Input
										placeholder="联系电话"
										value={this.state.telephone}
										onChange={(e) => {
											this.setState({ telephone: e.target.value });
										}}
									/>
								</FormItem>
								<FormItem className="half-item" {...formItemLayout} label="拟对接方式">
										<Select
											value={this.state.reserveButtMode}
											onChange={(e) => {
												this.setState({ reserveButtMode: e });
											}}
											placeholder="请选择对接方式"
										>
											<Select.Option value="0">直接对接</Select.Option>
											<Select.Option value="1">技术合作</Select.Option>
											
										</Select>
									</FormItem>
								<div className="clearfix">
									<FormItem
										labelCol={{ span: 3 }}
										wrapperCol={{ span: 18 }}
										label={
											<span>
												<strong style={{ color: '#f00' }}>*</strong>技术成果内容
											</span>
										}
										style={{ margin: '10px 0px' }}
									>
										<TextArea
											autosize={{ minRows: 4, maxRows: 10 }}
											placeholder="输入技术成果内容"
											value={this.state.introduce}
											onChange={(e) => {
												this.setState({ introduce: e.target.value });
											}}
										/>
									</FormItem>
								</div>
								<div className="clearfix">
									<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="企业简介">
										<TextArea autosize={{ minRows: 4, maxRows: 10 }} placeholder="输入企业简介" 
										value={this.state.pIntroduce}
											onChange={(e) => {
												this.setState({ pIntroduce: e.target.value });
											}}/>
									</FormItem>
								</div>
								{!this.state.addState&&<div className="clearfix" style={{marginTop:"10px"}}>
									<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 19 }} label="二维码">
										<img src={url} style={{width:"100px",height:"100px"}}/>
									</FormItem>
								</div>
								}
							</div>
						</Spin>
					</Form>
				</Modal>
			</div>
		);
	}
}

export default Achievement;
