import { useState, Fragment, useRef } from 'react'; import { useRecoilValue } from 'recoil'; import { useTranslation } from 'react-i18next'; import 'react-datepicker/dist/react-datepicker.css'; import { BattleConfigView, BattleEventDelete, BattleEventDetailView, BattleEventStop, BattleEventView, BattleRewardView, } from '../../apis/Battle'; import { authList } from '../../store/authList'; import { authType, modalTypes, landAuctionStatusType, landAuctionStatus, landSize, caliumStatus, commonStatus, } from '../../assets/data'; import { Title, FormWrapper, TableStyle, TableWrapper} from '../../styles/Components'; import { CheckBox, Button, DynamicModal, Pagination, ViewTableInfo, ExcelDownButton, } from '../../components/common'; import { convertKTC, convertKTCDate, convertUTC, timeDiffMinute } from '../../utils'; import { BattleEventModal } from '../../components/ServiceManage'; import { INITIAL_PAGE_SIZE, INITIAL_PAGE_LIMIT } from '../../assets/data/adminConstants'; import { useDataFetch, useModal, useTable, withAuth } from '../../utils/hook'; import { StatusWapper, StatusLabel } from '../../styles/ModuleComponents'; import { battleEventStatus, battleRepeatType } from '../../assets/data/options'; import BattleEventSearchBar, { useBattleEventSearch, } from '../../components/ServiceManage/searchBar/BattleEventSearchBar'; import { getDateOnly, getTimeOnly, secondToMinutes } from '../../utils/date'; import { battleEventStatusType } from '../../assets/data/types'; const BattleEvent = () => { const token = sessionStorage.getItem('token'); const userInfo = useRecoilValue(authList); const { t } = useTranslation(); const tableRef = useRef(null); const [detailData, setDetailData] = useState({}); const { modalState, handleModalView, handleModalClose } = useModal({ stopConfirm: 'hidden', stopComplete: 'hidden', detail: 'hidden', deleteConfirm: 'hidden', deleteComplete: 'hidden' }); const [alertMsg, setAlertMsg] = useState(''); const [modalType, setModalType] = useState('regist'); const { searchParams, data: dataList, handleSearch, handleReset, handlePageChange, handlePageSizeChange, handleOrderByChange, updateSearchParams } = useBattleEventSearch(token, INITIAL_PAGE_SIZE); const { selectedRows, handleSelectRow, isRowSelected } = useTable(dataList?.event_list || [], {mode: 'single'}); const { data: battleConfigData } = useDataFetch(() => BattleConfigView(token), [token]); const { data: battleRewardData } = useDataFetch(() => BattleRewardView(token), [token]); const endTime = (start_dt, operation_time) =>{ const startDate = new Date(start_dt); startDate.setSeconds(startDate.getSeconds() + operation_time); return startDate; } const isStopMinutes = (time) => { const givenDate = new Date(time); const currentDate = convertUTC(new Date()); // 시간만 비교하기 위해 년,월,일을 동일하게 설정 givenDate.setFullYear(currentDate.getFullYear()); givenDate.setMonth(currentDate.getMonth()); givenDate.setDate(currentDate.getDate()); // 두 시간의 차이를 밀리초로 계산 const timeDifference = currentDate.getTime() - givenDate.getTime(); // 밀리초로 변환 const fiveMinutesInMs = 5 * 60 * 1000; return timeDifference < 0 && Math.abs(timeDifference) <= fiveMinutesInMs; } const isRunningTime = (time, operation_time) =>{ const startDate = new Date(time); const endDate = new Date(startDate.getTime() + (operation_time * 1000)); const currentDate = convertUTC(new Date()); return currentDate >= startDate && currentDate <= endDate; } const handleModalSubmit = async (type, param = null) => { switch (type) { case "regist": setModalType('regist'); handleModalView('detail'); break; case "detail": await BattleEventDetailView(token, param).then(data => { setDetailData(data.event_detail); setModalType('modify'); handleModalView('detail'); }); break; case "delete": const date_check = selectedRows.every(row => { const timeDiff = timeDiffMinute(convertKTC(row.auction_start_dt), (new Date)); return timeDiff < 3; }); if(date_check){ setAlertMsg(t('LAND_AUCTION_DELETE_DATE_WARNING')); return; } if(selectedRows[0].status === landAuctionStatusType.auction_start || selectedRows[0].status === landAuctionStatusType.stl_end){ setAlertMsg(t('LAND_AUCTION_DELETE_STATUS_WARNING')); return; } handleModalView('deleteConfirm'); break; case "stop": const select_item = selectedRows[0]; if(select_item.status === battleEventStatusType.running){ setAlertMsg(t('BATTLE_EVENT_STATUS_RUNNING_WARNING')); return; } const isStopTimeCheck = isStopMinutes(select_item.event_start_dt); if(isStopTimeCheck){ setAlertMsg(t('BATTLE_EVENT_STOP_5MINUTES_DATE_WARNING')); return; } if(isRunningTime(select_item.event_start_dt, select_item.event_operation_time)){ setAlertMsg(t('BATTLE_EVENT_STATUS_RUNNING_WARNING')); return; } handleModalView('stopConfirm'); break; case "stopConfirm": const stop_item = selectedRows[0]; await BattleEventStop(token, stop_item.id, stop_item).then(data => { handleModalClose('stopConfirm'); if(data.result === "SUCCESS") { handleModalView('stopComplete'); }else if(data.result === "ERROR_BATTLE_EVENT_STATUS_START_IMPOSSIBLE"){ setAlertMsg(t('BATTLE_EVENT_STATUS_RUNNING_WARNING')); }else{ setAlertMsg(t('STOP_FAIL')); } }).catch(reason => { setAlertMsg(t('API_FAIL')); }); break; case "deleteConfirm": let list = []; let isChecked = false; selectedRows.map(data => { // const row = dataList.list.find(row => row.id === Number(data.id)); // if(row.status !== commonStatus.wait) isChecked = true; list.push({ id: data.id, }); }); if(isChecked) { setAlertMsg(t('LAND_AUCTION_WARNING_DELETE')) handleModalClose('deleteConfirm'); return; } await BattleEventDelete(token, list).then(data => { handleModalClose('deleteConfirm'); if(data.result === "SUCCESS") { handleModalView('deleteComplete'); }else if(data.result === "ERROR_AUCTION_STATUS_IMPOSSIBLE"){ setAlertMsg(t('LAND_AUCTION_ERROR_DELETE_STATUS')); }else{ setAlertMsg(t('DELETE_FAIL')); } }).catch(reason => { setAlertMsg(t('API_FAIL')); }); break; case "deleteComplete": handleModalClose('deleteComplete'); window.location.reload(); break; case "stopComplete": handleModalClose('stopComplete'); window.location.reload(); break; case "warning": setAlertMsg('') break; } } return ( <>