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

<?php
	include("conn.php");

	// Add new UPI ID
	if (isset($_POST['newupi'])) {
		$upiid = mysqli_real_escape_string($conn, $_POST['newupi']);
		$sql_q = "INSERT INTO deyya (maulya, sthiti) VALUES ('$upiid', '0')";
		$chk = mysqli_query($conn, $sql_q);
		if ($chk) {
			echo '<script>alert("UPI ID Added");</script>';
		} else {
			echo '<script>alert("UPI ID Failed");</script>';
		}
	}

	// Set UPI as active
	if (isset($_POST['upiid'])) {
		$a_id = mysqli_real_escape_string($conn, $_POST['upiid']);
		
		// Set all to 0
		mysqli_query($conn, "UPDATE deyya SET sthiti='0'");

		// Set selected UPI as active
		mysqli_query($conn, "UPDATE deyya SET sthiti='1' WHERE maulya='$a_id'");
	}

	// Delete UPI ID
	if (isset($_POST['delete_upi'])) {
		$delete_upi = mysqli_real_escape_string($conn, $_POST['delete_upi']);
		$delete_query = "DELETE FROM deyya WHERE maulya='$delete_upi'";
		if (mysqli_query($conn, $delete_query)) {
			echo '<script>alert("UPI ID Deleted");</script>';
		} else {
			echo '<script>alert("Failed to Delete UPI ID");</script>';
		}
	}
?>

<?php include 'header.php'; ?>

     <div class="main-panel">
  <div class="content-wrapper">
    <div class="row">
      <div class="col-sm-12 mb-4 mb-xl-0">
        <h4 class="font-weight-bold text-dark">Add UPI</h4>
      </div>
    </div>

    <!-- Add UPI Form -->
    <div class="row mb-4">
      <form action="" id="upiform" method="post" autocomplete="off" class="w-100 d-flex">
        <input name="newupi" type="text" placeholder="Add a UPI ID" class="form-control mr-2" style="height: 40px;" required />
        <button type="submit" class="btn btn-primary">Add</button>
      </form>
    </div>

    <!-- UPI List and Set Active/Delete -->
    <div class="row">
      <form action="" id="upisave" method="post" autocomplete="off" class="w-100">
        <div class="table-responsive">
          <table class="table table-bordered">
            <thead class="thead-light">
              <tr>
                <th>Select</th>
                <th>UPI ID</th>
                <th>Active</th>
                <th>Action</th>
              </tr>
            </thead>
            <tbody>
              <?php
                $sel_upi = "SELECT * FROM deyya ORDER BY shonu DESC";
                $upi_r = mysqli_query($conn, $sel_upi);
                while ($row = mysqli_fetch_array($upi_r)) {
              ?>
              <tr>
                <td>
                  <input name="upiid" type="radio" value="<?php echo $row['maulya']; ?>" <?php if ($row['sthiti'] == 1) echo "checked"; ?> />
                </td>
                <td><?php echo $row['maulya']; ?></td>
                <td><?php echo ($row['sthiti'] == 1) ? '<span class="badge badge-success">Active</span>' : '<span class="badge badge-secondary">Inactive</span>'; ?></td>
                <td>
                  <button type="submit" name="delete_upi" value="<?php echo $row['maulya']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure you want to delete this UPI?')">Delete</button>
                </td>
              </tr>
              <?php } ?>
            </tbody>
          </table>
        </div>
        <button type="submit" class="btn btn-success mt-2">Save Active UPI</button>
      </form>
    </div>
  </div>
</div>

        
      </div>
    </div>
  </div>
  
  <script>	
	if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
  </script>
</body>

</html>