//운영서비스 관리 - 이벤트 api 연결

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

// 이벤트 리스트 조회
export const EventView = async (token, title, content, status, startDate, endDate, order, size, currentPage) => {
	try {
		const res = await Axios.get(
			`/api/v1/event/list?title=${title}&content=${content}&status=${status}&start_dt=${startDate}&end_dt=${endDate}&orderby=${order}&page_no=${currentPage}
			&page_size=${size}`,
			{
				headers: { Authorization: `Bearer ${token}` },
			},
		);

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

// 이벤트 상세보기
export const EventDetailView = async (token, id) => {
	try {
		const res = await Axios.get(`/api/v1/event/detail/${id}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 이벤트 등록
export const EventSingleRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/event`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 우편 수정
export const EventModify = async (token, id, params) => {
	try {
		const res = await Axios.put(`/api/v1/event/${id}`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 우편 삭제
export const EventDelete = async (token, params, id) => {
	try {
		const res = await Axios.delete(`/api/v1/event/delete`, {
			headers: { Authorization: `Bearer ${token}` },
			data: { list: params },
		});

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

// 이벤트 우편 아이템 확인
export const EventIsItem = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/event/item`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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