import { get, post, del, put } from "../config/request";
import axiosAPI from "../config/axios";
import { createLog } from "./log";

export const getPelaporan = async (token, query = {}) => {
	try {
		let url = "/laporan";
		if (query != null) {
			const { jadwal, evaluasi, pt_id, delegasi, aktif, sanksi, all } = query;
			url += "?";
			const parseURL = [];
			if (jadwal) {
				parseURL.push(`jadwal=true`);
			} else if (evaluasi) {
				parseURL.push(`evaluasi=true`);
			} else if (sanksi) {
				parseURL.push(`sanksi=true`);
			}
			if (all) {
				parseURL.push(`all=true`);
			}
			if (delegasi) {
				parseURL.push(`delegasi=true`);
			}

			if (pt_id) {
				parseURL.push(`pt_id=${pt_id}`);
			}
			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 getOneLaporan = async (token, id, query = {}) => {
	try {
		let url = `/laporan/${id}`;
		if (query != null) {
			const { aktif, all } = query;
			url += "?";
			const parseURL = [];
			if (all) {
				parseURL.push(`all=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 getPelaporanPublic = async ({ number, noHp }) => {
	try {
		const res = await get(`/pelaporan/public?number=${number}&noHp=${noHp}`);
		return res.data;
	} catch (error) {
		console.log("error", error.response.data);
		return false;
	}
};

export const createPelaporan = async (token, data) => {
	const res = await axiosAPI.post("/laporan/create", data, { headers: { Authorization: token } });
	await createLog(token, { aktivitas: "Berhasil Membuat pelaporan" });
	return res.data;
};

// export const createLaporanPublic = async (data) => {
// 	try {
// 		const res = await axiosAPI.post("/public/laporan/create", data);
// 		return res.data;
// 	} catch (error) {
// 		console.log("error", error.response.data);
// 		return false;
// 	}
// };

export const createLaporanPublic = async (token, data) => {
	try {
		const res = await axiosAPI.post("/public/laporan/create", data, { headers: { Authorization: token } });
		return res.data;
	} catch (error) {
		console.log("error", error.response.data);
		return false;
	}
};

// export const addStatus = async ({ number, ptId }, data) => {
// 	try {
// 		const res = await post(`/pelaporan/status/add?number=${number}&ptId=${ptId}`, data);
// 		return res.data;
// 	} catch (error) {
// 		console.log("error", error);
// 		return false;
// 	}
// };

export const updateLaporan = async (token, id, data) => {
	try {
		const res = await axiosAPI.put(`/laporan/update/${id}`, data, { headers: { Authorization: token } });
		if (data.aktif === "false") await createLog(token, { aktivitas: `Berhasil menutup laporan, id: ${id}` });
		await createLog(token, { aktivitas: `Berhasil mengubah data laporan, id: ${id}` });
		return res.data;
	} catch (error) {
		console.log("error", error.response.data);
		await createLog(token, { aktivitas: `Gagal mengubah data pelaporan, id: ${id}` });
		return false;
	}
};

export const jumlahLaporan = async (token) => {
	try {
		const res = await axiosAPI.get(`/laporan/jumlah`, { headers: { Authorization: token } });
		return res.data;
	} catch (error) {
		console.log("error", error.response.data);
		return false;
	}
};
export const getlaporanByPembina = async (token, idPembina, query = {}) => {
	try {
		let url = `/laporan/byPembina/${idPembina}`;
		if (query != null) {
			const { idPembina, penjadwalan, pemeriksaan, sanksi, keberatan, banding, perbaikan, cabutSanksi, aktif } = query;
			url += "?";
			const parseURL = [];
			if (idPembina) {
				parseURL.push(`idPembina=${idPembina}`);
			};
			if (penjadwalan) {
				parseURL.push(`penjadwalan=true`);
			};
			if (pemeriksaan) {
				parseURL.push(`pemeriksaan=true`);
			};
			if (sanksi) {
				parseURL.push(`sanksi=true`);
			};
			if (keberatan) {
				parseURL.push(`keberatan=true`);
			};
			if (banding) {
				parseURL.push(`banding=true`);
			};
			if (perbaikan) {
				parseURL.push(`perbaikan=true`);
			};
			if (cabutSanksi) {
				parseURL.push(`cabutSanksi=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 activeLaporan = async ({ number, ptId }) => {
// 	try {
// 		const res = await put(`/pelaporan/active?number=${number}&ptId=${ptId}`);
// 		return res.data;
// 	} catch (error) {
// 		console.log("error", error);
// 		return false;
// 	}
// };

// export const changeRoleData = async ({ number, ptId }, data) => {
// 	try {
// 		const res = await post(`/pelaporan/change-role-data?number=${number}&ptId=${ptId}`, data);
// 		return res.data;
// 	} catch (error) {
// 		console.log("error", error);
// 		return false;
// 	}
// };
