import React from 'react';
import { Icon, Modal, Button, Form, message, Input, Spin, Checkbox, 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(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={item} key={theData}>{theData}</Select.Option>
                    );
                };
                this.setState({
                    contactsOption: theOption
                });
            }.bind(this),
        });
    },
    getInitialState() {
        return {
            loading: false,
            companyOption: [],
            firstCheck: 0,
            secondCheck: 0,
            thirdCheck: 0,
            accelerated: 0,
            acceleratedDate: 0,
            visible: "visible"
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        if (!this.state.companyName || this.state.companyName === "") {
            message.warning('请先选择公司！');
            return;
        };
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.props.spinState(true);
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/techservice/patent/manageApplyPatent",
                    data: {
                        'uid': this.state.companyName,
                        'firstInventorIdNumber': values.firstInventorID,
                        'firstInventorName': values.firstInventorName,
                        'patentDes': values.intro,
                        'secondInventorName': values.secondInventorName,
                        'thirdInventorName': values.thirdInventorName,
                        'firstInventorIsPublish': this.state.firstCheck,
                        'secondInventorIsPublish': this.state.secondCheck,
                        'thirdInventorIsPublish': this.state.thirdCheck,
                        'patentProryStatementUrl': this.state.avatarUrl
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this)).always(function () {
                    this.props.spinState(false);
                    this.props.okClick();
                    this.props.form.resetFields();
                }.bind(this));
            }
        });
    },
    acceleratedCheck(e) {
        if (e.target.checked == true) {
            this.state.accelerated = 1;
            this.setState({ visible: "show" });
        } else if (e.target.checked == false) {
            this.state.accelerated = 0;
            this.setState({ visible: "visible" });
        }
    },
    acceleratedSelect(value) {
        this.state.acceleratedDate = value;
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 3 },
            wrapperCol: { span: 14 },
        };
        const _me = this;
        return (
            <Form horizontal onSubmit={this.handleSubmit} className="person-form">
                <FormItem
                    wrapperCol={{ span: 24 }}
                >
                    <Row>
                        <Col span={12}>
                            <span>选择公司:  </span>
                            <Select
                                placeholder="请选择公司"
                                notFoundContent="未找到公司"
                                style={{ width: 200 }}
                                showSearch
                                filterOption={companySearch}
                                value={this.state.uid}
                                onSelect={(e, n) => { this.setState({ uid: e }); this.getContactsList(e); }}>
                                {this.props.companyOption}
                            </Select>
                        </Col>
                        <Col span={12}>
                            <span>选择联系人:  </span>
                            <Select placeholder="请选择联系人" style={{ width: 200 }}
                                notFoundContent="未获取到联系人列表"
                                value={this.state.contacts}
                                onSelect={(e, n) => { this.setState({ contacts: e }); }}>
                                {this.state.contactsOption}
                            </Select>
                        </Col>
                    </Row>
                </FormItem>
                <FormItem
                    labelCol={{ span: 24 }}
                    wrapperCol={{ span: 18 }}
                    label="派单信息"
                >
                    {getFieldDecorator('intro', {
                        initialValue: this.state.intro || null
                    })(
                        <Input type="textarea"
                            placeholder="多行输入"
                            rows={6} />
                        )}
                </FormItem>
                <FormItem
                    labelCol={{ span: 0 }}
                    wrapperCol={{ span: 18 }}
                >
                    <span>是否加急：</span>
                    <Checkbox onChange={this.acceleratedCheck}></Checkbox>
                    <Select className={this.state.visible} placeholder="请选择加急时间" style={{ width: 120 }} onChange={this.acceleratedSelect}>
                        <Select.Option value="3">3天</Select.Option>
                        <Select.Option value="5">5天</Select.Option>
                        <Select.Option value="10">10天</Select.Option>
                        <Select.Option value="20">20天</Select.Option>
                    </Select>
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="选择咨询师"
                >
                    <Select placeholder="请选择咨询师" style={{ width: 200 }}
                        value={this.state.consultant}
                        onSelect={(e, n) => { this.setState({ consultant: e }); }}>
                        {this.props.authorOption}
                    </Select>
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="外包公司"
                >
                    {getFieldDecorator('intro', {
                        initialValue: this.state.intro || null
                    })(
                        <Input placeholder="输入外包公司" />
                        )}
                </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}
                            okClick={() => { this.props.closeAdd(false, true) }}
                            companyOption={this.props.companyOption}
                            authorOption={this.props.authorOption} />
                    </Modal>
                </Spin>
            </div>
        );
    },
});
export default PatentAdd;