import api from './client';

export const apiGetTahunProject = async ({ user }: { user: string }) => {
   const response = await api.get(`mobile/project/tahun?user=${user}`);
   return response.data;
};

export const apiGetProject = async ({ user, status, search, group, kategori, page, year }: { user: string, status: string, search: string, group?: string, kategori?: string, page?: number, year?: string }) => {
   const response = await api.get(`mobile/project?user=${user}&status=${status}&group=${group}&search=${search}&cat=${kategori}&page=${page}&year=${year}`);
   return response.data;
};

export const apiGetProjectOne = async ({ user, cat, id }: { user: string, cat: 'data' | 'progress' | 'task' | 'file' | 'member' | 'link', id: string }) => {
   const response = await api.get(`mobile/project/${id}?user=${user}&cat=${cat}`);
   return response.data;
};

export const apiCreateProject = async (data: FormData) => {
   const response = await api.post(`/mobile/project`, data, {
      headers: { 'Content-Type': 'multipart/form-data' },
   })
   return response.data;
};

export const apiEditProject = async (data: { name: string, user: string }, id: string) => {
   const response = await api.put(`/mobile/project/${id}`, data)
   return response.data;
};

export const apiReportProject = async (data: { report: string, user: string }, id: string) => {
   const response = await api.put(`/mobile/project/${id}/lainnya`, data)
   return response.data;
};

export const apiCancelProject = async (data: { user: string, reason: string }, id: string) => {
   const response = await api.delete(`mobile/project/${id}`, { data })
   return response.data
};

export const apiDeleteProject = async (data: { user: string }, id: string) => {
   const response = await api.delete(`/mobile/project/${id}/lainnya`, { data })
   return response.data;
};

export const apiAddMemberProject = async ({ data, id }: { data: { user: string, member: any[] }, id: string }) => {
   const response = await api.post(`/mobile/project/${id}/member`, data)
   return response.data;
};

export const apiDeleteProjectMember = async (data: { user: string, idUser: string }, id: string) => {
   const response = await api.delete(`mobile/project/${id}/member`, { data })
   return response.data
};

export const apiAddLinkProject = async (data: { user: string, link: string }, id: string) => {
   const response = await api.post(`/mobile/project/${id}/link`, data)
   return response.data;
};

export const apiDeleteLinkProject = async (data: { idLink: string, user: string }, id: string) => {
   const response = await api.delete(`/mobile/project/${id}/link`, { data })
   return response.data;
};

export const apiAddFileProject = async ({ data, id }: { data: FormData, id: string }) => {
   const response = await api.post(`/mobile/project/file/${id}`, data, {
      headers: { 'Content-Type': 'multipart/form-data' },
   })
   return response.data;
};

export const apiCheckFileProject = async ({ data, id }: { data: FormData, id: string }) => {
   const response = await api.put(`/mobile/project/file/${id}`, data, {
      headers: { 'Content-Type': 'multipart/form-data' },
   })
   return response.data;
};

export const apiDeleteFileProject = async (data: { user: string }, id: string) => {
   const response = await api.delete(`/mobile/project/file/${id}`, { data })
   return response.data;
};

export const apiCreateProjectTask = async ({ data, id }: { data: { name: string, dateStart: string, user: string, dateEnd: string, dataDetail: any[] }, id: string }) => {
   const response = await api.post(`/mobile/project/${id}`, data)
   return response.data;
};

export const apiUpdateStatusProjectTask = async (data: { user: string, status: number, idProject: string }, id: string) => {
   const response = await api.put(`mobile/project/detail/${id}`, data)
   return response.data
};

export const apiDeleteProjectTask = async (data: { user: string, idProject: string }, id: string) => {
   const response = await api.delete(`mobile/project/detail/${id}`, { data })
   return response.data
};

export const apiGetProjectTask = async ({ user, id, cat }: { user: string, id: string, cat?: string }) => {
   const response = await api.get(`mobile/project/detail/${id}?user=${user}${cat ? `&cat=${cat}` : ""}`);
   return response.data;
};

export const apiEditProjectTask = async ({ data, id }: { data: { title: string, dateStart: string, user: string, dateEnd: string, dataDetail: any[] }, id: string }) => {
   const response = await api.post(`/mobile/project/detail/${id}`, data)
   return response.data;
};

export const apiGetProjectTaskFile = async ({ user, id }: { user: string, id: string }) => {
   const response = await api.get(`/mobile/project/task/file/${id}`, { params: { user } })
   return response.data;
};

export const apiAddProjectTaskFile = async ({ data, id }: { data: FormData, id: string }) => {
   const response = await api.post(`/mobile/project/task/file/${id}`, data, {
      headers: { 'Content-Type': 'multipart/form-data' },
   })
   return response.data;
};

export const apiLinkProjectTaskFile = async ({ user, idFile, id }: { user: string, idFile: string, id: string }) => {
   const response = await api.patch(`/mobile/project/task/file/${id}`, { user, idFile })
   return response.data;
};

export const apiDeleteProjectTaskFile = async (data: { user: string }, id: string) => {
   const response = await api.delete(`/mobile/project/task/file/${id}`, { data })
   return response.data;
};

export const apiGetProjectTaskApprovals = async ({ user, id }: { user: string, id: string }) => {
   const response = await api.get(`/mobile/project/task/${id}/approval`, { params: { user } })
   return response.data;
};

export const apiSubmitProjectTask = async ({ user, id }: { user: string, id: string }) => {
   const response = await api.post(`/mobile/project/task/${id}/approval`, { user })
   return response.data;
};

export const apiApproveRejectProjectTask = async ({ user, id, action, note }: { user: string, id: string, action: 'approve' | 'reject', note?: string }) => {
   const response = await api.put(`/mobile/project/task/${id}/approval`, { user, action, note })
   return response.data;
};
