import React, { useState, useEffect } from 'react'; import { PlusCircle, Trash2, Edit, BarChart2, LogIn, Calendar as CalendarIcon } from 'lucide-react'; const Admin = () => { // Authentication state const [isAuthenticated, setIsAuthenticated] = useState(false); const [authError, setAuthError] = useState(''); const [loginData, setLoginData] = useState({ username: '', password: '' }); // Static credentials - in a real app, these would be stored securely const validCredentials = { username: 'admin', password: 'satta123' }; const [teams, setTeams] = useState([ { id: 1, name: 'BIKANER SUPER', time: '02:20 AM', results: { '2025-03-11': '04', '2025-03-12': '61' } }, { id: 2, name: 'DESAWAR', time: '05:00 AM', results: { '2025-03-11': '79', '2025-03-12': '55' } }, { id: 3, name: 'FARIDABAD', time: '06:00 PM', results: { '2025-03-11': '78', '2025-03-12': '98' } }, { id: 4, name: 'GHAZIABAD', time: '09:30 PM', results: { '2025-03-11': '19', '2025-03-12': '23' } }, { id: 5, name: 'GALI', time: '11:30 PM', results: { '2025-03-11': '72', '2025-03-12': 'XX' } }, ]); const [selectedTeam, setSelectedTeam] = useState(null); const [showAddForm, setShowAddForm] = useState(false); const [showEditForm, setShowEditForm] = useState(false); const [showChartView, setShowChartView] = useState(false); const [showCalendar, setShowCalendar] = useState(false); const [formData, setFormData] = useState({ name: '', time: '', result: '' }); const [dates, setDates] = useState(['2025-03-11', '2025-03-12']); const [currentDate, setCurrentDate] = useState('2025-03-12'); // Calendar state const [calendarYear, setCalendarYear] = useState(new Date().getFullYear()); const [calendarMonth, setCalendarMonth] = useState(new Date().getMonth()); // Handle login input changes const handleLoginInputChange = (e) => { const { name, value } = e.target; setLoginData({ ...loginData, [name]: value }); setAuthError(''); }; // Handle login submission const handleLogin = (e) => { e.preventDefault(); if (loginData.username === validCredentials.username && loginData.password === validCredentials.password) { setIsAuthenticated(true); setAuthError(''); } else { setAuthError('Invalid username or password'); } }; // Handle logout const handleLogout = () => { setIsAuthenticated(false); setLoginData({ username: '', password: '' }); }; // Handle input changes for team forms const handleInputChange = (e) => { const { name, value } = e.target; setFormData({ ...formData, [name]: value }); }; // Add new team const handleAddTeam = () => { const newTeam = { id: teams.length + 1, name: formData.name, time: formData.time, results: { [dates[0]]: '', [dates[1]]: '' } }; setTeams([...teams, newTeam]); setFormData({ name: '', time: '', result: '' }); setShowAddForm(false); }; // Delete team const handleDeleteTeam = (id) => { setTeams(teams.filter(team => team.id !== id)); }; // Select team for editing const handleSelectTeam = (team) => { setSelectedTeam(team); setFormData({ name: team.name, time: team.time, result: team.results[currentDate] || '' }); setShowEditForm(true); setShowChartView(false); }; // Update team const handleUpdateTeam = () => { const updatedTeams = teams.map(team => { if (team.id === selectedTeam.id) { const updatedResults = { ...team.results }; updatedResults[currentDate] = formData.result; return { ...team, name: formData.name, time: formData.time, results: updatedResults }; } return team; }); setTeams(updatedTeams); setShowEditForm(false); setSelectedTeam(null); setFormData({ name: '', time: '', result: '' }); }; // Show chart for selected team const handleViewChart = (team) => { setSelectedTeam(team); setShowChartView(true); setShowEditForm(false); }; // Generate mock chart data for the selected team const generateChartData = () => { if (!selectedTeam) return []; // Generate some random data for demonstration const mockData = []; const currentDate = new Date(); for (let i = 0; i < 30; i++) { const date = new Date(currentDate); date.setDate(date.getDate() - i); const dateStr = date.toISOString().split('T')[0]; mockData.unshift({ date: dateStr, result: Math.floor(Math.random() * 100).toString().padStart(2, '0') }); } return mockData; }; // Calendar helpers const getDaysInMonth = (year, month) => { return new Date(year, month + 1, 0).getDate(); }; const getFirstDayOfMonth = (year, month) => { return new Date(year, month, 1).getDay(); }; const handlePrevMonth = () => { if (calendarMonth === 0) { setCalendarMonth(11); setCalendarYear(calendarYear - 1); } else { setCalendarMonth(calendarMonth - 1); } }; const handleNextMonth = () => { if (calendarMonth === 11) { setCalendarMonth(0); setCalendarYear(calendarYear + 1); } else { setCalendarMonth(calendarMonth + 1); } }; const handleDateSelect = (day) => { const selectedDate = new Date(calendarYear, calendarMonth, day); const formattedDate = selectedDate.toISOString().split('T')[0]; // Check if the date is in the dates array if (!dates.includes(formattedDate)) { // Add the date to the dates array const newDates = [...dates, formattedDate].sort(); setDates(newDates); // Update teams with the new date const updatedTeams = teams.map(team => { const updatedResults = { ...team.results }; if (!updatedResults[formattedDate]) { updatedResults[formattedDate] = ''; } return { ...team, results: updatedResults }; }); setTeams(updatedTeams); } setCurrentDate(formattedDate); setShowCalendar(false); }; // Calendar component const CalendarComponent = () => { const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; const daysInMonth = getDaysInMonth(calendarYear, calendarMonth); const firstDay = getFirstDayOfMonth(calendarYear, calendarMonth); const renderCalendarDays = () => { const days = []; // Add empty cells for days before the first day of the month for (let i = 0; i < firstDay; i++) { days.push(
); } // Add cells for each day of the month for (let day = 1; day <= daysInMonth; day++) { const date = new Date(calendarYear, calendarMonth, day).toISOString().split('T')[0]; const isSelected = dates.includes(date); const isCurrentDate = date === currentDate; days.push(| Date | Result |
|---|---|
| {new Date(item.date).toLocaleDateString()} | {item.result} |
| Games List |
{new Date(dates[0]).toLocaleDateString()} {new Date(dates[0]).toLocaleDateString("en-US", {weekday: 'short'})} |
{new Date(dates[1]).toLocaleDateString()} {new Date(dates[1]).toLocaleDateString("en-US", {weekday: 'short'})} |
Actions |
|---|---|---|---|
|
{team.name}
at {team.time}
|
{team.results[dates[0]] || 'XX'} | {team.results[dates[1]] || 'XX'} |
|