let callBookProcessingDialog = null;
let inCallbookProcessing = false;
let inCallbookItemProcessing = false;
let stateFixingDialog = null;
let inStateFixing = false;
let stateFixStats = {fixed: 0, skipped: 0, fixedDxcc: new Set(), skippedDxcc: new Set(), skipReasons: new Set(), skippedDetails: []};
let lastChecked = null;
let silentReset = false;
const filterDefaults = {};

document.addEventListener("DOMContentLoaded", function() {
  document.querySelectorAll('.dropdown').forEach(dd => {
    dd.addEventListener('hide.bs.dropdown', function (e) {
      if (e.clickEvent && e.clickEvent.target.closest('.dropdown-menu')) {
        e.preventDefault();
      }
    });

    dd.querySelectorAll('.dropdown-action').forEach(btn => {
      btn.addEventListener('click', function() {
        const dropdown = bootstrap.Dropdown.getInstance(dd.querySelector('[data-bs-toggle="dropdown"]'));
        if (dropdown) dropdown.hide();
      });
    });
  });

  // The visible "CSV" menu item triggers the (hidden) DataTables CSV export button
  $('#lbaExportCsv').on('click', function () {
    $('#csv-button-container button').trigger('click');
  });
});


$('#band').change(function () {
	var band = $("#band option:selected").text();
	if (band != "SAT") {
		$(".sats_dropdown").attr("hidden", true);
		$(".orbits_dropdown").attr("hidden", true);
	} else {
		$(".sats_dropdown").removeAttr("hidden");
		$(".orbits_dropdown").removeAttr("hidden");
	}
});

$('#selectPropagation').change(function () {
	var prop_mode = $("#selectPropagation option:selected").text();
	if (prop_mode != "Satellite") {
		$(".sats_dropdown").attr("hidden", true);
		$(".orbits_dropdown").attr("hidden", true);
	} else {
		$(".sats_dropdown").removeAttr("hidden");
		$(".orbits_dropdown").removeAttr("hidden");
	}
});

function getSelectedIds() {
	let id_list = [];
	$('#qsoList tbody input:checked').each(function () {
		let id = $(this).closest('tr').attr('id')?.replace(/\D/g, '');
		id_list.push(id);
	});
	return id_list;
}

