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

export const getPelaporan = async (query = {}) => {
	try {
		let url = "/pelaporan";
		if (query != null) {
			const { number, ptId, penjadwalan, pemeriksaan, active, role, orgId } = query;
			url += "?";
			const parseURL = [];
			if (number) parseURL.push(`number=${number}`);
			if (ptId) parseURL.push(`ptId=${ptId}`);
			if (penjadwalan) parseURL.push(`penjadwalan=true`);
			if (pemeriksaan) parseURL.push(`pemeriksaan=true`);
			if (active) parseURL.push(`active=${active}`);
			if (role) {
				parseURL.push(`role=${role}`);
				if (role === "lldikti" && orgId) parseURL.push(`orgId=${orgId}`);
			}
			url += parseURL.join("&");
		}

		const res = await get(url);
		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 (data) => {
	try {
		const res = await axiosAPI.post("/pelaporan/create", data);
		return res.data;
	} catch (error) {
		console.log("error", error);
		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 removeLaporan = async ({ number, ptId }) => {
	try {
		const res = await del(`/pelaporan/remove?number=${number}&ptId=${ptId}`);
		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;
	}
};
