import React from 'react';
import {
	Icon,
	Modal,
	message,
	Spin,
	Upload,
	Input,
	Button,
	Radio,
	Form
} from 'antd';
import {  splitUrl} from '@/tools.js';
const PicturesWall = React.createClass({
	getInitialState() {
		return {
			previewVisible: false,
			previewImage: '',
			fileList: [],
			tags: []
		};
	},
	handleCancel() {
		this.setState({ previewVisible: false });
	},
	handlePreview(file) {
		this.setState({
			previewImage: file.url || file.thumbUrl,
			previewVisible: true
		});
	},
	handleChange(info) {
		let fileList = info.fileList;
		this.setState({ fileList });
		this.props.fileList(fileList);
    },
	componentWillReceiveProps(nextProps) {
        this.state.fileList = nextProps.pictureUrl;
	},
	render() {
		const { previewVisible, previewImage, fileList } = this.state;
		const uploadButton = (
			
		);
		return (
			
				
					{fileList.length >= 1 ? null : uploadButton}
				
				
					 
				
			 
		);
	}
});
class BannerForm extends React.Component {
	constructor(props) {
        super(props);
		this.state = {
			loading: false,
            imgUrl: [],
            visible:false
		};
	}
	cancelFun(){
		this.props.closeDesc();
	}
    loadData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +  '/api/admin/banners/details' ,
            data: {
                id: id
            },
            success: function (data) {
                let thisData = data.data;
                if (!thisData) {
                    if (data.error && data.error.length) {
                        message.warning(data.error[0].message);
                    };
                    thisData = {};
                };
                this.setState({
                    id: thisData.id, //品类ID
                    data:thisData,
                    imgUrl: thisData.imgUrl ? splitUrl(thisData.imgUrl, ',', globalConfig.avatarHost + '/upload') : [],
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    }
	
    handleSubmit(e){
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            //url转化
            let thePictureUrl = [];
            if (this.state.imgUrl.length) {
                let picArr = [];
                this.state.imgUrl.map(function (item) {
                    if ( item.response && item.response.data && item.response.data.length ){
                        picArr.push(item.response.data);
                    }
                });
                thePictureUrl = picArr.join(",");
            }else{
                message.warning('请上传图片!');
                return false;
            };
            if (!err) {
                this.setState({
                    loading: true
                });
                $.ajax({
                    method: "post",
					dataType: "json",
					async:true,
                    url: this.props.data.id ? globalConfig.context + '/api/admin/banners/update' : globalConfig.context + '/api/admin/banners/save',
                    data: {
                        id:this.props.data.id,
                        text:values.text,
                        forwardUrl:values.forwardUrl,
                        apiUrl:values.apiUrl,
                        imgUrl:thePictureUrl.length?thePictureUrl:'',
                        deleteSign:false,
                        client:values.client
                    }
                }).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));
            }
        })
    }
    componentWillMount() {
        if (this.props.data&&this.props.data.id) {
            this.loadData(this.props.data.id);
        } else {
			this.state.data = {};
			this.state.imgUrl = [];
        };
    }
    componentWillReceiveProps(nextProps) {
        if (!this.props.visible && nextProps.visible) {
            this.state.textFileList = [];
            this.state.videoFileList = [];
            if (nextProps.data.id) {
                this.loadData(nextProps.data.id);
            } else {
				this.state.data = {};
				this.state.imgUrl = [];
            };
			this.props.form.resetFields();
        };
    }
	render() {
        const { getFieldDecorator } = this.props.form;
        const FormItem = Form.Item;
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
		};
        let theData = this.state.data||{};
		return (
			
		);
	}
}
export default Form.create()(BannerForm);