//운영서비스 관리 - 메뉴배너 api 연결

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

// 리스트 조회
export const MenuBannerView = async (token, searchData, status, startDate, endDate, order, size, currentPage) => {
	try {
		const res = await Axios.get(
			`/api/v1/menu/banner/list?search_data=${searchData}&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('MenuBannerView Error', e);
		}
	}
};

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

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

// 등록
export const MenuBannerSingleRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/menu/banner`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

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

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

// 삭제
export const MenuBannerDelete = async (token, id) => {
	try {
		const res = await Axios.delete(`/api/v1/menu/banner/delete?id=${id}`, {
			headers: { Authorization: `Bearer ${token}` }
		});

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

export const MenuImageUpload = async (token, file) => {
	const exelFile = new FormData();
	exelFile.append('file', file);
	try {
		const res = await Axios.post(`/api/v1/menu/image-upload`, exelFile, {
			headers: {
				'Content-Type': 'multipart/form-data',
				Authorization: `Bearer ${token}`,
			},
		});
		return res.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('MenuImageUpload', e);
		}
	}
};

export const MenuImageDelete = async (token, filename) => {
	try {
		const res = await Axios.get(`/api/v1/menu/image-delete?file=${filename}`,  {
			headers: {Authorization: `Bearer ${token}`},
		});
		return res.data;
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('MenuImageDelete', e);
		}
	}
};
