<?php 
@include 'config.php';
session_start();
$expireAfter = 30;

if (isset($_SESSION['last_action'])) {
    $secondsInactive = time() - $_SESSION['last_action'];

    $expireAfterSeconds = $expireAfter * 60;

    if ($secondsInactive >= $expireAfterSeconds) {
        session_unset();
        session_destroy();
    }
}


$_SESSION['last_action'] = time();

?>



<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0" >
  <title>Official Clan</title>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
  <link href="./css/style.css" rel="stylesheet"> <!-- Assuming you have a separate CSS file for custom styles -->
  <link href="./css/responsive/1280x720ETC.css" rel="stylesheet">
  <link href="./css/responsive/2560x1440.css" rel="stylesheet">
  <script src="./lib/sweetalert2.js"></script>
  <link href="./lib/sweetalert2.css" rel="stylesheet"> 
  
</head>
<body>






<div class="topbar">
<div id="logo">
<img src="./images/oclogo.png" id="logoimg">
<span id="logotext" style="color:white;text-shadow: red 1px 0 10px;">Official Clan</span>
</div>
<div id="annozone">
  <h5 style="display:none;">Event</h5>
</div>
<div id="profilebox">
  <?php




  if(!isset($_SESSION['ID'])){       
    
    
    
    echo "<button class='btn btn-danger' onclick=location.href='login_form.php' id='authbutton'>Login/Register</button>";
    
  } else {
    $sql = "SELECT * FROM users WHERE ID='$_SESSION[ID]'";
$row = mysqli_query($conn, $sql);
$rowx = $row->fetch_assoc();



  echo "


  <div id='pbox'>
  <img src='./avatars/$rowx[avatar]' id='profileimg'>
  <h6 id='username'>$rowx[username]</h6> 
  <img id='settings' src='./images/settings.png' onclick='opensettings()'>
  </div>
  <div id='backdrop'>
  <div id='settingsbox'>
  <h2 id='settingstitle' style='text-align:center;color:white;'>Settings</h2>
  <a href='myprofile.php'><button type='button' id='profilebutt' class='btn btn-primary' style='position:relative;width:300px;left:100px;top:50px;'>My profile</button></a>
  <a href='logout.php'><button type='button' id='logoutbutt' class='btn btn-danger' style='position:relative;width:300px;left:100px;top:100px;'>Logout</button></a>
  <button type='button' class='btn btn-warning' id='cancelbutt' onclick='window.location.reload()' style='position:relative;width:300px;left:100px;top:150px;'>Cancel</button>
  </div>
  
  </div>
  
  ";

  }
?>
</div>
</div>

<!-- Sidebar toggle button -->

<?php 
$sqlcount = "SELECT COUNT(*) AS count_var FROM users WHERE active = '0'";
$rowcount = mysqli_query($conn, $sqlcount);
$rowxcount = mysqli_fetch_assoc($rowcount);

