<script>
    (() => {
        const ctx = window.factoryCrud;
        if (!ctx) {
            return;
        }

        const updateUsuarioPhotoPreview = (source) => {
            const preview = document.getElementById(ctx.selectors.avatarPreview);
            const placeholder = document.getElementById(ctx.selectors.avatarPlaceholder);
            if (!preview || !placeholder) {
                return;
            }

            if (source) {
                preview.src = source;
                preview.classList.remove('d-none');
                placeholder.classList.add('d-none');
                return;
            }

            preview.removeAttribute('src');
            preview.classList.add('d-none');
            placeholder.classList.remove('d-none');
        };

        ctx.hooks.onFillFormValues.push(({ extras }) => {
            const avatarInput = document.getElementById(ctx.selectors.avatarInput);
            if (avatarInput) {
                avatarInput.value = '';
            }
            updateUsuarioPhotoPreview(extras.usuarioPhotoUrl || '');
        });

        ctx.hooks.afterBoot.push(() => {
            const avatarInput = document.getElementById(ctx.selectors.avatarInput);
            const avatarPickButton = document.getElementById(ctx.selectors.avatarPickButton);
            if (!avatarInput || !avatarPickButton) {
                return;
            }

            avatarPickButton.addEventListener('click', () => avatarInput.click());
            avatarInput.addEventListener('change', () => {
                const [file] = avatarInput.files || [];
                if (!file) {
                    updateUsuarioPhotoPreview('');
                    return;
                }

                const reader = new FileReader();
                reader.onload = (event) => updateUsuarioPhotoPreview(event.target?.result || '');
                reader.readAsDataURL(file);
            });
        });
    })();
</script><?php /**PATH C:\xampp\htdocs\almacen2\resources\views/administracion/crud/partials/comun/scripts/funcionalidades-modal/avatar-personal.blade.php ENDPATH**/ ?>