| 1 |
document.addEventListener("DOMContentLoaded", function () { |
| 2 |
const button = document.getElementById( |
| 3 |
"sync_basalam_refresh_vendor_status_btn" |
| 4 |
); |
| 5 |
|
| 6 |
if (!button) return; |
| 7 |
|
| 8 |
const showToast = (type, message) => { |
| 9 |
if (window.BasalamToast && typeof window.BasalamToast[type] === "function") { |
| 10 |
window.BasalamToast[type](message); |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
window.alert(message); |
| 15 |
}; |
| 16 |
|
| 17 |
button.addEventListener("click", function () { |
| 18 |
if (button.disabled) return; |
| 19 |
|
| 20 |
const originalText = button.textContent; |
| 21 |
const formData = new FormData(); |
| 22 |
formData.append("action", "sync_basalam_refresh_vendor_status"); |
| 23 |
formData.append("_wpnonce", button.dataset.nonce || ""); |
| 24 |
|
| 25 |
button.disabled = true; |
| 26 |
button.textContent = "در حال بررسی..."; |
| 27 |
|
| 28 |
fetch(window.ajaxurl, { |
| 29 |
method: "POST", |
| 30 |
credentials: "same-origin", |
| 31 |
body: formData, |
| 32 |
}) |
| 33 |
.then((response) => response.json()) |
| 34 |
.then((response) => { |
| 35 |
const data = response.data || {}; |
| 36 |
|
| 37 |
if (!response.success) { |
| 38 |
throw new Error(data.message || "بررسی وضعیت غرفه نا� |
| 39 |
وفق بود."); |
| 40 |
} |
| 41 |
|
| 42 |
const isActive = data.state && data.state.mode === "active"; |
| 43 |
showToast( |
| 44 |
isActive ? "success" : "warning", |
| 45 |
data.message || |
| 46 |
(isActive |
| 47 |
? "غرفه فعال شد." |
| 48 |
: "غرفه هنوز در باسلا� |
| 49 |
غیرفعال است.") |
| 50 |
); |
| 51 |
|
| 52 |
window.setTimeout(function () { |
| 53 |
window.location.reload(); |
| 54 |
}, 600); |
| 55 |
}) |
| 56 |
.catch((error) => { |
| 57 |
showToast("error", error.message || "خطا در ارتباط با سرور."); |
| 58 |
}) |
| 59 |
.finally(() => { |
| 60 |
button.disabled = false; |
| 61 |
button.textContent = originalText; |
| 62 |
}); |
| 63 |
}); |
| 64 |
}); |
| 65 |
|