function loadStationInfo() {
    $(".stationinfo").empty();
    $(".searchinfo").empty();
    $.ajax({
        url: base_url+'index.php/oqrs/get_station_info',
        type: 'post',
        data: {'station_id': $("#station").val()},
        success: function (data) {
            if (data.count > 0) {
                $(".stationinfo").append('
' + data.count + ' Qsos logged between ' + data.mindate + ' and ' + data.maxdate + '.
');
                $(".stationinfo").append('
');
                // Get the input field
                var input = document.getElementById("oqrssearch");
                // Execute a function when the user presses a key on the keyboard
                input.addEventListener("keypress", function(event) {
                // If the user presses the "Enter" key on the keyboard
                if (event.key === "Enter") {
                    // Cancel the default action, if needed
                    event.preventDefault();
                    // Trigger the button element with a click
                    document.getElementById("stationbuttonsubmit").click();
                }
                });
            } else {
                $(".stationinfo").append("No QSOs for this callsign was found!");
            }
        }
    });
}
function searchOqrs() {
    $(".searchinfo").empty();
    $.ajax({
        url: base_url+'index.php/oqrs/get_qsos',
        type: 'post',
        data: {'station_id': $("#station").val(), 'callsign': $("#oqrssearch").val().toUpperCase()},
        success: function (data) {
            $(".searchinfo").append(data);
        }
    });
}
function searchOqrsGrouped() {
    $(".searchinfo").empty();
    $.ajax({
        url: base_url+'index.php/oqrs/get_qsos_grouped',
        type: 'post',
        data: {'callsign': $("#oqrssearch").val().toUpperCase()},
        success: function (data) {
            $(".searchinfo").append(data);
            $('.qsotime').change(function() {
                var raw_time = $(this).val();
                if(raw_time.match(/^\d\[0-6]d$/)) {
                    raw_time = "0"+raw_time;
                }
                if(raw_time.match(/^[012]\d[0-5]\d$/)) {
                    raw_time = raw_time.substring(0,2)+":"+raw_time.substring(2,4);
                    $(this).val(raw_time);
                }
            });
            $('.result-table').DataTable({
                "pageLength": 25,
                responsive: false,
                ordering: false,
                "scrollY": "410px",
                "scrollCollapse": true,
                "paging": false,
                "scrollX": true,
            });
        }
    });
}
function notInLog() {
    $.ajax({
        url: base_url + 'index.php/oqrs/not_in_log',
        type: 'post',
        data: {'station_id': $("#station").val(), 'callsign': $("#oqrssearch").val().toUpperCase()},
        success: function(html) {
            $(".searchinfo").html(html);
            $('.qsotime').change(function() {
                var raw_time = $(this).val();
                if(raw_time.match(/^\d\[0-6]d$/)) {
                    raw_time = "0"+raw_time;
                }
                if(raw_time.match(/^[012]\d[0-5]\d$/)) {
                    raw_time = raw_time.substring(0,2)+":"+raw_time.substring(2,4);
                    $(this).val(raw_time);
                }
            });
        }
    }); 
}
function saveNotInLogRequest() {
    $(".alertinfo").remove();
    if ($("#emailInput").val() == '') {
        $(".searchinfo").prepend('×You need to fill out an email address!
 ×You need to fill the QSO information before submitting a request!
 
×Your not in log query has been saved!
 ');
                }
            });
        }
    }
}
function oqrsAddLine() {
    var rowCount = $('.notinlog-table tr').length;
    var $myTable = $('.notinlog-table');
    var $row = $('|
');
    var $iterator = $('').html(rowCount);
    var $date = $(' | ').html('');
    var $time = $(' | ').html('');
    var $band = $(' | ').html('');
    var $mode = $(' | ').html('');
    $row.append($iterator, $date, $time, $band, $mode);
    $myTable.append($row);
    $('.qsotime').change(function() {
        var raw_time = $(this).val();
        if(raw_time.match(/^\d\[0-6]d$/)) {
            raw_time = "0"+raw_time;
        }
        if(raw_time.match(/^[012]\d[0-5]\d$/)) {
            raw_time = raw_time.substring(0,2)+":"+raw_time.substring(2,4);
            $(this).val(raw_time);
        }
    });
}
function requestOqrs() {
    $.ajax({
        url: base_url + 'index.php/oqrs/request_form',
        type: 'post',
        data: {'station_id': $("#station").val(), 'callsign': $("#oqrssearch").val().toUpperCase()},
        success: function(html) {
            $(".searchinfo").html(html);
            /* time input shortcut */
            $('.qsotime').change(function() {
                var raw_time = $(this).val();
                if(raw_time.match(/^\d\[0-6]d$/)) {
                    raw_time = "0"+raw_time;
                }
                if(raw_time.match(/^[012]\d[0-5]\d$/)) {
                    raw_time = raw_time.substring(0,2)+":"+raw_time.substring(2,4);
                    $(this).val(raw_time);
                }
            });
            $('.result-table').DataTable({
                "pageLength": 25,
                responsive: false,
                ordering: false,
                "scrollY": "410px",
                "scrollCollapse": true,
                "paging": false,
                "scrollX": true,
            });
        }
    }); 
}
function submitOqrsRequest() {
    $(".alertinfo").remove();
    if ($("#emailInput").val() == '') {
        $(".searchinfo").prepend(' | ×You need to fill out an email address!
 ×You need to fill the QSO information before submitting a request!
 
×Your QSL request has been saved!
 ');
                }
            });
        }
    }
}
function submitOqrsRequestGrouped() {
    $(".alertinfo").remove();
    if ($("#emailInput").val() == '') {
        $(".searchinfo").prepend('×You need to fill out an email address!
 ×You need to fill the QSO information before submitting a request!
 
×Your QSL request has been saved!
 ');
                }
            });
        }
    }
}
function searchLog(callsign) {
    $.ajax({
        url: base_url + 'index.php/oqrs/search_log',
        type: 'post',
        data: {'callsign': callsign,
        },
        success: function(html) {
            BootstrapDialog.show({
                title: 'QSO List',
                size: BootstrapDialog.SIZE_WIDE,
                cssClass: 'qso-dialog',
                nl2br: false,
                message: html,
                onshown: function(dialog) {
                    $('[data-toggle="tooltip"]').tooltip();
                },
                buttons: [{
                    label: 'Close',
                    action: function (dialogItself) {
                        dialogItself.close();
                    }
                }]
            });
        }
    });
}
function searchLogTimeDate(id) {
    $.ajax({
        url: base_url + 'index.php/oqrs/search_log_time_date',
        type: 'post',
        data: {'time': $('#oqrsID_'+id+ ' td:nth-child(4)').text(),
            'date': $('#oqrsID_'+id+ ' td:nth-child(3)').text(),
            'band': $('#oqrsID_'+id+ ' td:nth-child(5)').text(),
            'mode': $('#oqrsID_'+id+ ' td:nth-child(6)').text()
        },
        success: function(html) {
            BootstrapDialog.show({
                title: 'QSO List',
                size: BootstrapDialog.SIZE_WIDE,
                cssClass: 'qso-dialog',
                nl2br: false,
                message: html,
                onshown: function(dialog) {
                    $('[data-toggle="tooltip"]').tooltip();
                },
                buttons: [{
                    label: 'Close',
                    action: function (dialogItself) {
                        dialogItself.close();
                    }
                }]
            });
        }
    });
}
function loadOqrsTable(rows) {
	var uninitialized = $('.oqrstable').filter(function() {
		return !$.fn.DataTable.fnIsDataTable(this);
	});
	uninitialized.each(function() {
	$(this).DataTable({
			searching: false,
			responsive: false,
			ordering: true,
			"scrollY": window.innerHeight - $('#searchForm').innerHeight() - 250,
			"scrollCollapse": true,
			"paging":         false,
			"scrollX": true,
			"order": [ 0, 'asc' ],
            'white-space': 'nowrap',
		});
	});
	var table = $('.oqrstable').DataTable();
	table.clear();
	
	for (i = 0; i < rows.length; i++) {
		let qso = rows[i];
		
		var data = [
			'',
			qso.requesttime,
			qso.date,
			qso.time,
			qso.band,
			qso.mode,
			qso.requestcallsign,
			qso.station_callsign,
			qso.email,
			qso.note,
			echo_qsl_method(qso.qslroute),
			echo_searchlog_button(qso.requestcallsign, qso.id),
            echo_status(qso.status),
		];
		
		let createdRow = table.row.add(data).index();
		table.rows(createdRow).nodes().to$().data('oqrsID', qso.id);
		table.row(createdRow).node().id = 'oqrsID_' + qso.id;
	}
    table.columns.adjust().draw();
}
function echo_status(status) {
	switch(status.toUpperCase()) {
		case '0': return 'Open request'; break;
		case '1': return 'Not in log request'; break;
		case '2': return 'Request done'; break;
        default: return '';
	}
}
function echo_qsl_method(method) {
	switch(method.toUpperCase()) {
		case 'B': return 'Bureau'; break;
		case 'D': return 'Direct'; break;
		case 'E': return 'Electronic'; break;
        default: return '';
	}
}
function echo_searchlog_button(callsign, id) {
    return ' ' +
    '';
}
$(document).ready(function () {
	$('#searchForm').submit(function (e) {
		$('#searchButton').prop("disabled", true);
		$.ajax({
			url: this.action,
			type: 'post',
			data: {
				de: this.de.value,
				dx: this.dx.value,
				status: this.status.value,
				oqrsResults: this.oqrsResults.value
			},
			dataType: 'json',
			success: function (data) {
				$('#searchButton').prop("disabled", false);
				loadOqrsTable(data);
			},
			error: function (data) {
				$('#searchButton').prop("disabled", false);
				BootstrapDialog.alert({
					title: 'ERROR',
					message: 'An error ocurred while making the request',
					type: BootstrapDialog.TYPE_DANGER,
					closable: false,
					draggable: false,
					callback: function (result) {
					}
				});
			},
		});
		return false;
	});
	$('.oqrstable').on('click', 'input[type="checkbox"]', function() {
		if ($(this).is(":checked")) {
			$(this).closest('tr').addClass('activeRow');
		} else {
			$(this).closest('tr').removeClass('activeRow');
		}
	});
	$('#deleteOqrs').click(function (event) {
		var elements = $('.oqrstable tbody input:checked');
		var nElements = elements.length;
		if (nElements == 0) {
			return;
		}
		$('#deleteOqrs').prop("disabled", true);
		var table = $('.oqrstable').DataTable();
		BootstrapDialog.confirm({
			title: 'DANGER',
			message: 'Warning! Are you sure you want to delete the marked OQRS request(s)?' ,
			type: BootstrapDialog.TYPE_DANGER,
			closable: true,
			draggable: true,
			btnOKClass: 'btn-danger',
			callback: function(result) {
				if(result) {
					elements.each(function() {
						let id = $(this).first().closest('tr').data('oqrsID')
						$.ajax({
							url: base_url + 'index.php/oqrs/delete_oqrs_line',
							type: 'post',
							data: {'id': id
							},
							success: function(data) {
								var row = $("#oqrsID_" + id);
								table.row(row).remove().draw(false);
							}
						});
						$('#deleteOqrs').prop("disabled", false);
					})
				}
			}
		});
	});
	$('#markOqrs').click(function (event) {
		var elements = $('.oqrstable tbody input:checked');
		var nElements = elements.length;
		if (nElements == 0) {
			return;
		}
		$('#markOqrs').prop("disabled", true);
		var table = $('.oqrstable').DataTable();
		BootstrapDialog.confirm({
			title: 'DANGER',
			message: 'Warning! Are you sure you want to mark OQRS request(s) as done?' ,
			type: BootstrapDialog.TYPE_DANGER,
			closable: true,
			draggable: true,
			btnOKClass: 'btn-danger',
			callback: function(result) {
				if(result) {
					elements.each(function() {
						let id = $(this).first().closest('tr').data('oqrsID')
						$.ajax({
							url: base_url + 'index.php/oqrs/mark_oqrs_line_as_done',
							type: 'post',
							data: {'id': id
							},
							success: function(data) {
								$('#searchForm').submit();
							}
						});
						$('#markOqrs').prop("disabled", false);
					})
				}
			}
		});
	});
	$('#checkBoxAll').change(function (event) {
		if (this.checked) {
			$('.oqrstable tbody tr').each(function (i) {
				selectQsoID($(this).data('oqrsID'))
			});
		} else {
			$('.oqrstable tbody tr').each(function (i) {
				unselectQsoID($(this).data('oqrsID'))
			});
		}
	});
	$('#searchForm').submit();
    $('#searchForm').on('reset', function(e) {
        setTimeout(function() {
            $('#searchForm').submit();
        });
    });
});
function selectQsoID(qsoID) {
	var element = $("#oqrsID_" + qsoID);
	element.find("input[type=checkbox]").prop("checked", true);
	element.addClass('activeRow');
}
function unselectQsoID(qsoID) {
	var element = $("#oqrsID_" + qsoID);
	element.find("input[type=checkbox]").prop("checked", false);
	element.removeClass('activeRow');
	$('#checkBoxAll').prop("checked", false);
}