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

const ApplyAdd = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            yearOption: []
        };
    },
    getContactsList(theUid) {
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/getContacts",
            data: {
                uid: theUid
            },
            success: function (data) {
                let theOption = [];
                if (data.error.length || !data.data) {
                    return;
                };
                for (let item in data.data) {
                    let theData = data.data[item];
                    theOption.push(
                        <Select.Option value={theData} key={theData}>{theData}</Select.Option>
                    );
                };
                this.setState({
                    contactsOption: theOption
                });
            }.bind(this),
        });
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showAdd
    },
    componentWillMount() {
        let d = new Date();
        let _me = this;
        d = d.getFullYear() - 1;
        this.state.thisYear = d;
        for (let i = d; i < d + 20; i++) {
            _me.state.yearOption.push(
                <Select.Option value={i.toString()} key={i}>{i}</Select.Option>
            )
        };
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            loading: true
        });
        if (!this.state.year) {
            message.warning('请选择年份！');
            this.setState({
                loading: false
            });
            return;
        };
        if (!this.state.contacts) {
            message.warning('请选择一个联系人！');
            this.setState({
                loading: false
            });
            return;
        };
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/applyCognizance",
            data: {
                year: this.state.year,
                contacts: this.state.contacts,
                comment: this.state.comment,
                consultant: this.state.consultant,
                uid: this.state.uid,
                state: this.state.year == this.state.thisYear ? 0 : 7
            }
        }).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}
                                onSelect={(e, n) => { this.state.uid = e; this.getContactsList(e); }}>
                                {this.props.companyOption}
                            </Select>
                        </div>
                        <div className="half-item">
                            <span className="item-title">选择联系人:  </span>
                            <Select placeholder="请选择联系人" style={{ width: 200 }}
                                onSelect={(e, n) => { this.state.contacts = e }}>
                                {this.state.contactsOption}
                            </Select>
                        </div>
                        
                    </div>
                    <div className="clearfix">
                        <div className="half-item">
                            <span className="item-title">选择申报科技部门:  </span>
                            <Select placeholder="请选择年份" style={{ width: 200 }}
                                onSelect={(e, n) => { this.state.year = e }}>
                                {this.state.yearOption}
                            </Select>
                        </div>
                    </div>
                    <div className="clearfix">
                    	<div>
                            <span className="item-title">派单信息:  </span>
                        </div>
                        <Input type="textarea" rows={6} onChange={(e) => { this.state.comment = e.target.value; }} />
                    </div>
                    <div className="clearfix">
	                 	<div className="half-item">
	                   		<span className="item-title">项目名称:  </span>
	                     	<Input  style={{ width: 200 }} />
	                	</div>
                    </div>
                    <div className="clearfix">
                    	<div className="half-item">
	                   		<span className="item-title">项目类型:  </span>
	                     	<Input  style={{ width: 200 }} />
	                	</div>
	                </div>
	                <div className="clearfix">
                    	<div className="half-item">
	                   		<span className="item-title">技术领域:  </span>
	                     	<Input  style={{ width: 200 }} />
	                	</div>
	                </div>
	                <div className="clearfix">
                    	<div>
                            <span className="item-title">项目介绍:  </span>
                        </div>
                        <Input type="textarea" rows={6} />
                    </div>
                    
                    <div className="clearfix">
                        <div className="half-item">
                            <span className="item-title">是否立项: </span>
                            <Checkbox ></Checkbox>
                        </div>
                        <div className="half-item">
                        	<span className="item-title">立项金额: </span>
                            <Input style={{ width: 200 }}  />
                        </div>
                    </div>
                    <div className="clearfix">
                        <div className="half-item">
                            <span className="item-title">是否后补助: </span>
                            <Checkbox onChange={this.subsidyCheck}></Checkbox>
                        </div>
                    </div>
                    <div className="clearfix">
                    	<div className="half-item">
                            <span className="item-title">选择咨询师:  </span>
                            <Select placeholder="请选择咨询师" style={{ width: 200 }}
                                onSelect={(e, n) => { this.state.consultant = e }}>
                                {this.props.authorOption}
                            </Select>
                        </div>
                    </div>
                </Modal>
            </div>
        );
    }
});

export default ApplyAdd;