//운영서비스 관리 - 이용자 제재 리스트 api 연결

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

// 블랙리스트 조회
export const BlackListView = async (token, searchType, data, email, status, sanctions, period, order, size, currentPage) => {
	try {
		const res = await Axios.get(
			`/api/v1/black-list/list?search_type=${searchType ? searchType : ''}
			&search_key=${data ? data : ''}
			&email=${email ? email : ''}
			&status=${status ? status : ''}
			&sanctions=${sanctions ? sanctions : ''}
			&period=${period ? period : ''}
			&orderby=${order}
			&page_no=${currentPage}
			&page_size=${size}
			`,
			{
				headers: { Authorization: `Bearer ${token}` },
			},
		);

		return res.data.data;
	} catch (e) {
		if (e instanceof Error) {
			
		}
	}
};

// 블랙 리스트 상세 조회
export const BlackListDetail = async (token, id) => {
	try {
		const res = await Axios.get(`api/v1/black-list/detail/${id}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 제재 삭제
export const BlackListDelete = async (token, params) => {
	try {
		const res = await Axios.delete(`api/v1/black-list`, {
			headers: { Authorization: `Bearer ${token}` },
			data: { list: params },
		});

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

// 제재 등록
export const BlackListRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/black-list`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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


// 엑셀 업로드
export const BlackListMultipleUpload = async (token, file) => {
    const exelFile = new FormData()
    exelFile.append('file', file)
	try {
		const res = await Axios.post(`/api/v1/black-list/excel-upload`, exelFile, {
			headers: { 
				"Content-Type": "multipart/form-data", 
				Authorization: `Bearer ${token}` 
			},
		});

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

export const BlackListExcelDown = async (token, filename) => {
	try {
		await Axios.get(`/api/v1/black-list/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('BlacklistExcelDown Error', e);
		}
	}
};