import axiosAPI from "../config/axios";
import axios 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, delegasi, turunSanksi, naikSanksi } = 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 (delegasi) parseURL.push(`delegasi=true`);
			if (turunSanksi) parseURL.push(`turunSanksi=true`);
			if (naikSanksi) parseURL.push(`naikSanksi=true`);
			if (aktif === false) parseURL.push(`aktif=false`);
			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, all } = query;
			url += "?";
			const parseURL = [];
			if (banding) parseURL.push(`banding=true`);
			if (aktif === false) parseURL.push(`aktif=false`);
			if (all) { parseURL.push(`all=true`); }
			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 addRekomendasiDelegasi = async (token, id, data) => {
	try {
		const res = await axiosAPI.post(`/rekomendasi/add/${id}`, data, { headers: { Authorization: token } });
		await createLog(token, { aktivitas: `Berhasil melakukan Rekomendasi Delegasi, id: ${id}` });
		return res.data;
	} catch (error) {
		console.log("error", error);
		await createLog(token, { aktivitas: `Gagal melakukan Rekomendasi Delegasi, id: ${id}` });
		return false;
	}
};
export const editTmt = async (token, id, data) => {
	const res = await axios.put(`sanksi/tmt/update/${id}`, data, { headers: { Authorization: token } });
	await createLog(token, { aktivitas: `Berhasil perpanjangan sanksi, id: ${id}` });
	return res.data;
};
export const update = async (token, id, data) => {
	const res = await axios.put(`sanksi/update/${id}`, data, { headers: { Authorization: token } });
	await createLog(token, { aktivitas: `Berhasil Merubah Sanksi, id: ${id}` });
	return res.data;
};
