import React from 'react';
import $ from 'jquery/src/ajax';
import {
	message,
	Spin,
	Input,
	Button,
	Form,
} from 'antd';
 
const NewDetailForm = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            textFileList: [],
            videoFileList: [],
        };
    },
    handleSubmit(e,index) {
		e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                this.setState({
                    loading: true
				});
                $.ajax({
                    method: "get",
					dataType: "json",
                    url: globalConfig.context +"/api/admin/message/mobileMessage",
                    data: {
						messageCode: values.messageCode,
                    }
                }).done(function (data) {
                    this.setState({
                        loading: false
					});
                    if (!data.error.length) {
                        message.success('推送成功！');
                        this.setState({
                            visible: false
                        });
                        this.props.handOk();
                    } else {
                        message.warning(data.error[0].message);
                    }
                }.bind(this));
            }
        });
	},
	cancelFun(){
		this.props.closeDesc(false,false);
	},
    componentWillMount() {
        if (this.props.data.id) {
           
        } else {
			this.state.visible=true;
			this.state.data = {};
        };
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.textFileList = [];
            this.state.videoFileList = [];
            if (nextProps.data.id) {
               
            } else {
				this.state.visible=true;
				this.state.data = {};
            };
			this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.props.data || {};
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item;
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
		};
        return (
			<div className='login'>
			<Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,1)}} id="demand-form">
				<Spin spinning={this.state.loading}>
					<div className="clearfix">
						<FormItem  {...formItemLayout} label="发送对象">
                            <span>所有用户</span>
                        </FormItem>	
					</div>
					<div>
						<FormItem {...formItemLayout} label="创建人">
							<span>{theData.admin?theData.admin:'管理员'}</span>
						</FormItem>	
					</div>
					<div className="clearfix">
						<FormItem  {...formItemLayout} label="短信模板">
							{getFieldDecorator('messageCode', {
								rules: [ { required: true, message: '此项为必填项!' } ],
								initialValue: theData.messageCode
							})(<Input placeholder="请输入短信模板" />)}
						</FormItem>
					</div>
					<div className="clearfix">
						<FormItem wrapperCol={{ span: 12, offset: 6 }}>
						    <Button className="set-submit" type="primary" htmlType="submit">
								推送
							</Button>
							<Button className="set-submit" type="ghost" onClick={this.cancelFun}>
								取消
							</Button>
						</FormItem>
					</div>
				</Spin>
			</Form>    
		</div>
        )
    }
}));
export default NewDetailForm;




