import React from 'react';
import $ from 'jquery/src/ajax';
import {
	Icon,
	Modal,
	message,
	Spin,
	Upload,
	Input,
	Button,
	Form,
	Select
} from 'antd';
import { videoType } from '@/dataDic.js';
import { getVideoType} from '@/tools.js';	
import VideoComponent from '@/crop/video';
import Crop from '@/crop/cropBlock';

const NewDetailForm = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            auditStatus: 0,
            textFileList: [],
			videoImg: [],
			vedioUrl:[]
        };
    },
    loadData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +  '/api/admin/policy/detail' ,
            data: {
                id: id
            },
            success: function (data) {
                let thisData = data.data,provice=[];
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
				};
				let proArr=[];
                this.setState({
					id:thisData.id,
					data: thisData,
					auditStatus:thisData.auditStatus,
					videoUrl:[],
                    videoImg: thisData.titleImg ? splitUrl(thisData.titleImg, ',', globalConfig.avatarHost + '/upload') : []
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
    getPictureUrl(e) {
        this.setState({ pictureUrl: e });
    },
    handleSubmit(e,index) {
		e.preventDefault();
        this.props.form.validateFields((err, values) => {
			//url转化
            let thePictureUrl = [];
            if (this.state.pictureUrl.length) {
                let picArr = [];
                this.state.pictureUrl.map(function (item) {
                    if ( item.response && item.response.data && item.response.data.length ){
                        picArr.push(item.response.data);
                    }
                });
                thePictureUrl = picArr.join(",");
			};
            if (!err) {
                this.setState({
                    loading: true
                });
                $.ajax({
                    method: "post",
					dataType: "json",
					async:true,
                    url: this.props.data.id ? globalConfig.context + '/api/admin/policy/update' : globalConfig.context + '/api/admin/policy/apply',
                    data: {
						id: this.props.data.id,
						type:0,
						title: values.title,
                        author: values.author,
                        titleImg: thePictureUrl.length?thePictureUrl:'',
						content:this.state.edit.content,
						hot:values.hot,
						source:values.source,
						sourceUrl:values.sourceUrl,
						keyword:values.keyword,
						auditStatus:index,
                        summary: values.summary,
                    }
                }).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.state.edit={};
		this.props.closeDesc();
	},
    componentWillMount() {
        if (this.props.data.id) {
            this.loadData(this.props.data.id);
        } else {
			this.state.data = {};
			this.state.videoImg = [];
			this.state.videoUrl=[];
        };
    },
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
				this.state.data = {};
				this.state.videoImg = [];
				this.state.theData={};
				this.state.videoUrl=[];
            };
			this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.state.data || {};
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
		};
		const formItemLayouts = {
            labelCol: { span: 3 },
            wrapperCol: { span: 18 },
		};
        return (
			<div className='login'>
			<Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
				<Spin spinning={this.state.loading}>
					<div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="视频名称">
							{getFieldDecorator('title', {
								rules: [ { required: true, message: '此项为必填项!' } ],
								initialValue: theData.title
							})(<Input placeholder="请输入视频的名称" />)}
						</FormItem>
						<FormItem className="half-item" {...formItemLayout} label="视频类型">
							{getFieldDecorator('title1', {
								rules: [ { required: true, message: '此项为必填项!' } ],
								initialValue: theData.title1
							})(<Select placeholder="请选择视频类型">
									{videoType.map(item=>{
										return <Select.Option value={item.value}>{item.key}</Select.Option>
									})}
								</Select>)}
						</FormItem>
					</div>
					<div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="视频文件">
							< VideoComponent
								fileList={(e)=>{this.setState({videoUrl:e})}}
								dataUrl={{
									url:'/api/user/demand/uploadPicture',
									keyWord:'demand_picture',
									type:'video',
									page:1
								}}
								videoUrl={this.state.videoUrl}
								visible={this.props.visible}
							/>
						</FormItem>
						<FormItem className="half-item" {...formItemLayout} label="封面图片">
							<Crop 
								getAllImg={(e)=>{this.setState({videoImg:e})}}
								number = {1} 
								aspectRatio = {1/1}
								url ={globalConfig.context + '/api/user/uploadPicture64'}
								uploadData = {{sign:'user_picture'}} 
								urlArr = {this.state.videoImg} 
							/>
						</FormItem>
					</div>
					<div className="clearfix">
						<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="视频简介">
							{getFieldDecorator('summary', {
								initialValue: theData.summary
							})(<Input type="textarea" rows={4} placeholder="输入视频简介" />)}
						</FormItem>
					</div>
					<div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="发布人">
							<span>{theData.releaseStatus?'已发布':'管理员'}</span>
						</FormItem>
						{theData.createTime&&<FormItem className="half-item" {...formItemLayout} label="发布状态">
							<span>{theData.releaseStatus?'已发布':'未发布'}</span>
						</FormItem>}
					</div>
					<div className="clearfix">
						<FormItem wrapperCol={{ span: 12, offset: 3 }}>
							<Button className="set-submit" type="primary" htmlType="submit">
								保存草稿
							</Button>
							<Button
								className="set-submit"
								type="ghost"
								onClick={(e) => {
										this.handleSubmit(e,2)
								}}
							>
								发布视频
							</Button>
							<Button className="set-submit" type="ghost" onClick={this.cancelFun}>
								取消
							</Button>
						</FormItem>
					</div>
				</Spin>
			</Form>    
		</div>
        )
    }
}));
export default NewDetailForm;




