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

include("conn.php");

// Fetch Current Rate
$query = "SELECT rate FROM tbl_pg WHERE value = 'usdt' LIMIT 1";
$result = mysqli_query($conn, $query);
$currentRate = "0.00";
if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $currentRate = $row['rate'];
}

// Update Rate Logic
if (isset($_POST['newupi'])) {
    $upiid = mysqli_real_escape_string($conn, $_POST['newupi']);
    if(is_numeric($upiid)){
        $sql_q = "UPDATE tbl_pg SET rate='" . $upiid . "' WHERE value = 'usdt'";
        $chk = mysqli_query($conn, $sql_q);
        if ($chk) {
            echo '<script>alert("✅ USDT Rate Updated Successfully!"); window.location.href="usdtkids.php";</script>';
        } else {
            echo '<script>alert("❌ Update Failed!");</script>';
        }
    } else {
        echo '<script>alert("⚠️ Please enter a valid number!");</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, user-scalable=no">
    <title>Manage USDT | Admin</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600;700;800;900&family=Rajdhani:wght@700&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>
        /* =========================================
           USDT PRO LIGHT THEME
           ========================================= */
        :root {
            --usdt-green: #26a17b;
            --primary-grad: linear-gradient(135deg, #26a17b 0%, #16644d 100%);
            --bg-body: #f7f9fc;
            --bg-white: #ffffff;
            --text-main: #2d3436;
            --border-color: #eef2f7;
        }

        body {
            background-color: var(--bg-body) !important;
            color: var(--text-main);
            font-family: 'Poppins', sans-serif;
            font-weight: 600;
        }

        /* --- NAVBAR & SIDEBAR --- */
        .navbar { background: #ffffff !important; box-shadow: 0 4px 20px rgba(0,0,0,0.03) !important; }
        .sidebar { background: #ffffff !important; border-right: 1px solid var(--border-color); }
        .user-profile { background: #f8f9fa !important; border-bottom: 1px solid var(--border-color); }
        .user-name { color: #000 !important; font-weight: 800 !important; }
        .nav-link { color: #555 !important; }
        .nav-link:hover { color: var(--usdt-green) !important; background: #f0fff4 !important; }
        .icon-menu { color: #333 !important; }

        /* 3-DOT BUTTON FIX */
        .navbar-toggler { border: none !important; outline: none !important; }
        .fa-bars { font-size: 1.5rem; color: #333 !important; }

        /* --- RATE CARD --- */
        .rate-card {
            background: #fff;
            border-radius: 20px;
            padding: 30px;
            text-align: center;
            box-shadow: 0 10px 40px rgba(38, 161, 123, 0.15);
            border: 1px solid #eef2f7;
            margin-bottom: 30px;
            position: relative;
            overflow: hidden;
        }
        
        .rate-card::before {
            content: ''; position: absolute; top: 0; left: 0; width: 100%; height: 6px;
            background: var(--primary-grad);
        }

        .coin-icon {
            width: 80px; height: 80px;
            background: #e6f7f2;
            border-radius: 50%;
            display: inline-flex; align-items: center; justify-content: center;
            font-size: 2.5rem; color: var(--usdt-green);
            margin-bottom: 15px;
            box-shadow: 0 5px 15px rgba(38, 161, 123, 0.2);
        }

        .rate-title { font-size: 1rem; color: #888; text-transform: uppercase; letter-spacing: 2px; font-weight: 700; }
        
        .rate-value {
            font-family: 'Rajdhani', sans-serif;
            font-size: 3.5rem; font-weight: 800; color: #2d3436;
            margin: 10px 0; line-height: 1;
        }
        .rate-currency { font-size: 1.5rem; color: var(--usdt-green); vertical-align: super; }

        /* --- UPDATE FORM --- */
        .update-box {
            background: #fff;
            border-radius: 20px;
            padding: 25px;
            box-shadow: 0 5px 20px rgba(0,0,0,0.05);
            border: 1px solid #eef2f7;
        }

        .box-header {
            display: flex; align-items: center; gap: 10px; margin-bottom: 20px;
            font-size: 1.2rem; font-weight: 800; color: #333;
        }

        .modern-input-group { position: relative; margin-bottom: 20px; }

        .input-icon {
            position: absolute; left: 15px; top: 50%; transform: translateY(-50%);
            color: #888; font-size: 1.1rem;
        }

        .modern-input {
            width: 100%; padding: 15px 15px 15px 45px;
            background: #f9fbfd;
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            font-size: 1.1rem; font-weight: 700; color: #333;
            transition: 0.3s; outline: none;
        }
        .modern-input:focus {
            border-color: var(--usdt-green);
            background: #fff;
            box-shadow: 0 0 0 4px rgba(38, 161, 123, 0.1);
        }

        .btn-update {
            width: 100%;
            background: var(--primary-grad);
            color: #fff; padding: 15px; border: none; border-radius: 12px;
            font-weight: 800; font-size: 1rem; cursor: pointer;
            box-shadow: 0 8px 20px rgba(38, 161, 123, 0.3);
            text-transform: uppercase; letter-spacing: 1px; transition: 0.3s;
            display: flex; align-items: center; justify-content: center; gap: 10px;
        }
        .btn-update:hover { transform: translateY(-3px); box-shadow: 0 12px 25px rgba(38, 161, 123, 0.4); }
        .btn-update:active { transform: scale(0.98); }

        /* Mobile Fix */
        @media (max-width: 768px) {
            .content-wrapper { padding: 15px !important; }
            .rate-value { font-size: 2.8rem; }
        }
    </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:var(--usdt-green); font-weight:900; margin:0;">USDT<span style="color:#333;">RATE</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" style="font-size: 1.5rem; color: #333;"></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" id="notificationDropdown" href="#" data-toggle="dropdown">
                            <i class="icon-cog" style="color:#333; font-size:1.2rem;"></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">
                    <i class="fa-solid fa-bars" style="font-size: 1.5rem; color: #333;"></i>
                </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="row justify-content-center">
                        <div class="col-md-6 col-lg-5">
                            
                            <div class="rate-card">
                                <div class="coin-icon">
                                    <i class="fa-solid fa-sack-dollar"></i>
                                </div>
                                <div class="rate-title">Current Exchange Rate</div>
                                <div class="rate-value">
                                    <span class="rate-currency">₹</span> <?php echo htmlspecialchars($currentRate); ?>
                                </div>
                                <div style="color:#26a17b; font-size:0.9rem; background:#e6f7f2; display:inline-block; padding:5px 15px; border-radius:20px;">
                                    <i class="fa-solid fa-arrow-trend-up"></i> Live USDT Price
                                </div>
                            </div>

                            <div class="update-box">
                                <div class="box-header">
                                    <i class="fa-solid fa-pen-to-square" style="color:var(--usdt-green);"></i> Update Rate
                                </div>
                                
                                <form action="#" id="upiform" method="post" autocomplete="off">
                                    <div class="modern-input-group">
                                        <i class="fa-solid fa-indian-rupee-sign input-icon"></i>
                                        <input name="newupi" type="number" step="0.01" placeholder="Enter New Rate (e.g. 92.50)" class="modern-input" required />
                                    </div>
                                    
                                    <button type="submit" class="btn-update">
                                        <i class="fa-solid fa-floppy-disk"></i> Update Now
                                    </button>
                                </form>
                            </div>

                        </div>
                    </div>

                </div>
                
                <footer class="footer" style="padding:20px; text-align:center; color:#888; font-size:0.85rem; background:transparent;">
                    <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="vendors/chart.js/Chart.min.js"></script>
    <script src="js/dashboard.js"></script>

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

    
    </script>
</body>
</html>