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

const CopyrightDescForm = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            stateOption: [],
            contactsOption: [],
            stateColumns: [{
                title: '申请状态',
                dataIndex: 'state',
                key: 'state',
                render: (text) => { return getCopyrightState(text) }
            }, {
                title: '处理时间',
                dataIndex: 'recordTimeFormattedDate',
                key: 'recordTimeFormattedDate',
            }, {
                title: '负责人',
                dataIndex: 'principal',
                key: 'principal',
            }, {
                title: '操作人',
                dataIndex: 'operator',
                key: 'operator',
            }, {
                title: '备注',
                dataIndex: 'comment',
                key: 'comment',
            }]
        };
    },
    componentWillMount() {
        let _me = this;
        copyrightStateList.map(function (item) {
            _me.state.stateOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
    },
    loadData(uid, cid, year) {
        this.setState({
            loading: true
        });
        $.when($.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/cognizanceLog",
            data: {
                pid: this.state.xid
            }
        }), $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/getContacts"
        }), $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/getContacts",
        })).done((data1, data2, data3) => {
            //状态流转table
            if (data1[0].error.length) {
                message.warning(data1[0].error[0].message);
                return;
            };
            let _me = this;
            this.state.stateTable = [];
            data1[0].data.map(function (item) {
                this.state.stateTable.push({
                    key: i,
                    recordTimeFormattedDate: item.recordTimeFormattedDate,
                    state: item.state,
                    principal: item.principal,
                    operator: item.operator,
                    comment: item.comment
                });
            });
            //获取联系人
            if (data2[0].error.length) {
                message.warning(data2[0].error[0].message);
                return;
            };
            this.state.contactsOption = [];
            for (let item in data2[0].data) {
                let theData = data2[0].data[item];
                contactsOption.push(
                    <Select.Option value={item} key={theData}>{theData}</Select.Option>
                );
            };
            //获取详细数据
            if (data3[0].error.length || !data3[0].data) {
                return;
            };
            let thisdata = data3[0].data;
            this.setState({

            });
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    componentWillReceiveProps(nextProps) {
        if (!this.state.visible && nextProps.showDesc) {
            this.loadData();
        };
        this.state.visible = nextProps.showDesc;
    },
    handleSubmit(e) {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (values.state || values.principal || values.recordTime || values.comment) {
                if (!values.state && !values.principal && !values.recordTime) {
                    message.warning("请填写完整的状态流转信息！");
                    return;
                };
            };
            if (!err) {
                this.props.spinState(true);
                $.ajax({
                    method: "POST",
                    dataType: "json",
                    crossDomain: false,
                    url: globalConfig.context + "/api/admin/disposeCognizanceDetail",
                    data: {
                        id: this.props.data.cid,
                        uid: this.props.data.uid,
                        technicalField1: values.technicalField[0],
                        technicalField2: values.technicalField[1],
                        technicalField3: values.technicalField[2],
                        basicResearchCost: values.basicResearchCost,
                        contacts: values.contacts,
                        accident: values.accident,
                        certificateNumber: values.certificateNumber,
                        recordTimeFormattedDate: values.recordTime ? values.recordTime.format("YYYY-MM-DD") : undefined,
                        state: values.state,
                        principal: values.principal,
                        comment: values.comment,
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                        this.props.clickOk();
                        this.props.form.resetFields();
                        this.state.targetKeys = [];
                        this.state.selectedKeys = [];
                        this.props.spinState(false);
                    } else {
                        message.warning(data.error[0].message);
                        this.props.spinState(false);
                    }
                }.bind(this));
            }
        });
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        //const theData = this.state.data;
        const theData = {};
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
        };
        const _me = this;
        return (
            <Form onSubmit={this.handleSubmit} id="CopyrightDesc-form">
                <Row className="express-desc-row">
                    <Col span={4}>公司名称</Col>
                    <Col span={4}>{theData.nuitName}</Col>
                    <Col span={4}>公司组织机构代码</Col>
                    <Col span={4}>{theData.unitNumber}</Col>
                </Row>
                <Row className="express-desc-row">
                    <Col span={4}>公司地址</Col>
                    <Col span={4}>{theData.address}</Col>
                    <Col span={4}>联系人</Col>
                    <Col span={4}>
                        {getFieldDecorator('contacts', {
                            rules: [{ required: true, message: '此项为必填项!' }],
                            initialValue: theData.contacts
                        })(
                            <Select
                                style={{ display: 'inline-block', width: '200px' }}
                                placeholder='选择一个联系人'>
                                {this.state.contactsOption}
                            </Select>
                            )}
                    </Col>
                </Row>
                <Row className="express-desc-row">
                    <Col span={4}>编号</Col>
                    <Col span={4}>xxxxxx</Col>
                    <Col span={4}>外包公司</Col>
                    <Col span={4}>
                        {getFieldDecorator('contacts', {
                            initialValue: theData.contacts
                        })(
                            <Input placeholder="请输入外包公司" />
                            )}
                    </Col>
                </Row>
                <Row className="express-desc-row">
                    <Col span={3}>派单详情</Col>
                    <Col span={16}>
                        {getFieldDecorator('contacts', {
                            initialValue: theData.contacts
                        })(
                            <Input type="textarea"
                                placeholder="多行输入"
                                rows={6} />
                            )}
                    </Col>
                </Row>
                <Row className="express-desc-row">
                    <Col span={3}>备注</Col>
                    <Col span={16}>
                        {getFieldDecorator('contacts', {
                            initialValue: theData.contacts
                        })(
                            <Input type="textarea"
                                placeholder="多行输入"
                                rows={6} />
                            )}
                    </Col>
                </Row>
                <Row className="express-desc-row">
                    <Col span={3}>软著名称</Col>
                    <Col span={6}>
                        {getFieldDecorator('contacts', {
                            initialValue: theData.contacts
                        })(
                            <Input placeholder="请输入软著名称" />
                            )}
                    </Col>
                </Row>
                <Row className="express-desc-row">
                    <Col span={3}>下证时间(预计)</Col>
                    <Col span={6}>
                        {getFieldDecorator('contacts', {
                            initialValue: theData.contacts
                        })(
                            <DatePicker />
                            )}
                    </Col>
                </Row>
                <p>状态流转记录: </p>
                <Table style={{ margin: '10px 0' }}
                    pagination={false}
                    dataSource={this.state.stateTable}
                    columns={this.state.stateColumns} />
                <div className="clearfix">
                    <FormItem className="third-item"
                        {...formItemLayout}
                        label="状态流转"
                    >
                        {getFieldDecorator('state')(
                            <Select placeholder="选择一个状态">{this.state.stateOption}</Select>
                        )}
                    </FormItem>
                    <FormItem className="third-item"
                        labelCol={{ span: 6 }}
                        wrapperCol={{ span: 12 }}
                        label="处理时间"
                    >
                        {getFieldDecorator('recordTime')(
                            <DatePicker />
                        )}
                    </FormItem>
                    <FormItem className="third-item"
                        {...formItemLayout}
                        label="负责人"
                    >
                        {getFieldDecorator('principal')(
                            <Select placeholder="选择负责人">{this.props.authorOption}</Select>
                        )}
                    </FormItem>
                </div>
                <FormItem
                    labelCol={{ span: 2 }}
                    wrapperCol={{ span: 12 }}
                    label="备注"
                >
                    {getFieldDecorator('comment')(
                        <Input />
                    )}
                </FormItem>
                <FormItem style={{ marginTop: '20px' }}>
                    <Button className="set-submit" type="primary" htmlType="submit">保存</Button>
                    <Button type="ghost" style={{ marginLeft: '20px' }} onClick={this.props.closeModal}>取消</Button>
                </FormItem>
            </Form>
        );
    }
}));

const CopyrightDesc = React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false
        };
    },
    componentWillReceiveProps(nextProps) {
        this.state.visible = nextProps.showDesc
    },
    showModal() {
        this.setState({
            visible: true,
        });
    },
    handleOk() {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false, true);
    },
    handleCancel(e) {
        this.setState({
            visible: false,
        });
        this.props.closeDesc(false);
    },
    spinChange(e) {
        this.setState({
            loading: e
        });
    },
    render() {
        return (
            <div className="patent-addNew">
                <Spin spinning={this.state.loading} className='spin-box'>
                    <Modal title="软著详情" visible={this.state.visible}
                        onOk={this.handleOk} onCancel={this.handleCancel}
                        width='800px'
                        footer=''
                    >
                        <CopyrightDescForm
                            visible={this.state.visible}
                            authorOption={this.props.authorOption}
                            data={this.props.data}
                            spinState={this.spinChange}
                            closeModal={this.handleCancel}
                            clickOk={this.handleOk} />
                    </Modal>
                </Spin>
            </div>
        );
    }
});
export default CopyrightDesc;
