import { addLog } from "./log";

export const createSanksi = async ({ number, ptId }, data) => {
	try {
		const res = await fetch(`http://localhost:5000/sanksi/create?number=${number}&ptId=${ptId}`, {
			method: "POST",
			body: data,
		});
		const result = await res.json();
		console.log(result);
		// addLog({ status: "SUCCESS", action: "CREATE", from: { id: result.added._id, data: "sanksi" }, description: "membuat sanksi" });
		return result;
	} catch (error) {
		console.log("error", error);
		addLog({ status: "FAIL", action: "CREATE", from: { data: "sanksi" }, description: error.message || "membuat sanksi" });
		return false;
	}
};

export const getSanksi = async (query = {}) => {
	try {
		let url = "http://localhost:5000/sanksi";
		if (query != null) {
			const { ptId, noSanksi, keberatan, jawaban, banding, active, cabutSanksi, docPerbaikan } = query;
			url += "?";
			const parseURL = [];
			if (noSanksi) parseURL.push(`noSanksi=${noSanksi}`);
			if (ptId) parseURL.push(`ptId=${ptId}`);
			if (keberatan) parseURL.push(`keberatan=true`);
			if (banding) parseURL.push(`banding=true`);
			if (cabutSanksi) parseURL.push(`cabutSanksi=true`);
			if (docPerbaikan) parseURL.push(`docPerbaikan=true`);
			if (jawaban) parseURL.push(`jawaban=true`);
			url += parseURL.join("&");
			url += "&active=true";
		}

		const res = await fetch(url);
		return await res.json();
	} catch (error) {
		console.log("error", error);
		return false;
	}
};
