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

// --- 1. ADD NEW USDT ---
if (isset($_POST['newupi'])) {
    $upiid = mysqli_real_escape_string($conn, $_POST['newupi']);
    if(trim($upiid) != ""){
        $sql_q = "INSERT INTO deyyamrici (maulya, sthiti) VALUES ('" . $upiid . "', '0')";
        $chk = mysqli_query($conn, $sql_q);
        if ($chk) {
            echo '<script>alert("✅ USDT Address Added Successfully!");</script>';
        } else {
            echo '<script>alert("❌ Failed to Add USDT!");</script>';
        }
    }
}

// --- 2. ACTIVATE USDT ---
if (isset($_POST['upiid'])) {
    $a_id = $_POST['upiid'];
    
    // Sabko 0 (Inactive) karo
    $deactivate = mysqli_query($conn, "UPDATE deyyamrici SET sthiti='0'");
    
    // Selected wale ko 1 (Active) karo
    $activate = mysqli_query($conn, "UPDATE deyyamrici SET sthiti='1' WHERE maulya='$a_id'");
    
    if($activate) {
        echo '<script>alert("✨ Active USDT Updated Successfully!");</script>';
    }
}

// --- 3. DELETE USDT ---
if (isset($_GET['delete'])) {
    $del_id = $_GET['delete'];
    $del_sql = "DELETE FROM deyyamrici WHERE maulya = '$del_id'";
    if(mysqli_query($conn, $del_sql)){
        echo '<script>alert("🗑️ USDT Address Deleted Successfully!"); window.location.href="addusdt.php";</script>';
    }
}
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=1200, initial-scale=0.1">
    <title>Manage USDT | Admin</title>
    
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;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>
        /* =========================================
           ULTRA PRO LIGHT THEME (Similar to Add UPI)
           ========================================= */
        :root {
            --primary-grad: linear-gradient(135deg, #26a17b 0%, #16644d 100%); /* USDT Green */
            --success-grad: linear-gradient(135deg, #11998e 0%, #38ef7d 100%);
            --danger-grad: linear-gradient(135deg, #ff416c 0%, #ff4b2b 100%);
            --bg-body: #f7f9fc;
            --bg-white: #ffffff;
            --text-main: #2d3436;
            --text-light: #636e72;
            --border-color: #eef2f7;
            --usdt-color: #26a17b;
        }

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

        /* --- SIDEBAR & NAVBAR FIX --- */
        .sidebar { background: #ffffff !important; border-right: 1px solid var(--border-color); }
        .sidebar .nav-link { color: #555 !important; font-weight: 600; }
        .sidebar .nav-link:hover { background: #f0fff4 !important; color: var(--usdt-color) !important; }
        .sidebar .nav-link i { color: var(--usdt-color) !important; }
        .user-profile { background: #fff !important; border-bottom: 1px solid var(--border-color); }
        .user-name { color: #000 !important; font-weight: 800 !important; }
        
        .navbar { background: #ffffff !important; box-shadow: 0 4px 20px rgba(0,0,0,0.03) !important; }
        .navbar .navbar-brand-wrapper { background: #ffffff !important; }
        
        /* 3-DOT / MENU ICON FIX */
        .navbar-toggler { border: none !important; outline: none !important; }
        .fa-bars { font-size: 1.5rem; color: #333 !important; }

        /* --- 3D CARD STYLE --- */
        .usdt-card {
            background: var(--bg-white);
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.08);
            border: 1px solid #fff;
            margin-bottom: 25px;
            position: relative;
            overflow: hidden;
            transition: transform 0.3s;
        }
        .usdt-card:hover { transform: translateY(-5px); }
        
        .card-title {
            font-size: 1.3rem; font-weight: 800; color: #2d3436; margin-bottom: 25px;
            display: flex; align-items: center; gap: 12px; text-transform: uppercase; letter-spacing: 0.5px;
        }

        /* --- BOLD INPUT --- */
        .modern-input {
            width: 100%; padding: 15px 20px;
            background: #f8faff;
            border: 2px solid #e2e8f0;
            border-radius: 12px;
            font-size: 1rem; font-weight: 700; color: #2d3436;
            transition: 0.3s; outline: none;
        }
        .modern-input:focus {
            border-color: var(--usdt-color);
            background: #fff;
            box-shadow: 0 0 0 4px rgba(38, 161, 123, 0.1);
        }

        /* --- BUTTONS --- */
        .btn-add {
            background: var(--primary-grad);
            color: #fff; padding: 12px 30px; border: none; border-radius: 12px;
            font-weight: 800; font-size: 1rem; cursor: pointer; transition: 0.3s;
            display: inline-flex; align-items: center; gap: 10px;
            box-shadow: 0 8px 20px rgba(38, 161, 123, 0.3);
        }
        .btn-add:hover { transform: translateY(-2px); box-shadow: 0 12px 25px rgba(38, 161, 123, 0.4); }

        .btn-save {
            width: 100%;
            background: var(--success-grad);
            color: #fff; padding: 15px; border: none; border-radius: 12px;
            font-weight: 900; font-size: 1.1rem; cursor: pointer; margin-top: 25px;
            box-shadow: 0 8px 20px rgba(56, 239, 125, 0.3);
            text-transform: uppercase; letter-spacing: 1px; transition: 0.3s;
        }
        .btn-save:hover { transform: translateY(-2px); }

        /* --- LIST ITEMS --- */
        .usdt-list { display: flex; flex-direction: column; gap: 15px; }

        .usdt-item {
            display: flex; align-items: center; justify-content: space-between;
            padding: 15px 20px;
            border: 2px solid #eef2f7;
            border-radius: 15px;
            background: #fff;
            transition: 0.2s;
        }
        .usdt-item:hover { border-color: #cbd5e0; background: #fafbff; }
        
        .usdt-item.active {
            border-color: #38ef7d;
            background: #f0fff4;
            box-shadow: 0 5px 15px rgba(56, 239, 125, 0.1);
        }

        .radio-wrapper {
            display: flex; align-items: center; gap: 15px; flex-grow: 1; cursor: pointer;
        }
        .custom-radio { accent-color: #38ef7d; transform: scale(1.6); cursor: pointer; }
        .usdt-text { font-size: 1rem; font-weight: 700; color: #2d3436; word-break: break-all; } /* Break long address */

        /* Delete Button */
        .btn-delete {
            background: #fff0f0; color: #ff4b2b;
            width: 40px; height: 40px; border-radius: 50%;
            display: flex; align-items: center; justify-content: center;
            font-size: 1.1rem; transition: 0.3s; border: 1px solid #ffe0e0;
            text-decoration: none; margin-left: 10px;
        }
        .btn-delete:hover {
            background: var(--danger-grad); color: #fff;
            box-shadow: 0 5px 15px rgba(255, 75, 43, 0.3);
        }

        /* Status Badge */
        .status-badge {
            font-size: 0.7rem; font-weight: 900;
            padding: 5px 12px; border-radius: 30px;
            text-transform: uppercase; margin-right: 5px;
            background: linear-gradient(135deg, #11998e, #38ef7d); color: #fff;
        }

        /* Mobile Fix */
        @media (max-width: 768px) {
            .content-wrapper { padding: 15px !important; }
            .usdt-item { padding: 15px; }
            .status-badge { display: none; } /* Hide badge on mobile */
            .usdt-text { font-size: 0.9rem; }
        }
    </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-color); font-weight:900; margin:0; letter-spacing:-1px;">ADD<span style="color:#333;">USDT</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-8 col-lg-6">
                            <div class="usdt-card">
                                <div class="card-title">
                                    <i class="fa-solid fa-circle-plus" style="color:var(--usdt-color); font-size:1.5rem;"></i> Add New USDT
                                </div>
                                <form action="#" method="post" autocomplete="off" style="display:flex; gap:10px; align-items:center;">
                                    <input name="newupi" type="text" placeholder="Enter USDT Address (TRC20)" class="modern-input" required />
                                    <button type="submit" class="btn-add">ADD</button>
                                </form>
                            </div>
                        </div>
                    </div>

                    <div class="row justify-content-center">
                        <div class="col-md-8 col-lg-6">
                            <div class="usdt-card" style="border-top: 5px solid #38ef7d;">
                                <div class="card-title">
                                    <i class="fa-solid fa-list-check" style="color:#38ef7d; font-size:1.5rem;"></i> Active Address List
                                </div>
                                
                                <form action="#" id="upisave" method="post">
                                    <div class="usdt-list">
                                        <?php
                                            $sel_upi = "SELECT * FROM deyyamrici WHERE sthiti='0' OR sthiti='1'";
                                            $upi_r = mysqli_query($conn, $sel_upi);
                                            while ($row = mysqli_fetch_array($upi_r)) {
                                                $isActive = ($row['sthiti'] == 1);
                                        ?>
                                        
                                        <div class="usdt-item <?php echo $isActive ? 'active' : ''; ?>">
                                            
                                            <label class="radio-wrapper">
                                                <input name="upiid" type="radio" class="custom-radio" value="<?php echo $row['maulya']; ?>" <?php if($isActive){echo "checked";} ?> />
                                                <span class="usdt-text"><?php echo $row['maulya']; ?></span>
                                            </label>

                                            <div style="display:flex; align-items:center;">
                                                <?php if($isActive): ?>
                                                    <span class="status-badge">ON</span>
                                                <?php endif; ?>
                                                
                                                <a href="addusdt.php?delete=<?php echo $row['maulya']; ?>" class="btn-delete" onclick="return confirm('⚠️ Are you sure you want to delete this USDT Address?');">
                                                    <i class="fa-solid fa-trash-can"></i>
                                                </a>
                                            </div>

                                        </div>
                                        <?php } ?>
                                    </div>
                                    
                                    <button type="submit" class="btn-save">
                                        ✅ Save Changes
                                    </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>