import React from 'react';
import { Button, Input, Select, message, Modal } from 'antd';
import { companySearch } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';

const WebsiteAdd = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false
        };
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showAdd
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            loading: true
        });
        if (!this.state.unitName) {
            message.warning('请选择一个公司！');
            this.setState({
                loading: false
            });
            return;
        };
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admintechproject/disposeTechWebsite",
            data: {
                uid: this.state.unitName,
                department: this.state.department,
                website: this.state.website,
                accountNumber: this.state.accountNumber,
                password: this.state.password
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('保存成功！');
                this.setState({
                    visible: false
                });
                this.props.closeAdd(false);
            } else {
                message.warning(data.error[0].message);
                this.setState({
                    loading: false
                });
            }
        }.bind(this));
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeAdd(false);
    },
    spinChange(e) {
        this.setState({
            loading: e
        });
    },
    render() {
        return (
            <div className="cognizance-add">
                <Modal title="添加科技部门网址" visible={this.state.visible}
                    onOk={this.handleOk} onCancel={this.handleCancel}
                    width='800px'
                    footer={[
                        <Button key="submit" type="primary" loading={this.state.loading} onClick={this.handleOk}>
                            确认
                            </Button>,
                        <Button key="back" onClick={this.handleCancel}>
                            取消
                            </Button>,
                    ]}
                >
                    <div className="clearfix" style={{ marginBottom: '10px' }}>
                        <div className="half-item">
                            <span className="item-title">选择公司:  </span>
                            <Select
                                placeholder="请选择公司"
                                style={{ width: 200 }}
                                showSearch
                                filterOption={companySearch}
                                onChange={(e)=>{ this.state.unitName = e;}}>
                                {this.props.companyOption}
                            </Select>
                        </div>
                    </div>
                    <div className="clearfix">
                    	<div className="half-item">
                            <span className="item-title">科技部门名称:  </span>
                            <Input value={this.state.department} onChange={(e) => { this.setState({ department: e.target.value }); }} style={{ width: 200 }} />
                        </div>
                    </div>

                    <div className="clearfix">
	                 	<div className="half-item">
	                   		<span className="item-title">网址:  </span>
	                     	<Input value={this.state.website} onChange={(e) => { this.setState({ website: e.target.value }); }} style={{ width: 200 }} />
	                	</div>
                    </div>
                    <div className="clearfix">
                    	<div className="half-item">
	                   		<span className="item-title">账号:  </span>
	                     	<Input value={this.state.accountNumber} onChange={(e) => { this.setState({ accountNumber: e.target.value }); }} style={{ width: 200 }} />
	                	</div>
	                </div>
	                <div className="clearfix">
                    	<div className="half-item">
	                   		<span className="item-title">密码:  </span>
	                     	<Input value={this.state.password} onChange={(e) => { this.setState({ password: e.target.value }); }} style={{ width: 200 }} />
	                	</div>
	                </div>
                </Modal>
            </div>
        );
    }
});

export default WebsiteAdd;


