﻿(function (window, document, undefined) {
	'use strict';

	/*==============================
	Header
	==============================*/
	if (document.querySelector('.header')) {
		const headerBtn = document.querySelector('.header__btn');
		const headerNav = document.querySelector('.sidebar');

		function toggleHeaderMenu() {
			headerBtn.classList.toggle('header__btn--active');
			headerNav.classList.toggle('sidebar--active');
		}

		headerBtn.addEventListener('click', toggleHeaderMenu);
	}

	/*==============================
	Scrollbar
	==============================*/
	var Scrollbar = window.Scrollbar;

	if (document.querySelector('.dashbox__table-bank-infos')) {
		Scrollbar.init(document.querySelector('.dashbox__table-bank-infos'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: true,
			continuousScrolling: true
		});
	}

	if (document.querySelector('.dashbox__table-lode')) {
		Scrollbar.init(document.querySelector('.dashbox__table-lode'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: true,
			continuousScrolling: true
		});
	}

	if (document.querySelector('#recently-histories')) {
		Scrollbar.init(document.querySelector('#recently-histories'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: true,
			continuousScrolling: true
		});
	}


	if (document.querySelector('.dashbox__table-wrap-win-games')) {
		Scrollbar.init(document.querySelector('.dashbox__table-wrap-win-games'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: true,
			continuousScrolling: true
		});
	}

	if (document.querySelector('.dashbox__table-wrap--4')) {
		Scrollbar.init(document.querySelector('.dashbox__table-wrap--4'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: true,
			continuousScrolling: true
		});
	}
	if (document.querySelector('.vip-privilege')) {
		Scrollbar.init(document.querySelector('.vip-privilege'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: true,
			continuousScrolling: true
		});
	}
	if (document.querySelector('.sidebar__nav')) {
		Scrollbar.init(document.querySelector('.sidebar__nav'), {
			damping: 0.1,
			renderByPixels: true,
			alwaysShowTracks: false,
			continuousScrolling: true
		});
	}

	/*==============================
	Filter
	==============================*/
	const selectElements = document.querySelectorAll('.sign__selectjs');

	if (selectElements.length > 0) {
		// Lặp qua từng thẻ và khởi tạo SlimSelect
		selectElements.forEach(selectElement => {
			new SlimSelect({
				select: selectElement,
				settings: {
					showSearch: true,
				}
			});
		});
	}

	/*==============================
	Upload
	==============================*/
	if (document.getElementById('sign__gallery-upload')) {
		var galleryUpload = document.getElementById('sign__gallery-upload');

		galleryUpload.addEventListener('change', function(event) {
			var length = event.target.files.length;
			var galleryLabel = galleryUpload.getAttribute('data-name');
			var label = document.querySelector(galleryLabel);

			if (length > 1) {
				label.textContent = length + " files selected";
			} else {
				label.textContent = event.target.files[0].name;
			}
		});
	}

	/*==============================
	Section bg
	==============================*/
	if (document.querySelector('.section--bg')) {
		var mainBg = document.querySelector('.section--bg');

		if (mainBg.getAttribute('data-bg')) {
			mainBg.style.background = `url(${mainBg.getAttribute('data-bg')})`;
			mainBg.style.backgroundPosition = 'center center';
			mainBg.style.backgroundRepeat = 'no-repeat';
			mainBg.style.backgroundSize = 'cover';
		}
	}

})(window, document);
$(document).on("click", ".show-notes", function () {
	var data = $(this).attr("data-content");
	$('#modal-notes .comments__text').html(data);
});
$(document).on("click", ".show-fix", function () {
	var data = $(this).attr("data-content");
	$('#modal-fix .hoan-cuoc').attr('bs-transid', data);
});
var isSubmitting = false;