function updateRow(qso) {
	let row = $('#qsoID-' + qso.qsoID);
	let cells = row.find('td');

	// ColReorder lets the user drag columns into a different VISUAL order, but the
	// fields below are written in SOURCE (original) column order. When columns have
	// been reordered, row.find('td') returns cells in visual order, so writing by
	// position would put each value in the wrong cell. Remap the DOM cells back into
	// source order via ColReorder's mapping (order[visualPos] == sourceIndex) so
	// cells.eq(c) always refers to source column c, wherever it currently sits.
	if ($.fn.DataTable.isDataTable('#qsoList')) {
		const dt = $('#qsoList').DataTable();
		if (dt.colReorder && $.isFunction(dt.colReorder.order)) {
			const order = dt.colReorder.order();
			if (Array.isArray(order) && order.length === cells.length) {
				const reordered = new Array(cells.length);
				cells.each(function (v) { reordered[order[v]] = this; });
				cells = $(reordered);
			}
		}
	}

	let c = 1;
	if ((user_options.datetime.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.qsoDateTime);
	}
	if ((user_options.duration.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.duration);
	}
	if ((user_options.last_modification.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.last_modified);
	}
	if ((user_options.de.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.de);
	}
	if ((user_options.dx.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.dx);
	}
	if ((user_options.mode.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.mode);
	}
	if ((user_options.rsts.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.rstS);
	}
	if ((user_options.rstr.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.rstR);
	}
	if ((user_options.band.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.band);
	}
	if ((user_options.frequency.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.frequency);
	}
	if ( (user_options.gridsquare) && ((user_options.gridsquare.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.gridsquare);
	}
	if ((user_options.name.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.name);
	}
	if ((user_options.qth.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.qth);
	}
	if ((user_options.qslvia.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.qslVia).addClass('callsign');
	}
	if ((user_options.clublog.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.clublog);
	}
	if ((user_options.qsl.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.qsl);
	}
	if ($(".eqslconfirmation")[0] && ((user_options.eqsl.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.eqsl);
	}
	if ($(".lotwconfirmation")[0] && ((user_options.lotw.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.lotw);
	}
	if ((user_options.qrz.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.qrz);
	}
	if ((user_options.dcl.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.dcl);
	}
	if ((user_options.qslmsgs.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.qslMessage);
	}
	if ((user_options.qslmsgr.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.qslMessageR);
	}
	if ((user_options.dxcc.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.dxcc+qso.flag);
	}
	if ((user_options.state.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.state);
	}
	if ((user_options.county.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.county);
	}
	if ((user_options.cqzone.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.cqzone);
	}
	if ((user_options.ituzone.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.ituzone);
	}
	if ((user_options.iota.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.iota);
	}
	if ((user_options.pota.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.pota);
	}
	if ((user_options.sota) && ((user_options.sota.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.sota);
	}
	if ((user_options.dok) && ((user_options.dok.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.dok);
	}
	if ((user_options.wwff) && ((user_options.wwff.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.wwff);
	}
	if ((user_options.sig) && ((user_options.sig.show ?? 'true') == "true")){
		cells.eq(c++).text(qso.sig);
	}
	if ((user_options.sig_info) && ((user_options.sig_info.show ?? 'false') == "true")){
		cells.eq(c++).text(qso.sig_info);
	}
	if ((user_options.region) && ((user_options.region.show ?? 'true') == "true")){
		cells.eq(c++).text(qso.region);
	}
	if ((user_options.operator) && ((user_options.operator.show ?? 'true') == "true")){
		cells.eq(c++).html(qso.operator);
	}
	if ((user_options.comment) && ((user_options.comment.show ?? 'true') == "true")){
		cells.eq(c++).text(qso.comment);
	}
	if ((user_options.propagation) && ((user_options.propagation.show ?? 'true') == "true")){
		cells.eq(c++).text(qso.propagation);
	}
	if ((user_options.contest) && ((user_options.contest.show ?? 'true') == "true")){
		cells.eq(c++).text(qso.contest);
	}
	if ((user_options.myrefs.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.deRefs);
	}
	if ((user_options.continent.show ?? 'true') == "true"){
		cells.eq(c++).html(qso.continent);
	}
	if ((user_options.distance.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.distance);
	}
	if ((user_options.antennaazimuth.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.antennaazimuth);
	}
	if ((user_options.antennaelevation.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.antennaelevation);
	}
	if ((user_options.profilename.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.profilename);
	}
	if ((user_options.stationpower.show ?? 'true') == "true"){
		cells.eq(c++).text(qso.stationpower);
	}

	$('[data-bs-toggle="tooltip"]').tooltip();
	return row;
}

function loadQSOTable(rows) {
	const $table = $('#qsoList');

	// Prevent initializing if already a DataTable
	if ($.fn.DataTable.isDataTable($table)) {
		// Remove ALL buttons containers to prevent duplicates
		$('#qsoList').prev('.dt-buttons').remove();
		$('#qsoList_wrapper').find('.dt-buttons').remove();
		$('.dt-buttons').remove();
		$table.DataTable().clear().destroy();
	}

	const langUrl = getDataTablesLanguageUrl();

	const initTable = function(language) {
		$.fn.dataTable.moment(custom_date_format + ' HH:mm');

		// Reset ColReorder and savestate when showing/hiding columns to avoid errors when the number
		// of columns changes. This is a workaround for a known DataTables issue.
		const colCount = $table.find('thead th').length;
		const statePrefix = 'DataTables_' + $table.attr('id') + '_';
		for (let i = localStorage.length - 1; i >= 0; i--) {
			const key = localStorage.key(i);
			if (!(key && key.indexOf(statePrefix) === 0)) continue;
			let state;
			try {
				state = JSON.parse(localStorage.getItem(key));
			} catch (e) { continue; /* ignore malformed state */ }
			if (!state) continue;
			// Detect a column-set change via the saved column count (fall back to
			// the colReorder array length if columns wasn't saved).
			const savedColCount = Array.isArray(state.columns) ? state.columns.length
				: Array.isArray(state.colReorder) ? state.colReorder.length
				: colCount;
			if (savedColCount === colCount) continue;
			let dirty = false;
			if (Array.isArray(state.colReorder)) { delete state.colReorder; dirty = true; }
			if (Array.isArray(state.order))      { delete state.order;      dirty = true; }
			if (dirty) localStorage.setItem(key, JSON.stringify(state));
		}

		const table = $table.DataTable({
			colReorder: true,
			stateSave: true,
			searching: false,
			responsive: false,
			ordering: true,
			order: [],
			scrollY: window.innerHeight - $('#searchForm').innerHeight() - 250,
			scrollCollapse: true,
			paging: false,
			language: language,
			createdRow: function (row, data, dataIndex) {
				$(row).attr('id', data.id);
			},
			columnDefs: [
				{ orderable: false, targets: 0 },
				{ targets: $(".distance-column-sort").index(), type: "numbersort" },
				{ targets: $(".antennaazimuth-column-sort").index(), type: "numbersort" },
				{ targets: $(".antennaelevation-column-sort").index(), type: "numbersort" },
				{ targets: $(".stationpower-column-sort").index(), type: "numbersort" },
			],
			dom: 'frtip',
			buttons: [
						{
							extend: 'csv',
							text: 'CSV',
							className: 'dropdown-item',
							filename: function() {
								return 'qso_export_' + new Date().toISOString().slice(0,10);
							},
								exportOptions: {
								columns: ':visible:not(:eq(0))', // export all visible except column 4
								format: {
									body: function (data, row, column, node) {
									// QSL badge cell: extract sent + received status text from title attrs
									if (typeof data === 'string' && (data.includes('&#9650') || data.includes('&#9660'))) {
										const titles = [];
										const titleRe = /title="([^"]*)"/g;
										let m;
										while ((m = titleRe.exec(data)) !== null) titles.push(m[1]);
										const cleaned = titles
											.map(t => t.replace(/\s+\d.*$/, '').trim())
											.filter(t => t !== '');
										return cleaned.join(' / ');
									}
										// strip HTML tags first (like DataTables does by default)
										if (typeof data === 'string' && data.includes('<br />')) {
												data = data.replace(/<br \/>/g, '');
										}
										if (typeof data === 'string') {
											data = data.replace(/<[^>]*>/g, '');
										}
										// strip HTML from cell text
									if (column === 1 || column === 2 || column === 3 || column === 4) {
										// remove a trailing "L" and trim whitespaces
										data = data.replace(/\s*L\s*$/, '').trim();
									}
										if (typeof data === 'string' && data.includes('&#9650')) {
												data = data.replace(/&#9650;/g, '');
										}
										if (typeof data === 'string' && data.includes('&#9660')) {
												data = data.replace(/&#9660;/g, '');
										}

										data = data.replace(/ data-bs-toggle="tooltip" data-bs-html="true" class="[^"]*">/g, '');
										return data;
									}
								}
							}
						}
                    ]
		});

		// Place buttons in custom container
		table.buttons().container().appendTo('#csv-button-container');
		// Match .dropdown-action behaviour: close the Actions dropdown after export
		$('#csv-button-container button').on('click', function () {
			var toggle = document.getElementById('actionsDropdown');
			if (toggle) {
				var dropdown = bootstrap.Dropdown.getInstance(toggle);
				if (dropdown) dropdown.hide();
			}
		});

	for (i = 0; i < rows.length; i++) {
		let qso = rows[i];

		var data = [];
		data.push('<div class="form-check"><input class="row-check form-check-input" type="checkbox" /></div>');
		if ((user_options.datetime.show ?? 'true') == "true"){
			if (qso.datetime === '') {
				data.push('<span class="bg-danger">Missing date</span>');
			} else {
				data.push(qso.qsoDateTime);
			}
		}
		if ((user_options.duration.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.duration));
		}
		if ((user_options.last_modification.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.last_modified));
		}
		if ((user_options.de.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.de));
		}
		if ((user_options.dx.show ?? 'true') == "true"){

			data.push(qso.dx);
		}
		if ((user_options.mode.show ?? 'true') == "true"){
			data.push(qso.mode);
		}
		if ((user_options.rsts.show ?? 'true') == "true"){
			data.push(qso.rstS);
		}
		if ((user_options.rstr.show ?? 'true') == "true"){
			data.push(qso.rstR);
		}
		if ((user_options.band.show ?? 'true') == "true"){
			data.push(qso.band);
		}
		if ((user_options.frequency.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.frequency));
		}
		if ((user_options.gridsquare.show ?? 'true') == "true"){
			data.push(qso.gridsquare);
		}
		if ((user_options.name.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.name));
		}
		if ((user_options.qth.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.qth));
		}
		if ((user_options.qslvia.show ?? 'true') == "true"){
			data.push('<span class="callsign">' + escapeHtml(qso.qslVia) + '</span>');
		}
		if ((user_options.clublog.show ?? 'true') == "true"){
			data.push(qso.clublog);
		}
		if ((user_options.qsl.show ?? 'true') == "true"){
			data.push(qso.qsl);
		}
		if ($(".eqslconfirmation")[0] && (user_options.eqsl.show ?? 'true') == "true"){
			data.push(qso.eqsl);
		}
		if ($(".lotwconfirmation")[0] && (user_options.lotw.show ?? 'true') == "true"){
			data.push(qso.lotw);
		}
		if ((user_options.qrz.show ?? 'true') == "true"){
			data.push(qso.qrz);
		}
		if ((user_options.dcl.show ?? 'true') == "true"){
			data.push(qso.dcl);
		}
		if ((user_options.qslmsgs.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.qslMessage));
		}
		if ((user_options.qslmsgr.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.qslMessageR));
		}
		if ((user_options.dxcc.show ?? 'true') == "true"){
			data.push(qso.dxcc+qso.flag+(qso.end == null ? '' : ' <span class="badge bg-danger">Deleted DXCC</span>'));
		}
		if ((user_options.state.show ?? 'true') == "true"){
			data.push(qso.state);
		}
		if ((user_options.county.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.county));
		}
		if ((user_options.cqzone.show ?? 'true') == "true"){
			data.push(qso.cqzone);
		}
		if ((user_options.ituzone.show ?? 'true') == "true"){
			data.push(qso.ituzone);
		}
		if ((user_options.iota.show ?? 'true') == "true"){
			data.push(qso.iota);
		}
		if ((user_options.pota.show ?? 'true') == "true"){
			data.push(qso.pota);
		}
		if ((user_options.sota.show ?? 'true') == "true"){
			data.push(qso.sota);
		}
		if ((user_options.dok.show ?? 'true') == "true"){
			data.push(qso.dok);
		}
		if ((user_options.wwff.show ?? 'true') == "true"){
			data.push(qso.wwff);
		}
		if ((user_options.sig.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.sig));
		}
		if ((user_options.sig_info.show ?? 'false') == "true"){
			data.push(escapeHtml(qso.sig_info));
		}
		if ((user_options.region.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.region));
		}
		if ((user_options.operator.show ?? 'true') == "true"){
			data.push(qso.operator);
		}
		if ((user_options.comment.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.comment));
		}
		if ((user_options.propagation.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.propagation));
		}
		if ((user_options.contest.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.contest));
		}
		if ((user_options.myrefs.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.deRefs));
		}
		if ((user_options.continent.show ?? 'true') == "true"){
			data.push(qso.continent);
		}
		if ((user_options.distance.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.distance));
		}
		if ((user_options.antennaazimuth.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.antennaazimuth));
		}
		if ((user_options.antennaelevation.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.antennaelevation));
		}
		if ((user_options.profilename.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.profilename));
		}
		if ((user_options.stationpower.show ?? 'true') == "true"){
			data.push(escapeHtml(qso.stationpower));
		}
		data.id='qsoID-' + qso.qsoID;
		let createdRow = table.row.add(data).index();
		table.rows(createdRow).nodes().to$().data('qsoID', qso.qsoID);
	//	table.row(createdRow).node().to$().attr("id", 'qsoID-' + qso.qsoID);
	}
	try {
		table.columns.adjust().draw(true);
	} catch (e) {
		table.draw(true);
	}
	rebind_checkbox_trigger();
	$('[data-bs-toggle="tooltip"]').tooltip();

		document.querySelectorAll('.row-check').forEach(checkbox => {
			checkbox.addEventListener('click', function (e) {
				const checkboxes = Array.from(document.querySelectorAll('.row-check'));
				if (e.shiftKey && lastChecked) {
					let start = checkboxes.indexOf(this);
					let end = checkboxes.indexOf(lastChecked);
					[start, end] = [Math.min(start, end), Math.max(start, end)];

					for (let i = start; i <= end; i++) {
						checkboxes[i].checked = lastChecked.checked;
						$(checkboxes[i]).closest('tr').toggleClass('activeRow', lastChecked.checked);
					}
				}
				lastChecked = this;
			});
		});
	};

	if (langUrl) {
		// Load language file first
		$.getJSON(langUrl)
			.done(function(language) {
				initTable(language);
			})
			.fail(function() {
				console.error("Failed to load DataTables language file at " + langUrl);
				initTable({});  // fallback to default English
			});
	} else {
		// No language file needed (English)
		initTable({});
	}
}

$.fn.dataTable.ext.type.order['numbersort-pre'] = function(data) {
    var num = parseFloat(data);
    return isNaN(num) ? 0 : num;
};

function processNextCallbookItem(gridsquareAccuracyCheck) {
	if (!inCallbookProcessing) return;

	var elements = $('#qsoList tbody input:checked');
	var nElements = elements.length;
	if (nElements == 0) {
		inCallbookProcessing = false;
		callBookProcessingDialog.close();
		let table = $('#qsoList').DataTable();
		table.draw(false);
		return;
	}

	let id = elements.first().closest('tr').attr('id')?.replace(/\D/g, ''); // Removes non-numeric characters

	callBookProcessingDialog.setMessage("Retrieving callbook data : " + nElements + " remaining");

	$.ajax({
		url: site_url + '/logbookadvanced/updateFromCallbook',
		type: 'post',
		data: {
			qsoID: id,
			gridsquareAccuracyCheck: gridsquareAccuracyCheck ? 1 : 0
		},
		dataType: 'json',
		success: function (data) {
			if (data && data.dx) {
				updateRow(data);
			}
			unselectQsoID(id);
			setTimeout(function() {
				processNextCallbookItem(gridsquareAccuracyCheck);
			}, 50);
		},
		error: function (data) {
			unselectQsoID(id);
			setTimeout(function() {
				processNextCallbookItem(gridsquareAccuracyCheck);
			}, 50);
		},
	});
}

