// transient flag set when a meeting is opened from history to indicate we should create a NEW meeting
var meetingNewFromHistory = false;

$(document).ready(function() {
	loadScheduleList();
}) ;

// document.addEventListener('show.bs.collapse', function (event) {
// 	const target = event.target;
// 	if (target.id.startsWith('conv_')) {
// 		const contactId = target.id.replace('conv_', '');
// 		const container = document.getElementById(`convlist_${contactId}`);
// 		if (!container.dataset.loaded) {
// 			fetch(`conversation_history.php?contact_id=${contactId}`)
// 				.then(res => res.text())
// 				.then(html => {
// 					container.innerHTML = html;
// 					container.dataset.loaded = true;
// 				})
// 				.catch(() => {
// 					container.innerHTML = "<p class='text-danger small'>Failed to load conversation history.</p>";
// 				});
// 		}
// 	}
// });

function loadScheduleList() {
	var action_status = $("#statusFilter").val() ;
	var action_date = $("#filterDate").val() ;
	var data = { "type": "q", "action_status": action_status, "action_date": action_date } ;
	var url = "request.pzx?c=" + schedule_url + "&d=" + new Date().getTime() ;
	callServer(url,"json",data,onScheduleResponse,null) ;
}

function onScheduleResponse(obj,resp) {
	if(resp.status == '9') {
		swal.fire('Error',resp.mesg,'error');
		return;
	}

	if(resp.type == "q") {
		$("#historyList").html('') ;
		$("#historyList").html(resp.data) ;
	}
}

function clearFilter() {
	$("#statusFilter").val('all') ;
	$("#filterDate").val('') ;
	loadScheduleList() ;
}

// Generic expand/collapse helper — pass the icon selector and the detail selector
function expandCollapse(iconSelector, detailSelector) {
    var $icon = $(iconSelector);
    if ($icon.attr("class") == "fa fa-sort-up") {
        $icon.attr("class", "fa fa-sort-down");
    } else {
        $icon.attr("class", "fa fa-sort-up");
    }
    $(detailSelector).slideToggle(300);
}
