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(theUid) {
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/customer/findAllContacts",
            data: {
                uid: theUid || this.state.uid
            },
            success: function (data) {
                let theOption = [];
                if (!data.data) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                        return;
                    };
                };
                for (let item in data.data) {
                    let theData = data.data[item];
                    theOption.push(
                        <Select.Option value={item} key={theData.name}>{theData.name}</Select.Option>
                    );
                };
                this.setState({
                    contactsOption: theOption
                });
            }.bind(this),
        });
    },
    getInitialState() {
        return {
            loading: false,
            companyOption: [],
            accelerated: 0,
            acceleratedDate: 0,
            visible: "visible"
        };
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!values.uid || values.uid === "") {
                message.warning('请先选择公司！');
                return;
            };
            if (!err) {
                this.props.spinState(true);
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/admin/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
                        "comment": values.workIssue, // 派单信息, size 0-128
                        "inUrgent": values.inUrgent, //  加急天数,
                        "salesman": values.salesman,//营销员
                        //"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));
            }
        });
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const formItemLayout = {
            labelCol: { span: 2 },
            wrapperCol: { span: 14 },
        };
        const _me = this;
        return (
            <Form horizontal onSubmit={this.handleSubmit} id="CopyrightDesc-form">
                <Row>
                    <Col span={12}>
                        <span>选择公司:  </span>
                        {getFieldDecorator('uid')(
                            <Select
                                placeholder="请选择公司"
                                notFoundContent="未找到公司"
                                style={{ width: 260 }}
                                showSearch
                                filterOption={companySearch}
                                onSelect={(e, n) => { this.getContactsList(e); }}>
                                {this.props.companyOption}
                            </Select>
                        )}
                    </Col>
                    <Col span={12}>
                        <span>选择联系人:  </span>
                        {getFieldDecorator('contact')(
                            <Select placeholder="请选择联系人" style={{ width: 260 }}
                                notFoundContent="未获取到联系人列表"
                                onSelect={(e, n) => { this.setState({ contact: e }); }}>
                                {this.state.contactsOption}
                            </Select>
                        )}
                    </Col>
                </Row>
                <FormItem
                    labelCol={{ span: 24 }}
                    wrapperCol={{ span: 18 }}
                    label="派单信息"
                >
                    {getFieldDecorator('workIssue')(
                        <Input type="textarea"
                            placeholder="多行输入"
                            rows={6} />
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="是否加急"
                >
                    {getFieldDecorator('inUrgent', {
                        initialValue: '0'
                    })(
                        <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.Option value="35">31-35个工作日</Select.Option>
                        </Select>
                        )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="外包公司"
                >
                    {getFieldDecorator('outsource')(
                        <Input placeholder="输入外包公司" />
                    )}
                </FormItem>
                <FormItem
                    {...formItemLayout}
                    label="营销员"
                >
                    {getFieldDecorator('salesman', {
                        rules: [{ required: true, message: '请选择一个营销员!' }]
                    })(
                        <Select
                            placeholder="请选择加急时间"
                            style={{ width: 200 }}>
                            {this.props.salesmanOption}
                        </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>
                <Modal maskClosable={false} title="申请新软著"
                    visible={this.state.visible}
                    onCancel={this.handleCancel}
                    width='800px'
                    footer='' >
                    <Spin spinning={this.state.loading} className='spin-box'>
                        <PatentAddFrom
                            spinState={this.spinChange}
                            closeModal={this.handleCancel}
                            okClick={() => { this.props.closeAdd(false, true) }}
                            companyOption={this.props.companyOption}
                            salesmanOption={this.props.salesmanOption}
                            consultantOption={this.props.consultantOption} />
                    </Spin>
                </Modal>
            </div>
        );
    },
});
export default PatentAdd;