function processNextStateFixItem() {
	if (!inStateFixing) return;

	var elements = $('#qsoList tbody input:checked');
	var nElements = elements.length;

	if (nElements == 0) {
		inStateFixing = false;
		stateFixingDialog.close();

		// Show summary
		let message = '';
		message += lang_gen_advanced_logbook_fixed_with_count.replace('%s', stateFixStats.fixed);
		message += '<br>';
		message += lang_gen_advanced_logbook_skipped_with_count.replace('%s', stateFixStats.skipped);
		if (stateFixStats.skippedDetails.length > 0) {
			message += '<div class="border rounded p-2 mt-2" style="max-height: 150px; overflow-y: auto; background-color: var(--bs-body-bg); color: var(--bs-body-color);">';
			message += '<small>';
			stateFixStats.skippedDetails.forEach(function(detail, index) {
				if (index > 0) message += '<br>';
				message += detail;
			});
			message += '</small>';
			message += '</div>';
		}

		if (stateFixStats.skipped > 0) {
			message += '<div class="alert alert-info mt-3">';
			message += '<small>' + lang_gen_advanced_logbook_state_not_supported.replace('%s', lang_gen_advanced_logbook_github_link);
			message += '</small>';
			message += '</div>';
		}

		BootstrapDialog.alert({
			title: lang_gen_advanced_logbook_state_fix_complete,
			message: function(dialog) {
				return message;
			},
			type: stateFixStats.fixed > 0 ? BootstrapDialog.TYPE_SUCCESS : BootstrapDialog.TYPE_INFO,
			nl2br: false
		});

		let table = $('#qsoList').DataTable();
		table.draw(false);
		return;
	}

	let id = elements.first().closest('tr').attr('id')?.replace(/\D/g, '');

	stateFixingDialog.setMessage(lang_gen_advanced_logbook_fixing_state_remaining.replace('%s', nElements));

	$.ajax({
		url: site_url + '/logbookadvanced/fixStateProgress',
		type: 'post',
		data: {
			qsoID: id
		},
		dataType: 'json',
		success: function (data) {
			if (data.success && data.qso) {
				updateRow(data.qso);
				stateFixStats.fixed++;
				if (data.dxcc_name) {
					stateFixStats.fixedDxcc.add(data.dxcc_name);
				}
			} else if (data.skipped) {
				stateFixStats.skipped++;
				if (data.dxcc_name) {
					stateFixStats.skippedDxcc.add(data.dxcc_name);
				}
				if (data.reason) {
					stateFixStats.skipReasons.add(data.reason);
					// Build detailed skip entry: CALLSIGN - DXCC - reason
					let skipDetail = (data.callsign || 'Unknown') + ' - ' + (data.dxcc_name || 'Unknown DXCC') + ' - ' + data.reason;
					stateFixStats.skippedDetails.push(skipDetail);
				}
			}
			unselectQsoID(id);
			setTimeout("processNextStateFixItem()", 50);
		},
		error: function (data) {
			stateFixStats.skipped++;
			unselectQsoID(id);
			setTimeout("processNextStateFixItem()", 50);
		},
	});
}

function selectQsoID(qsoID) {
	var element = $("#qsoID-" + qsoID);
	element.find("input[type=checkbox]").prop("checked", true);
	element.addClass('activeRow');
}

function unselectQsoID(qsoID) {
	var element = $("#qsoID-" + qsoID);
	element.find("input[type=checkbox]").prop("checked", false);
	element.removeClass('activeRow');
	$('#checkBoxAll').prop("checked", false);
}

// Capture default values for all filter fields on page load
function captureFilterDefaults() {
	$('.filter-field').each(function() {
		const $el = $(this);
		const id = $el.attr('id');
		const name = $el.attr('name');
		// Use id as key if available, otherwise use name
		const key = id ? '#' + id : '[name="' + name + '"]';
		filterDefaults[key] = $el.val();
	});
}

