import React from 'react';
import $ from 'jquery/src/ajax';
import {
	Modal,
	message,
	Spin,
	Upload,
	Button,
	Form,
} from 'antd';
import {getProvince} from '@/NewDicProvinceList';
import '../policy/policy.less';
import {getReleaseStateList,getNewPosition,splitUrl,getNewType} from '@/tools';
const NewDetail = Form.create()(React.createClass({
    getInitialState() {
        return {
            visible: false,
            loading: false,
            auditStatus: 0,
            textFileList: [],
            videoFileList: [],
            pictureUrl: [],
        };
    },
    loadData(id) {
        this.setState({
            loading: true
        });
        $.ajax({
            method: "get",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +  '/api/admin/news/detail' ,
            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 = {};
                };
                document.querySelector('.txt').innerHTML='';
                let publishPagesArr = [];
                let pageList =  thisData.publishPages?(thisData.publishPages).split(','):[];
                pageList.map(item=>{
					publishPagesArr.push(item.publishPage)
                });
                let ProvinceS = thisData.provinceId,page=[], //getprovince
                    ProvinceCity = getProvince(ProvinceS);
                    let pages =thisData.newsPublishs ||[];
                    pages.map(item=>{
                        page.push(item.publishPage)
                    })
                    let eleDiv = document.createElement('div');
                    eleDiv.innerHTML=thisData.content;
                    document.querySelector('.txt').append(eleDiv);
                this.setState({
                    id:thisData.id,
                    publishPagesArr:publishPagesArr,
                    data: thisData,
                    page:page,
                    eleDiv:eleDiv,
                    ProvinceCity:ProvinceCity,
                    edit:{content:thisData.content},
                    auditStatus:thisData.auditStatus,
                    pictureUrl: thisData.titleImg ? splitUrl(thisData.titleImg, ',', globalConfig.avatarHost + '/upload') : []
                });
            }.bind(this),
        }).always(function () {
            this.setState({
                loading: false
            });
        }.bind(this));
    },
   
	cancelFun(){
		this.props.closeDesc();
    },
    //刷新发布
    updateFun(inex){
        this.setState({
            loading:true
        })
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +'/api/admin/news/updateStatus',
            data: {
                id: this.state.id,
                auditStatus:this.state.auditStatus,
                refresh:1
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('刷新成功！');
                this.setState({
                    loading: false,
                });
            } else {
                message.warning(data.error[0].message);
            };
            this.props.handOk();
           
        }.bind(this));
    },
    //撤销发布
    revokeFun(){
        this.setState({
            loading:true
        })
        $.ajax({
            method: "POST",
            dataType: "json",
            crossDomain: false,
            url: globalConfig.context +  '/api/admin/news/updateStatus',
            data: {
                id: this.state.id,
                auditStatus:4,
            }
        }).done(function (data) {
            if (!data.error.length) {
                message.success('撤销成功！');
                this.setState({
                    loading: false,
                });
            } else {
                message.warning(data.error[0].message);
            };
            this.props.handOk();
        }.bind(this));
    },
    componentWillMount() {
        if (this.props.data.id) {
            this.loadData(this.props.data.id);
           
        } else {
            this.state.data = {};
        };
    },
    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.pictureUrl = [];
            };
            this.props.form.resetFields();
        };
    },
    render() {
        const theData = this.state.data || {};
        const FormItem = Form.Item
        const formItemLayout = {
            labelCol: { span: 6 },
            wrapperCol: { span: 12 },
        };
        return (
			<div className='login'>
			<Form layout="horizontal" id="demand-form">
				<Spin spinning={this.state.loading}>
					<div className="clearfix">
                        <FormItem className="half-item" {...formItemLayout} label="新闻标题">
							<span>{theData.title}</span>
						</FormItem>
                        <FormItem className="half-item" {...formItemLayout} label="新闻类型">
							<span>{getNewType(theData.type)}</span>
						</FormItem>
						<FormItem className="half-item" {...formItemLayout} label="热门">
							<span>{theData.hot=='0'&&'否'}{theData.hot=='1'&&'是'}</span>
						</FormItem>
						<FormItem className="half-item" {...formItemLayout} label="新闻来源">
							<span>{theData.source}</span>
						</FormItem>
						
                        <FormItem className="half-item" {...formItemLayout} label="省市区">
							<span>{this.state.ProvinceCity}</span>
						</FormItem>
					</div>
                    <div>
                    <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="来源链接">
							<span>{theData.sourceUrl}</span>
						</FormItem>
                    </div>
                    <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻图片">
                        <div className="clearfix">
                                <Upload className="demandDetailShow-upload"
                                    listType="picture-card"
                                    fileList={this.state.pictureUrl}                               
                                    onPreview={(file) => {
                                        this.setState({
                                            previewImage: file.url || file.thumbUrl,
                                            previewVisible: true,
                                        });
                                    }} >
                                </Upload>
                                <Modal maskClosable={false} footer={null}
                                    visible={this.state.previewVisible}
                                    onCancel={() => { this.setState({ previewVisible: false }) }}>
                                    <img alt="" style={{ width: '100%' }} src={this.state.previewImage || ''} />
                                </Modal>
                            </div> 
					</FormItem>
                    <div className="clearfix">
						<FormItem className="half-item" {...formItemLayout} label="发布状态">
							<span>{getReleaseStateList(theData.auditStatus)}</span>
						</FormItem>
                       
                        <FormItem className="half-item" {...formItemLayout} label="发布时间">
                            <span>{theData.releaseDate?(new Date(theData.releaseDate)).toLocaleString():''}</span>
                        </FormItem>
					</div>
                    <div className="clearfix">
                        <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="发布位置">
                                <span>{getNewPosition(this.state.page)}</span>
						</FormItem>
                    </div>
                    <div className="clearfix">
						<FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻简介">
							<span>{theData.summary}</span>
						</FormItem>
                        <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="新闻正文">
							<div className="txt"></div>
						</FormItem>
					</div>
					<div className="clearfix">
						{this.props.data.auditStatus=='2'&&<FormItem wrapperCol={{ span: 12, offset: 3 }}>
							<Button className="set-submit" type="primary"  onClick={(e)=>{this.updateFun()}} >
								刷新发布
							</Button>
							<Button type="danger" style={{marginRight:20}} onClick={(e)=>{this.revokeFun()}}>
								撤销发布
							</Button>
							<Button className="set-submit" type="ghost" onClick={this.cancelFun}>
								取消
							</Button>
						</FormItem>}
					</div>
				</Spin>
			</Form>    
		</div>
        )
    }
}));
export default NewDetail;




