| 1 |
jQuery(document).ready(function ($) { |
| 2 |
// Toggle dropdown on arrow click |
| 3 |
$(document).on("click", ".basalam-dropdown-arrow-btn", function (e) { |
| 4 |
e.preventDefault(); |
| 5 |
e.stopPropagation(); |
| 6 |
const $wrapper = $(this).closest(".basalam-orders-fetch-wrapper"); |
| 7 |
const $dropdown = $wrapper.find(".basalam-orders-fetch-dropdown"); |
| 8 |
$dropdown.toggle(); |
| 9 |
}); |
| 10 |
|
| 11 |
// Close dropdown when clicking outside |
| 12 |
$(document).on("click", function (e) { |
| 13 |
if (!$(e.target).closest(".basalam-orders-fetch-wrapper").length) { |
| 14 |
$(".basalam-orders-fetch-dropdown").hide(); |
| 15 |
} |
| 16 |
}); |
| 17 |
|
| 18 |
// Fetch orders with default 7 days |
| 19 |
$(document).on("click", ".basalam-fetch-orders-btn", function (e) { |
| 20 |
e.preventDefault(); |
| 21 |
const $btn = $(this); |
| 22 |
const $wrapper = $btn.closest(".basalam-orders-fetch-wrapper"); |
| 23 |
const nonce = $btn.data("nonce"); |
| 24 |
|
| 25 |
$btn.prop("disabled", true).find(".basalam-btn-text").text("در حال شروع..."); |
| 26 |
|
| 27 |
$.ajax({ |
| 28 |
url: ajaxurl, |
| 29 |
type: "POST", |
| 30 |
data: { |
| 31 |
action: "add_unsync_orders_from_basalam", |
| 32 |
_wpnonce: nonce, |
| 33 |
days: 7, |
| 34 |
}, |
| 35 |
success: function (response) { |
| 36 |
if (response.success) { |
| 37 |
window.BasalamToast.success(response.data?.message || "ع� |
| 38 |
لیات با � |
| 39 |
وفقیت انجا� |
| 40 |
شد."); |
| 41 |
location.reload(); |
| 42 |
} else { |
| 43 |
window.BasalamToast.error(response.data?.message || "خطایی رخ داده است."); |
| 44 |
$btn.prop("disabled", false).find(".basalam-btn-text").text("بررسی سفارشات باسلا� |
| 45 |
"); |
| 46 |
} |
| 47 |
}, |
| 48 |
error: function (jqXHR) { |
| 49 |
let message = "خطایی در ارتباط با سرور رخ داد."; |
| 50 |
try { |
| 51 |
const response = JSON.parse(jqXHR.responseText); |
| 52 |
message = response.data?.message || message; |
| 53 |
} catch (e) {} |
| 54 |
window.BasalamToast.error(message); |
| 55 |
$btn.prop("disabled", false).find(".basalam-btn-text").text("بررسی سفارشات باسلا� |
| 56 |
"); |
| 57 |
}, |
| 58 |
}); |
| 59 |
}); |
| 60 |
|
| 61 |
// Submit fetch orders with custom days |
| 62 |
$(document).on("click", ".basalam-dropdown-submit", function (e) { |
| 63 |
e.preventDefault(); |
| 64 |
|
| 65 |
const $btn = $(this); |
| 66 |
const $wrapper = $btn.closest(".basalam-orders-fetch-wrapper"); |
| 67 |
const $dropdown = $wrapper.find(".basalam-orders-fetch-dropdown"); |
| 68 |
const $daysInput = $dropdown.find(".basalam-dropdown-input"); |
| 69 |
const $mainBtn = $wrapper.find(".basalam-fetch-orders-btn"); |
| 70 |
const nonce = $btn.data("nonce"); |
| 71 |
let days = parseInt($daysInput.val()); |
| 72 |
|
| 73 |
// Validate days |
| 74 |
if (isNaN(days) || days < 1 || days > 30) { |
| 75 |
window.BasalamToast.warning("عدد وارد شده باید بین ۱ تا ۳۰ باشد."); |
| 76 |
$daysInput.focus(); |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
$btn.text("در حال بررسی...").prop("disabled", true); |
| 81 |
$mainBtn.prop("disabled", true).find(".basalam-btn-text").text("در حال بررسی..."); |
| 82 |
|
| 83 |
$.ajax({ |
| 84 |
url: ajaxurl, |
| 85 |
type: "POST", |
| 86 |
data: { |
| 87 |
action: "add_unsync_orders_from_basalam", |
| 88 |
_wpnonce: nonce, |
| 89 |
days: days, |
| 90 |
}, |
| 91 |
success: function (response) { |
| 92 |
if (response.success) { |
| 93 |
window.BasalamToast.success(response.data?.message || "ع� |
| 94 |
لیات با � |
| 95 |
وفقیت انجا� |
| 96 |
شد."); |
| 97 |
location.reload(); |
| 98 |
} else { |
| 99 |
window.BasalamToast.error(response.data?.message || "خطایی رخ داده است."); |
| 100 |
$btn.text("بررسی سفارشات").prop("disabled", false); |
| 101 |
$mainBtn.prop("disabled", false).find(".basalam-btn-text").text("بررسی سفارشات باسلا� |
| 102 |
"); |
| 103 |
} |
| 104 |
}, |
| 105 |
error: function (jqXHR) { |
| 106 |
let message = "خطایی در ارتباط با سرور رخ داد."; |
| 107 |
try { |
| 108 |
const response = JSON.parse(jqXHR.responseText); |
| 109 |
message = response.data?.message || message; |
| 110 |
} catch (e) {} |
| 111 |
window.BasalamToast.error(message); |
| 112 |
$btn.text("بررسی سفارشات").prop("disabled", false); |
| 113 |
$mainBtn.prop("disabled", false).find(".basalam-btn-text").text("بررسی سفارشات باسلا� |
| 114 |
"); |
| 115 |
}, |
| 116 |
}); |
| 117 |
}); |
| 118 |
|
| 119 |
// Cancel fetch orders |
| 120 |
$(document).on("click", ".basalam-cancel-orders-btn", function (e) { |
| 121 |
e.preventDefault(); |
| 122 |
|
| 123 |
const $btn = $(this); |
| 124 |
const nonce = $btn.data("nonce"); |
| 125 |
|
| 126 |
if (!confirm("آیا � |
| 127 |
ط� |
| 128 |
ئن هستید که � |
| 129 |
یخواهید ه� |
| 130 |
گا� |
| 131 |
سازی سفارشات را لغو کنید؟")) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$btn.prop("disabled", true); |
| 136 |
|
| 137 |
$.ajax({ |
| 138 |
url: ajaxurl, |
| 139 |
type: "POST", |
| 140 |
data: { |
| 141 |
action: "cancel_fetch_orders", |
| 142 |
_wpnonce: nonce, |
| 143 |
}, |
| 144 |
success: function (response) { |
| 145 |
if (response.success) { |
| 146 |
window.BasalamToast.info(response.data?.message || "ع� |
| 147 |
لیات لغو شد."); |
| 148 |
location.reload(); |
| 149 |
} else { |
| 150 |
window.BasalamToast.error(response.data?.message || "خطایی رخ داده است."); |
| 151 |
$btn.prop("disabled", false); |
| 152 |
} |
| 153 |
}, |
| 154 |
error: function (jqXHR) { |
| 155 |
let message = "خطایی در ارتباط با سرور رخ داد."; |
| 156 |
try { |
| 157 |
const response = JSON.parse(jqXHR.responseText); |
| 158 |
message = response.data?.message || message; |
| 159 |
} catch (e) {} |
| 160 |
window.BasalamToast.error(message); |
| 161 |
$btn.prop("disabled", false); |
| 162 |
}, |
| 163 |
}); |
| 164 |
}); |
| 165 |
}); |
| 166 |
|