$(document).on('click', '.nhan-thuong', function () {
	if (!isSubmitting) {
		isSubmitting = true;

		var level = $(this).data('bs-level');
		
		// Thêm các giá trị này vào form ẩn
		$('#_level').val(level);
		// Gửi form
		var form = document.getElementById('form-nvngay');
		if (form.checkValidity()) {
			form.submit();
		} else {
			isSubmitting = false;
            form.reportValidity();
		}
	}
	$('#form-nvngay').submit(function () {
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });
});
$(document).on('click', '.nhan_code', function () {
	if (!isSubmitting) {
		isSubmitting = true;

		var form = document.getElementById('form-gc');
		if (form.checkValidity()) {
			form.submit();
		} else {
			isSubmitting = false;
            form.reportValidity();
		}
	}
	$('#form-gc').submit(function () {
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });
});
$(document).on('click', '.check-transid', function () {
	if (!isSubmitting) {
		isSubmitting = true;

		var form = document.getElementById('form-check-transid');
		if (form.checkValidity()) {
			form.submit();
		} else {
			isSubmitting = false;
            form.reportValidity();
		}
	}
	$('#form-check-transid').submit(function () {
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });
});
$(document).on('click', '.hoan-cuoc', function () {
	if (!isSubmitting) {
		isSubmitting = true;

		var form = document.getElementById('form-hoan-cuoc');
		if (form.checkValidity()) {
			form.submit();
		} else {
			isSubmitting = false;
            form.reportValidity();
		}
	}
	$('#form-hoan-cuoc').submit(function () {
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });
});
$(document).on('click', '.check-nvn', function () {
	if (!isSubmitting) {
		isSubmitting = true;

		var form = document.getElementById('form-check-nvn');
		if (form.checkValidity()) {
			form.submit();
		} else {
			isSubmitting = false;
            form.reportValidity();
		}
	}
	$('#form-check-nvn').submit(function () {
        $('input[type=submit]', this).attr('disabled', 'disabled');
    });
});
$(document).on("click", ".show-bill", function () {
	var bc = $(this).attr('data-bt');
	if (bc != 'MB') {
		$('.bill-frame').css('max-width', '390px');
	}
	else {
		$('.bill-frame').css('max-width', '1360px');
	}
	$('#show-bill-ifr').attr('src', "/showbankbill/?d=" + $(this).attr('data-bi'));
	$('#ifr-bill-panel').fadeIn(300);
})
$(document).on("click", ".close-bill-frame", function () {
	$('#show-bill-ifr').attr('src', 'about:blank');
	$('#ifr-bill-panel').fadeOut(300);
});
$(document).on("click", ".sidebar__user-btn", function () {
	window.location.href = '/auth/logout';
});
var qrCode;
function loadQR(data, img, bgc) {
	qrCode = new QRCodeStyling({
		width: 200,
		height: 200,
		data: data,
		image: img,
		dotsOptions: { color: bgc, type: "rounded" },
		backgroundOptions: { color: "#FFF" },
		imageOptions: { crossOrigin: "anonymous", margin: 5, imageSize: 0.5 },
	});
	console.log(qrCode);
	$("#canvasQr").html(""), qrCode.append(document.getElementById("canvasQr"));
}
$(document).on("click", ".qrc", function () {
	var bankCode = $(this).attr('data-bankcode');
	var bgc = "#bb2c7d";
	var img = window.location.protocol + '//' + window.location.host + '/client/img/momo/logo.png';
	var data = $(this).attr('data-content');
	var md = $('#qr-download').attr("mobile-device");
	if (bankCode == "ZLP") {
		$('.qr-notes').show();
	}
	else {
		$('.qr-notes').hide();
	}
	if (bankCode == "ZLP" && md == "1") {
		$('#qr-download').text('Thanh Toán Ngay').attr("data-content", data);
	}
	else {
		$('#qr-download').text('Tải QrCode về máy').attr("data-content", "");
	}
	loadQR(data, img, bgc);
});
$(document).on("click", "#qr-download", function () {
	var data = $(this).attr("data-content");
	if (data) {
		window.location.href = data;
	}
	else {
		qrCode.download({
			extension: "png",
			name: "qrcode"
		})
	}
});
$(document).on("click", ".resend-reward", function () {
	var pr = $(this).parent();
	pr.html('<span><img src="/Content/loading.gif" style="height: 13px;vertical-align: middle;" alt="loading ..." /></span>');
	gh = false;
	$.post("/home/resendreward/?d=" + $(this).attr('data-bi')).always(function () {
		pr.html('<span class="gstatus wait">WAIT</span>');
		gh = true;
	});
});
$(window).on('load', function () {
	$('.preloader').fadeOut(500);
	$('.preloader1').fadeOut(500);
});
function showLoader() {
	$('.preloader').fadeIn(500);
}
$(window).on('beforeunload', function () {
	showLoader();
});
$(window).on('pagehide', function () {
	$('.preloader').fadeOut(500);
});
$(window).on('unload', function () {
	$('.preloader').fadeOut(500);
});