$(document).ready(function () {
	// Capture default filter values BEFORE any other initialization
	captureFilterDefaults();

	// initialize multiselect dropdown for locations
	// Documentation: https://davidstutz.github.io/bootstrap-multiselect/index.html

	$('#de').multiselect({
		// template is needed for bs5 support
		enableFiltering: true,
		enableCaseInsensitiveFiltering: true,
		filterPlaceholder: lang_general_word_search,
		templates: {
		  button: '<button type="button" class="multiselect dropdown-toggle btn btn-sm btn-secondary me-2 w-auto" data-bs-toggle="dropdown" aria-expanded="false"><span class="multiselect-selected-text"></span></button>',
		},
		numberDisplayed: 1,
		inheritClass: true,
		includeSelectAllOption: true
	});

	$('#dxcc').multiselect({
		// template is needed for bs5 support
		templates: {
			button: '<button type="button" class="multiselect dropdown-toggle btn btn-sm btn-secondary me-2 w-auto" data-bs-toggle="dropdown" aria-expanded="false"><span class="multiselect-selected-text"></span></button>',
		},
		enableFiltering: true,
		enableFullValueFiltering: false,
		enableCaseInsensitiveFiltering: true,
		filterPlaceholder: lang_general_word_search,
		numberDisplayed: 1,
		inheritClass: true,
		buttonWidth: '100%',
		maxHeight: 600
	});
	$('.multiselect-container .multiselect-filter', $('#dxcc').parent()).css({
		'position': 'sticky', 'top': '0px', 'z-index': 1, 'background-color':'inherit', 'width':'100%', 'height':'37px'
	})

	/*Pull from localStorage to set form input value*/
	if (localStorage.hasOwnProperty(`user_${user_id}_qsoresults`)) {
		document.getElementById('qsoResults').value = localStorage.getItem(`user_${user_id}_qsoresults`);
	}

	if (localStorage.hasOwnProperty(`user_${user_id}_selectedlocations`)) {
		const selectedLocations = localStorage.getItem(`user_${user_id}_selectedlocations`);
		const locationsArray = selectedLocations ? selectedLocations.split(',') : [];
		// First, deselect all options
		$('#de').multiselect('deselectAll', false);

		// Then, select the stored locations
		$('#de').multiselect('select', locationsArray);
	}

	$('#searchForm').submit(function (e) {
		let container = L.DomUtil.get('advancedmap');
		let selectedlocations = $('#de').val();
		let qsoids = '';
		if (Array.isArray(selectedlocations) && selectedlocations.length === 0) {
			BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_info,
					message: lang_gen_advanced_logbook_select_at_least_one_location,
					type: BootstrapDialog.TYPE_INFO,
					closable: false,
					draggable: false,
					callback: function (result) {
					}
				});
			return false;
		}

		if(container != null){
			container._leaflet_id = null;
			container.remove();
			$(".coordinates").remove();
		}

		$("#qsoList").attr("Hidden", false);
		$("#qsoList_wrapper").attr("Hidden", false);
		$("#qsoList_info").attr("Hidden", false);

		localStorage.setItem(`user_${user_id}_qsoresults`, this.qsoresults.value);
		localStorage.setItem(`user_${user_id}_selectedlocations`, $('#de').val());
		$('#searchButton').prop("disabled", true).addClass("running");

		let qsoresults = this.qsoresults.value;

		if (localStorage.hasOwnProperty(`user_${user_id}_qsoids`)) {
			qsoids = localStorage.getItem(`user_${user_id}_qsoids`);

			qsoresults = qsoids
				.split(',')
				.filter(i => i.trim() !== '').length;
			localStorage.removeItem(`user_${user_id}_qsoids`);
		}
		$.ajax({
			url: this.action,
			type: 'post',
			data: {
				dateFrom: this.dateFrom.value,
				dateTo: this.dateTo.value,
				de: selectedlocations,
				dx: this.dx.value,
				mode: this.mode.value,
				band: this.band.value,
				qslSent: this.qslSent.value,
				qslReceived: this.qslReceived.value,
				qslSentMethod: this.qslSentMethod.value,
				qslReceivedMethod: this.qslReceivedMethod.value,
				iota: this.iota.value,
				operator: this.operator.value,
				dxcc: this.dxcc.value,
				propmode: this.propmode.value,
				gridsquare: this.gridsquare.value,
				state: this.state.value,
				county: this.county.value,
				qsoresults: qsoresults,
				sats: this.sats.value,
				orbits: this.orbits.value,
				cqzone: this.cqzone.value,
				ituzone: this.ituzone.value,
				lotwSent: this.lotwSent.value,
				lotwReceived: this.lotwReceived.value,
				clublogSent: this.clublogSent.value,
				clublogReceived: this.clublogReceived.value,
				eqslSent: this.eqslSent.value,
				eqslReceived: this.eqslReceived.value,
				dclSent: this.dclSent.value,
				dclReceived: this.dclReceived.value,
				qslvia: $('[name="qslvia"]').val(),
				sota: this.sota.value,
				pota: this.pota.value,
				wwff: this.wwff.value,
				qslimages: this.qslimages.value,
				dupes: this.dupes.value,
				dupedate: this.dupedate.value,
				dupemode: this.dupemode.value,
				dupeband: this.dupeband.value,
				dupesat: this.dupesat.value,
				dupedateval: this.dupedateval.value,
				contest: this.contest.value,
				invalid: this.invalid.value,
				continent: this.continent.value,
				comment: this.comment.value,
				qsoids: qsoids,
				dok: this.dok.value,
				qrzSent: this.qrzSent.value,
				qrzReceived: this.qrzReceived.value,
				distance: this.distance.value,
				sortcolumn: this.sortcolumn.value,
				sortdirection: this.sortdirection.value,
				duration: this.duration.value,
			},
			dataType: 'json',
			success: function (data) {
				$('#searchButton').prop("disabled", false).removeClass("running");
				loadQSOTable(data);
				if (qsoids !== '') {
					$('#checkBoxAll').prop("checked", true);
					$('#checkBoxAll').trigger('change');
				}
				$('#checkBoxAll').prop("checked", false);
			},
			error: function (data) {
				$('#searchButton').prop("disabled", false).removeClass("running");
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_error,
					message: lang_gen_advanced_logbook_an_error_occurred_while_making_request,
					type: BootstrapDialog.TYPE_DANGER,
					closable: false,
					draggable: false,
					callback: function (result) {
					}
				});
			},
		});
		$("#dupes").val("");
		$("#invalid").val("");
		return false;
	});

	$('#qsoList').on('click', 'input[type="checkbox"]', function() {
		if ($(this).is(":checked")) {
			$(this).closest('tr').addClass('activeRow');
		} else {
			$(this).closest('tr').removeClass('activeRow');
		}
	});

	$('#btnUpdateFromCallbook').click(function (event) {
		let elements = $('#qsoList tbody input:checked');
		let nElements = elements.length;
		if (nElements == 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_at_least_one_row_callbook,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/callbookDialog',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: 'Callbook options',
					size: BootstrapDialog.SIZE_NORMAL,
					cssClass: 'options',
					nl2br: false,
					message: html,
					buttons: [
					{
						label: lang_admin_close,
						cssClass: 'btn btn-sm btn-secondary',
						id: 'closeButton',
						action: function (dialogItself) {
							dialogItself.close();
						}
					},
					{
						label: 'Update',
						cssClass: 'btn btn-sm btn-primary',
						id: 'updateButton',
						action: function (dialogItself) {
							startProcessingCallbook(nElements, $('[name="gridsquareaccuracycheck"]').is(":checked"));
							dialogItself.close();
						}
					}],
					onhide: function(dialogRef){
						return;
					},
				});
			}
		});


	});

	function startProcessingCallbook(nElements, gridsquareAccuracyCheck) {
		inCallbookProcessing = true;

		callBookProcessingDialog = BootstrapDialog.show({
			title: "Retrieving callbook data for " + nElements + " QSOs",
			message: "Retrieving callbook data for " + nElements + " QSOs",
			type: BootstrapDialog.TYPE_DANGER,
			closable: false,
			draggable: false,
			buttons: [{
				label: 'Cancel',
				action: function(dialog) {
					inCallbookProcessing = false;
					dialog.close();
				}
			}]
		});
		processNextCallbookItem(gridsquareAccuracyCheck);
	}

	$('#helpButton').click(function (event) {
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/helpDialog',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_help,
					size: BootstrapDialog.SIZE_WIDE,
					cssClass: 'options',
					nl2br: false,
					message: html,
					buttons: [
					{
						label: lang_admin_close,
						cssClass: 'btn-sm btn-secondary',
						id: 'closeButton',
						action: function (dialogItself) {
							$('#optionButton').prop("disabled", false);
							dialogItself.close();
						}
					}],
					onhide: function(dialogRef){
						$('#optionButton').prop("disabled", false);
					},
				});
			}
		});

	});

	$('#deleteQsos').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_at_least_one_row_delete,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		$('#deleteQsos').prop("disabled", true);

		var table = $('#qsoList').DataTable();

		BootstrapDialog.confirm({
			title: lang_general_word_danger,
			message: lang_filter_actions_delete_warning+'<br/>'+id_list.length+lang_filter_actions_delete_warning_details,
			type: BootstrapDialog.TYPE_DANGER,
			closable: true,
			draggable: true,
			btnOKClass: 'btn-danger',
			callback: function(result) {
				if(result) {
					$.ajax({
						url: base_url + 'index.php/logbookadvanced/batchDeleteQsos',
						type: 'post',
						data: {
							'ids': JSON.stringify(id_list, null, 2)
						},
						success: function(data) {
							var deleted = (data && data.deleted) ? data.deleted : id_list;
							deleted.forEach(function(id) {
								let row = $("#qsoID-" + id);
								table.row(row).remove();
							});
							$('#deleteQsos').prop("disabled", false);
							table.draw(false);
							$('#checkBoxAll').prop("checked", false);

							var requested = (data && data.requested) ? data.requested : id_list.length;
							var skipped = requested - deleted.length;
							if (skipped > 0) {
								BootstrapDialog.alert({
									title: lang_gen_advanced_logbook_warning,
									message: lang_lba_delete_skipped.replace('%d', skipped).replace('%d', requested),
									type: BootstrapDialog.TYPE_WARNING,
								});
							}
						}
					})
				}
			},
			onhide: function(dialogRef){
				$('#deleteQsos').prop("disabled", false);
			},
		});
	});

	$('#exportAdif').click(function (event) {
		$('#exportAdif').prop("disabled", true);
		const id_list = getSelectedIdsForMap();

		xhttp = new XMLHttpRequest();
			xhttp.onreadystatechange = function() {
				var a;
				if (xhttp.readyState === 4 && xhttp.status === 200) {
					// Trick for making downloadable link
					a = document.createElement('a');
					a.href = window.URL.createObjectURL(xhttp.response);
					// Give filename you wish to download
					a.download = "logbook_export.adi";
					a.style.display = 'none';
					document.body.appendChild(a);
					a.click();
				}
			};

		if (id_list.length > 0) {
			reverse = false;
			// Post data to URL which handles post request
			xhttp.open("POST", site_url+'/logbookadvanced/export_to_adif', true);
			if(event.shiftKey) {
				reverse = true;
			}
			xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			// You should set responseType as blob for binary responses
			xhttp.responseType = 'blob';
			xhttp.send("id=" + JSON.stringify(id_list, null, 2)+"&sortcolumn=" +$('#sortcolumn').val()+"&sortdirection=" +$('#sortdirection').val()+"&reverse="+reverse);
		} else {

			// Post data to URL which handles post request
			xhttp.open("POST", site_url+'/logbookadvanced/export_to_adif_params', true);
			xhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			// You should set responseType as blob for binary responses

			xhttp.responseType = 'blob';
			xhttp.send($('#searchForm').serialize()+"&de=" +$("#de").val());
		}
		$('#exportAdif').prop("disabled", false);
	});

	$('#queueBureau').click(function (event) {
		handleQsl('Q','B', 'queueBureau');
	});

	$('#queueDirect').click(function (event) {
		handleQsl('Q','D', 'queueDirect');
	});

    $('#queueElectronic').click(function (event) {
		handleQsl('Q','E', 'queueElectronic');
	});

	$('#sentBureau').click(function (event) {
		handleQsl('Y','B', 'sentBureau');
	});

	$('#sentDirect').click(function (event) {
		handleQsl('Y','D', 'sentDirect');
	});

    $('#sentElectronic').click(function (event) {
		handleQsl('Y','E', 'sentElectronic');
	});

	$('#dontSend').click(function (event) {
		handleQsl('N','', 'dontSend');
	});
	$('#notRequired').click(function (event) {
		handleQsl('I','', 'notRequired');
	});
	$('#notReceived').click(function (event) {
		handleQslReceived('N','', 'notReceived');
	});
	$('#receivedBureau').click(function (event) {
		handleQslReceived('Y','B', 'receivedBureau');
	});

	$('#receivedDirect').click(function (event) {
		handleQslReceived('Y','D', 'receivedDirect');
	});

	$('#receivedElectronic').click(function (event) {
		handleQslReceived('Y','E', 'receivedElectronic');
	});

	$('#searchGridsquare').click(function (event) {
		quickSearch('gridsquare');
	});

	$('#searchState').click(function (event) {
		quickSearch('state');
	});

	$('#searchIota').click(function (event) {
		quickSearch('iota');
	});

	$('#searchDxcc').click(function (event) {
		quickSearch('dxcc');
	});

	$('#searchCallsign').click(function (event) {
		quickSearch('dx');
	});

	$('#searchDate').click(function (event) {
		quickSearch('date');
	});

	$('#searchCqZone').click(function (event) {
		quickSearch('cqzone');
	});

	$('#searchItuZone').click(function (event) {
		quickSearch('ituzone');
	});

	$('#searchMode').click(function (event) {
		quickSearch('mode');
	});

	$('#searchBand').click(function (event) {
		quickSearch('band');
	});

	$('#searchSota').click(function (event) {
		quickSearch('sota');
	});

	$('#searchWwff').click(function (event) {
		quickSearch('wwff');
	});

	$('#searchPota').click(function (event) {
		quickSearch('pota');
	});

	$('#searchOperator').click(function (event) {
		quickSearch('operator');
	});

	$('#dupeButton').click(function (event) {
		dupeSearchDialog();
	});

	$('#invalidButton').click(function (event) {
		invalidSearch();
	});

	$('#editButton').click(function (event) {
		editQsos();
	});

	$('#optionButton').click(function (event) {
		$('#optionButton').prop("disabled", true);
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/userOptions',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_options,
					size: BootstrapDialog.SIZE_NORMAL,
					cssClass: 'options',
					nl2br: false,
					message: html,
					onshown: function(dialog) {
					},
					buttons: [{
						label: lang_gen_advanced_logbook_save,
						cssClass: 'btn-primary btn-sm',
						id: 'saveButton',
						action: function (dialogItself) {
							$('#optionButton').prop("disabled", false);
							$('#closeButton').prop("disabled", true);
							saveOptions().then(() => {
								dialogItself.close();
								location.reload();
							}).catch(error => {
								BootstrapDialog.alert({
									title: lang_gen_advanced_logbook_error,
									message: lang_gen_advanced_logbook_error_saving_options + error,
									type: BootstrapDialog.TYPE_DANGER, // Sets the dialog style to "danger"
									closable: true,
									buttonLabel: lang_gen_advanced_logbook_close
								});
							});
						}
					},
					{
						label: lang_admin_close,
						cssClass: 'btn-sm',
						id: 'closeButton',
						action: function (dialogItself) {
							$('#optionButton').prop("disabled", false);
							dialogItself.close();
						}
					}],
					onhide: function(dialogRef){
						$('#optionButton').prop("disabled", false);
					},
				});
			}
		});
	});

	$('#qslSlideshow').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_at_least_one_row_qslcard,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}
		$('#qslSlideshow').prop("disabled", true);

		$.ajax({
			url: base_url + 'index.php/logbookadvanced/qslSlideshow',
			type: 'post',
			data: {
				ids: JSON.stringify(id_list),
			},
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_qsl_card,
					size: BootstrapDialog.SIZE_WIDE,
					cssClass: 'lookup-dialog',
					nl2br: false,
					message: html,
					onshown: function(dialog) {

					},
					buttons: [{
						label: lang_admin_close,
						action: function (dialogItself) {
							$('#qslSlideshow').prop("disabled", false);
							dialogItself.close();
						}
					}],
					onhide: function(dialogRef){
						$('#qslSlideshow').prop("disabled", false);
					},
				});
			}
		});
	});

	$('#fixCqZones').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_row_cq_zones,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		$.ajax({
			url: base_url + 'index.php/logbookadvanced/fixCqZones',
			type: 'POST',
			data: { 'ids': JSON.stringify(id_list, null, 2) },
			success: function (response) {
				if (response != []) {
					$.each(response, function(k, v) {
						updateRow(this);
						unselectQsoID(this.qsoID);
					});
				}
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_success,
					message: lang_gen_advanced_logbook_cq_zones_updated,
					type: BootstrapDialog.TYPE_SUCCESS
				});
			},
			error: function () {
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_error,
					message: lang_gen_advanced_logbook_problem_fixing_cq_zones,
					type: BootstrapDialog.TYPE_DANGER
				});
			}
		});
	});

	$('#fixItuZones').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_row_itu_zones,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		$.ajax({
			url: base_url + 'index.php/logbookadvanced/fixItuZones',
			type: 'post',
			data: {
				'ids': JSON.stringify(id_list, null, 2)
			},
			success: function (response) {
				if (response != []) {
					$.each(response, function(k, v) {
						updateRow(this);
						unselectQsoID(this.qsoID);
					});
				}
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_success,
					message: lang_gen_advanced_logbook_itu_zones_updated,
					type: BootstrapDialog.TYPE_SUCCESS
				});
			},
			error: function () {
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_error,
					message: lang_gen_advanced_logbook_problem_fixing_itu_zones,
					type: BootstrapDialog.TYPE_DANGER
				});
			}
		});
	});

	// Fix State button handler
	$('#fixState').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_row_state,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		$.ajax({
			url: base_url + 'index.php/logbookadvanced/stateDialog',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_fixing_state,
					size: BootstrapDialog.SIZE_NORMAL,
					cssClass: 'options',
					nl2br: false,
					message: html,
					buttons: [
					{
						label: lang_gen_advanced_logbook_update_now + ' <div class="ld ld-ring ld-spin"></div>',
						cssClass: 'btn btn-sm btn-primary ld-ext-right',
						id: 'updateStateButton',
						action: function (dialogItself) {
							const id_list = getSelectedIds();

							if (inStateFixing) {
								return;
							}
							inStateFixing = true;

							// Close the info dialog
							dialogItself.close();

							// Reset statistics
							stateFixStats = {fixed: 0, skipped: 0, fixedDxcc: new Set(), skippedDxcc: new Set(), skipReasons: new Set(), skippedDetails: []};

							const nElements = id_list.length;
							stateFixingDialog = BootstrapDialog.show({
								title: lang_gen_advanced_logbook_fixing_state_qsos.replace('%s', nElements),
								message: lang_gen_advanced_logbook_fixing_state_remaining.replace('%s', nElements),
								type: BootstrapDialog.TYPE_INFO,
								closable: false,
								draggable: false,
								buttons: [{
									label: lang_admin_close,
									action: function(dialog) {
										inStateFixing = false;
										dialog.close();
									}
								}]
							});
							processNextStateFixItem();
						}
					},
					{
						label: lang_admin_close,
						cssClass: 'btn btn-sm btn-secondary',
						id: 'closeStateDialogButton',
						action: function (dialogItself) {
							dialogItself.close();
						}
					}],
				});
			}
		});
	});

	// Merge QSOs button handler
	$('#mergeQsos').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0 || id_list.length === 1 || id_list.length > 2) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_row_merge_qso,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		// Load merge dialog
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/mergeDialog',
			type: 'post',
			data: {
				qsoIds: id_list
			},
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_merge_qsos,
					size: BootstrapDialog.SIZE_WIDE,
					cssClass: 'merge-dialog',
					nl2br: false,
					message: html,
					buttons: [
					{
						label: 'Merge QSOs <div class="ld ld-ring ld-spin"></div>',
						cssClass: 'btn btn-sm btn-danger ld-ext-right',
						id: 'mergeButton',
						action: function (dialogItself) {
							// Show confirmation dialog
							BootstrapDialog.confirm({
								title: lang_gen_advanced_logbook_danger,
								message: lang_gen_advanced_logbook_confirm_merge_qsos,
								type: BootstrapDialog.TYPE_DANGER,
								closable: true,
								draggable: true,
								btnCancelLabel: lang_gen_advanced_logbook_cancel,
								btnOKLabel: lang_gen_advanced_logbook_yes_merge_qsos,
								btnOKClass: 'btn-danger',
								callback: function(result) {
									if (result) {
										// User confirmed, proceed with merge
										const formData = $('#mergeForm').serialize();

										$('#mergeButton').prop("disabled", true).addClass("running");
										$('#closeMergeButton').prop("disabled", true);

										$.ajax({
											url: base_url + 'index.php/logbookadvanced/mergeQsos',
											type: 'post',
											data: formData,
											dataType: 'json',
											success: function (response) {
												dialogItself.close();
												if (response.success) {
													BootstrapDialog.alert({
														title: lang_gen_advanced_logbook_success,
														message: lang_gen_advanced_logbook_qsos_merged,
														type: BootstrapDialog.TYPE_SUCCESS,
														closable: false,
														draggable: false,
														callback: function (result) {
															$('#searchButton').click();
														}
													});
												} else {
													BootstrapDialog.alert({
														title: lang_gen_advanced_logbook_error,
														message: response.message || lang_gen_advanced_logbook_error_merging_qsos,
														type: BootstrapDialog.TYPE_DANGER,
														closable: false,
														draggable: false,
														callback: function (result) {
														}
													});
												}
											},
											error: function () {
												dialogItself.close();
												BootstrapDialog.alert({
													title: lang_gen_advanced_logbook_error,
													message: lang_gen_advanced_logbook_error_merging_qsos,
													type: BootstrapDialog.TYPE_DANGER,
													closable: false,
													draggable: false,
													callback: function (result) {
													}
												});
											}
										});
									}
								}
							});
						}
					},
					{
						label: lang_admin_close,
						cssClass: 'btn btn-sm btn-secondary',
						id: 'closeMergeButton',
						action: function (dialogItself) {
							dialogItself.close();
						}
					}],
				});
			},
			error: function () {
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_error,
					message: lang_gen_advanced_logbook_error_loading_merge_dialog,
					type: BootstrapDialog.TYPE_DANGER,
					closable: false,
					draggable: false,
					callback: function (result) {
					}
				});
			}
		});
	});

	// Attach QSOs to Contest button handler
	$('#attachContest').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_least_one,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		// Load attach dialog
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/attachContestDialog',
			type: 'post',
			data: {
				qsoIds: id_list
			},
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_attach_qsos,
					size: BootstrapDialog.SIZE_WIDE,
					cssClass: 'attach-dialog',
					nl2br: false,
					message: html,
					buttons: [
					{
						label: 'Attach QSOs <div class="ld ld-ring ld-spin"></div>',
						cssClass: 'btn btn-sm btn-info ld-ext-right',
						id: 'attachButton',
						action: function (dialogItself) {
							// User submitted, proceed with attachment
							const formData = $('#attachForm').serialize();

							$('#attachButton').prop("disabled", true).addClass("running");
							$('#closeAttachButton').prop("disabled", true);

							$.ajax({
								url: base_url + 'index.php/logbookadvanced/attachContestQsos',
								type: 'post',
								data: formData,
								dataType: 'json',
								success: function (response) {
									dialogItself.close();
									if (response.success) {
										BootstrapDialog.alert({
											title: lang_gen_advanced_logbook_success,
											message: lang_gen_advanced_logbook_qsos_attached,
											type: BootstrapDialog.TYPE_SUCCESS,
											closable: false,
											draggable: false,
											callback: function (result) {
												$('#searchButton').click();
											}
										});
									} else {
										BootstrapDialog.alert({
											title: lang_gen_advanced_logbook_error,
											message: response.message || lang_gen_advanced_logbook_error_attaching,
											type: BootstrapDialog.TYPE_DANGER,
											closable: false,
											draggable: false,
											callback: function (result) {
											}
										});
									}
								},
								error: function () {
									dialogItself.close();
									BootstrapDialog.alert({
										title: lang_gen_advanced_logbook_error,
										message: lang_gen_advanced_logbook_error_attaching,
										type: BootstrapDialog.TYPE_DANGER,
										closable: false,
										draggable: false,
										callback: function (result) {
										}
									});
								}
							});

						}
					},
					{
						label: lang_admin_close,
						cssClass: 'btn btn-sm btn-secondary',
						id: 'closeAttachButton',
						action: function (dialogItself) {
							dialogItself.close();
						}
					}],
				});
			},
			error: function () {
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_error,
					message: lang_gen_advanced_logbook_error_loading_attach_dialog,
					type: BootstrapDialog.TYPE_DANGER,
					closable: false,
					draggable: false,
					callback: function (result) {
					}
				});
			}
		});
	});

	// Detach QSOs to Contest button handler
	$('#detachContest').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_least_one,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		// Load detach dialog
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/detachContestDialog',
			type: 'post',
			data: {
				qsoIds: id_list
			},
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_detach_qsos,
					size: BootstrapDialog.SIZE_WIDE,
					cssClass: 'detach-dialog',
					nl2br: false,
					message: html,
					buttons: [
					{
						label: 'Detach QSOs <div class="ld ld-ring ld-spin"></div>',
						cssClass: 'btn btn-sm btn-info ld-ext-right',
						id: 'detachButton',
						action: function (dialogItself) {
							// User submitted, proceed with detachment
							const formData = $('#detachForm').serialize();

							$('#detachButton').prop("disabled", true).addClass("running");
							$('#closeDetachButton').prop("disabled", true);

							$.ajax({
								url: base_url + 'index.php/logbookadvanced/detachContestQsos',
								type: 'post',
								data: formData,
								dataType: 'json',
								success: function (response) {
									dialogItself.close();
									if (response.success) {
										BootstrapDialog.alert({
											title: lang_gen_advanced_logbook_success,
											message: lang_gen_advanced_logbook_qsos_detached,
											type: BootstrapDialog.TYPE_SUCCESS,
											closable: false,
											draggable: false,
											callback: function (result) {
												$('#searchButton').click();
											}
										});
									} else {
										BootstrapDialog.alert({
											title: lang_gen_advanced_logbook_error,
											message: response.message || lang_gen_advanced_logbook_error_detaching,
											type: BootstrapDialog.TYPE_DANGER,
											closable: false,
											draggable: false,
											callback: function (result) {
											}
										});
									}
								},
								error: function () {
									dialogItself.close();
									BootstrapDialog.alert({
										title: lang_gen_advanced_logbook_error,
										message: lang_gen_advanced_logbook_error_detaching,
										type: BootstrapDialog.TYPE_DANGER,
										closable: false,
										draggable: false,
										callback: function (result) {
										}
									});
								}
							});

						}
					},
					{
						label: lang_admin_close,
						cssClass: 'btn btn-sm btn-secondary',
						id: 'closeAttachButton',
						action: function (dialogItself) {
							dialogItself.close();
						}
					}],
				});
			},
			error: function () {
				BootstrapDialog.alert({
					title: lang_gen_advanced_logbook_error,
					message: lang_gen_advanced_logbook_error_loading_detach_dialog,
					type: BootstrapDialog.TYPE_DANGER,
					closable: false,
					draggable: false,
					callback: function (result) {
					}
				});
			}
		});
	});

	function dupeSearchDialog() {
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/dupeSearchDialog',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: lang_gen_advanced_logbook_dupe_search,
					size: BootstrapDialog.SIZE_NORMAL,
					cssClass: 'options',
					nl2br: false,
					message: html,
					buttons: [
						{
							label: lang_gen_advanced_logbook_search + ' <div class="ld ld-ring ld-spin"></div>',
							cssClass: 'btn btn-sm btn-primary ld-ext-right',
							id: 'dupeSearchButton',
							action: function (dialogItself) {
								dialogItself.close();
								$('#dupedate').val($('#date_check').is(':checked') ? "Y" : "N");
								$('#dupemode').val($('#mode_check').is(':checked') ? "Y" : "N");
								$('#dupeband').val($('#band_check').is(':checked') ? "Y" : "N");
								$('#dupesat').val($('#satellite_check').is(':checked') ? "Y" : "N");
								$('#dupedateval').val($('#dupe_time').length ? $('#dupe_time').val() * 60 : '1800');
								dupeSearch();
							}
						},
						{
							label: lang_admin_close,
							cssClass: 'btn btn-sm btn-secondary',
							id: 'closeDupeDialogButton',
							action: function (dialogItself) {
								dialogItself.close();
							}
						}],
				});
				// Restore the last-used time window from localStorage
				if (localStorage.hasOwnProperty(`user_${user_id}_dupetimewindow`)) {
					var savedDupeTime = parseInt(localStorage.getItem(`user_${user_id}_dupetimewindow`), 10);
					if (savedDupeTime >= 1 && savedDupeTime <= 60) {
						$('#dupe_time').val(savedDupeTime);
						$('#dupe_time_display').text(savedDupeTime + ' min');
					}
				}
			}
		});
	}

	function dupeSearch() {
		$("#dupes").val("Y");
		$('#dupeButton').prop('disabled', true).addClass('running');
		setTimeout(() => {
			$('#dupeButton').prop('disabled', false).removeClass("running");
		}, 1000);
		$('#searchForm').submit();
	}

	function invalidSearch() {
		$("#invalid").val("Y");
		$('#invalidButton').prop('disabled', true).addClass('running');
		setTimeout(() => {
			$('#invalidButton').prop('disabled', false).removeClass("running");
		}, 1000);
		$('#searchForm').submit();
	}

	function quickSearch(type) {
		var elements = $('#qsoList tbody input:checked');
		var nElements = elements.length;
		if (nElements == 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_at_least_one_row_quickfilter,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}
		if (nElements > 1) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_warning,
				message: lang_gen_advanced_logbook_select_only_one_row_quickfilter,
				type: BootstrapDialog.TYPE_WARNING,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}

		elements.each(function() {
			var currentRow = $(this).first().closest('tr');
			var col1 = '';
			switch (type) {
				case 'dxcc': 		col1 = currentRow.find('#dxcc').html(); col1 = col1.match(/\d/g); col1 = col1.join(""); break;
				case 'cqzone': 		col1 = currentRow.find('#cqzone').text(); break;
				case 'ituzone': 	col1 = currentRow.find('#ituzone').text(); break;
				case 'iota': 		col1 = currentRow.find('#iota').text(); col1 = col1.trim(); break;
				case 'state': 		col1 = currentRow.find('#state').text(); break;
				case 'dx': 			col1 = currentRow.find('#lbadx').text(); col1 = col1.match(/^([^\s]+)/gm); break;
				case 'gridsquare': 	col1 = $(currentRow).find('#dxgrid').text(); col1 = col1.substring(0, 4); break;
				case 'sota': 		col1 = $(currentRow).find('#dxsota').text(); break;
				case 'wwff': 		col1 = $(currentRow).find('#dxwwff').text(); break;
				case 'pota': 		col1 = $(currentRow).find('#dxpota').text(); break;
				case 'operator': 	col1 = $(currentRow).find('#operator').text(); break;
				case 'mode': 		col1 = currentRow.find("#lbamode").text(); break;
				case 'band': 		col1 = currentRow.find("#lbaband").text(); col1 = col1.match(/\S\w*/); break;
				case 'date': 		col1 = currentRow.find("#qsoDateTime").text(); break;
			}
			if (col1.length == 0) return;

			// Preserve selected locations before reset
			const selectedLocations = $('#de').val();

			silentReset = true;
			$('#searchForm').trigger("reset");

			// Restore selected locations after reset
			if (selectedLocations && selectedLocations.length > 0) {
				$("#de").multiselect('deselectAll');
				$('#de').multiselect('select', selectedLocations);
			}

			if (type == 'date') {
				let dateParts;
				let formattedDate;

				switch (custom_date_format) {
					case "DD/MM/YY":
						dateParts = col1.split(' ')[0].split('/');
						formattedDate = `${ensureFourDigitYear(dateParts[2])}-${dateParts[1]}-${dateParts[0]}`;
						break;

					case "DD/MM/YYYY":
						dateParts = col1.split(' ')[0].split('/');
						formattedDate = `${dateParts[2]}-${dateParts[1]}-${dateParts[0]}`;
						break;

					case "MM/DD/YY":
						dateParts = col1.split(' ')[0].split('/');
						formattedDate = `${ensureFourDigitYear(dateParts[2])}-${dateParts[0]}-${dateParts[1]}`;
						break;

					case "MM/DD/YYYY":
						dateParts = col1.split(' ')[0].split('/');
						formattedDate = `${dateParts[2]}-${dateParts[0]}-${dateParts[1]}`;
						break;

					case "DD.MM.YYYY":
						dateParts = col1.split(' ')[0].split('.');
						formattedDate = `${dateParts[2]}-${dateParts[1]}-${dateParts[0]}`;
						break;

					case "YY/MM/DD":
						dateParts = col1.split(' ')[0].split('/');
						formattedDate = `${ensureFourDigitYear(dateParts[0])}-${dateParts[1]}-${dateParts[2]}`;
						break;

					case "YYYY-MM-DD":
						dateParts = col1.split(' ')[0].split('-');
						formattedDate = `${dateParts[0]}-${dateParts[1]}-${dateParts[2]}`;
						break;

					case "MMM DD, YY":
					case "MMM DD, YYYY":
						const monthNames = {
							Jan: "01",
							Feb: "02",
							Mar: "03",
							Apr: "04",
							May: "05",
							Jun: "06",
							Jul: "07",
							Aug: "08",
							Sep: "09",
							Oct: "10",
							Nov: "11",
							Dec: "12"
						};

						// Split by space and comma
						const parts = col1.replace(',', '').split(' '); // Example: ["Dec", "03", "24"]

						const month = monthNames[parts[0]]; // Convert month name to numeric format
						const day = parts[1].padStart(2, '0'); // Ensure day has leading zero
						const year = ensureFourDigitYear(parts[2]); // Ensure 4-digit year

						formattedDate = `${year}-${month}-${day}`; // Convert to 'YYYY-MM-DD'
						break;

					default:
						dateParts = col1.split(' ')[0].split('/');
						formattedDate = `${dateParts[2]}-${dateParts[1]}-${dateParts[0]}`;
				}
				$("#dateFrom").val(formattedDate);
				$("#dateTo").val(formattedDate);
			} else {
				$("#"+type).val(col1);
			}
			updateFilterButtonStates();
			$('#searchForm').submit();
		});
	}

	function ensureFourDigitYear(year) { 				// Utility function to handle 2-digit year conversion
		if (year.length === 2) {
			return parseInt(year, 10) <= 49
				? `20${year}` // Years 00-49 are in the 21st century
				: `19${year}`; // Years 50-99 are in the 20th century
		}
		return year; // If already 4 digits, return as is
	}

	$('#printLabel').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_at_least_one_row_label,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}
		$('#printLabel').prop("disabled", true);

		$.ajax({
			url: base_url + 'index.php/logbookadvanced/startAtLabel',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: '<i class="fas fa-print me-2"></i>'+lang_label_print_options,
					size: BootstrapDialog.SIZE_NORMAL,
					cssClass: 'qso-dialog',
					nl2br: false,
					message: html,
					onshown: function(dialog) {
						// A designed label template has its own layout — hide the
						// classic text-layout options while one is selected.
						$('#print_template').on('change', function() {
							$('#classicOptions').toggle(!this.value);
						});
					},
					buttons: [{
						label: 'Print',
						cssClass: 'btn btn-primary btn-sm',
						action: function (dialogItself) {
							printlabel(id_list);
							dialogItself.close();
						}
					},
						{
						label: lang_admin_close,
						cssClass: 'btn btn-secondary btn-sm',
						action: function (dialogItself) {
							$('#printLabel').prop("disabled", false);
							dialogItself.close();
						}
					}],
					onhide: function(dialogRef){
						$('#printLabel').prop("disabled", false);
					},
				});
			}
		});
	});

	$('#printQslCard').click(function (event) {
		const id_list = getSelectedIds();

		if (id_list.length === 0) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_info,
				message: lang_gen_advanced_logbook_select_at_least_one_row_qslcard_print,
				type: BootstrapDialog.TYPE_INFO,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			return;
		}
		$('#printQslCard').prop("disabled", true);

		$.ajax({
			url: base_url + 'index.php/logbookadvanced/printQslForm',
			type: 'post',
			success: function (html) {
				BootstrapDialog.show({
					title: '<i class="fas fa-print me-2"></i>'+lang_gen_advanced_logbook_qslcard_print_option,
					size: BootstrapDialog.SIZE_NORMAL,
					cssClass: 'qso-dialog',
					nl2br: false,
					message: html,
					onshown: function(dialog) {
						// Inject the selected QSO ids into the form so the postcard
						// PDF endpoint (qslpostcard/pdfselected) receives them. The
						// form owns the action button: it picks the chosen template
						// and POSTs the ids to render the PDF download.
					var $ids = $('#qslcard_selected_ids');
					if ($ids.length) {
						$ids.empty();
						$('<input>').attr({ type: 'hidden', name: 'selected_ids' }).val(JSON.stringify(id_list)).appendTo($ids);
					}
						$('#btnPrintQslCard').off('click').on('click', function () {
							var tplId = $('#qslcard_template_id').val();
							if (!tplId) {
								return;
							}
							var $form = $('#printQslCardForm');
						$form.attr('action', base_url + 'index.php/qslpostcard/pdfselected/' + tplId);
						$form.attr('target', '_blank');
						$form[0].submit();
						dialog.close();
					});
					$('#btnPrintQslCardSave').off('click').on('click', function () {
						var tplId = $('#qslcard_template_id').val();
						if (!tplId) {
							return;
						}
						var $form = $('#printQslCardForm');
						$form.attr('action', base_url + 'index.php/qslpostcard/pdfselected/' + tplId + '?download=1');
						$form.attr('target', '_blank');
						$form[0].submit();
						dialog.close();
					});
					},
					buttons: [{
						label: lang_admin_close,
						cssClass: 'btn btn-secondary btn-sm',
						action: function (dialogItself) {
							$('#printQslCard').prop("disabled", false);
							dialogItself.close();
						}
					}],
					onhide: function(dialogRef){
						$('#printQslCard').prop("disabled", false);
					},
				});
			}
		});
	});

	$('#searchForm').on('reset', function(e) {
		if (silentReset) {
    	    silentReset = false; // reset flag
        	return; // skip submit
		}

		// Preserve selected locations during normal reset
		const selectedLocations = $('#de').val();

		requestAnimationFrame(function() {
			// Restore locations after reset
			if (selectedLocations && selectedLocations.length > 0) {
				$("#de").multiselect('deselectAll');
				$('#de').multiselect('select', selectedLocations);
			}
			updateFilterButtonStates();
		});
		setTimeout(function() {
			$('#searchForm').submit();
		});
	});

	$('#searchForm').on('change', 'input, select', function() {
		updateFilterButtonStates();
	});

	rebind_checkbox_trigger();

	$('#searchForm').submit();

	setTimeout(function() {
		updateFilterButtonStates();
	}, 100);

});

