//사용자 관리 - 그룹 조회 api 연결

import { Axios } from '../utils';

// 그룹 권한 조회
export const GroupViewList = async (token, orderBy, size, currentPage) => {
	try {
		const res = await Axios.get(`/api/v1/groups/list?orderby=${orderBy ? orderBy : 'DESC'}
		&page_no=${size}&page_size=${currentPage}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('GroupViewList Error', e);
		}
	}
};

// 그룹 상세 권한 조회
export const GroupDetailViewList = async (token, id) => {
	try {
		const res = await Axios.get(`/api/v1/groups/detail/${id}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('GroupViewList Error', e);
		}
	}
};

// 권한 그룹 등록
export const GroupRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/groups`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});
		return res;
	} catch (e) {
		if (e instanceof Error) {
			// e.response.data.message.map(message => alert(message));
		}
	}
};

// 권한 그룹 선택 삭제
export const GroupDelete = async (token, params) => {
	try {
		const res = await Axios.delete('/api/v1/groups', {
			headers: { Authorization: `Bearer ${token}` },
			data: { list: params },
		});

		return res;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('GroupDelete', e);
		}
	}
};

// 권한 수정
export const GroupModify = async (token, id, params) => {
	try {
		const res = await Axios.put(
			`/api/v1/groups/${id}`,
			{ list: params },
			{
				headers: { Authorization: `Bearer ${token}` },
			},
		);
		return res;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('GroupModify Error', e);
		}
	}
};
