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

include("conn.php");

$user_records = null;
$show_form = false;

if(isset($_POST['search_user'])) {
    $byabaharkarta = mysqli_real_escape_string($conn, $_POST['byabaharkarta']);
    if(!empty($byabaharkarta)) {
        $user_records = mysqli_query($conn, "SELECT * FROM khate WHERE byabaharkarta = '$byabaharkarta'");
        if(mysqli_num_rows($user_records) > 0) {
            $show_form = true;
        } else {
            echo '<script>alert("No records found for this User ID!");</script>';
        }
    }
}

if(isset($_POST['update'])) {
    $shonu = mysqli_real_escape_string($conn, $_POST['shonu']);
    $byabaharkarta = mysqli_real_escape_string($conn, $_POST['byabaharkarta']);
    $khatesankhye = mysqli_real_escape_string($conn, $_POST['khatesankhye']);
    $khatakrama = mysqli_real_escape_string($conn, $_POST['khatakrama']);
    $phalanubhavi = mysqli_real_escape_string($conn, $_POST['phalanubhavi']);
    $kodprakara = mysqli_real_escape_string($conn, $_POST['kodprakara']);
    $daka = mysqli_real_escape_string($conn, $_POST['daka']);
    $kod = mysqli_real_escape_string($conn, $_POST['kod']);
    $khatehesaru = mysqli_real_escape_string($conn, $_POST['khatehesaru']);
    $duravani = mysqli_real_escape_string($conn, $_POST['duravani']);
    $sthiti = mysqli_real_escape_string($conn, $_POST['sthiti']);
    
    $update_sql = "UPDATE khate SET khatesankhye='$khatesankhye', khatakrama='$khatakrama', phalanubhavi='$phalanubhavi', kodprakara='$kodprakara', daka='$daka', kod='$kod', khatehesaru='$khatehesaru', duravani='$duravani', sthiti='$sthiti' WHERE shonu='$shonu' AND byabaharkarta='$byabaharkarta'";
    
    if(mysqli_query($conn, $update_sql)) {
        echo '<script>alert("Bank details updated successfully!");</script>';
        $user_records = mysqli_query($conn, "SELECT * FROM khate WHERE byabaharkarta = '$byabaharkarta'");
        $show_form = true;
    } else {
        echo '<script>alert("Failed to update bank details!");</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-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
          </div>
          <div class="user-designation">
              Admin
          </div>
        </div>
        <?php include 'compass.php';?>
      </nav>
      <div class="main-panel">
    <div class="content-wrapper">
        <!-- Container with improved padding -->
        <div class="container px-md-4 px-2">
            <div class="row justify-content-center">
                <div class="col-xl-10 col-lg-12 col-md-12"> <!-- Width thoda badhaya -->

                    <!-- Search Form Card -->
                    <div class="card mb-4 shadow-sm">
                        <div class="card-header">
                            <h2 class="mb-0">Search User Bank Details</h2>
                        </div>
                        <div class="card-body">
                            <form action="#" method="post" class="row g-3">
                                <div class="col-md-8">
                                    <label for="byabaharkarta" class="form-label">User ID:</label>
                                    <input type="number" name="byabaharkarta" id="byabaharkarta" class="form-control" required>
                                </div>
                                <div class="col-md-4 d-flex align-items-end">
                                    <button type="submit" name="search_user" class="btn btn-primary w-100">Search</button>
                                </div>
                            </form>
                        </div>
                    </div>

                    <!-- Edit Bank Details Card -->
                    <?php if ($show_form && $user_records): ?>
                    <div class="card shadow-sm mb-4">
                        <div class="card-header">
                            <h2 class="mb-0">Edit Bank Details</h2>
                        </div>
                        <div class="card-body">
                            <?php while ($row = mysqli_fetch_assoc($user_records)): ?>
                            <form action="#" method="post" class="row g-3">
                                <input type="hidden" name="shonu" value="<?php echo $row['shonu']; ?>">
                                <input type="hidden" name="byabaharkarta" value="<?php echo $row['byabaharkarta']; ?>">

                                <div class="col-md-6">
                                    <label class="form-label">Bank Account Number:</label>
                                    <input type="text" name="khatesankhye" class="form-control" value="<?php echo $row['khatesankhye']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Account Order:</label>
                                    <input type="text" name="khatakrama" class="form-control" value="<?php echo $row['khatakrama']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Account Holder:</label>
                                    <input type="text" name="phalanubhavi" class="form-control" value="<?php echo $row['phalanubhavi']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Code Type:</label>
                                    <input type="text" name="kodprakara" class="form-control" value="<?php echo $row['kodprakara']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Email:</label>
                                    <input type="email" name="daka" class="form-control" value="<?php echo $row['daka']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">IFSC Code:</label>
                                    <input type="text" name="kod" class="form-control" value="<?php echo $row['kod']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Bank Name:</label>
                                    <input type="text" name="khatehesaru" class="form-control" value="<?php echo $row['khatehesaru']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Phone Number:</label>
                                    <input type="text" name="duravani" class="form-control" value="<?php echo $row['duravani']; ?>">
                                </div>
                                <div class="col-md-6">
                                    <label class="form-label">Status:</label>
                                    <select name="sthiti" class="form-control">
                                        <option value="Active" <?php echo ($row['sthiti'] == 'Active') ? 'selected' : ''; ?>>Active</option>
                                        <option value="Inactive" <?php echo ($row['sthiti'] == 'Inactive') ? 'selected' : ''; ?>>Inactive</option>
                                    </select>
                                </div>
                                <div class="col-12">
                                    <button type="submit" name="update" class="btn btn-success w-100">Update</button>
                                </div>
                            </form>
                            <?php endwhile; ?>
                        </div>
                    </div>
                    <?php endif; ?>

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

</body>

</html>