<?php
// --- Error Reporting (Clean UI) ---
error_reporting(0);

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

// Initialize Variables
$uid = "";
$holder = "";
$acc_no = "";
$ifsc = "";
$bank_name = "";

// --- 1. FETCH DATA (User ID se Data Dhundo) ---
if (isset($_GET['uid'])) {
    $uid = mysqli_real_escape_string($conn, $_GET['uid']);

    // Check karo ki user ka data 'khate' table me hai ya nahi
    $sql = "SELECT * FROM khate WHERE byabaharkarta = '$uid' ORDER BY shonu DESC LIMIT 1"; 
    $query = mysqli_query($conn, $sql);
    
    // Agar Data Mila to Form me dikhao
    if($row = mysqli_fetch_assoc($query)){
        $holder = $row['phalanubhavi'];   // Account Holder
        $acc_no = $row['khatesankhye'];   // Account No
        $ifsc = $row['kod'];              // IFSC Code
        $bank_name = $row['khatehesaru']; // Bank Name
    }
}

// --- 2. SAVE DATA (Update or Insert) ---
if (isset($_POST['save_bank'])) {
    $p_uid = $_POST['uid'];
    $p_holder = mysqli_real_escape_string($conn, $_POST['holder']);
    $p_acc_no = mysqli_real_escape_string($conn, $_POST['acc_no']);
    $p_ifsc = mysqli_real_escape_string($conn, $_POST['ifsc']);
    $p_bank_name = mysqli_real_escape_string($conn, $_POST['bank_name']);

    // Check karo record pehle se hai ya nahi
    $check_sql = "SELECT * FROM khate WHERE byabaharkarta = '$p_uid'";
    $check_res = mysqli_query($conn, $check_sql);

    if(mysqli_num_rows($check_res) > 0) {
        // --- CASE A: UPDATE (Agar pehle se hai) ---
        $sql_final = "UPDATE khate SET 
                      phalanubhavi = '$p_holder', 
                      khatesankhye = '$p_acc_no', 
                      kod = '$p_ifsc', 
                      khatehesaru = '$p_bank_name' 
                      WHERE byabaharkarta = '$p_uid'";
    } else {
        // --- CASE B: INSERT (Agar naya user hai / First Time) ---
        
        // Pehle User ka Mobile aur Email nikalo (shonu_subjects table se)
        // Taki 'duravani' aur 'daka' column khali na rahein
        $u_sql = "SELECT mobile, email FROM shonu_subjects WHERE id = '$p_uid'";
        $u_q = mysqli_query($conn, $u_sql);
        $u_row = mysqli_fetch_assoc($u_q);
        
        $mob = $u_row['mobile'];
        $email = $u_row['email'];

        // Naya Record Banao (Default values ke sath)
        // sthiti = 1 (Active), khatakrama = 1, kodprakara = 6 (Sample data ke hisab se)
        $sql_final = "INSERT INTO khate (byabaharkarta, phalanubhavi, khatesankhye, kod, khatehesaru, duravani, daka, sthiti, khatakrama, kodprakara) 
                      VALUES ('$p_uid', '$p_holder', '$p_acc_no', '$p_ifsc', '$p_bank_name', '$mob', '$email', '1', '1', '6')";
    }

    if (mysqli_query($conn, $sql_final)) {
        echo "<script>alert('✅ Bank Details Saved Successfully!'); window.location.href='manage_withdraw.php';</script>";
    } else {
        echo "<script>alert('❌ Error Saving Data: " . mysqli_error($conn) . "');</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>Edit Bank Details</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@500;600;700;800;900&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="vendors/mdi/css/materialdesignicons.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="css/style.css">
    <link rel="shortcut icon" href="images/favicon.png" />

    <style>
        /* UI Styling */
        :root { --primary: #4834d4; --success: #20bf6b; --dark: #2d3436; --light: #f4f7f6; --white: #ffffff; }
        body { background-color: var(--light); font-family: 'Poppins', sans-serif; color: var(--dark); font-weight: 600; }
        .edit-card { background: var(--white); border-radius: 20px; padding: 30px; border-bottom: 6px solid #d1ccc0; box-shadow: 0 10px 30px rgba(0,0,0,0.05); margin: 20px auto; max-width: 600px; width: 95%; transition: 0.3s; }
        .card-header-flex { text-align: center; margin-bottom: 25px; border-bottom: 2px dashed #eee; padding-bottom: 15px; }
        .card-title { font-size: 1.5rem; font-weight: 900; color: var(--primary); text-transform: uppercase; letter-spacing: 1px; }
        .form-group { margin-bottom: 20px; text-align: left; }
        .form-label { font-size: 0.85rem; font-weight: 700; color: #636e72; margin-bottom: 8px; display: block; }
        .input-wrapper { position: relative; }
        .input-icon { position: absolute; left: 15px; top: 50%; transform: translateY(-50%); color: var(--primary); font-size: 1.1rem; }
        .modern-input { width: 100%; padding: 12px 15px 12px 45px; border: 2px solid #dfe6e9; border-radius: 12px; font-weight: 700; font-size: 1rem; color: var(--dark); outline: none; transition: 0.3s; background: #f9f9f9; }
        .modern-input:focus { border-color: var(--primary); background: #fff; box-shadow: 0 0 0 4px rgba(72, 52, 212, 0.1); }
        .btn-row { display: flex; gap: 15px; margin-top: 30px; }
        .btn-3d { flex: 1; border: none; border-radius: 12px; padding: 12px; font-weight: 800; text-transform: uppercase; font-size: 0.9rem; cursor: pointer; position: relative; top: 0; color: #fff; transition: all 0.1s; display: flex; align-items: center; justify-content: center; gap: 8px; }
        .btn-save { background: var(--success); box-shadow: 0 5px 0 #169352; }
        .btn-back { background: var(--dark); box-shadow: 0 5px 0 #000; text-decoration: none; }
        .btn-3d:active { top: 5px; box-shadow: 0 0 0; }
        @media (max-width: 768px) { .content-wrapper { padding: 10px !important; } .edit-card { padding: 20px; margin-top: 10px; } .btn-row { flex-direction: column; } }
    </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" style="background:#fff;">
                <a class="navbar-brand brand-logo" href="dashboard.php">
                    <h4 style="color:var(--primary); font-weight:900; margin:0;">EDIT<span style="color:var(--dark);">BANK</span></h4>
                </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"><i class="fa-solid fa-bars"></i></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" href="#" data-toggle="dropdown"><i class="fa-solid fa-gear" style="color:var(--dark); font-size:1.2rem;"></i></a>
                        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list">
                            <a class="dropdown-item preview-item" href="logout.php"><i class="fa-solid fa-power-off"></i> Logout</a>
                        </div>
                    </li>
                </ul>
                <button class="navbar-toggler navbar-toggler-right d-lg-none align-self-center" type="button" data-toggle="offcanvas"><i class="fa-solid fa-bars"></i></button>
            </div>
        </nav>

        <div class="container-fluid page-body-wrapper">
            <nav class="sidebar sidebar-offcanvas" id="sidebar" style="background:#fff;">
                <div class="user-profile" style="background:#fff; border-bottom:2px dashed #eee;">
                    <div class="user-image"><img src="images/faces/face28.png"></div>
                    <div class="user-name" style="color:var(--dark);">AB Coders</div>
                    <div class="user-designation">Admin</div>
                </div>
                <?php include 'compass.php'; ?>
            </nav>

            <div class="main-panel">
                <div class="content-wrapper">
                    
                    <div class="edit-card">
                        <div class="card-header-flex">
                            <div class="card-title"><i class="fa-solid fa-pen-to-square"></i> Modify Bank</div>
                            <div style="font-size:0.9rem; color:#888;">User ID: <span style="color:var(--primary); font-weight:800;"><?php echo $uid; ?></span></div>
                        </div>

                        <form method="post">
                            <input type="hidden" name="uid" value="<?php echo $uid; ?>">

                            <div class="form-group">
                                <label class="form-label">Account Holder Name (Phalanubhavi)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-user input-icon"></i>
                                    <input type="text" name="holder" class="modern-input" value="<?php echo $holder; ?>" placeholder="Enter Name" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Account Number (Khate Sankhye)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-hashtag input-icon"></i>
                                    <input type="number" name="acc_no" class="modern-input" value="<?php echo $acc_no; ?>" placeholder="Enter Account No" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="form-label">IFSC Code (Kod)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-building-columns input-icon"></i>
                                    <input type="text" name="ifsc" class="modern-input" value="<?php echo $ifsc; ?>" placeholder="Enter IFSC" style="text-transform:uppercase;" required>
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="form-label">Bank Name (Khate Hesaru)</label>
                                <div class="input-wrapper">
                                    <i class="fa-solid fa-money-check-dollar input-icon"></i>
                                    <input type="text" name="bank_name" class="modern-input" value="<?php echo $bank_name; ?>" placeholder="Enter Bank Name" required>
                                </div>
                            </div>

                            <div class="btn-row">
                                <a href="manage_withdraw.php" class="btn-3d btn-back">
                                    <i class="fa-solid fa-arrow-left"></i> Back
                                </a>
                                <button type="submit" name="save_bank" class="btn-3d btn-save">
                                    <i class="fa-solid fa-floppy-disk"></i> Save Data
                                </button>
                            </div>

                        </form>
                    </div>

                </div>
                <footer class="footer" style="padding:20px; text-align:center; color:#999; font-size:0.8rem;"><span>© 2026 AB Coders</span></footer>
            </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="js/dashboard.js"></script>
    <script>
    
    </script>
</body>
</html>