import React, { useState, useEffect } from 'react';
const NumberLogicGenerator = () => {
const [input, setInput] = useState("");
const [error, setError] = useState("");
const [results, setResults] = useState([]);
//logic 1
// time box 2 start
const [options, setOptions] = useState([
{ id: 1, name: 'Mohsan', time: '11:00 AM' },
{ id: 2, name: 'John', time: '12:00 PM' },
{ id: 3, name: 'Alex', time: '1:00 PM' },
{ id: 4, name: 'Sarah', time: '2:00 PM' },
{ id: 5, name: 'David', time: '3:00 PM' },
{ id: 6, name: 'Emma', time: '4:00 PM' },
{ id: 7, name: 'Michael', time: '5:00 PM' },
{ id: 8, name: 'James', time: '6:00 PM' },
{ id: 9, name: 'Liam', time: '7:00 PM' },
{ id: 10, name: 'Noah', time: '8:00 PM' },
{ id: 11, name: 'Olivia', time: '9:00 PM' },
{ id: 12, name: 'Sophia', time: '10:00 PM' },
{ id: 13, name: 'Ava', time: '11:00 PM' },
]);
const [selectedOption, setSelectedOption] = useState(() => {
const savedOption = localStorage.getItem('selectedOption');
// If savedOption exists, parse it safely; otherwise, use the first option
let parsedOption = options[0]; // Default to the first option
if (savedOption) {
try {
parsedOption = JSON.parse(savedOption); // Try parsing saved option
} catch (e) {
console.error('Error parsing savedOption:', e);
// Fallback to the first option if parsing fails
parsedOption = options[0];
}
}
return parsedOption;
// return savedOption ? JSON.parse(savedOption) : null;
// return savedOption ? JSON.parse(savedOption) : options[0]; // Default to first option if nothing is saved
});
const [remainingTime, setRemainingTime] = useState('');
const [closingTime, setClosingTime] = useState('');
const [nextOptionRemainingTime, setNextOptionRemainingTime] = useState('');
// Function to calculate the remaining time or "Closed" for today
const calculateTimeRemaining = (selectedTime) => {
const currentTime = new Date();
const currentDate = currentTime.toLocaleDateString();
const currentTimeInSeconds = currentTime.getTime() / 1000;
let selectedOptionTime = new Date(`${currentDate} ${selectedTime}`);
const selectedTimeInSeconds = selectedOptionTime.getTime() / 1000;
// If the current time is already past the target time, calculate the time for the next day
if (selectedTimeInSeconds <= currentTimeInSeconds) {
// Mark the time as "Closed" if it's past for today
return 'Closed';
}
const selectedTimeInSecondsNextDay = selectedOptionTime.getTime() / 1000;
// Calculate the remaining time until the next available time
const timeRemaining = selectedTimeInSecondsNextDay - currentTimeInSeconds;
const hours = Math.floor(timeRemaining / 3600);
const minutes = Math.floor((timeRemaining % 3600) / 60);
const seconds = Math.floor(timeRemaining % 60);
return `${hours}hrs ${minutes}mins ${seconds}sec left`;
};
// Function to calculate closing time (9 minutes before selected time)
const calculateClosingTime = (selectedTime) => {
const currentDate = new Date().toLocaleDateString();
const selectedOptionTime = new Date(`${currentDate} ${selectedTime}`);
// Subtract 9 minutes from the selected option time
selectedOptionTime.setMinutes(selectedOptionTime.getMinutes() - 9);
return selectedOptionTime.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
};
// Function to handle option selection
const handleOptionSelect = (option) => {
setSelectedOption(option);
localStorage.setItem('selectedOption', JSON.stringify(option)); // Save selected option in localStorage
// Calculate remaining time for the selected option
const time = calculateTimeRemaining(option.time);
setRemainingTime(time); // Set the remaining time immediately when selected
// Calculate and set the closing time (9 minutes before the selected option)
const closing = calculateClosingTime(option.time);
setClosingTime(closing);
};
// Handle automatic switching when the option has passed
const autoSwitchOption = () => {
const currentTime = new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
// Find the next available option (where current time is earlier than the option time)
const nextOption = options.find(option => {
const optionTime = new Date(`${new Date().toLocaleDateString()} ${option.time}`).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
return currentTime < optionTime; // Choose the next available option
});
// Automatically switch to the next option when the current one closes
if (nextOption && selectedOption && nextOption.id !== selectedOption.id) {
handleOptionSelect(nextOption); // Update to the next option
}
};
// Function to continuously update the remaining time for the default option
useEffect(() => {
const interval = setInterval(() => {
if (selectedOption) {
const time = calculateTimeRemaining(selectedOption.time);
setRemainingTime(time);
// Also update the next option remaining time
// const nextOption = options.find(o => o.id === selectedOption.id + 1);
if (time === 'Closed') {
autoSwitchOption(); // Switch to the next option when "Closed" is reached
}
// if (nextOption) {
// const nextTime = calculateTimeRemaining(nextOption.time);
// setNextOptionRemainingTime(nextTime);
// }
} else {
// If no option is selected, update for the first available option (default behavior)
const firstOptionTime = options[0].time;
const time = calculateTimeRemaining(firstOptionTime);
setRemainingTime(time);
if (time === 'Closed') {
autoSwitchOption(); // Switch to the next option when "Closed" is reached
}
// Set the next option's time
const nextOption = options[1];
const nextTime = calculateTimeRemaining(nextOption.time);
setNextOptionRemainingTime(nextTime);
}
}, 1000); // Update every second
return () => clearInterval(interval); // Cleanup interval on component unmount
}, [selectedOption, options]);
// Reset the timer at midnight (next day)
useEffect(() => {
const now = new Date();
const nextDay = new Date(now);
nextDay.setDate(now.getDate() + 1);
nextDay.setHours(0, 0, 0, 0); // Set the time to midnight
const timeUntilMidnight = nextDay.getTime() - now.getTime();
const resetTimer = setTimeout(() => {
setRemainingTime(''); // Reset remaining time at midnight
}, timeUntilMidnight);
return () => clearTimeout(resetTimer); // Cleanup timeout on component unmount
}, []);
useEffect(() => {
console.log("Initial selectedOption:", selectedOption);
console.log("Initial options:", options);
}, []);
// time box 2 end
//logic 2
// Current date box start
const currentDate = new Date();
const day = String(currentDate.getDate()).padStart(2, '0'); // Ensure day has two digits
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Ensure month has two digits
const year = currentDate.getFullYear();
const formattedDate = `${day}/${month}/${year}`;
// Current date box end
//logic 3
const handleInputChange = (e) => {
setInput(e.target.value);
setError(""); // Clear error when input changes
};
const generateLogic1 = () => {
if (input.length !== 6) {
setError("Please enter exactly 6 digits.");
return;
}
const digits = input.split("");
// const combinations = [];
const combinations = new Set(); // Use a Set to ensure uniqueness
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i === j) continue;
for (let k = 0; k < digits.length; k++) {
if (k === i || k === j) continue;
// combinations.push(digits[i] + digits[j] + digits[k]);
combinations.add(digits[i] + digits[j] + digits[k]); // Add to Set to avoid duplicates
}
}
}
// setResults(combinations);
// Convert Set back to an array for displaying the results
setResults(Array.from(combinations));
};
const generateLogic2 = () => {
if (input.length !== 4) {
setError("Please enter exactly 4 digits.");
return;
}
const digits = input.split("");
// const combinations = [];
const combinations = new Set(); // Use a Set to ensure uniqueness
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i === j) continue;
for (let k = 0; k < digits.length; k++) {
if (k === i || k === j) continue;
// combinations.push(digits[i] + digits[j] + digits[k]);
combinations.add(digits[i] + digits[j] + digits[k]);
}
}
}
// setResults(combinations);
// Convert Set back to an array for displaying the results
setResults(Array.from(combinations));
};
const generateLogic3 = () => {
if (input.length !== 3) {
setError("Please enter exactly 3 digits.");
return;
}
const digits = input.split("");
// const combinations = [];
const combinations = new Set(); // Use a Set to ensure uniqueness
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i === j) continue;
for (let k = 0; k < digits.length; k++) {
if (k === i || k === j) continue;
// combinations.push(digits[i] + digits[j] + digits[k]);
combinations.add(digits[i] + digits[j] + digits[k]); // Add to Set to avoid duplicates
}
}
}
// setResults(combinations);
// Convert Set back to an array for displaying the results
setResults(Array.from(combinations));
};
const generateLogic4 = () => {
if (input.length !== 5) {
setError("Please enter exactly 5 digits.");
return;
}
const digits = input.split("");
// const combinations = [];
const combinations = new Set(); // Use a Set to ensure uniqueness
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i === j) continue;
for (let k = 0; k < digits.length; k++) {
if (k === i || k === j) continue;
// combinations.push(digits[i] + digits[j] + digits[k]);
combinations.add(digits[i] + digits[j] + digits[k]);
}
}
}
// setResults(combinations);
// Convert Set back to an array for displaying the results
setResults(Array.from(combinations));
};
const generateLogic5 = () => {
if (input.length !== 3) {
setError("Please enter exactly 3 digits.");
return;
}
const result = [input, input.slice(0, 2)];
setResults(result);
};
// New logic for 3-digit number combinations (generate 2-digit pairs)
// const generateLogic6 = () => {
// if (input.length !== 3) {
// setError("Please enter exactly 3 digits.");
// return;
// }
// const digits = input.split("");
// const combinations = [];
// // Generate all possible 2-digit combinations from the 3 digits
// for (let i = 0; i < digits.length; i++) {
// for (let j = 0; j < digits.length; j++) {
// if (i !== j) {
// combinations.push(digits[i] + digits[j]);
// }
// }
// }
// setResults(combinations);
// };
const generateLogic6 = () => {
if (input.length !== 3) {
setError("Please enter exactly 3 digits.");
return;
}
const digits = input.split("");
const combinations = new Set(); // Use a Set to ensure uniqueness
// Generate all possible 2-digit combinations from the 3 digits
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i !== j) {
combinations.add(digits[i] + digits[j]); // Add to Set to avoid duplicates
}
}
}
// Convert Set back to an array for displaying the results
setResults(Array.from(combinations));
};
const generateLogic7 = () => {
if (input.length !== 4) {
setError("Please enter exactly 4 digits.");
return;
}
const digits = input.split("");
const combinations = [];
// Generate 2-digit combinations
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i !== j) {
combinations.push(digits[i] + digits[j]);
}
}
}
setResults(combinations);
};
const generateLogic8 = () => {
if (input.length !== 5) {
setError("Please enter exactly 5 digits.");
return;
}
const digits = input.split("");
const combinations = [];
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i !== j) {
combinations.push(digits[i] + digits[j]);
}
}
}
setResults(combinations);
};
const generateLogic9 = () => {
if (input.length !== 6) {
setError("Please enter exactly 6 digits.");
return;
}
const digits = input.split("");
const combinations = [];
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i !== j) {
combinations.push(digits[i] + digits[j]);
}
}
}
setResults(combinations);
};
const generateLogic10 = () => {
if (input.length !== 4) {
setError("Please enter exactly 4 digits.");
return;
}
const digits = input.split("");
const combinations = [];
// Generate all possible 4-digit combinations
for (let i = 0; i < digits.length; i++) {
for (let j = 0; j < digits.length; j++) {
if (i === j) continue;
for (let k = 0; k < digits.length; k++) {
if (k === i || k === j) continue;
for (let l = 0; l < digits.length; l++) {
if (l === i || l === j || l === k) continue;
combinations.push(digits[i] + digits[j] + digits[k] + digits[l]);
}
}
}
}
setResults(combinations);
};
// Logic for 2 digits (the new logic you requested)
const generateLogic11 = () => {
if (input.length !== 2) {
setError("Please enter exactly 2 digits.");
return;
}
const num = input;
const combinations = [
num,
`+${num}+`,
`++${num}`
];
setResults(combinations);
};
return (
<>
<div className="p-6 bg-gray-300 shadow-lg rounded-lg mx-auto my-6">
<div className="text-center mb-4">
<h2 className="text-xl font-semibold text-gray-800">User Information</h2>
</div>
<div className="space-y-4">
<div className="flex justify-between items-center">
<span className="text-gray-600">NAME:</span>
<span className="font-medium text-gray-900">AM</span>
</div>
<div className="flex justify-between items-center">
<span className="text-gray-600">CITY:</span>
<span className="font-medium text-gray-900">DEALER ID:</span>
</div>
<div className="flex justify-between items-center">
<span className="text-gray-600">223344</span>
</div>
<div className="flex justify-between items-center">
<span className="text-gray-600">BALANCE:</span>
<span className="font-medium text-red-500">OFF</span>
</div>
<div className="flex justify-between items-center">
<span className="text-gray-600">LEDGER:</span>
<span className="font-medium text-gray-900">Available</span>
</div>
<div className="flex justify-between items-center">
<span className="text-gray-600">DRAW NAME:</span>
<span className="font-medium text-gray-900">Ritmu 11AM</span>
</div>
<div className="flex justify-between items-center">
<span className="text-gray-600">DRAW DATE:</span>
<span className="font-medium text-gray-900">16/11/2024</span>
</div>
</div>
</div>
{/* option with time div start */}
<div className='bg-yellow-500'>
<h2>Current Closing Time</h2>
<p>Closing Time: {closingTime}</p>
</div>
<div className='bg-red-300'>
<h1>Time-Based Dropdown</h1>
<select
onChange={(e) =>
handleOptionSelect(options.find((opt) => opt.id === parseInt(e.target.value)))
}
value={selectedOption?.id || ''}
>
<option value="">Select an option</option>
{options.map((option) => (
<option key={option.id} value={option.id}>
{option.name} - {calculateTimeRemaining(option.time)}
</option>
))}
</select>
{selectedOption && (
<div>
<h2>{selectedOption.name}</h2>
<p>{selectedOption.name} is scheduled for {selectedOption.time}. {remainingTime}</p>
</div>
)}
</div>
{/* option with time div end */}
<div className='bg-green-300'>
<h1>Current Date: {formattedDate}</h1>
</div>
<div>
<h1>Number Logic Generator</h1>
<input
type="text"
maxLength="8"
value={input}
onChange={handleInputChange}
placeholder="Enter a number"
className="bg-yellow-400"
/>
<div className="bg-red-400 text-white">
<button className="bg-blue-400 text-white" onClick={generateLogic1}>Logic 1 :6 FIGURE TINDOLA (6 digits)</button>
<br></br>
<button className="bg-blue-400 text-white" onClick={generateLogic2}>Logic 2 : 4 FIGURE TINDOLA (4 digits)</button>
<br></br>
<button onClick={generateLogic3}>Logic 3 : palti TINDOLA (3 digits)</button>
<br></br>
<button onClick={generateLogic4}>Logic 4 :5 FIGURE TINDOLA (5 digits)</button>
<br></br>
<button onClick={generateLogic5}>Logic 5 :Tandola + AKDA(3 digits, reduce)</button>
<br></br>
<button onClick={generateLogic6}>Logic 6: Palti Akda 2-digit combinations from 3 digits</button>
<br></br>
<button onClick={generateLogic7}>Logic 7 :4 figure akda 2-Digit Combinations from 4 Digits</button>
<br></br>
<button onClick={generateLogic8}>Logic 8 :5 FIGURE AKDA 5-Digit Combination (2 digits)</button>
<br></br>
<button onClick={generateLogic9}>Logic 9 :6 FIGURE AKDA 6-Digit Combination (2 digits)</button>
<br></br>
<button onClick={generateLogic10}>Logic 10:PULTI PANGODA 4-Digit Combinations</button>
<br></br>
<button onClick={generateLogic11}>Logic 11: FRONT MIDDLE BACK AKDA 2-Digit Pattern</button>
</div>
{error && <p style={{ color: "red" }}>{error}</p>}
<h2>Generated Results:</h2>
<table border="1">
<thead>
<tr>
<th>Number</th>
</tr>
</thead>
<tbody>
{results.map((num, index) => (
<tr key={index}>
<td>{num}</td>
</tr>
))}
</tbody>
</table>
</div>
</>
);
};
export default NumberLogicGenerator;