import React from 'react';
import { Icon, Form, Button, Input, Select, Spin, Table, DatePicker, message, Modal, Checkbox, Upload } from 'antd';
import { cognizanceStateList } from '../../../dataDic.js';
import { getCognizanceState, beforeUpload, getBase64 } from '../../../tools.js';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';


const WebsiteChangeFrom = Form.create()(React.createClass({
    loadData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "post",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context + "/api/admin/techproject/techWebsiteDetail",
            data: {
                id: id || this.props.data.id
            },
            success: function (data) {
                this.state.data = data.data;
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getInitialState() {
        return {
            loading: false,
            stateOption: [],
            introduce: '',
            projectCheck: 0,
            subsidy: 0,
            stateTable: []
        };
    },
    componentWillMount() {
        let _me = this;
        cognizanceStateList.map(function (item) {
            _me.state.stateOption.push(
                <Select.Option value={item.value} key={item.key}>{item.key}</Select.Option>
            )
        });
        this.loadData();
    },
    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/admin/techproject/disposeTechWebsite",
                    data: {
                        id: this.state.data.id,
                        uid: this.state.data.uid,
                        department: values.department,
                        website: values.website,
                        accountNumber: values.accountNumber,
                        password: values.password,
                    }
                }).done(function (data) {
                    if (!data.error.length) {
                        message.success('保存成功！');
                        this.props.closeModal();
                        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));
            }
        });
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.loadData(nextProps.data.id);
        };
    },
    projectEstablishmentCheck(e) {
        if (e.target.checked == true) {
            this.state.projectCheck = 1;
        } else if (e.target.checked == false) {
            this.state.projectCheck = 0;
        };
    },
    subsidyCheck(e) {
        if (e.target.checked == true) {
            this.state.subsidy = 1;
        } else if (e.target.checked == false) {
            this.state.subsidy = 0;
        };
    },
    render() {
        const FormItem = Form.Item;
        const { getFieldDecorator } = this.props.form;
        const theData = this.state.data;
        const formItemLayout = {
            labelCol: { span: 4 },
            wrapperCol: { span: 6 },
        };
        const _me = this;
        if (this.state.data) {
            return (
                <Form onSubmit={this.handleSubmit} id="highTechApply-form">
                    <div className="half-item">
                        <FormItem
                            labelCol={{ span: 8 }}
                            wrapperCol={{ span: 12 }}
                            label="选择公司:"
                        >
                            <span>{theData.unitName}</span>
                        </FormItem>
                    </div>
                    <div className="clearfix">
                        <FormItem
                            {...formItemLayout}
                            label="科技部门名称:"
                        >
                            {getFieldDecorator('department', {
                                initialValue: theData.department
                            })(
                                <Input />
                                )}
                        </FormItem>
                    </div>
                    <div className="clearfix">
                        <FormItem
                            {...formItemLayout}
                            label="网址:"
                        >
                            {getFieldDecorator('website', {
                                initialValue: theData.website
                            })(
                                <Input />
                                )}
                        </FormItem>
                    </div>
                    <div className="clearfix">
                        <FormItem
                            {...formItemLayout}
                            label="账号:"
                        >
                            {getFieldDecorator('accountNumber', {
                                initialValue: theData.accountNumber
                            })(
                                <Input />
                                )}
                        </FormItem>
                    </div>
                    <div className="clearfix">
                        <FormItem
                            {...formItemLayout}
                            label="密码:"
                        >
                            {getFieldDecorator('password', {
                                initialValue: theData.password
                            })(
                                <Input />
                                )}
                        </FormItem>
                    </div>
                    <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 >
            );
        } else {
            return (<div></div>)
        };
    },
}));

const WebsiteChange = 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);
    },
    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=''
                    >
                        <WebsiteChangeFrom
                            visible={this.state.visible}
                            companyOption={this.props.companyOption}
                            data={this.props.data}
                            spinState={this.spinChange} closeModal={this.handleCancel} />
                    </Modal>
                </Spin>
            </div>
        );
    }
});

export default WebsiteChange;