//운영서비스 관리 - 랜드 경매 api 연결

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

// 랜드 경매 리스트 조회
export const LandAuctionView = async (token, landType, landData, userType, userData, landSize, status, startDate, endDate, order, size, currentPage) => {
	try {
		const res = await Axios.get(
			`/api/v1/land/auction/list?land_type=${landType}&land_data=${landData}&user_type=${userType}&user_data=${userData}&land_size=${landSize}&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('LandAuctionView Error', e);
		}
	}
};

export const LandInfoData = async (token, landType, landData, landSize, category, status, startDate, endDate, order, size, currentPage) => {
	try {
		const res = await Axios.get(
			`/api/v1/land/info?land_type=${landType}&land_data=${landData}&land_size=${landSize}&category=${category}&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('LandInfoData Error', e);
		}
	}
};

// 랜드 경매 상세보기
export const LandAuctionDetailView = async (token, id) => {
	try {
		const res = await Axios.get(`/api/v1/land/auction/detail/${id}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 랜드 경매 등록
export const LandAuctionSingleRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/land/auction`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 랜드 소유권 변경 등록
export const LandOwnedChangesRegist = async (token, params) => {
	try {
		const res = await Axios.post(`/api/v1/land/change`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 랜드 경매 수정
export const LandAuctionModify = async (token, id, params) => {
	try {
		const res = await Axios.put(`/api/v1/land/auction/${id}`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 랜드 소유권 변경 수정
export const LandOwnedChangesModify = async (token, id, params) => {
	try {
		const res = await Axios.put(`/api/v1/land/change/${id}`, params, {
			headers: { Authorization: `Bearer ${token}` },
		});

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

// 랜드 경매 삭제
export const LandAuctionDelete = async (token, params, id) => {
	try {
		const res = await Axios.delete(`/api/v1/land/auction/delete`, {
			headers: { Authorization: `Bearer ${token}` },
			data: { list: params },
		});

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

// 랜드 소유권 변경 예약 삭제
export const LandOwnerChangesDelete = async (token, params) => {
	try {
		const res = await Axios.delete(`/api/v1/land/change/delete`, {
			headers: { Authorization: `Bearer ${token}` },
			data: params,
		});

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

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

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

export const BuildingView = async (token) => {
	try {
		const res = await Axios.get(
			`/api/v1/land/building/list`,
			{
				headers: { Authorization: `Bearer ${token}` },
			},
		);

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

export const LandDetailView = async (token, id) => {
	try {
		const res = await Axios.get(`/api/v1/land/detail/${id}`, {
			headers: { Authorization: `Bearer ${token}` },
		});

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