<?php

session_start();
require_once '../models/User.php';
require_once '../include/Helper.php';
require_once '../models/Document.php';
require_once '../models/Withdrawal.php';
require_once '../models/Invoice.php';
require_once '../include/DatabaseConn.php';
require_once '../include/Constants.php';
require_once '../models/Admin.php';
require_once '../models/Wallet.php';
require_once '../models/Referral.php';

$admin = Admin::getCurrentAdmin();

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}
$db = new DatabaseConn();
$link = $db->connect();

if (isset($_POST['data'])) {
    $emails = filter_input(INPUT_POST, 'data');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::suspendUser($value, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['delete'])) {
    $emails = filter_input(INPUT_POST, 'delete');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::deleteUser($value, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['unsuspend'])) {
    $emails = filter_input(INPUT_POST, 'unsuspend');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::unsuspendUser($value, $link);
    }
    $success = "Operation was successful";
} else if (isset($_POST['bypass'])) {
    $emails = filter_input(INPUT_POST, 'bypass');
    $emails = json_decode($emails);
    foreach ($emails as $value) {
        User::bypassUser($value, $link);
    }
    $success = "Operation was successful";
}else if (isset($_POST['percentage'])) {
    $name = mysqli_real_escape_string($link, $_POST['name']);
    $percentage = (int)$_POST['percentage'];

    // Update the record in the database
    $query = "UPDATE generation SET percentage = $percentage WHERE `name` = '$name'";
    if (mysqli_query($link, $query)) {
       $success = "Percentage changed";
    } else {
       $success = "An error occurred";
    }
}
$users = User::getUsers($link);
$referrals = Referral::getAllReferrals22( $link);
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <?php require_once './partials/head.php'; ?>
        <link href="../css/datatables.css" rel="stylesheet" type="text/css"/>
        <link href="../css/theme-checkbox-radio.css" rel="stylesheet" type="text/css"/>
        <link href="../css/dt-global_style.css" rel="stylesheet" type="text/css"/>
        <link href="../css/custom_dt_custom.css" rel="stylesheet" type="text/css"/>
        <link href="../css/snackbar.min.css" rel="stylesheet" type="text/css"/>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.1/css/lightbox.min.css" />
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.1/animate.min.css" />
    </head>
    <body>
        <div id="app">
            <?php require_once './partials/top-header.php'; ?>
            <!--  BEGIN MAIN CONTAINER  -->
            <div class="main-container" id="container">
                <div class="overlay"></div>
                <div class="search-overlay"></div>
                <?php require_once './partials/side-bar.php'; ?>
                <!--  BEGIN CONTENT AREA  -->
                <div id="content" class="main-content">
                    <div class="layout-px-spacing">
                        <div class="row layout-top-spacing layout-spacing">
                            <div class="col-lg-12">
                                <div class="statbox widget box box-shadow">
                                    <div class="widget-content widget-content-area">
                                       <div class="table-responsive mb-4 style-1">
                                            <div class="table-responsive mb-4 style-1">  
                                                <table id="style-2" class="table style-1 table-hover non-hover">
                                                                    <thead>
                                                                        <tr>
                                                                            <th>Generation</th>
                                                                            <th>Percentage</th>
                                                                         </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                        <?php
                                                                            $query = mysqli_query($link, "select id, `name`, percentage from generation");
                                                                            while($row = mysqli_fetch_assoc($query)) {
                                                                        ?>
                                                                           <tr>
                                                                                <td data=""><?php echo ($row['name']); ?></td>
                                                                               <td><?php echo ($row['percentage']); ?>%</td>
                                                                               <td style="color: #25b9d7; cursor: pointer">Edit</td>
                                                                            </tr>
                                                                        <?php 
                                                                            }
                                                                        ?>
                                                                    </tbody>
                                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                    <?php require_once './partials/footer.php'; ?>
                </div>
                <!--  END CONTENT AREA  -->
            </div>
            <!-- Bootstrap Modal -->
            <!-- Bootstrap Modal with Form -->
            <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <form action="ref.php" method="POST" id="editForm">
        <div class="modal-header">
          <h5 class="modal-title" id="editModalLabel">Edit Percentage</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">
          <div class="mb-3">
            <label for="percentageInput" class="form-label">Enter Percentage</label>
            <input type="number" class="form-control" id="percentageInput" name="percentage" min="1" required>
            <!-- Hidden input to store the name -->
            <input type="hidden" id="nameInput" name="name">
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
          <button type="submit" class="btn btn-primary">Save changes</button>
        </div>
      </form>
    </div>
  </div>
</div>


        </div>
        <?php require_once './partials/scripts.php'; ?>
        <script src="../js/datatables.js" type="text/javascript"></script>
        <script src="../js/snackbar.min.js" type="text/javascript"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/lightbox2/2.11.1/js/lightbox.min.js"></script>
         <script src="../js/jqueryblockui.js" type="text/javascript"></script>
        <script src="../js/customblockui.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
  // Event handler for the Edit click
  $('td[style*="cursor: pointer"]').on('click', function() {
    var currentName = $(this).siblings('td:first').text().trim(); // Get the name from the first cell
    var currentPercentage = $(this).siblings('td:nth-child(2)').text().replace('%', '').trim(); // Get the percentage

    // Set the values in the modal form
    $('#nameInput').val(currentName);
    $('#percentageInput').val(currentPercentage);
    
    $('#editModal').modal('show'); // Show the Bootstrap modal
  });
});
</script>

        <?php if (isset($error)) { ?>
            <script>Snackbar.show({
              text: '<?php echo "$error"; ?>',
            pos: 'top-right'
                                                                            });</script>
        <?php } else if (isset($success)) { ?>
            <script> Snackbar.show({
                    text: '<?php echo "$success"; ?>',
                    pos: 'top-right'
                });</script>
        <?php } ?>
        <?php if (isset($_SESSION[SUCCESS])) { ?>
            <script>Snackbar.show({
                    text: '<?php echo ($_SESSION[SUCCESS]); ?>',
                    pos: 'top-right'
                });</script>
        <?php } unset($_SESSION[SUCCESS]); ?>

    </body>
</html>