import axiosAPI from "../config/axios";
import { createLog } from "./log";

export const createSanksi = async (token, id, data) => {
	const res = await axiosAPI.post(`/sanksi/create/${id}`, data, { headers: { Authorization: token } });
	await createLog(token, { aktivitas: `Berhasil melakukan penetapan sanksi, id: ${id}` });
	return res.data;
};

export const getSanksi = async (token, query = {}) => {
	try {
		let url = "/sanksi";
		if (query != {}) {
			const { keberatan, jawaban, banding, cabutSanksi, perbaikan, aktif } = query;
			url += "?";
			const parseURL = [];
			if (keberatan) parseURL.push(`keberatan=true`);
			if (banding) parseURL.push(`banding=true`);
			if (cabutSanksi) parseURL.push(`cabutSanksi=true`);
			if (perbaikan) parseURL.push(`perbaikan=true`);
			if (jawaban) parseURL.push(`jawaban=true`);
			if (aktif) parseURL.push(`aktif=${aktif}`);
			url += parseURL.join("&");
		}

		const res = await axiosAPI.get(url, { headers: { Authorization: token } });
		return res.data;
	} catch (error) {
		console.log("error", error);
		return false;
	}
};

export const getOneSanksi = async (token, id, query = {}) => {
	try {
		let url = `/sanksi/${id}`;
		if (query != {}) {
			const { banding, aktif } = query;
			url += "?";
			const parseURL = [];
			if (banding) parseURL.push(`banding=true`);
			if (aktif) parseURL.push(`aktif=${aktif}`);
			url += parseURL.join("&");
		}
		const res = await axiosAPI.get(url, { headers: { Authorization: token } });
		return res.data;
	} catch (error) {
		console.log("error", error);
		return false;
	}
};