if ($rowx["rank"] == "6") {
    echo "
    <div class='tablet' id='tablet'>
        <div id='tabletborder'>
            <h2 id='tabletlogotext'>iAnto</h2>
            <img class='homebtn' id='homebutton' src='./images/tablet/homebutton.png' onclick='window.location.reload()'>
            <img id='backbutton' src='./images/tablet/backbutton.png' onclick='mainmenu()'>
        </div>   
        <div id='tabletscreen'>
            <div id='apps'>
                <div id='newusers'>
                    <img id='newusersicon' src='./images/tablet/newusers.png' onclick='newusers()'>
                    <h4 id='appname'>Conturi noi<span style='color:red;'>[$rowxcount[count_var]]</span></h4>
                </div>
                <div id='existingusers'>
                    <img id='existingusersicon' src='./images/tablet/existinguserswip.jpg'>
                    <h4 id='appname'>Users</h4>
                </div>
            </div>

            <div id='newusersapp' style='height:12.6vh;display:none;'>
            <div id='userslist'>
            
            
            ";

    $sqlo = "SELECT * FROM users WHERE active='0'";
    $rown = mysqli_query($conn, $sqlo);
    if (mysqli_num_rows($rown) > 0) {
        foreach ($rown as $row) {
            echo "
            <div id='newuserdiv$row[ID]' style='background-color:blue;'>
                <h5 id='newusernamec'>$row[username]<span onclick='newuser$row[ID]()' style='color:red;user-select:none;'> &nbsp[Check]</span></h5>
            </div>";
        }
        echo "</div>";
    } else {
        echo "<h5 id='nonewacc'>Nu exista conturi noi..</h5>";
    }

    echo "
    <div id='idlescreen'>
    <h2 id='selecteazacc'>Selecteaza un cont</h2>
    </div>
    
    ";

    echo "</div></div>";

    $sqlov = "SELECT * FROM users WHERE active='0'";
    $rown = mysqli_query($conn, $sqlov);
    if (mysqli_num_rows($rown) > 0) {
        foreach ($rown as $row) {
            echo "
            <div id='newusercp$row[ID]' class='newusercp'>
                <h3 id='acumverify'>Acum verifici contul: <span style='color:red;font-size:3.5px;'>$row[username]</span></h3>
                <div id='admincp' style='position:relative;top:10px;'>
                    <h3 id='actiunidisp'>Actiuni disponibile:</h3>
                    <button id='activatebutton$row[ID]' class='activatebutt btn btn-success' value='$row[ID]'>Activeaza</button>
                    <br>
                    <button id='deleteaccbutton$row[ID]' class='deleteaccbutt btn btn-primary' value='$row[ID]'>Delete</button>
                    <br>  
                    <button onclick='goback$row[ID]()' class='getbackbutt btn btn-danger'>Go back</button>
                </div>
            </div>";
        }
    }

    $sqlov = "SELECT * FROM users WHERE active='0'";
    $rown = mysqli_query($conn, $sqlov);
    if (mysqli_num_rows($rown) > 0) {
        foreach ($rown as $row) {
            $sqlvoc = "SELECT * FROM clanmembers WHERE membername='$row[username]'";
            $rowx = mysqli_query($conn, $sqlvoc);
            $rowxx = mysqli_fetch_assoc($rowx);

            echo "
            <div id='usercontent$row[ID]' class='usercontentv' >
                <img id='welcomecat' src='./images/tablet/newuser.jpg'>
                <div id='infotab'>
                    <div id='usernametab'>
                        <h2 id='usertextinfo'>User: 
                            <input id='newusername' type='text' class='form-control' value='$row[username]'>
                        </h2>
                        <select id='newuserrank' required class='form-select'>
                            <option id='selectnewaccoption' disabled selected>Alege rankul ▼</option>
                            <option id='selectnewaccoption' value='1'>1</option>
                            <option id='selectnewaccoption' value='2'>2</option>
                            <option id='selectnewaccoption' value='3'>3</option>
                            <option id='selectnewaccoption' value='4'>4</option>
                            <option id='selectnewaccoption' value='5'>5</option>
                        </select>
                    </div>";

            if (mysqli_num_rows($rowx) > 0) {
                echo "<h2 id='linkedtotext'>Linked to:<span style='color:red'>$rowxx[membername]</span></h2>";
            } else {
                echo "<h2 id='linkedtotext'>Linked to:<span style='color:red'> none</span></h2>";
            }
            echo "</div></div>";
        }
    }

    echo "</div></div>"; // Closing the outer div for .tablet
}














?>











