import React from 'react';
import { Icon, Modal, Button, Form, message, Input, Spin, Select, Row, Col } from 'antd';
import './comprehensive.less';
import { companySearch } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js'
import $ from 'jquery/src/ajax';

const PatentAddFrom = Form.create()(React.createClass({
    getContactsList() {
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/user/cognizance/getContacts",
            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={item} key={theData}>{theData}</Select.Option>
                    );
                };
                this.setState({
                    contactsOption: theOption
                });
            }.bind(this),
        });
    },
    getInitialState() {
        return {
            loading: false,
            companyOption: [],
            accelerated: 0,
            visible: "visible"
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.props.spinState(true);
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/user/copyright/apply",
                    data: {
                        //"uid": values.uid, // 公司用户id，size 0-36, required
                        "contact": values.contact, // 联系人， 1，2，3
                        "copyrightInfo": values.copyrightInfo, // 软著简介，size 0-255
                        "copyrightName": values.copyrightName, // 软著名称, size 0-60
                        //"outsource": values.outsource, // 外包公司， size 0-60
                        "workIssue": values.workIssue, // 派单信息, size 0-128
                        "inUrgent": values.inUrgent, //  加急天数,
                        //"comment": values.comment, // 备注 size 0-128
                        //"principal": values.principal // 负责人id
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                        this.props.okClick();
                        this.props.closeModal();
                        this.props.form.resetFields();
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this)).always(function () {
                    this.props.spinState(false);
                }.bind(this));
            }
        });
    },
    componentWillMount() {
        this.getContactsList();
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.visible = "visible";
            this.props.form.resetFields();
        };
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 14 },
        };
        const _me = this;
        return (
            <Form horizontal onSubmit={this.handleSubmit} id="CopyrightDesc-form">
                <Row>
                    <Col span={12}>
                        <FormItem
                            {...formItemLayout}
                            label="软著名称"
                        >
                            {getFieldDecorator('copyrightName')(
                                <Input placeholder="请输入软著名称" />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={12}>
                        <span>选择联系人： </span>
                        {getFieldDecorator('contact')(
                            <Select placeholder="请选择联系人" style={{ width: 200 }}
                                notFoundContent="未获取到联系人列表"
                                onSelect={(e, n) => { this.setState({ contact: e }); }}>
                                {this.state.contactsOption}
                            </Select>
                        )}
                    </Col>
                </Row>
                <FormItem
                    labelCol={{ span: 24 }}
                    wrapperCol={{ span: 18 }}
                    label="软著简介"
                >
                    {getFieldDecorator('copyrightInfo')(
                        <Input type="textarea"
                            placeholder="多行输入"
                            rows={6} />
                    )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 24 }}
                    wrapperCol={{ span: 18 }}
                    label="备注"
                >
                    {getFieldDecorator('workIssue')(
                        <Input type="textarea"
                            placeholder="多行输入"
                            rows={6} />
                    )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 0 }}
                    wrapperCol={{ span: 18 }}
                >
                    <span>是否加急：</span>
                    {getFieldDecorator('inUrgent')(
                        <Select
                            placeholder="请选择加急时间"
                            style={{ width: 200 }}>
                            <Select.Option value="0">不加急(45个工作日)</Select.Option>
                            <Select.Option value="3">3个工作日</Select.Option>
                            <Select.Option value="5">5个工作日</Select.Option>
                            <Select.Option value="10">6-10个工作日</Select.Option>
                            <Select.Option value="15">11-15个工作日</Select.Option>
                            <Select.Option value="20">16-20个工作日</Select.Option>
                            <Select.Option value="25">21-25个工作日</Select.Option>
                            <Select.Option value="30">26-30个工作日</Select.Option>
                        </Select>
                    )}
                </FormItem>
                <FormItem>
                    <Button className="set-submit" type="primary" htmlType="submit">确认</Button>
                    <Button type="ghost" style={{ marginLeft: '20px' }} onClick={this.props.closeModal}>取消</Button>
                </FormItem>
            </Form >
        );
    },
}));



const PatentAdd = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false
        };
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeAdd(false);
    },
    spinChange(e) {
        this.setState({
            loading: e
        });
    },
    render() {
        return (
            <div className="patent-addNew">
                <Button className="patent-addNew" type="primary" onClick={this.showModal}>申请新软著<Icon type="plus" /></Button>
                <Spin spinning={this.state.loading} className='spin-box'>
                    <Modal title="申请新软著"
                        visible={this.state.visible}
                        onCancel={this.handleCancel}
                        width='800px'
                        footer=''
                    >
                        <PatentAddFrom
                            spinState={this.spinChange}
                            visible={this.state.visible}
                            closeModal={this.handleCancel}
                            okClick={() => { this.props.closeAdd(false, true) }} />
                    </Modal>
                </Spin>
            </div>
        );
    },
});
export default PatentAdd;