/* Trang Ưu đãi thành viên (/point) — trước đây là 2 script nhúng trong
* point/index.php (lịch sử điểm) và point/point.php (đổi điểm).
* Giữ nguyên request và luồng xử lý: đổi điểm vẫn POST point/change.php, hỏi
* xác nhận, thành công thì tải lại trang để cập nhật số dư/điểm. Khác bản cũ:
* - chặn bấm "Đổi ngay" lần 2 khi yêu cầu trước chưa xong (chống gửi trùng);
* - sự kiện gắn trong nội dung trang, rời trang là gỡ; kết quả về muộn bỏ qua. */
WGNav.page('point', function (root, ctx) {
var $ = window.jQuery;
function $q(sel) { return $(root).find(sel); }
/* Khách chưa đăng nhập: trang chỉ có nút Đăng nhập/Đăng ký */
if (!$q('#data').length && !$q('#money-checkboxes').length) return;
/* ================= Lịch sử điểm ================= */
function loadData(page) {
$.ajax({
url: '/point/load-more.php',
method: 'POST',
data: { page: page },
success: function (data) { if (ctx.alive) $q('#data').html(data); },
error: function () {
if (ctx.alive) $q('#data').html('
Không tải được lịch sử, thử lại sau nhé.
');
}
});
}
if ($q('#data').length) loadData(1);
ctx.on(root, 'click', '#data .page-link', function (e, a) {
e.preventDefault();
loadData($(a).data('page'));
});
/* Thu gọn / mở lịch sử */
ctx.on(root, 'click', '#toggleButton', function (e, btn) {
var $box = $q('#collapseContent');
var open = $box.is(':visible');
$box.slideToggle(180);
$(btn).toggleClass('collapsed', open).attr('aria-expanded', !open);
});
/* Xoá lịch sử (nút hiện đang ẩn, giữ lại để dùng khi cần) */
ctx.on(root, 'click', '#clear-ls', function () {
if (typeof Swal === 'undefined') return;
Swal.fire({
title: 'Xoá toàn bộ lịch sử điểm?',
text: 'Thao tác này không thể hoàn tác.',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#dc2626',
cancelButtonColor: '#6b7280',
confirmButtonText: 'Xoá',
cancelButtonText: 'Huỷ'
}).then(function (r) {
if (!r.isConfirmed || !ctx.alive) return;
$q('#pt-overlay').addClass('show');
$.ajax({
url: '/point/clear_ls.php', type: 'POST', dataType: 'json',
success: function (res) {
$q('#pt-overlay').removeClass('show');
Swal.fire({ title: res.success ? 'Đã xoá' : 'Lỗi', text: res.message, icon: res.success ? 'success' : 'error' });
if (res.success && ctx.alive) loadData(1);
},
error: function () {
$q('#pt-overlay').removeClass('show');
Swal.fire({ title: 'Lỗi', text: 'Không kết nối được máy chủ.', icon: 'error' });
}
});
});
});
/* ================= Đổi điểm ================= */
var $out = $q('#money-result');
var busy = false; // đang gửi yêu cầu đổi điểm
function fmt(n) { return Number(n).toLocaleString('vi-VN'); }
function sync() {
var $sel = $q('#money-checkboxes input[name=point]:checked');
if (!$sel.length) { $out.text('0'); $q('#point-change').prop('disabled', true); return; }
$out.text(fmt($sel.data('value')));
$q('#point-change').prop('disabled', busy);
}
sync();
ctx.on(root, 'change', '#money-checkboxes input[name=point]', sync);
ctx.on(root, 'click', '#point-change', function () {
if (busy) return;
var $sel = $q('#money-checkboxes input[name=point]:checked');
if (!$sel.length) {
$q('#money-error-message').text('Bạn chưa chọn mốc điểm muốn đổi.');
return;
}
$q('#money-error-message').text('');
var pts = $sel.data('value');
Swal.fire({
title: 'Xác nhận đổi điểm',
html: 'Dùng ' + fmt(pts) + ' điểm để nhận ' + fmt(pts) + ' VNĐ vào số dư?',
icon: 'question',
showCancelButton: true,
confirmButtonColor: '#1668ff',
cancelButtonColor: '#6b7280',
confirmButtonText: 'Đổi ngay',
cancelButtonText: 'Để sau'
}).then(function (r) {
if (!r.isConfirmed || busy || !ctx.alive) return;
busy = true;
$q('#point-change').prop('disabled', true);
$q('#pt-overlay').addClass('show');
$.ajax({
type: 'POST',
url: '/point/change.php',
data: { option: $sel.val() },
dataType: 'json',
success: function (res) {
$q('#pt-overlay').removeClass('show');
var ok = (res.code != '0');
Swal.fire({
title: ok ? 'Đổi điểm thành công' : 'Chưa đổi được',
html: res.msg,
icon: ok ? 'success' : 'error',
confirmButtonColor: '#1668ff',
confirmButtonText: 'Đã hiểu'
}).then(function () {
if (ok) location.reload(); // như cũ: tải lại để cập nhật số dư / điểm
else { busy = false; sync(); }
});
},
error: function () {
$q('#pt-overlay').removeClass('show');
busy = false;
sync();
Swal.fire({ title: 'Lỗi kết nối', text: 'Không gọi được máy chủ, thử lại sau nhé.', icon: 'error', confirmButtonColor: '#1668ff' });
}
});
});
});
});