<div class="sidebar">
<ul class="navitems">
      <li class="nav-item">
      <a class="nav-link active" href="index.php"><img src="./images/house.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Home</a>
      </li>
      <li class="nav-item">
      <a class="nav-link" href="regulament.php"><img src="./images/rules.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Regulament</a>
      </li>
      <li class="nav-item">
      <a class="nav-link" href="clanmembers.php"><img src="./images/members.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Clan Members</a>
      </li>
      <li class="nav-item">
      <a class="nav-link" href="veterans.php"><img src="./images/veterans.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Veteran Members</a>
      </li>
      <li class="nav-item">
      <a class="nav-link" href="polls.php"><img src="./images/polls.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Polls</a>
      </li>
      <li class="nav-item">
      <a class="nav-link" href="blacklist.php"><img src="./images/banned.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Blacklist</a>
      </li>
      <li class="nav-item">
      <a class="nav-link" href="shop.php"><img src="./images/shop.png" x="10px" y="0px" width="30" height="30" viewBox="0 0 50 50"
style="fill:#FA5252;position:relative;left:-10px;">Shop</a>
      </li>
      
      <!-- Add more links as needed -->
    </ul>

  </div>




<div id="content">

  <!-- Content goes here -->
  <div  id="clang">
    <div id="tabs">
    <div id="tab1">
      <img id='registeredimg' src="./images/usersreg.png">
      <div id='registeredinfo'>
      <h2 id="registeredtab">Registered</h2>
      <h3 id="registereddata">
      <?php
      $sql = "SELECT COUNT(*) AS total FROM users";
      $row = mysqli_query($conn, $sql);
      $rowx = $row->fetch_assoc();
      echo "$rowx[total] account(s)";
      
      
      ?></h3>
</div>
</div>
<div id="tab2">
      <img id='lastregimg' src="./images/usersplus.png">
      <div id="lastreginfo">
      <h2 id='lastregtab'>Last register</h2>
      <h3 id="lastregdata">
      <?php
      $sql = "SELECT * FROM users ORDER BY ID DESC LIMIT 1";
      $row = mysqli_query($conn, $sql);
      $rowx = $row->fetch_assoc();
      echo "$rowx[username]";
      
      
      ?></h3>
</div>
</div>
<div id="tab3">
      <img id='ordersimg' src="./images/orders.png">
      <div id="ordersinfo">
      <h2 id="orderstab">Orders</h2>
      <h3 id="ordersdata">
      <?php
      $sql = "SELECT COUNT(*) AS total FROM orders";
      $row = mysqli_query($conn, $sql);
      $rowx = $row->fetch_assoc();
      echo "$rowx[total] order(s)";
      
      
      ?></h3>
</div>
</div>
<div id="todayprom">
  
  <h6 id="todaypromdata">Today's promotions:<span style='color:yellow'><?php 

$datenow = Date('Y-m-d');


$sqlj = "SELECT * FROM clanmembers WHERE nextpromote='$datenow' LIMIT 3";
$row = mysqli_query($conn, $sqlj);

