{Application 'MENU' logic file generated by CSPro}
PROC GLOBAL

// Arrays for dynamic value sets
array string codesString(100);
array string labels(100);

file pffFile;
file reportFile;

function launchHouseholdDataEntry()

	string pffFilename = pathname(Application) + "../Questionnaire/PopstanLFS.pff";
	setfile(pffFile,pffFilename,create);

	filewrite(pffFile,"[Run Information]");
	filewrite(pffFile,"Version=CSPro 7.1");
	filewrite(pffFile,"AppType=Entry");

	filewrite(pffFile,"[DataEntryInit]");
	filewrite(pffFile,"ShowInApplicationListing=Hidden");
	filewrite(pffFile,"StartMode=add;%s", CHOOSE_HOUSEHOLD);
	filewrite(pffFile,"Lock=CaseListing");
	
	filewrite(pffFile,"[Files]");
	filewrite(pffFile,"Application=%s","./PopstanLFS.ent");
	filewrite(pffFile,"InputData=%s","./PopstanLFS.csdb|CSPRODB");

	filewrite(pffFile,"[ExternalFiles]");
	filewrite(pffFile,"DISTRICT_DICT=%s","./resources/Districts.csdb|CSPRODB");
	filewrite(pffFile,"ISCO_DICT=%s","./resources/ISCO.csdb|CSPRODB");
	filewrite(pffFile,"ISCO_INDEX_DICT=%s","./resources/ISCO-Index.csdb|CSPRODB");

	filewrite(pffFile,"[Parameters]");
	filewrite(pffFile,"OnExit=%s","../Menu/Menu.pff");
	
	filewrite(pffFile,"TO_PROVINCE=%d", SAMPROV);
	filewrite(pffFile,"TO_DISTRICT=%d", SAMDIST);
	filewrite(pffFile,"TO_GHDWNO=%d", SAMDWNO);
	filewrite(pffFile,"TO_GHHHNO=%d", SAMHHNO);
	filewrite(pffFile,"TO_GHAREA=%d", SAMAREA);
	filewrite(pffFile,"TO_GHHRPL=%d", SAMRPL);
	filewrite(pffFile,"TO_GHINTID=%d", LOGIN);
	
	close(pffFile);

	execpff(filename(pffFile), stop);

end;

function showCompletionReport(staffCode)

	numeric totalAssigned = 0;
	numeric completed = 0;
	numeric partial = 0;
	numeric notInterviewed = 0;
	numeric notVisited = 0;

	// Loop through the sample file and increment
	// variables based on interview result	
	forcase SAMPLE_DICT where staffCode = SAMINT or staffCode = SAMSUP do
		
		inc(totalAssigned);
		if SAMFIR = 0 then
			inc(notVisited);
		elseif SAMFIR = 1 then
			inc(completed);
		elseif SAMFIR = 2 then
			inc(partial);
		else
			inc(notInterviewed);
		endif;
	endfor;
	
	// Write report to file
	string tempDirectory = pathname(Application) + "../temp/";
	dircreate(tempDirectory); // make sure directory exists
	setfile(reportFile, tempDirectory + "completionreport.txt");
	
	filewrite(reportFile, "Completion Status for %s", STAFF_NAME);
	filewrite(reportFile, "-----------------------------");
	filewrite(reportFile, "Total assigned households: %d", totalAssigned);
	filewrite(reportFile, "Completed households: %d", completed);
	filewrite(reportFile, "Partly completed: %d", partial);
	filewrite(reportFile, "Not interviewed: %d", notInterviewed);
	filewrite(reportFile, "Not visited: %d", notVisited);

	close(reportFile);

	// View report
	if getos() = 10 then
		// windows	
		execsystem(maketext("explorer.exe %s", filename(reportFile)));
	elseif getos() = 20 then
		// Android
		execsystem(maketext("view:%s", filename(reportFile)));
	else
		// Win universal
		errmsg("Reports not supported on this platform");
	endif;
		
end;

function showPopulationReport(staffCode)

	numeric male = 0;
	numeric female = 0;

	// Loop through the sample file and increment
	// to find assigned households	
	forcase SAMPLE_DICT where staffCode = SAMINT or staffCode = SAMSUP do
		// Load household from questionnaire data file and
		// update totals.
		string hhid = key(SAMPLE_DICT);
		if loadcase(POPSTANLFS_DICT, hhid) then
			male = male + count(POPSTANLFS_DICT.DEM_REC where DEMSEX = 1);
			female = female + count(POPSTANLFS_DICT.DEM_REC where DEMSEX = 2);
		endif;
	endfor;
	
	// Write report to file
	string tempDirectory = pathname(Application) + "../temp/";
	dircreate(tempDirectory); // make sure directory exists
	setfile(reportFile, tempDirectory + "populationreport.txt");
	
	filewrite(reportFile, "Total Population for %s", STAFF_NAME);
	filewrite(reportFile, "-----------------------------");
	filewrite(reportFile, "Male: %d", male);
	filewrite(reportFile, "Female: %d", female);
	filewrite(reportFile, "Total: %d", male + female);

	close(reportFile);
	

	// View report
	if getos() = 10 then
		// windows	
		execsystem(maketext("explorer.exe %s", filename(reportFile)));
	elseif getos() = 20 then
		// Android
		execsystem(maketext("view:%s", filename(reportFile)));
	else
		// Win universal
		errmsg("Reports not supported on this platform");
	endif;
		
end;

function string GetStatusIcon(status)
	if status = 0 then
		GetStatusIcon = "";
	elseif status = 1 then	
		// Complete
		GetStatusIcon = " ✓";
	elseif status = 2 then 
		// Partial
		GetStatusIcon = " ⌛";
	else
		GetStatusIcon = " ✘"
	endif;
end;

