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.ptId && query.noSanksi) {
			url += `?noSanksi=${query.noSanksi}&ptId=${query.ptId}`; // Detail
		} else if (query.ptId && query.keberatan && query.jawaban) {
			url += `?ptId=${query.ptId}&keberatan=true&jawaban=true`; // untuk PT Jawaban Keberatan
		} else if (query.ptId && query.keberatan) {
			url += `?ptId=${query.ptId}&keberatan=true`; // untuk PT Keberatan & Detail
		} else if (query.keberatan) {
			url += `?keberatan=true`;
		} else if (query.ptId) {
			url += `?ptId=${query.ptId}`; // Untuk LLDIKTI
		}

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