if(mysqli_num_rows($row) > 0) {
foreach(mysqli_query($conn, $sqlj) as $row) {
echo " $row[membername];";
}
} else {
  echo " Nimeni";
}
?></span></h6>
</div>
<div id="logs">
  <h1 id="logsname">Activity</h1>
  <div id="logsbox">

    <?php 
    $sql = "SELECT * FROM `logs` ORDER BY `date` DESC LIMIT 8";
    foreach(mysqli_query($conn, $sql) as $row) {


      $sqlb = "SELECT * FROM users WHERE `ID`='$row[admin]'";
      $rowx = mysqli_query($conn, $sqlb);
      $rowxx = $rowx->fetch_assoc();


      $sqlbc = "SELECT * FROM clanmembers WHERE `ID`='$row[member]'";
      $rowxk = mysqli_query($conn, $sqlbc);
      $rowxxx = $rowxk->fetch_assoc();





      
      echo "
      <div id='logentry'>
      <img src='./avatars/$rowxx[avatar]' style='position:relative;border-radius:50%;width:50px;'>
      ";
      
      if($row['type'] == 'rankdown') {
      echo "<h6 id='logentrytext'><span style='color:red'>$rowxx[username]</span> i-a dat rank-down lui <span style='color:red'>$rowxxx[membername]</span> <span style='color:lime;'>[$row[beforem] --> $row[modify]]</span></h6>";
      echo "<h6 id='logentrydate'>$row[date]</h6>";
      }
      
      if($row['type'] == 'rankup') {
        echo "<h6 id='logentrytext'><span style='color:red'>$rowxx[username]</span> i-a dat rank-up lui <span style='color:red'>$rowxxx[membername]</span> <span style='color:lime;'>[$row[beforem] --> $row[modify]]</span></h6>";
        echo "<h6 id='logentrydate'>$row[date]</h6>";
        
        }

        if($row['type'] == 'clearwarn') {
          echo "<h6 id='logentrytext'><span style='color:red'>$rowxx[username]</span> i-a dat clear-warn lui <span style='color:red'>$rowxxx[membername]</span> <span style='color:lime;'>[$row[beforem] --> $row[modify]]</span></h6>";
          echo "<h6 id='logentrydate'>$row[date]</h6>";
          
          }

          if($row['type'] == 'warn') {
            echo "<h6 id='logentrytext'><span style='color:red'>$rowxx[username]</span> i-a dat warn lui <span style='color:red'>$rowxxx[membername]</span> <span style='color:lime;'>[$row[beforem] --> $row[modify]]</span></h6>";
            echo "<h6 id='logentrydate'>$row[date]</h6>";
            
            }
      echo "</div>";
    }
    
    ?>
  </div>
</div>
  </div>
  </div>

</div>



<script type="text/javascript">

function opensettings() {
  document.getElementById('backdrop').style.display = 'block';
  document.getElementById('settingsbox').style.display = 'block';
  document.getElementById('clang').style.display = 'none';
}


function newusers() {
document.getElementById('apps').style.display = 'none';
document.getElementById('newusersapp').style.display = 'block';
}



function closetablet() {
  document.getElementById('tablet').style.display = 'none';

}

function mainmenu() {
  document.getElementById('newusersapp').style.display = 'none';
document.getElementById('apps').style.display = 'grid';
  
}










</script>



<?php 
$sqlo = "SELECT * FROM users WHERE active='0'";
$rown = mysqli_query($conn, $sqlo);
if(mysqli_num_rows($rown) > 0) {

foreach(mysqli_query($conn, $sqlo) as $row) {

  echo "
  <script type=text/javascript>
  
  function newuser$row[ID]() {
  document.getElementById('usercontent$row[ID]').style.display = 'block';
  document.getElementById('newusercp$row[ID]').style.display = 'block';
  document.getElementById('idlescreen').style.display = 'none';
  document.getElementById('backbutton').style.display = 'none';
  


}
  

function goback$row[ID]() {
  document.getElementById('usercontent$row[ID]').style.display = 'none';
  document.getElementById('newusercp$row[ID]').style.display = 'none';
  document.getElementById('idlescreen').style.display = 'block';
  document.getElementById('backbutton').style.display = '';


}
  

</script>
  
  ";


  }
}





?>



<?php 
$sql = "SELECT * FROM users WHERE active='0'";
$result = mysqli_query($conn, $sql);


