@kareem you have to declare the index in the global scope (so it will be available for the button_click function. Something like:
import WixData from 'wix-data';
let index;
$w.onReady (function () {
$w("#gallery1").onItemClicked ((event)=>{
index = event.itemIndex;
//.....