| 1 |
/* |
| 2 |
* AI Assistant setting js |
| 3 |
*/ |
| 4 |
function wpsc_ai_assistant_setting(is_humbargar = false) { |
| 5 |
if (is_humbargar) { |
| 6 |
wpsc_toggle_humbargar(); |
| 7 |
} |
| 8 |
|
| 9 |
jQuery(".wpsc-setting-nav, .wpsc-humbargar-menu-item").removeClass("active"); |
| 10 |
jQuery( |
| 11 |
".wpsc-setting-nav.ai-assistant, .wpsc-humbargar-menu-item.ai-assistant", |
| 12 |
).addClass("active"); |
| 13 |
jQuery(".wpsc-humbargar-title").html( |
| 14 |
supportcandy.humbargar_titles.ai_assistant, |
| 15 |
); |
| 16 |
|
| 17 |
if (supportcandy.current_section !== "ai-assistant") { |
| 18 |
supportcandy.current_section = "ai-assistant"; |
| 19 |
supportcandy.current_tab = "general"; |
| 20 |
} |
| 21 |
|
| 22 |
window.history.replaceState( |
| 23 |
{}, |
| 24 |
null, |
| 25 |
"admin.php?page=wpsc-settings§ion=" + supportcandy.current_section, |
| 26 |
); |
| 27 |
jQuery(".wpsc-setting-body").html(supportcandy.loader_html); |
| 28 |
|
| 29 |
wpsc_scroll_top(); |
| 30 |
var data = { |
| 31 |
action: "wpsc_ai_assistant_setting", |
| 32 |
tab: supportcandy.current_tab, |
| 33 |
}; |
| 34 |
|
| 35 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 36 |
jQuery(".wpsc-setting-body").html(response); |
| 37 |
wpsc_reset_responsive_style(); |
| 38 |
jQuery( |
| 39 |
".wpsc-setting-tab-container button." + supportcandy.current_tab, |
| 40 |
).trigger("click"); |
| 41 |
}); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Load general setting tab ui |
| 46 |
*/ |
| 47 |
function wpsc_get_aia_general_setting() { |
| 48 |
supportcandy.current_tab = "general"; |
| 49 |
jQuery(".wpsc-setting-tab-container button").removeClass("active"); |
| 50 |
jQuery( |
| 51 |
".wpsc-setting-tab-container button." + supportcandy.current_tab, |
| 52 |
).addClass("active"); |
| 53 |
|
| 54 |
window.history.replaceState( |
| 55 |
{}, |
| 56 |
null, |
| 57 |
"admin.php?page=wpsc-settings§ion=" + |
| 58 |
supportcandy.current_section + |
| 59 |
"&tab=" + |
| 60 |
supportcandy.current_tab, |
| 61 |
); |
| 62 |
jQuery(".wpsc-setting-section-body").html(supportcandy.loader_html); |
| 63 |
|
| 64 |
wpsc_scroll_top(); |
| 65 |
|
| 66 |
const data = { action: "wpsc_get_aia_general_setting" }; |
| 67 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 68 |
jQuery(".wpsc-setting-section-body").html(response); |
| 69 |
wpsc_reset_responsive_style(); |
| 70 |
}); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Load AI assistant tab ui |
| 75 |
*/ |
| 76 |
function wpsc_get_aia_assistant_setting() { |
| 77 |
supportcandy.current_tab = "assistant"; |
| 78 |
jQuery(".wpsc-setting-tab-container button").removeClass("active"); |
| 79 |
jQuery( |
| 80 |
".wpsc-setting-tab-container button." + supportcandy.current_tab, |
| 81 |
).addClass("active"); |
| 82 |
|
| 83 |
window.history.replaceState( |
| 84 |
{}, |
| 85 |
null, |
| 86 |
"admin.php?page=wpsc-settings§ion=" + |
| 87 |
supportcandy.current_section + |
| 88 |
"&tab=" + |
| 89 |
supportcandy.current_tab, |
| 90 |
); |
| 91 |
jQuery(".wpsc-setting-section-body").html(supportcandy.loader_html); |
| 92 |
|
| 93 |
wpsc_scroll_top(); |
| 94 |
|
| 95 |
const data = { action: "wpsc_get_aia_assistant_setting" }; |
| 96 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 97 |
jQuery(".wpsc-setting-section-body").html(response); |
| 98 |
wpsc_reset_responsive_style(); |
| 99 |
}); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Load AI logs tab ui |
| 104 |
*/ |
| 105 |
function wpsc_get_aia_logs_setting() { |
| 106 |
|
| 107 |
supportcandy.current_tab = "ai-logs"; |
| 108 |
jQuery(".wpsc-setting-tab-container button").removeClass("active"); |
| 109 |
jQuery(".wpsc-setting-tab-container button." + supportcandy.current_tab).addClass("active"); |
| 110 |
|
| 111 |
window.history.replaceState( |
| 112 |
{}, |
| 113 |
null, |
| 114 |
"admin.php?page=wpsc-settings§ion=" + |
| 115 |
supportcandy.current_section + |
| 116 |
"&tab=" + |
| 117 |
supportcandy.current_tab |
| 118 |
); |
| 119 |
jQuery(".wpsc-setting-section-body").html(supportcandy.loader_html); |
| 120 |
wpsc_scroll_top(); |
| 121 |
|
| 122 |
const data = { |
| 123 |
action: "wpsc_get_aia_logs_setting" |
| 124 |
}; |
| 125 |
|
| 126 |
/* check if page opened from onclick */ |
| 127 |
const filter = sessionStorage.getItem("wpsc_ai_logs_filter"); |
| 128 |
if (filter) { |
| 129 |
data.filter = JSON.parse(filter); |
| 130 |
/* remove after using */ |
| 131 |
sessionStorage.removeItem("wpsc_ai_logs_filter"); |
| 132 |
} |
| 133 |
|
| 134 |
jQuery.post(supportcandy.ajax_url, data, function (response) { |
| 135 |
jQuery(".wpsc-setting-section-body").html(response); |
| 136 |
wpsc_reset_responsive_style(); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get edit category modal |
| 142 |
*/ |
| 143 |
function wpsc_get_edit_ai_category(id, nonce) { |
| 144 |
|
| 145 |
wpsc_show_modal(); |
| 146 |
var data = { action: 'wpsc_get_edit_ai_category', id, _ajax_nonce: nonce }; |
| 147 |
jQuery.post( |
| 148 |
supportcandy.ajax_url, |
| 149 |
data, |
| 150 |
function (res) { |
| 151 |
|
| 152 |
// Set to modal. |
| 153 |
jQuery( '.wpsc-modal-header' ).text( res.title ); |
| 154 |
jQuery( '.wpsc-modal-body' ).html( res.body ); |
| 155 |
jQuery( '.wpsc-modal-footer' ).html( res.footer ); |
| 156 |
// Display modal. |
| 157 |
wpsc_show_modal_inner_container(); |
| 158 |
} |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Save setting |
| 164 |
*/ |
| 165 |
function wpsc_set_ai_settings(el) { |
| 166 |
var form = jQuery(".wpsc-frm-ai-settings")[0]; |
| 167 |
var dataform = new FormData(form); |
| 168 |
|
| 169 |
if (dataform.get("wpsc-ai-api-key").trim() == "") { |
| 170 |
alert(supportcandy.translations.req_fields_missing); |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 175 |
jQuery |
| 176 |
.ajax({ |
| 177 |
url: supportcandy.ajax_url, |
| 178 |
type: "POST", |
| 179 |
data: dataform, |
| 180 |
processData: false, |
| 181 |
contentType: false, |
| 182 |
}) |
| 183 |
.done(function (res) { |
| 184 |
window.location.reload(); |
| 185 |
}); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Save AI assistant tab settings |
| 190 |
*/ |
| 191 |
function wpsc_set_ai_assistant_settings(el) { |
| 192 |
var form = jQuery(".wpsc-frm-ai-assistant-settings")[0]; |
| 193 |
var dataform = new FormData(form); |
| 194 |
|
| 195 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 196 |
jQuery |
| 197 |
.ajax({ |
| 198 |
url: supportcandy.ajax_url, |
| 199 |
type: "POST", |
| 200 |
data: dataform, |
| 201 |
processData: false, |
| 202 |
contentType: false, |
| 203 |
}) |
| 204 |
.done(function (res) { |
| 205 |
window.location.reload(); |
| 206 |
}); |
| 207 |
} |
| 208 |
|
| 209 |
/* |
| 210 |
* Reset settings |
| 211 |
*/ |
| 212 |
function wpsc_reset_ai_settings(el, nonce) { |
| 213 |
var data = { |
| 214 |
action: "wpsc_reset_ai_settings", |
| 215 |
_ajax_nonce: nonce, |
| 216 |
}; |
| 217 |
|
| 218 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 219 |
jQuery |
| 220 |
.ajax({ |
| 221 |
url: supportcandy.ajax_url, |
| 222 |
type: "POST", |
| 223 |
data: data, |
| 224 |
}) |
| 225 |
.done(function (res) { |
| 226 |
window.location.reload(); |
| 227 |
}); |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
* onclick load respective card ticket list based on agent id |
| 232 |
*/ |
| 233 |
function wpsc_filter_ai_logs_by_agent(agent_id, date_range) { |
| 234 |
|
| 235 |
if (!agent_id) { |
| 236 |
return; |
| 237 |
} |
| 238 |
const data = { |
| 239 |
agent_id: agent_id, |
| 240 |
date_range: date_range |
| 241 |
}; |
| 242 |
|
| 243 |
sessionStorage.setItem( |
| 244 |
"wpsc_ai_logs_filter", |
| 245 |
JSON.stringify(data) |
| 246 |
); |
| 247 |
|
| 248 |
window.location.href = "admin.php?page=wpsc-settings§ion=ai-assistant&tab=ai-logs"; |
| 249 |
} |
| 250 |
|
| 251 |
// AI Training |
| 252 |
|
| 253 |
/** |
| 254 |
* Get add AI training item |
| 255 |
*/ |
| 256 |
function wpsc_add_ai_training_item(el) { |
| 257 |
|
| 258 |
wpsc_show_modal(); |
| 259 |
var data = { action: 'wpsc_add_ai_training_item' }; |
| 260 |
jQuery.post( |
| 261 |
supportcandy.ajax_url, |
| 262 |
data, |
| 263 |
function (response) { |
| 264 |
|
| 265 |
// Set to modal. |
| 266 |
jQuery( '.wpsc-modal-header' ).text( response.title ); |
| 267 |
jQuery( '.wpsc-modal-body' ).html( response.body ); |
| 268 |
jQuery( '.wpsc-modal-footer' ).html( response.footer ); |
| 269 |
// Display modal. |
| 270 |
wpsc_show_modal_inner_container(); |
| 271 |
} |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Delete all training data |
| 277 |
*/ |
| 278 |
function wpsc_delete_all_training_data(el, nonce, type) { |
| 279 |
var flag = confirm(supportcandy.translations.confirm); |
| 280 |
if (!flag) { |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
var data = { |
| 285 |
action: "wpsc_delete_all_training_data_" + type, |
| 286 |
_ajax_nonce: nonce, |
| 287 |
type: type |
| 288 |
}; |
| 289 |
jQuery.post(supportcandy.ajax_url, data, function (res) { |
| 290 |
window.location.reload(); |
| 291 |
}); |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* Set an AI training item |
| 296 |
*/ |
| 297 |
function wpsc_set_add_ai_training_item(el) { |
| 298 |
|
| 299 |
var form = jQuery('.wpsc-frm-add-ai-training-item')[0]; |
| 300 |
var dataform = new FormData(form); |
| 301 |
|
| 302 |
// Validate required fields |
| 303 |
if ( dataform.get('wpsc_training_type').trim() == '' ) { |
| 304 |
alert(supportcandy.translations.req_fields_missing); |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
// If file type is selected, validate the file input |
| 309 |
if (dataform.get('wpsc_training_type').trim() === 'wpsc_ai_file_type') { |
| 310 |
const fileInput = dataform.getAll('wpsc_ai_file_input[]'); |
| 311 |
if ( !fileInput || fileInput.length === 0 || (fileInput.length === 1 && (!fileInput[0] || !fileInput[0].name)) ) { |
| 312 |
alert(supportcandy.translations.req_fields_missing); |
| 313 |
return; |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
// If URL type is selected, validate the URLs |
| 318 |
if (dataform.get('wpsc_training_type').trim() == 'wpsc_ai_url_type') { |
| 319 |
var urlInput = dataform.get('wpsc_ai_url_input'); |
| 320 |
if (!urlInput || urlInput.trim() === '') { |
| 321 |
alert(supportcandy.translations.req_fields_missing); |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
// Split by newlines and validate each line as a URL |
| 326 |
var urls = urlInput.split(/\r?\n/).map(function(line) { return line.trim(); }).filter(Boolean); |
| 327 |
var urlPattern = /^(https?:\/\/)[^\s/$.?#].[^\s]*$/i; |
| 328 |
var allValid = urls.every(function(url) { return urlPattern.test(url); }); |
| 329 |
if (!allValid) { |
| 330 |
alert(supportcandy.translations.valid_url); |
| 331 |
return; |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
jQuery('.wpsc-modal-footer button').attr('disabled', true); |
| 336 |
jQuery(el).text(supportcandy.translations.please_wait); |
| 337 |
jQuery |
| 338 |
.ajax({ |
| 339 |
url: supportcandy.ajax_url, |
| 340 |
type: 'POST', |
| 341 |
data: dataform, |
| 342 |
processData: false, |
| 343 |
contentType: false, |
| 344 |
}) |
| 345 |
.done(function (res) { |
| 346 |
wpsc_close_modal(); |
| 347 |
if(trainingTable && trainingTable.ajax){ |
| 348 |
trainingTable.ajax.reload(null, false); |
| 349 |
} |
| 350 |
}); |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Delete an AI training item |
| 355 |
*/ |
| 356 |
function wpsc_get_delete_ai_training_item(el, id, nonce) { |
| 357 |
var flag = confirm(supportcandy.translations.confirm); |
| 358 |
if (!flag) { |
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
var data = { |
| 363 |
action: "wpsc_get_delete_ai_training_item", |
| 364 |
id, |
| 365 |
_ajax_nonce: nonce, |
| 366 |
}; |
| 367 |
jQuery.post(supportcandy.ajax_url, data, function (res) { |
| 368 |
if(trainingTable && trainingTable.ajax){ |
| 369 |
trainingTable.ajax.reload(null, false); |
| 370 |
} |
| 371 |
}); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* View the recorded failure reason of an AI training item |
| 376 |
*/ |
| 377 |
function wpsc_view_reason_for_failed_ai_training_item(el, id, nonce) { |
| 378 |
|
| 379 |
wpsc_show_modal(); |
| 380 |
var data = { |
| 381 |
action: 'wpsc_view_reason_for_failed_ai_training_item', |
| 382 |
id, |
| 383 |
_ajax_nonce: nonce, |
| 384 |
}; |
| 385 |
jQuery.post( |
| 386 |
supportcandy.ajax_url, |
| 387 |
data, |
| 388 |
function (res) { |
| 389 |
|
| 390 |
// Set to modal. |
| 391 |
jQuery( '.wpsc-modal-header' ).text( res.title ); |
| 392 |
jQuery( '.wpsc-modal-body' ).html( res.body ); |
| 393 |
jQuery( '.wpsc-modal-footer' ).html( res.footer ); |
| 394 |
// Display modal. |
| 395 |
wpsc_show_modal_inner_container(); |
| 396 |
} |
| 397 |
); |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Download an AI training item |
| 402 |
*/ |
| 403 |
function wpsc_download_ai_training_item(el, id, nonce) { |
| 404 |
var flag = confirm(supportcandy.translations.confirm); |
| 405 |
if (!flag) { |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
var data = { |
| 410 |
action: "wpsc_download_ai_training_item", |
| 411 |
id, |
| 412 |
_ajax_nonce: nonce, |
| 413 |
}; |
| 414 |
jQuery.post(supportcandy.ajax_url, data, function (res) { |
| 415 |
window.location.reload(); |
| 416 |
}); |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Get bulk archive ticket |
| 421 |
*/ |
| 422 |
function wpsc_bulk_delete_training(nonce) { |
| 423 |
|
| 424 |
const training_ids = jQuery(".wpsc-bulk-select:checked") |
| 425 |
.map(function () { |
| 426 |
return this.value; |
| 427 |
}) |
| 428 |
.get(); |
| 429 |
|
| 430 |
jQuery(".wpsc-bulk-selector").prop( "checked", training_ids.length > 0 ); |
| 431 |
|
| 432 |
if (!training_ids.length) { |
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
if (!confirm(supportcandy.translations.confirm)) { |
| 437 |
return; |
| 438 |
} |
| 439 |
|
| 440 |
jQuery.post( |
| 441 |
supportcandy.ajax_url, |
| 442 |
{ |
| 443 |
action: "wpsc_bulk_delete_training", |
| 444 |
training_ids: training_ids, |
| 445 |
_ajax_nonce: nonce |
| 446 |
}, |
| 447 |
function () { |
| 448 |
window.location.reload(); |
| 449 |
} |
| 450 |
); |
| 451 |
} |
| 452 |
|
| 453 |
|