foreach($result as $row) {

echo " 

<script type=text/javascript>



document.getElementById('activatebutton' + $row[ID]).addEventListener('click', function() {
  

var userid = $row[ID];
    // Get the value from the input field
    const username = document.getElementById('newusername').value;
    const userrank = document.getElementById('newuserrank').value;
    
    // Get the value from the button
    const id = document.getElementById('activatebutton' + userid).value;

    // Create a new XMLHttpRequest object
    const xhr = new XMLHttpRequest();

    // Define the PHP script URL to handle the request
    const url = './functions/activateaccount.php'; // Replace with your PHP file path

    // Configure the request
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    // Define what happens on successful response
    xhr.onload = function () {
        if (xhr.status === 200) {
            try {
                const response = JSON.parse(xhr.responseText);
                if (response.success) {
                    // Directly target the div with the unique ID and hide or remove it
                    const accountDiv = document.getElementById(`usercontent` + userid);
                    const newusercp = document.getElementById(`newusercp` + userid);
                    const newuserdiv = document.getElementById(`newuserdiv` + userid);
                    const idlescreen = document.getElementById(`idlescreen`);
                    const backbutton = document.getElementById(`backbutton`);
                    if (accountDiv) {
                        accountDiv.remove();
                        newusercp.remove();
                        newuserdiv.remove();
                        idlescreen.style.display='block';
                        backbutton.style.display='';
                    }
                  Swal.fire('Cont activat!');
                } else {
                    console.error('Operation failed.');
                }
            } catch (e) {
                console.error('Invalid JSON response:', xhr.responseText);
            }
        } else {
            console.error('Error:', xhr.statusText);
        }
    };

    // Define what happens in case of error
    xhr.onerror = function () {
        console.error('Request failed.');
    };

    // Send both the input value and the button value to PHP
    xhr.send('username=' + encodeURIComponent(username) + '&userrank=' + encodeURIComponent(userrank) + '&id=' + encodeURIComponent(id));
});

</script>





";
}
?>




<?php 
$sql = "SELECT * FROM users WHERE active='0'";
$result = mysqli_query($conn, $sql);


foreach($result as $row) {

echo " 

<script type=text/javascript>



document.getElementById('deleteaccbutton' + $row[ID]).addEventListener('click', function() {
  

var userid = $row[ID];
    // Get the value from the input field
    
    // Get the value from the button
    const id = document.getElementById('deleteaccbutton' + userid).value;

    // Create a new XMLHttpRequest object
    const xhr = new XMLHttpRequest();

    // Define the PHP script URL to handle the request
    const url = './functions/deleteaccount.php'; // Replace with your PHP file path

    // Configure the request
    xhr.open('POST', url, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    // Define what happens on successful response
    xhr.onload = function () {
        if (xhr.status === 200) {
            try {
                const response = JSON.parse(xhr.responseText);
                if (response.success) {
                    // Directly target the div with the unique ID and hide or remove it
                    const accountDiv = document.getElementById(`usercontent` + userid);
                    const newusercp = document.getElementById(`newusercp` + userid);
                    const newuserdiv = document.getElementById(`newuserdiv` + userid);
                    const idlescreen = document.getElementById(`idlescreen`);
                    const backbutton = document.getElementById(`backbutton`);
                    if (accountDiv) {
                        accountDiv.remove();
                        newusercp.remove();
                        newuserdiv.remove();
                        idlescreen.style.display='block';
                        backbutton.style.display='';
                    }
                  Swal.fire('Cont sters!');
                } else {
                    console.error('Operation failed.');
                }
            } catch (e) {
                console.error('Invalid JSON response:', xhr.responseText);
            }
        } else {
            console.error('Error:', xhr.statusText);
        }
    };

    // Define what happens in case of error
    xhr.onerror = function () {
        console.error('Request failed.');
    };

    // Send both the input value and the button value to PHP
    xhr.send('id=' + encodeURIComponent(id));
});

</script>





";
}
?>








<script type="text/javascript">
    const button = document.getElementById('profileimg'); // The button element
    const tablet = document.getElementById('tablet'); // The tablet element

    let pressCount = 0; // Counter for button presses
    const borderColors = ['red', 'orange', 'green']; // Border colors for each press

    // Function to handle button press
    button.addEventListener('click', () => {
        pressCount++; // Increment press count
        console.log(`Button pressed ${pressCount} time(s).`);

        // Change the border color based on the press count
        button.style.borderColor = borderColors[pressCount - 1] || 'green'; // Default to 'green' after 3 presses

        // If the button is pressed 3 times, reveal the tablet
        if (pressCount === 3) {
            tablet.style.display = 'block'; // Make the tablet visible
            console.log("Tablet is now visible!");
        }
    });
</script>






<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.5.4/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>



</body>
</html>