function hasActiveFilters() {
	return Object.keys(filterDefaults).some(selector => {
		const $el = $(selector);
		if (!$el.length) return false;
		const currentVal = $el.val();
		const defaultVal = filterDefaults[selector];

		// Handle arrays (multi-select)
		if (Array.isArray(currentVal)) {
			return false; // Multi-selects not currently used
		}

		// Compare current value to stored default
		return currentVal !== defaultVal;
	});
}

function updateFilterButtonStates() {
	const hasActive = hasActiveFilters();

	if (hasActive) {
		$('#filterDropdown').addClass('btn-filter-active');
	} else {
		$('#filterDropdown').removeClass('btn-filter-active');
	}
}

function rebind_checkbox_trigger() {
	$('#checkBoxAll').change(function (event) {
		if (this.checked) {
			$('#qsoList tbody tr').each(function (i) {
				selectQsoID($(this).first().closest('tr').attr('id')?.replace(/\D/g, ''));
			});
		} else {
			$('#qsoList tbody tr').each(function (i) {
				unselectQsoID($(this).first().closest('tr').attr('id')?.replace(/\D/g, ''));
			});
		}
	});
}

function handleQsl(sent, method, tag) {
	const id_list = getSelectedIdsForMap();

	if (id_list.length === 0) {
		BootstrapDialog.alert({
			title: lang_gen_advanced_logbook_info,
			message: lang_gen_advanced_logbook_select_at_least_one_row,
			type: BootstrapDialog.TYPE_INFO,
			closable: false,
			draggable: false,
			callback: function (result) {
			}
		});
		return;
	}

	$('#'+tag).prop("disabled", true);

	$.ajax({
		url: base_url + 'index.php/logbookadvanced/update_qsl',
		type: 'post',
		data: {'id': JSON.stringify(id_list, null, 2),
			'sent' : sent,
			'method' : method
		},
		success: function(data) {
			if (data != []) {
				$.each(data, function(k, v) {
					updateRow(this);
					unselectQsoID(this.qsoID);
				});
			}
			$('#'+tag).prop("disabled", false);
		}
	});
}

