<?php
/* =========================================================
   ADMIN PROFILE SETTINGS (DB CONNECTED)
   ========================================================= */

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

include("conn.php");

// --- 1. CONFIGURATION ---
$table_name = "nirvahaka_shonu";
$username_col = "nirvahaka_hesaru"; // Username Column
$password_col = "guptapada";        // Password Column (MD5)
$id_col = "unohs";                  // ID Column
$admin_id = 1;                      // Target Admin ID

$msg = "";
$msg_type = "";

// --- 2. FETCH CURRENT ADMIN DATA ---
$fetch_sql = "SELECT * FROM $table_name WHERE $id_col = '$admin_id'";
$run_fetch = mysqli_query($conn, $fetch_sql);
$admin_data = mysqli_fetch_assoc($run_fetch);

// Current Values
$current_username = $admin_data[$username_col];

// --- 3. HANDLE UPDATE FORM ---
if (isset($_POST['update_profile'])) {
    $new_username = mysqli_real_escape_string($conn, $_POST['username']);
    $new_password = mysqli_real_escape_string($conn, $_POST['password']);
    
    // Check if password field is filled
    if (!empty($new_password)) {
        // Update Both (Password with MD5)
        $hashed_password = md5($new_password);
        $update_sql = "UPDATE $table_name SET $username_col='$new_username', $password_col='$hashed_password' WHERE $id_col='$admin_id'";
    } else {
        // Update Username Only
        $update_sql = "UPDATE $table_name SET $username_col='$new_username' WHERE $id_col='$admin_id'";
    }

    if (mysqli_query($conn, $update_sql)) {
        $msg = "✅ Profile Updated Successfully!";
        $msg_type = "success";
        $current_username = $new_username; // Refresh display
    } else {
        $msg = "❌ Error: " . mysqli_error($conn);
        $msg_type = "danger";
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=1200, initial-scale=0.5">
    <title>Admin Security Settings</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="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>
        /* --- PROFESSIONAL UI STYLES --- */
        body { background-color: #f0f2f5; font-family: 'Poppins', sans-serif; min-width: 1200px; }
        
        .settings-card {
            background: #fff;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.08);
            border-top: 6px solid #4834d4;
            padding: 40px;
            max-width: 700px;
            margin: 30px auto;
        }

        .card-header-title {
            font-size: 1.8rem; font-weight: 900; color: #2d3436;
            margin-bottom: 5px; display: flex; align-items: center; gap: 15px;
        }
        .card-header-title i { color: #4834d4; }
        .card-subtitle { color: #636e72; font-size: 0.95rem; margin-bottom: 30px; border-bottom: 2px dashed #eee; padding-bottom: 20px; }

        .form-group label {
            font-weight: 800; color: #2d3436; font-size: 0.9rem;
            text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 10px;
        }

        .input-group-text {
            background: #f8f9fa; border: 2px solid #dfe6e9; border-right: none;
            color: #4834d4; font-size: 1.2rem; width: 50px; justify-content: center;
        }

        .modern-input {
            height: 55px; border: 2px solid #dfe6e9; border-left: none;
            border-radius: 0 12px 12px 0; font-weight: 700; color: #2d3436;
            font-size: 1rem; padding-left: 15px;
        }
        .modern-input:focus { border-color: #4834d4; box-shadow: none; background: #fff; }
        .input-group:focus-within .input-group-text { border-color: #4834d4; background: #fff; }

        .btn-update {
            background: linear-gradient(135deg, #4834d4, #686de0);
            color: #fff; font-weight: 900; text-transform: uppercase; letter-spacing: 1px;
            padding: 18px; border-radius: 12px; border: none; width: 100%;
            box-shadow: 0 8px 20px rgba(72, 52, 212, 0.25);
            transition: 0.3s; margin-top: 10px; cursor: pointer;
        }
        .btn-update:hover { transform: translateY(-3px); box-shadow: 0 12px 25px rgba(72, 52, 212, 0.4); }

        .alert-box { padding: 15px; border-radius: 10px; margin-bottom: 25px; font-weight: 700; text-align: center; font-size: 1rem; }
        .alert-success { background: #d1fae5; color: #065f46; border: 2px solid #34d399; }
        .alert-danger { background: #fee2e2; color: #991b1b; border: 2px solid #f87171; }
    </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"><h4 style="color:#4834d4; font-weight:900; margin:0;">ADMIN<span style="color:#2d3436;">PANEL</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">
                    <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" href="#" data-toggle="dropdown">
                            <i class="icon-cog"></i>
                        </a>
                        <div class="dropdown-menu dropdown-menu-right navbar-dropdown preview-list">
                            <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">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="settings-card">
                        <div class="card-header-title"><i class="fa-solid fa-shield-halved"></i> SECURITY SETTINGS</div>
                        <div class="card-subtitle">Update your admin login details securely.</div>

                        <?php if($msg != "") { ?>
                            <div class="alert-box alert-<?php echo $msg_type; ?>">
                                <?php echo $msg; ?>
                            </div>
                        <?php } ?>

                        <form method="post" autocomplete="off">
                            <div class="row">
                                <div class="col-md-12 form-group">
                                    <label>Admin Username</label>
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text"><i class="fa-solid fa-user-gear"></i></span>
                                        </div>
                                        <input type="text" name="username" class="form-control modern-input" value="<?php echo $current_username; ?>" required>
                                    </div>
                                </div>

                                <div class="col-md-12 form-group mt-3">
                                    <label>New Password <small style="color:#888; font-weight:500;">(Optional)</small></label>
                                    <div class="input-group">
                                        <div class="input-group-prepend">
                                            <span class="input-group-text"><i class="fa-solid fa-lock"></i></span>
                                        </div>
                                        <input type="text" name="password" class="form-control modern-input" placeholder="Enter new password">
                                    </div>
                                    <small style="color:#666; margin-top:5px; display:block;">* Leave empty if you don't want to change the password.</small>
                                </div>

                                <div class="col-md-12 mt-2">
                                    <button type="submit" name="update_profile" class="btn-update">
                                        <i class="fa-solid fa-floppy-disk"></i> SAVE CHANGES
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>

                </div>
                
                <footer class="footer">
                    <div class="d-sm-flex justify-content-center justify-content-sm-between">
                        <span class="text-muted d-block text-center text-sm-left d-sm-inline-block">Copyright © 2026 AB Coders</span>
                    </div>
                </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="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>
    // Prevent Form Resubmission
    if ( window.history.replaceState ) {
        window.history.replaceState( null, null, window.location.href );
    }
    </script>
</body>
</html>