<?php
session_start();
if ($_SESSION['unohs'] == null) {
    header("location:index.php?msg=unauthorized");
}

include "conn.php"; // Database connection

$updateSuccess = false;
$updateError = false;

// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $taskAmounts = $_POST['taskAmount'];
    $rechargeAmounts = $_POST['rechargeAmount'];
    $taskPeoples = $_POST['taskPeople'];
    $ids = $_POST['id'];  // IDs to identify which row to update
    
    for ($i = 0; $i < count($ids); $i++) {
        $taskAmount = $taskAmounts[$i];
        $rechargeAmount = $rechargeAmounts[$i];
        $taskPeople = $taskPeoples[$i];
        $id = $ids[$i];

        // Fetch the existing data to check if there's an actual change
        $query = "SELECT taskAmount, rechargeAmount, taskPeople FROM tbl_invitebonus WHERE id = ?";
        $stmt = $conn->prepare($query);
        $stmt->bind_param("i", $id);
        $stmt->execute();
        $result = $stmt->get_result();
        $row = $result->fetch_assoc();

        if ($row && ($taskAmount != $row['taskAmount'] || $rechargeAmount != $row['rechargeAmount'] || $taskPeople != $row['taskPeople'])) {
            // Only update if values are changed
            $sql = "UPDATE tbl_invitebonus SET taskAmount = ?, rechargeAmount = ?, taskPeople = ? WHERE id = ?";
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("diii", $taskAmount, $rechargeAmount, $taskPeople, $id);

            if ($stmt->execute()) {
                $updateSuccess = true;
            } else {
                $updateError = true;
            }
            $stmt->close();
        }
    }
}

// Fetch all data to display in the table
$result = $conn->query("SELECT * FROM tbl_invitebonus");
?>


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>Dashboard</title>
  <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
  <link rel="stylesheet" href="vendors/feather/feather.css">
  <link rel="stylesheet" href="vendors/base/vendor.bundle.base.css">
  <link rel="stylesheet" href="vendors/flag-icon-css/css/flag-icon.min.css"/>
  <link rel="stylesheet" href="vendors/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="vendors/jquery-bar-rating/fontawesome-stars-o.css">
  <link rel="stylesheet" href="vendors/jquery-bar-rating/fontawesome-stars.css">
  <link rel="stylesheet" href="css/style.css">
  <link rel="stylesheet" href="plugins/datatables/dataTables.bootstrap.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.2.3/css/fixedHeader.dataTables.min.css">
  <link rel="shortcut icon" href="images/favicon.png" />
  <style>
  
  .table-responsive {
    overflow-x: auto;
    width: 100%;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 12px;
    text-align: center;
}
	.cool-input {
        border: 2px solid #007bff;
        border-radius: 0.25rem;
        padding: 0.5rem 1rem;
        font-size: 1rem;
        transition: all 0.3s ease;
    }
    .cool-input:focus {
        border-color: #0056b3;
        box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
    }
    .cool-input::placeholder {
        color: #6c757d;
        opacity: 1;
    }
	.cool-button {
        padding: 0.5rem 1rem;
        font-size: 1rem;
        border-radius: 0.25rem;
        transition: all 0.3s ease;
    }
    .cool-button:hover {
        background-color: #0056b3;
        color: #fff;
    }
    .cool-button.btn-secondary:hover {
        background-color: #343a40;
        color: #fff;
    }
	#copied{
		visibility: hidden;
		z-index: 1;
		position: fixed;
		bottom: 50%;
		background-color: #333;
		color: #fff;
		border-radius: 6px;
		padding: 16px;
		max-width: 250px;
		font-size: 17px;
	}	   
	#copied.show {
		visibility: visible;
		-webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
		animation: fadein 0.5s, fadeout 0.5s 2.5s;
	}
  </style>
