import React from 'react'
import { Upload,message } from 'antd';
import config from '@/webPublic/one_stop_public/config'
export default class ImgUploadCom extends React.Component {
    constructor(props){
        super(props)
        const value = props.value
         this.state = {
            url:value

        }
    }



      triggerChange = (changedValue) => {
        // Should provide an event to pass value to Form.
        const onChange = this.props.onChange;
        if (onChange) {
            onChange( changedValue);
        }
    }
    componentWillReceiveProps(nextProps) {
        // Should be a controlled component.
        if ('value' in nextProps) {
            const value = nextProps.value;
            this.setState({url:value});
        }
    }
    changeUrl = (info, key) => {

        if (info.file.status === 'done') {
          message.success(`图片上传成功`);

          if (!('value' in this.props)) {
            this.setState({url: info.file.response });
         }
         this.triggerChange(info.file.response);
        } else if (info.file.status === 'error') {
          message.error(`图片上传失败`);
        }
      }
    changePos=(obj)=>{


              if (!('value' in this.props)) {
                this.setState({...obj});
            }
            this.triggerChange({...obj});

    }
    render() {
        const{json,disabled}=this.props
        const {url}=this.state
        return (
            <Upload.Dragger disabled={disabled} accept={"image/*"} url={url} showUploadList={false} name="file" action={config.uploadUrl} onChange={this.changeUrl} multiple={false} style={{ padding: 0 }}>
            {url ? <img src={config.httpServer + (url )} style={{ height: json.height, width: json.width }} /> : <div style={{ height: json.height, width: json.width }}>

            </div>}

          </Upload.Dragger>
          );

    }
}
