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

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

			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) => {
	try {
		const res = await axiosAPI.get(`/laporan/${id}`, { 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 } });
	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 } });
		return res.data;
	} catch (error) {
		console.log("error", error.response.data);
		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;
// 	}
// };
