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

	/* ===== ADD UPI (ORIGINAL) ===== */
	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 type="text/JavaScript"> alert("UPI ID Added"); </script>';
		}else {echo '<script type="text/JavaScript"> alert("UPI ID Failed"); </script>';}
	}

	/* ===== SET ACTIVE UPI (ORIGINAL) ===== */
	if(isset($_POST['upiid'])){
		$a_id = $_POST['upiid'];
		$sql_s = "SELECT * FROM deyya WHERE maulya = '".$a_id."'";
		$run = mysqli_query($conn, $sql_s);

		$sql_d = "SELECT * FROM deyya WHERE sthiti = '1'";		
		$run_d = mysqli_query($conn, $sql_d);
		$rund_f = mysqli_fetch_array($run_d);

		$ch_s0 = "UPDATE deyya SET sthiti='0' WHERE shonu='".$rund_f['shonu']."'";
		$exe_ch_s0 = mysqli_query($conn, $ch_s0);

		$run_f = mysqli_fetch_array($run);
		$ch_s1 = "UPDATE deyya SET sthiti='1' WHERE shonu='".$run_f['shonu']."'";
		$exe_ch_s1 = mysqli_query($conn, $ch_s1);
	}

	/* ===== DELETE UPI (ONLY NEW PART) ===== */
	if(isset($_POST['delete_upi'])){
		$del_upi = mysqli_real_escape_string($conn, $_POST['delete_upi']);
		$del_sql = "DELETE FROM deyya WHERE maulya = '".$del_upi."'";
		if(mysqli_query($conn, $del_sql)){
			echo '<script>alert("UPI ID Deleted");</script>';
		}else{
			echo '<script>alert("Failed to Delete UPI");</script>';
		}
	}
?>
<!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>
	.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-button {
        padding: 0.5rem 1rem;
        font-size: 1rem;
        border-radius: 0.25rem;
        transition: all 0.3s ease;
    }

	/* ===== Responsive UPI Row (NEW CSS ONLY) ===== */
	.upi-row{
		display:flex;
		align-items:center;
		justify-content:space-between;
		padding:6px 10px;
		border-bottom:1px solid #eee;
		flex-wrap:wrap;
	}
	.upi-left{
		display:flex;
		align-items:center;
		gap:8px;
		flex:1;
		word-break:break-all;
	}
	.upi-right{
		margin-top:5px;
	}
	@media (max-width: 600px){
		.upi-row{
			flex-direction:column;
			align-items:flex-start;
		}
		.upi-right{
			width:100%;
			text-align:right;
			margin-top:8px;
		}
	}
  </style>
</head>

<body>
<div class="container-scroller">

<!-- (NAVBAR + SIDEBAR ORIGINAL SAME) -->

<div class="main-panel">
<div class="content-wrapper">

<h4 class="font-weight-bold text-dark">Add UPI</h4>

<form action="#" id="upiform" method="post" autocomplete="off">
	<div class="d-flex align-items-center">	
		<input name="newupi" type="text" placeholder="Add an UPI ID" class="flex-grow-1 cool-input" style="height: 40px;" />
	</div>
	<div class="d-flex align-items-center mt-3">
		<button type="submit" class="btn btn-primary cool-button mr-2">Add</button>
	</div>
</form>

<?php
$sel_upi = "SELECT * FROM deyya WHERE sthiti='0' OR sthiti='1'";
$upi_r = mysqli_query($conn, $sel_upi);
?>

<form action="#" id="upisave" method="post" autocomplete="off">

<?php while ($row = mysqli_fetch_array($upi_r)) { ?>

<div class="upi-row">
	<div class="upi-left">
		<input name="upiid" type="radio"
			value="<?php echo $row['maulya']; ?>"
			<?php if($row['sthiti']==1){echo "checked";} ?> />

		<label><?php echo $row['maulya']; ?></label>
	</div>

	<div class="upi-right">
		<button type="submit"
			name="delete_upi"
			value="<?php echo $row['maulya']; ?>"
			class="btn btn-danger btn-sm"
			onclick="return confirm('Delete this UPI ID?');">
			Delete
		</button>
	</div>
</div>

<?php } ?>

<button type="submit" class="btn btn-primary cool-button mr-2">Save</button>
</form>

</div>
</div>
</div>

<!-- ORIGINAL JS (UNCHANGED) -->
<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>

<!-- ⚠️ History replaceState BILKUL NAHI CHHEDA -->
<script>	
	if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
</script>

</body>
</html>
