'use client';

import '../style.scss';
import { useSearchParams } from 'next/navigation';

export default function ChargeFailPage()
{
	const searchParams = useSearchParams();
	const errorCode = searchParams.get('errorCode') || '';
	const errorMessage = searchParams.get('errorMessage') || '결제가 취소되었거나 실패했습니다.';

	if (errorCode === '9999') {
		return window.location.replace('/charge');
	}

	const handleClose = () => {
		if (window.opener) {
			window.close();
		} else {
			window.location.href = '/';
		}
	};

	return (
		<div className="charge-page">
			<div className="charge-page__card">
				<div className="charge-page__result">
					<div className="charge-page__result-icon charge-page__result-icon--error">✕</div>
					<p className="charge-page__result-message">{errorMessage}</p>
					{errorCode && <p className="charge-page__result-code">오류 코드: {errorCode}</p>}
					<button type="button" className="charge-page__submit" onClick={handleClose}>
						닫기
					</button>
				</div>
			</div>
		</div>
	);
}