import { Fragment, useEffect, useRef, useState } from 'react';

import Button from '../../components/common/button/Button';

import { Title, TableStyle, TableInfo, ListOption, IndexTableWrap } from '../../styles/Components';
import { UserIndexSearchBar, DailyDashBoard } from '../../components/IndexManage/index';

import { userIndexView, userIndexExport } from '../../apis';
import Loading from '../common/Loading';
import { ExcelDownButton } from '../common';
import { useTranslation } from 'react-i18next';
import { formatStringDate } from '../../utils';

const UserContent = () => {
	const token = sessionStorage.getItem('token');
	const { t } = useTranslation();
	let d = new Date();
	const START_DATE = new Date(new Date(d.setDate(d.getDate() - 1)).setHours(0, 0, 0, 0));
	const END_DATE = new Date();
	const [loading, setLoading] = useState(false);

	const tableRef = useRef(null);

	const [dataList, setDataList] = useState([]);
	const [resultData, setResultData] = useState([]);

	// const [sendDate, setSendDate] = useState(START_DATE);
	// const [finishDate, setFinishDate] = useState(END_DATE);

	const headers = [
		{key: 'date', label: '일자'},
		{key: 'nru', label: 'NRU'},
		{key: 'ugqCreate', label: '일자'},
		{key: 'dglc', label: '일자'},
		{key: 'dau', label: '일자'},
		{key: 'mcu', label: '일자'},
		{key: 'date', label: '일자'},
		{key: 'date', label: '일자'},
	]

	useEffect(() => {
		fetchData(START_DATE, END_DATE);
	}, []);

	// 이용자 지표 데이터
	const fetchData = async (startDate, endDate) => {
		setLoading(true);

		const startDateToLocal = formatStringDate(startDate);
		const endDateToLocal = formatStringDate(endDate);

		await userIndexView(token, startDateToLocal, endDateToLocal).then(data => {
			setDataList(data);
			setLoading(false);
		});

		// setSendDate(startDateToLocal);
		// setFinishDate(endDateToLocal);
	};

	// 검색 함수
	const handleSearch = (send_dt, end_dt) => {
		fetchData(send_dt, end_dt);
	};

	// 엑셀 다운로드
	// const handleXlsxExport = () => {
	// 	const fileName = 'Caliverse_User_Index.xlsx';
	// 	userIndexExport(token, fileName, sendDate, finishDate);
	// };

	return (
		<>
			<DailyDashBoard />
			<UserIndexSearchBar setResultData={setResultData} resultData={resultData} handleSearch={handleSearch} fetchData={fetchData} />
			<TableInfo>
				<ListOption>
					<ExcelDownButton tableRef={tableRef} fileName={t('FILE_INDEX_USER_CONTENT')} />
				</ListOption>
			</TableInfo>
			<IndexTableWrap>
				<TableStyle ref={tableRef}>
					<caption></caption>
					<thead>
						<tr>
							{/*<th width="100">국가</th>*/}
							<th width="120">일자</th>
							<th width="100">NRU</th>
							<th width="100">UGQ(생성)</th>
							<th width="100">DGLC</th>
							<th width="100">DAU</th>
							<th width="100">MCU</th>
							<th width="100">총 Playtime(HR)</th>
							<th width="100">평균 Playtime</th>
							<th width="100">WAU</th>
							<th width="100">MAU</th>
							<th width="100">ServerCount</th>
							<th width="100">DB Read Count</th>
							<th width="100">DB Write Count</th>
							<th width="100">DB Sum</th>
							{/*<th width="200">PU</th>*/}
						</tr>
					</thead>
					<tbody>
						{dataList &&
							dataList.map((data, index) => (
								<tr key={index}>
									{/*<td rowSpan={data.length}>ALL</td>*/}
									<td>{data.date}</td>
									<td>{data.nru}</td>
									<td>{data.ugqCreate}</td>
									<td>{data.dglc}</td>
									<td>{data.dau}</td>
									<td>{data.mcu}</td>
									<td>{Math.ceil(data.playtime / 3600) || 0}</td>
									<td>{Math.round(((data.playtime / 3600) / data.dau) * 100) / 100 || 0}</td>
									<td>{data.wau}</td>
									<td>{data.mau}</td>
									<td>{data.serverCount}</td>
									<td>{data.readCapacity}</td>
									<td>{data.writeCapacity}</td>
									<td>{data.readCapacity + data.writeCapacity}</td>
								</tr>
							))}
					</tbody>
				</TableStyle>
			</IndexTableWrap>
			{loading && <Loading/>}
		</>
	);
};

export default UserContent;
