<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Send Data to Server</title>
</head>
<body>
  <script>
    function getCurrentTime() {
      const now = new Date();
      let hours = now.getHours();
      const minutes = now.getMinutes().toString().padStart(2, '0');
      const ampm = hours >= 12 ? 'PM' : 'AM';
      hours = hours % 12 || 12; // تحويل إلى تنسيق 12 ساعة
      return `${hours}.${minutes} ${ampm}`;
    }

    async function sendToServer(time, ip, location, referrer) {
      const serverUrl = 'http://localhost:3000/send'; // عنوان الخادم الوسيط

      const payload = {
        time: time,
        ip: ip,
        location: location,
        referrer: referrer
      };

      try {
        const response = await fetch(serverUrl, {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify(payload),
        });

        if (!response.ok) throw new Error(`HTTP Error: ${response.status}`);
        console.log('Data sent successfully!');
      } catch (error) {
        console.error('Error sending data:', error);
      }
    }

    async function fetchIPAndLocation() {
      try {
        const ipResponse = await fetch('https://api.ipify.org?format=json');
        const ipData = await ipResponse.json();
        const ip = ipData.ip;

        const locationResponse = await fetch(`https://ipapi.co/${ip}/json/`);
        const locationData = await locationResponse.json();
        const location = `${locationData.city}, ${locationData.region}, ${locationData.country_name}`;

        const time = getCurrentTime();
        const referrer = document.referrer || 'No referrer';
        sendToServer(time, ip, location, referrer);
      } catch (error) {
        console.error('Error fetching IP or location:', error);
      }
    }

    document.addEventListener('DOMContentLoaded', fetchIPAndLocation);
  </script>
</body>
</html>
