//운영서비스 관리 - 메일 api 연결

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

// 메일 리스트 조회
export const MailView = async (token, mailTitle, content, sendType, sendStatus, mailType, receiveType, sendDate, endDate, order, size, currentPage) => {
	try {
		const res = await Axios.get(
			`/api/v1/mail/list?mail_title=${mailTitle}&content=${content}&send_type=${sendType}&send_status=${sendStatus}&mail_type=${mailType}&receive_type=${receiveType}&start_dt=${sendDate}&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('MailView Error', e);
		}
	}
};

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

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

export const MailCaliumTotalView = async (token) => {
	try {
		const res = await Axios.get(`/api/v1/mail/calium`, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 우편 단일 등록
export const MailSingleRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/mail`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

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

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

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

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

// 우편 다운로드 
export const MailExcelDown = async (token, filename) => {
	try {
		await Axios.get(`/api/v1/mail/excel-down?file=${filename}`, {
			headers: { Authorization: `Bearer ${token}` },
			responseType: 'blob',
		}).then(response => {
			const href = URL.createObjectURL(response.data);

			const link = document.createElement('a');
			link.href = href;
			link.setAttribute('download', `${filename}`);
			document.body.appendChild(link);
			link.click();

			document.body.removeChild(link);
			URL.revokeObjectURL(href);
		});
	} catch (e) {
		if (e instanceof Error) {
			throw new Error('MailExcelDown Error', e);
		}
	}
};

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

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

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