PROC MENU_FF

PROC LOGIN
preproc

// Check to see if there is saved login
if loadsetting("login") <> "" then
	LOGIN = tonumber(loadsetting("login"));
	noinput;
endif;

postproc
if loadcase(STAFF_DICT, LOGIN) then

	// Save login code to settings so we can load it when menu restarts
	savesetting("login", maketext("%d", LOGIN));
	
	if STAFF_ROLE = 1 then
		// Interviewer
		skip to INTERVIEWER_MENU;
	else
		// Supervisor
		skip to SUPERVISOR_MENU;
	endif;
else
	errmsg("Invalid login. Try again.");
	reenter;
endif;
PROC INTERVIEWER_MENU

if INTERVIEWER_MENU = 1 then
	skip to CHOOSE_HOUSEHOLD;
elseif INTERVIEWER_MENU = 2 then
	// Sync with supervisor
	if syncconnect(Bluetooth) = 1 then
	
		// Upload changes in questionnaire data file to server
		syncdata(PUT, POPSTANLFS_DICT);
		
		// Download latest pff and pen files from server
		syncfile(GET, "/Menu/Menu.pen", "Menu.pen");
		syncfile(GET, "/Menu/Menu.pff", "Menu.pff");
		syncfile(GET, "/Questionnaire/PopstanLFS.pen",
			"../Questionnaire/PopstanLFS.pen");
		syncfile(GET, "/Questionnaire/PopstanLFS.pff",
			"../Questionnaire/PopstanLFS.pff");
		
		syncdisconnect();
		
		errmsg("Sync Successfull!");
	
	endif;
	
	
else
	// Logout
	savesetting("login", "");
	stop(1);
endif;

// Show menu again
INTERVIEWER_MENU = notappl;
reenter;
PROC SUPERVISOR_MENU

if SUPERVISOR_MENU = 1 then
	// Reports
	skip to SUPERVISOR_REPORTS;	
elseif SUPERVISOR_MENU = 2 then
	// Sync HQ
	if syncconnect(CSWeb, "http://csweb.teleyah.com/api") = 1 then
	
		// Upload changes in questionnaire data file to server
		syncdata(PUT, POPSTANLFS_DICT);
		
		// Download latest pff and pen files from server
		syncfile(GET, "/PopstanLFS/Menu/Menu.pen", "Menu.pen");
		syncfile(GET, "/PopstanLFS/Menu/Menu.pff", "Menu.pff");
		syncfile(GET, "/PopstanLFS/Questionnaire/PopstanLFS.pen",
			"../Questionnaire/PopstanLFS.pen");
		syncfile(GET, "/PopstanLFS/Questionnaire/PopstanLFS.pff",
			"../Questionnaire/PopstanLFS.pff");
		
		syncdisconnect();
		
		errmsg("Sync Successfull!");
	
	endif;
	
elseif SUPERVISOR_MENU = 3 then
	// Sync interviewer
	syncserver(Bluetooth, "..");
	
elseif SUPERVISOR_MENU = 4 then
	// Review questionnaires
else
	// Logout
	savesetting("login", "");
	stop(1);
endif;

// Back to menu
SUPERVISOR_MENU = notappl;
reenter;
PROC CHOOSE_HOUSEHOLD

onfocus
// Make value set from assigned households in sample
numeric nextEntry = 1;
forcase SAMPLE_DICT where SAMINT = LOGIN do
	codesString(nextEntry) = key(SAMPLE_DICT);
		
	labels(nextEntry) = maketext("%s: %s %s", strip(SAMHNA), strip(SAMHA), GetStatusIcon(SAMFIR));
	nextEntry = nextEntry + 1;
endfor;

if nextEntry = 1 then
	// No assigned households
	errmsg("No households currently assigned");
	reenter INTERVIEWER_MENU;
endif;

codesString(nextEntry) = "";
setvalueset(CHOOSE_HOUSEHOLD, codesString, labels);

postproc
skip to HOUSEHOLD_ACTION;
PROC HOUSEHOLD_ACTION
onfocus

// Load selected case from sample file
// so that variables can be used in question text
loadcase(SAMPLE_DICT, CHOOSE_HOUSEHOLD);

postproc

if HOUSEHOLD_ACTION = 1 then
	// Open questionnaire
	launchHouseholdDataEntry();
elseif HOUSEHOLD_ACTION = 2 then
	skip to HOUSEHOLD_FINAL_RESULT;
elseif HOUSEHOLD_ACTION = 3 then
	// Display household GPS point on map 
	execsystem(maketext("gps:%f,%f", SAMGPSLAT, SAMGPSLON));
endif;

reenter
PROC HOUSEHOLD_FINAL_RESULT
preproc
// prefill with current value from sample file
loadcase(SAMPLE_DICT, CHOOSE_HOUSEHOLD);
HOUSEHOLD_FINAL_RESULT = SAMFIR;

postproc
// Verify that interview is complete by looking at main questionnaire
// data file.
if HOUSEHOLD_FINAL_RESULT = 1 and 
	(loadcase(POPSTANLFS_DICT, CHOOSE_HOUSEHOLD) = 0 or 
	 ispartial(POPSTANLFS_DICT)) then
	 
	 errmsg("Questionnaire is missing or partially complete. Please complete the interview");

endif;

// Copy from what was entered in this field to
// sample file
SAMFIR = HOUSEHOLD_FINAL_RESULT;
writecase(SAMPLE_DICT);

reenter INTERVIEWER_MENU;
PROC SUPERVISOR_REPORTS

if SUPERVISOR_REPORTS = 1 then
	// Completion report
	showCompletionReport(LOGIN);
elseif SUPERVISOR_REPORTS = 2 then
	// Population report
	showPopulationReport(LOGIN);
endif;

reenter;
