import React from 'react';
import { Button, Input, Select, message, Modal,Spin } 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) {
        if (!this.state.visible && nextProps.showAdd) {
            this.state.unitName = undefined;
            this.state.department = undefined;
            this.state.website = undefined;
            this.state.accountNumber = undefined;
            this.state.password = undefined;
        };
        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/admin/techproject/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({
                    loading: false,
                    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 maskClosable={false} title="添加科技部门网址" visible={this.state.visible}
                    onOk={this.handleOk} onCancel={this.handleCancel}
                    width='500px'
                    footer={[
                        <Button key="submit" type="primary" loading={this.state.loading} onClick={this.handleOk}>
                            确认
                            </Button>,
                        <Button key="back" onClick={this.handleCancel}>
                            取消
                            </Button>,
                    ]} >
                    <Spin spinning={this.state.loading} className='spin-box'>
                        <div>
                            <span className="item-title">选择公司:  </span>
                            <Select
                                placeholder="请选择公司"
                                style={{ width: 200 }}
                                value={this.state.unitName}
                                showSearch
                                filterOption={companySearch}
                                onChange={(e) => { this.setState({ unitName: e }); }}>
                                {this.props.companyAddOption}
                            </Select>
                        </div>
                        <div>
                            <span className="item-title">科技部门名称:  </span>
                            <Input value={this.state.department} onChange={(e) => { this.setState({ department: e.target.value }); }} style={{ width: 200 }} />
                        </div>
                        <div>
                            <span className="item-title">网址:  </span>
                            <Input value={this.state.website} onChange={(e) => { this.setState({ website: e.target.value }); }} style={{ width: 200 }} />
                        </div>
                        <div>
                            <span className="item-title">账号:  </span>
                            <Input value={this.state.accountNumber} onChange={(e) => { this.setState({ accountNumber: e.target.value }); }} style={{ width: 200 }} />
                        </div>
                        <div>
                            <span className="item-title">密码:  </span>
                            <Input value={this.state.password} onChange={(e) => { this.setState({ password: e.target.value }); }} style={{ width: 200 }} />
                        </div>
                    </Spin>
                </Modal>
            </div>
        );
    }
});

export default WebsiteAdd;