function handleQslReceived(sent, method, tag) {
	const id_list = getSelectedIdsForMap();

	if (id_list.length === 0) {
		BootstrapDialog.alert({
			title: lang_gen_advanced_logbook_info,
			message: lang_gen_advanced_logbook_select_at_least_one_row,
			type: BootstrapDialog.TYPE_INFO,
			closable: false,
			draggable: false,
			callback: function (result) {
			}
		});
		return;
	}

	$.ajax({
		url: base_url + 'index.php/logbookadvanced/update_qsl_received',
		type: 'post',
		data: {'id': JSON.stringify(id_list, null, 2),
			'sent' : sent,
			'method' : method
		},
		success: function(data) {
			if (data != []) {
				$.each(data, function(k, v) {
					updateRow(this);
					unselectQsoID(this.qsoID);
				});
			}
			$('#'+tag).prop("disabled", false);
		}
	});
}

function printlabel(id_list) {
	if (!Array.isArray(id_list) || id_list.length === 0) {
		BootstrapDialog.alert({
			title: lang_gen_advanced_logbook_error,
			message: lang_gen_advanced_logbook_select_at_least_one_row_label,
			type: BootstrapDialog.TYPE_DANGER,
			closable: false,
			draggable: false,
			callback: function () {}
		});
		return;
	}
	let markchecked = $('#markprinted')[0].checked;

	$.ajax({
		url: base_url + 'index.php/labels/printids',
		type: 'post',
		data: {'id': JSON.stringify(id_list, null, 2),
				'startat': $('#startat').val(),
				'grid': $('#gridlabel')[0].checked,
				'via': $('#via')[0].checked,
				'tnxmsg': $('#tnxmsg')[0].checked,
				'qslmsg': $('#qslmsg')[0].checked,
				'reference': $('#reference')[0].checked,
				'mycall': $('#mycall')[0].checked,
				'opcall': $('#opcall')[0].checked,
				'print_template': $('#print_template').length ? $('#print_template').val() : ''
			},
		xhr:function(){
			var xhr = new XMLHttpRequest();
			xhr.responseType= 'blob'
			return xhr;
		},
		success: function(data) {
			if (markchecked) {
				handleQsl('Y','B', 'sentBureau');
			} else {
				$.each(id_list, function(k, v) {
					unselectQsoID(this);
				});
			}
			$.each(BootstrapDialog.dialogs, function(id, dialog){
				dialog.close();
			});
			if(data){
				var file = new Blob([data], {type: 'application/pdf'});
				var fileURL = URL.createObjectURL(file);
				window.open(fileURL);
			}
			$('#printLabel').prop("disabled", false);
		},
		error: function (data) {
			BootstrapDialog.alert({
				title: lang_gen_advanced_logbook_error,
				message: lang_gen_advanced_logbook_label_print_error,
				type: BootstrapDialog.TYPE_DANGER,
				closable: false,
				draggable: false,
				callback: function (result) {
				}
			});
			$.each(id_list, function(k, v) {
				unselectQsoID(this);
			});
			$('#printLabel').prop("disabled", false);
		},
	});
}