</head>
<body>
  <div class="container-scroller">
    <nav class="navbar col-lg-12 col-12 p-0 fixed-top d-flex flex-row">
      <div class="text-center navbar-brand-wrapper d-flex align-items-center justify-content-center">
        <a class="navbar-brand brand-logo" href="dashboard.php"><img src="images/logo.png" alt="logo"/></a>
        <a class="navbar-brand brand-logo-mini" href="dashboard.php"><img src="images/logo-mini.png" alt="logo"/></a>
      </div>
      <div class="navbar-menu-wrapper d-flex align-items-center justify-content-end">
        <button class="navbar-toggler navbar-toggler align-self-center" type="button" data-toggle="minimize">
          <span class="icon-menu"></span>
        </button>       
        <ul class="navbar-nav navbar-nav-right">           
          <li class="nav-item dropdown d-flex mr-4 ">
            <a class="nav-link count-indicator dropdown-toggle d-flex align-items-center justify-content-center" id="notificationDropdown" href="#" data-toggle="dropdown">
              <i class="icon-cog"></i>
            </a>
            <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list" aria-labelledby="notificationDropdown">
              <p class="mb-0 font-weight-normal float-left dropdown-header">Settings</p>              
              <a class="dropdown-item preview-item" href="logout.php">
                  <i class="icon-inbox"></i> Logout
              </a>
            </div>
          </li>
        </ul>
        <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas">
          <span class="icon-menu"></span>
        </button>
      </div>
    </nav>
    <div class="container-fluid page-body-wrapper">
      <nav class="sidebar sidebar-offcanvas" id="sidebar">
        <div class="user-profile">
          <div class="user-image">
            <img src="images/faces/face28.png">
          </div>
          <div class="user-name">
              Game Admin
          </div>
          <div class="user-designation">
              Admin
          </div>
        </div>
        <?php include 'compass.php';?>
      </nav>
      
      <div class="container mt-4">
    <h2 class="text-center">Edit Invite Bonuses</h2>

    <!-- Show success or error message -->
    <?php if ($updateSuccess): ?>
        <div class="alert alert-success text-center">Update done successfully!</div>
    <?php elseif ($updateError): ?>
        <div class="alert alert-danger text-center">Update error occurred!</div>
    <?php endif; ?>

    <form method="POST">
        <div class="table-responsive">
            <table class="table table-striped table-hover text-center align-middle w-100">
                <thead class="table-primary">
                    <tr>
                        <th>Task ID 🆔</th>
                        <th>Task Amount ₹</th>
                        <th>Recharge Amount ₹</th>
                        <th>Task People ➤</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <tr>
                            <td>
                                <input type="hidden" name="id[]" value="<?php echo $row['id']; ?>">
                                <strong><?php echo $row['taskID']; ?></strong>
                            </td>
                            <td>
                                <input type="number" name="taskAmount[]" 
                                       class="form-control text-center fw-bold border-primary"
                                       value="<?php echo $row['taskAmount']; ?>" min="0" required>
                            </td>
                            <td>
                                <input type="number" name="rechargeAmount[]" 
                                       class="form-control text-center fw-bold border-success"
                                       value="<?php echo $row['rechargeAmount']; ?>" min="0" required>
                            </td>
                            <td>
                                <input type="number" name="taskPeople[]" 
                                       class="form-control text-center fw-bold border-info"
                                       value="<?php echo $row['taskPeople']; ?>" min="0" required>
                            </td>
                        </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
        <div class="text-center mt-4">
            <button type="submit" class="btn btn-lg btn-success w-100">Update All</button>
        </div>
    </form>
</div>

</div>
    </div>
  </div>
  <script src="vendors/base/vendor.bundle.base.js"></script>
  <script src="js/off-canvas.js"></script>
  <script src="js/hoverable-collapse.js"></script>
  <script src="js/template.js"></script>
  <script src="vendors/chart.js/Chart.min.js"></script>
  <script src="vendors/jquery-bar-rating/jquery.barrating.min.js"></script>
  <script src="js/dashboard.js"></script>
  <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
  <script>	
	if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
  </script>
</html>

</body>
</html>


</body>
</html>

<?php
// Close the database connection
$conn->close();
?>
