import { Component, createRef } from 'react';
import Box from '@mui/material/Box';
import Button from '@mui/material/Button';
import Chip from '@mui/material/Chip';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogTitle from '@mui/material/DialogTitle';
import IconButton from '@mui/material/IconButton';
import ContentPasteIcon from '@mui/icons-material/ContentPaste';
import UploadFileIcon from '@mui/icons-material/UploadFile';
import ImageSearchIcon from '@mui/icons-material/ImageSearch';
import DeleteOutlineOutlinedIcon from '@mui/icons-material/DeleteOutlineOutlined';
import KeyboardArrowDownIcon from '@mui/icons-material/KeyboardArrowDown';
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';
import { enqueueSnackbar } from 'notistack';
import { article as articleApi } from '../../api';
import artikelAutomat from '../../automats/artikelAutomat';
import { requireArray, requireObject } from '../../utils/apiShape';
import {
articleWithPhotoOrder,
articleWithUploadedPhoto,
articleWithoutPhoto,
formatPhotoDimensions,
formatPhotoSizeKb,
sortedPhotos,
} from '../../utils/articleDetail';
import { notifyError } from '../../utils/notify';
import { readClipboardImage } from '../../utils/pasteImage';
import { prepareArticlePhoto } from '../../utils/prepareArticlePhoto';
import ArticlePhoto from './ArticlePhoto';
import ArticleImageSearchDialog from './ArticleImageSearchDialog';
const overlayChipSx = {
height: 22,
bgcolor: 'rgba(0, 0, 0, 0.65)',
color: '#fff',
'& .MuiChip-label': { px: 0.75, fontSize: 11 },
};
const overlayIconSx = {
color: '#fff',
bgcolor: 'rgba(0, 0, 0, 0.55)',
'&:hover': { bgcolor: 'rgba(0, 0, 0, 0.75)' },
'&.Mui-disabled': { color: 'rgba(255,255,255,0.35)', bgcolor: 'rgba(0,0,0,0.35)' },
};
class ArticlePhotosDialog extends Component {
constructor(props) {
super(props);
this.fileInputRef = createRef();
this.state = { busy: false, searchOpen: false };
this.handleClose = this.handleClose.bind(this);
this.handlePasteClick = this.handlePasteClick.bind(this);
this.handleUploadClick = this.handleUploadClick.bind(this);
this.handleSearchOpen = this.handleSearchOpen.bind(this);
this.handleSearchClose = this.handleSearchClose.bind(this);
this.handleFileChange = this.handleFileChange.bind(this);
this.uploadRawImage = this.uploadRawImage.bind(this);
}
handleClose() {
if (this.state.busy) return;
this.props.onClose();
}
async uploadRawImage(rawBlob) {
const { article: articleData, onArticleUpdated } = this.props;
this.setState({ busy: true });
try {
const file = await prepareArticlePhoto(rawBlob);
const uploadStarted = performance.now();
const res = await articleApi.uploadImage(articleData.sku, file);
console.log('[article-photo] upload', {
uploadMs: Math.round(performance.now() - uploadStarted),
bytes: file.size,
sku: articleData.sku,
});
requireObject(res, 'POST /api/article/:sku/image');
requireObject(res.image, 'image');
const updated = articleWithUploadedPhoto(articleData, res.image);
artikelAutomat.actions.setArticle(updated);
onArticleUpdated?.(updated);
} catch (err) {
notifyError(err);
} finally {
this.setState({ busy: false });
}
}
async handlePasteClick() {
if (this.state.busy) return;
try {
const image = await readClipboardImage();
if (image) {
await this.uploadRawImage(image);
} else {
enqueueSnackbar('Kein Bild in der Zwischenablage gefunden.', { variant: 'info' });
}
} catch (err) {
console.warn('Clipboard read error:', err);
enqueueSnackbar('Zugriff auf Zwischenablage nicht möglich.', { variant: 'warning' });
}
}
handleUploadClick() {
if (this.state.busy) return;
this.fileInputRef.current?.click();
}
handleSearchOpen() {
if (this.state.busy) return;
this.setState({ searchOpen: true });
}
handleSearchClose() {
this.setState({ searchOpen: false });
}
handleFileChange(event) {
const file = event.target.files?.[0];
event.target.value = '';
if (!file) return;
void this.uploadRawImage(file);
}
async deletePhoto(fullHash) {
const { article: articleData, onArticleUpdated } = this.props;
this.setState({ busy: true });
try {
await articleApi.deleteImage(articleData.sku, fullHash);
const updated = articleWithoutPhoto(articleData, fullHash);
artikelAutomat.actions.setArticle(updated);
onArticleUpdated?.(updated);
} catch (err) {
notifyError(err);
} finally {
this.setState({ busy: false });
}
}
async movePhoto(fullHash, delta) {
const { article: articleData, onArticleUpdated } = this.props;
const photos = sortedPhotos(articleData);
const index = photos.findIndex((row) => row.fullHash === fullHash);
const target = index + delta;
if (index < 0 || target < 0 || target >= photos.length) return;
const next = [...photos];
const [row] = next.splice(index, 1);
next.splice(target, 0, row);
const fullHashes = next.map((item) => item.fullHash);
this.setState({ busy: true });
try {
const res = await articleApi.orderImages(articleData.sku, fullHashes);
requireObject(res, 'PUT /api/article/:sku/images/order');
requireArray(res.fullHashes, 'fullHashes');
const updated = articleWithPhotoOrder(articleData, res.fullHashes);
artikelAutomat.actions.setArticle(updated);
onArticleUpdated?.(updated);
} catch (err) {
notifyError(err);
} finally {
this.setState({ busy: false });
}
}
renderPhotoTile(photo, index, total) {
const { busy } = this.state;
const hasDimensions = photo.fullW != null && photo.fullH != null;
const hasSize = photo.fullSize != null;
return (
{hasDimensions ? (
) : null}
{hasSize ? (
) : null}
void this.movePhoto(photo.fullHash, -1)}
sx={overlayIconSx}
>
= total - 1}
onClick={() => void this.movePhoto(photo.fullHash, 1)}
sx={overlayIconSx}
>
void this.deletePhoto(photo.fullHash)}
sx={overlayIconSx}
>
);
}
render() {
const { open, article: articleData } = this.props;
const { busy } = this.state;
const photos = sortedPhotos(articleData);
return (
<>
>
);
}
}
export default ArticlePhotosDialog;