function saveOptions() {
	$('#saveButton').prop("disabled", true);
	$('#closeButton').prop("disabled", true);
	return new Promise((resolve, reject) => {
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/setUserOptions',
			type: 'post',
			data: {
				datetime: $('input[name="datetime"]').is(':checked') ? true : false,
				duration: $('input[name="duration"]').is(':checked') ? true : false,
				last_modification: $('input[name="last_modification"]').is(':checked') ? true : false,
				de: $('input[name="de"]').is(':checked') ? true : false,
				dx: $('input[name="dx"]').is(':checked') ? true : false,
				mode: $('input[name="mode"]').is(':checked') ? true : false,
				rsts: $('input[name="rsts"]').is(':checked') ? true : false,
				rstr: $('input[name="rstr"]').is(':checked') ? true : false,
				band: $('input[name="band"]').is(':checked') ? true : false,
				myrefs: $('input[name="myrefs"]').is(':checked') ? true : false,
				name: $('input[name="name"]').is(':checked') ? true : false,
				qslvia: $('input[name="qslvia"]').is(':checked') ? true : false,
				qsl: $('input[name="qsl"]').is(':checked') ? true : false,
				clublog: $('input[name="clublog"]').is(':checked') ? true : false,
				lotw: $('input[name="lotw"]').is(':checked') ? true : false,
				eqsl: $('input[name="eqsl"]').is(':checked') ? true : false,
				qslmsgs: $('input[name="qslmsgs"]').is(':checked') ? true : false,
				qslmsgr: $('input[name="qslmsgr"]').is(':checked') ? true : false,
				dxcc: $('input[name="dxcc"]').is(':checked') ? true : false,
				state: $('input[name="state"]').is(':checked') ? true : false,
				county: $('input[name="county"]').is(':checked') ? true : false,
				cqzone: $('input[name="cqzone"]').is(':checked') ? true : false,
				ituzone: $('input[name="ituzone"]').is(':checked') ? true : false,
				iota: $('input[name="iota"]').is(':checked') ? true : false,
				pota: $('input[name="pota"]').is(':checked') ? true : false,
				operator: $('input[name="operator"]').is(':checked') ? true : false,
				comment: $('input[name="comment"]').is(':checked') ? true : false,
				propagation: $('input[name="propagation"]').is(':checked') ? true : false,
				contest: $('input[name="contest"]').is(':checked') ? true : false,
				gridsquare: $('input[name="gridsquare"]').is(':checked') ? true : false,
				sota: $('input[name="sota"]').is(':checked') ? true : false,
				dok: $('input[name="dok"]').is(':checked') ? true : false,
				wwff: $('input[name="wwff"]').is(':checked') ? true : false,
				sig: $('input[name="sig"]').is(':checked') ? true : false,
				sig_info: $('input[name="sig_info"]').is(':checked') ? true : false,
				region: $('input[name="region"]').is(':checked') ? true : false,
				continent: $('input[name="continent"]').is(':checked') ? true : false,
				distance: $('input[name="distance"]').is(':checked') ? true : false,
				antennaazimuth: $('input[name="antennaazimuth"]').is(':checked') ? true : false,
				antennaelevation: $('input[name="antennaelevation"]').is(':checked') ? true : false,
				qrz: $('input[name="qrz"]').is(':checked') ? true : false,
				profilename: $('input[name="profilename"]').is(':checked') ? true : false,
				stationpower: $('input[name="stationpower"]').is(':checked') ? true : false,
				gridsquare_layer: $('input[name="gridsquareoverlay"]').is(':checked') ? true : false,
				path_lines: $('input[name="pathlines"]').is(':checked') ? true : false,
				cqzone_layer: $('input[name="cqzones"]').is(':checked') ? true : false,
				ituzone_layer: $('input[name="ituzones"]').is(':checked') ? true : false,
				nightshadow_layer: $('input[name="nightshadow"]').is(':checked') ? true : false,
				qth: $('input[name="qth"]').is(':checked') ? true : false,
				frequency: $('input[name="frequency"]').is(':checked') ? true : false,
				dcl: $('input[name="dcl"]').is(':checked') ? true : false
			},
			success: function(data) {
				$('#saveButton').prop("disabled", false);
				$('#closeButton').prop("disabled", false);
				resolve(data);
			},
			error: function(error) {
				$('#saveButton').prop("disabled", false);
				reject(error);
			},
		});
	});
}
// Preset functionality
    function applyPreset(preset) {
        const dateFrom = document.getElementById('dateFrom');
        const dateTo = document.getElementById('dateTo');
        const today = new Date();

        // Format date as YYYY-MM-DD
        function formatDate(date) {
            const year = date.getUTCFullYear();
            const month = String(date.getUTCMonth() + 1).padStart(2, '0');
            const day = String(date.getUTCDate()).padStart(2, '0');
            return `${year}-${month}-${day}`;
        }

        switch(preset) {
            case 'today':
                dateFrom.value = formatDate(today);
                dateTo.value = formatDate(today);
                break;

            case 'yesterday':
                const yesterday = new Date(today);
                yesterday.setDate(yesterday.getUTCDate() - 1);
                dateFrom.value = formatDate(yesterday);
                dateTo.value = formatDate(yesterday);
                break;

            case 'last7days':
                const sevenDaysAgo = new Date(today);
                sevenDaysAgo.setDate(sevenDaysAgo.getUTCDate() - 7);
                dateFrom.value = formatDate(sevenDaysAgo);
                dateTo.value = formatDate(today);
                break;

            case 'last30days':
                const thirtyDaysAgo = new Date(today);
                thirtyDaysAgo.setDate(thirtyDaysAgo.getUTCDate() - 30);
                dateFrom.value = formatDate(thirtyDaysAgo);
                dateTo.value = formatDate(today);
                break;

            case 'thismonth':
                const firstDayOfMonth = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), 1));
                dateFrom.value = formatDate(firstDayOfMonth);
                dateTo.value = formatDate(today);
                break;

            case 'lastmonth':
                const firstDayOfLastMonth = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth() - 1, 1));
                const lastDayOfLastMonth = new Date(Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), 0));
                dateFrom.value = formatDate(firstDayOfLastMonth);
                dateTo.value = formatDate(lastDayOfLastMonth);
                break;

            case 'thisyear':
                const firstDayOfYear = new Date(Date.UTC(today.getUTCFullYear(), 0, 1));
                dateFrom.value = formatDate(firstDayOfYear);
                dateTo.value = formatDate(today);
                break;

            case 'lastyear':
                const lastYear = today.getUTCFullYear() - 1;
                const firstDayOfLastYear = new Date(Date.UTC(lastYear, 0, 1));
                const lastDayOfLastYear = new Date(Date.UTC(lastYear, 11, 31));
                dateFrom.value = formatDate(firstDayOfLastYear);
                dateTo.value = formatDate(lastDayOfLastYear);
                break;

            case 'alltime':
                dateFrom.value = '';
                dateTo.value = '';
                break;
        }
        updateFilterButtonStates();
    }

    // Reset dates function
    function resetDates() {
        const dateFrom = document.getElementById('dateFrom');
        const dateTo = document.getElementById('dateTo');
        dateFrom.value = '';
        dateTo.value = '';
        updateFilterButtonStates();
    }

	function selectAllQso1Fields() {
		$('#primaryQso').val($('input[name="primaryQsoRadio"]:checked').val());
		$('#mergeForm input[type="radio"][name="secondaryQsoRadio"]').prop('checked', false);
		$('#mergeForm input[type="radio"][name^="mergeData"]').prop('checked', false);
		$('#mergeForm input[type="radio"][name^="mergeData"][value="qso1"]').prop('checked', true);
	}

	function selectAllQso2Fields() {
		$('#primaryQso').val($('input[name="secondaryQsoRadio"]:checked').val());
		$('#mergeForm input[type="radio"][name="primaryQsoRadio"]').prop('checked', false);
		$('#mergeForm input[type="radio"][name^="mergeData"]').prop('checked', false);
		$('#mergeForm input[type="radio"][name^="mergeData"][value="qso2"]').prop('checked', true);
	}

	function getQsos(id) {
		$.ajax({
			url: base_url + 'index.php/logbookadvanced/getQsos',
			type: 'post',
			data: {
				id: id
			},
			success: function (data) {
				updateRow(data);
			}
		});
	}

	// Dupe search dialog: live time-window slider label (slider value is minutes).
	// Bound via delegation because the dialog HTML is injected via AJAX.
	$(document).on('input', '#dupe_time', function () {
		$('#dupe_time_display').text(this.value + ' min');
		localStorage.setItem(`user_${user_id}_dupetimewindow`, this.value);
	});

	$(document).on('change', '#date_check', function () {
		var checked = $(this).is(':checked');
		$('#dupe_time').prop('disabled', !checked);
		$('#dupe_time_group').css('opacity', checked ? 1 : 0.5);
	});
