/**
 * Generated by APIlite
 * https://gitea.tpsoft.org/TPsoft.org/APIlite
 *
 * 2025-10-16 01:38:51 */

class backend {
	endpoint = import.meta.env.VITE_BACKENDAPI_URL;

	/* ----------------------------------------------------
	 * General API call
	 */
	call(method, data, callback) {
		var xhttp = new XMLHttpRequest();
		xhttp.withCredentials = true;
		xhttp.onreadystatechange = function() {
			if (this.readyState == 4) {
				if (this.status == 200) {
					if (callback != null) callback(JSON.parse(this.responseText));
				} else {
					if (callback != null) callback({'status': 'ERROR', 'message': 'HTTP STATUS ' + this.status});
				}
			}
		}
		var form_data  = new FormData();
		Object.keys(data).forEach(key => {
			let val = data[key];
			if (typeof val == 'object') val = JSON.stringify(val);
			form_data.append(key, val);
		});
		xhttp.open('POST', this.endpoint + '?action=' + method);
		xhttp.send(form_data);
	}

	callPromise(method, data) {
		return new Promise((resolve, reject) => {
			this.call(method, data, function(response) {
				if (method == '__HELP__') {
					resolve(response);
					return;
				}
				if (response.status == 'OK') {
					resolve(response.data);
				} else {
					reject(response.msg);
				}
			});
		})
	}

	/* ----------------------------------------------------
	 * API actions
	 */
	help() {
		return this.callPromise('__HELP__', {});
	}

	add(title, description, status, group, priority) {
		return this.callPromise('add', {title: title, description: description, status: status, group: group, priority: priority});
	}

	update(report_id, report_data) {
		return this.callPromise('update', {report_id: report_id, report_data: report_data});
	}

	delete(report_id) {
		return this.callPromise('delete', {report_id: report_id});
	}

	get(report_id) {
		return this.callPromise('get', {report_id: report_id});
	}

	getAll(status, page) {
		return this.callPromise('getAll', {status: status, page: page});
	}

	getAllGrouped(status, page) {
		return this.callPromise('getAllGrouped', {status: status, page: page});
	}

	getArchived(page) {
		return this.callPromise('getArchived', {page: page});
	}

	updateOrdNum(ordnums) {
		return this.callPromise('updateOrdNum', {ordnums: ordnums});
	}

	updateStatus(report_id, status) {
		return this.callPromise('updateStatus', {report_id: report_id, status: status});
	}

	attachmentAdd(report_id, attachment_type, attachment_content) {
		return this.callPromise('attachmentAdd', {report_id: report_id, attachment_type: attachment_type, attachment_content: attachment_content});
	}

	attachmentUpdate(attachment_id, attachment_content) {
		return this.callPromise('attachmentUpdate', {attachment_id: attachment_id, attachment_content: attachment_content});
	}

	attachmentGetAll(report_id) {
		return this.callPromise('attachmentGetAll', {report_id: report_id});
	}

	attachmentDelete(attachment_id) {
		return this.callPromise('attachmentDelete', {attachment_id: attachment_id});
	}

	attachmentDownload(filename) {
		return this.callPromise('attachmentDownload', {filename: filename});
	}


};

export default new backend();
