import React from "react";
import ZpureComponent from "../ZpureComponent";
import PropTypes from "prop-types";
import classNames from "classnames";
export class ZbgImage extends ZpureComponent {
	static propTypes = {
		url: PropTypes.string,
		position: PropTypes.string, // top | center
		className: PropTypes.string,
		style: PropTypes.object,
		onClick: PropTypes.func,
	};
	static defaultProps = {
		position: "center",
	};
	render() {
		const baseClassname = "z-bg-image-box";
		const flexClassname = "z-flex-items-center";
		const otherClassname = this.props.className || "";
		const noUrlClassname = this.props.url ? "" : "no-url";
		let boxclassName = classNames(baseClassname, flexClassname, otherClassname, noUrlClassname);
		return (
			<div
				className={boxclassName}
				onClick={e => {
					this.props.onClick && this.props.onClick(e);
				}}
				style={this.props.style}
			>
				{this.props.url ? null : <span>无图片</span>}
				<div
					className={classNames("z-bg-image", `is-bg-${this.props.position}`)}
					style={{ backgroundImage: `url(${this.props.url})` }}
				/>
				{this.props.children}
			</div>
		);
	}
}

export default ZbgImage;
