
class WorkflowScanner extends BufferedPersonScanner {

	constructor(sfxCount, people, markColors, markUrl, recordId, fieldId) {
		
		super(sfxCount, people);
		
		this.$markDropdown = $('#qr-workflow-marklist');
		
		this.markColors = markColors;
		this.recordId = recordId;
		this.fieldId = fieldId;
		
		this.markUrl = markUrl;
	}

	getSelectedMark() {
		var selected = this.$markDropdown.find(':selected');
		return {
			id: selected.val(),
			name: selected.text()
		};
	}
	
	// [Override]
	processScan(reference) {
		
		// Note: Success hasn't been confirmed yet. Ajax[Success/Fail] below are callbacks to finalise
		// register entry submission.
		
		// Show the loading image
		this.setLoading();

		var markReceived = this.getSelectedMark();
		
		var markPostData = {
			student: this.people[reference]['id'],
			mark: markReceived['id'],
			field: this.fieldId,
			record: this.recordId
		};

		$.ajax({
			type: "POST",
			url: this.markUrl,
			data: markPostData,
			dataType: "text",
			success: (resp) => this._onWorkflowAjaxSuccess(resp, reference, markReceived),
			error: (req) => this._onWorkflowAjaxError(req)
		});
	}

	
	_onWorkflowAjaxSuccess(response, reference, markReceived) {

		if (response) { // On success we expect an empty response.
			this.onFailedScan('Server Error. Try refreshing the page if this keeps happening.');
			return;
		}
		
		// Show the mark dialog
		this.showDialog(markReceived['name'], this.markColors[markReceived['id']]);
		
		// Set buffered person scan results.
		super.processScan(reference);
	}
	
	_onWorkflowAjaxError(request) {
		this.setScanResultsToDefault();
		this.showDialog('Error: '+request.responseText);
	}
}