'use client';

import './style.scss';
import { useState, useEffect } from 'react';
import { LoginLogType } from '@/constants/common';
import { fetchApi, getDateTime } from '@/lib/utils/client';
import type { ExpLogsResponse } from '@/types/response/account/expLogs';
import Loading from '@/app/component/Loading';
import Pagination from '@/app/component/Pagination';
import NavTabs from '../navTabs';

export default function ExpLogs()
{
	const [error, setError] = useState<string>('');
	const [loading, setLoading] = useState<boolean>(true);
	const [page, setPage] = useState<number>(1);
	const [type, setType] = useState<LoginLogType>(LoginLogType.Today);
	const [data, setData] = useState<ExpLogsResponse>({
		total: 0,
		list: []
	});

	useEffect(() => {
		if (error) {
			alert(error);
			setError('');
		}
	}, [error]);

	useEffect(() => {
		setLoading(true);
		fetchApi<ExpLogsResponse>(`/api/mypage/exp-logs?type=${type}&page=${page}&perPage=20`).then((res) => {
			setData(res.data!);
		}).catch(err => {
			setError(err.message);
		}).finally(() => {
			setLoading(false);
		});
	}, [type, page]);

	useEffect(() => {
		setPage(1);
	}, [type]);

	const tabItems = [
		{ label: "오늘", value: LoginLogType.Today },
		{ label: "1주일", value: LoginLogType.Week },
		{ label: "1개월", value: LoginLogType.Month },
		{ label: "3개월", value: LoginLogType.QuarterYear },
		{ label: "6개월", value: LoginLogType.HalfYear }
	];

	return (
		<>
		<NavTabs />

		<div id="expLogs">
			{ loading && <Loading /> }

			<h1>경험치 내역</h1>
			<div className="list-header">
				<div className="summary">합계： {data.total}</div>
				<div id="expTypeTab">
					{tabItems.map((item, i) => (
						<button type="button" key={i} className={type === item.value ? 'active' : ''}
							onClick={() => setType(item.value)}>{item.label}
						</button>
					))}
				</div>
			</div>

			<section className="mypage-list">
				<article>
					<ul>
						<li>일시</li>
						<li>사유</li>
						<li>변동</li>
						<li>잔액</li>
					</ul>
				</article>
				<article>
					{data.list.length > 0 ? (
						data.list.map((row) => (
							<section key={row.id}>
								{/* PC */}
								<ol>
									<li>{getDateTime(row.createdAt)}</li>
									<li className="reason">{row.reason}</li>
									<li>
										<span className={row.amount > 0 ? 'amount-plus' : 'amount-minus'}>
											{row.amount > 0 ? `+${row.amount}` : row.amount}
										</span>
									</li>
									<li>{row.balance.toLocaleString()}</li>
								</ol>

								{/* Mobile */}
								<dl hidden>
									<dt>{row.reason}</dt>
									<dd>
										<ul>
											<li>
												<span className={row.amount > 0 ? 'amount-plus' : 'amount-minus'}>
													{row.amount > 0 ? `+${row.amount}` : row.amount}
												</span>
											</li>
											<li>잔액 {row.balance.toLocaleString()}</li>
											<li>{getDateTime(row.createdAt)}</li>
										</ul>
									</dd>
								</dl>
							</section>
						))
					) : (
						<p className="empty">기록이 없습니다.</p>
					)}
				</article>
			</section>

			{data.list.length > 0 && (
				<Pagination total={data.total} page={page} perPage={20} onChange={setPage} />
			)}
		</div>
		</>
	);
}
