<?php
/* =========================================================
   DUPLICATE IP MANAGER - DASHBOARD VERSION (FIXED NAME)
   ========================================================= */

// --- 1. CONFIGURATION ---
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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

include("conn.php");

// ==========================================
// COLUMN CONFIGURATION (YAHAN CHANGES KIYE HAIN)
// ==========================================
$col_id = "id";
$col_name = "codechorkamukala"; // <-- User Name Column Updated
$col_mobile = "mobile";         // Check karein ki mobile column yahi hai na?
$col_ip = "ishonup";
$col_status = "status";         // 1 = Active, 0 = Banned

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

// --- 2. HANDLE ACTIONS ---
if (isset($_POST['action_type'])) {
    $action = $_POST['action_type'];
    
    // Single User Action
    if ($action == 'single_update') {
        $uid = intval($_POST['user_id']);
        $new_status = intval($_POST['new_status']);
        $stmt = $conn->prepare("UPDATE shonu_subjects SET $col_status = ? WHERE $col_id = ?");
        $stmt->bind_param("ii", $new_status, $uid);
        if ($stmt->execute()) {
            $msg = ($new_status == 0) ? "🚫 User Banned!" : "✅ User Unbanned!";
            $msg_type = ($new_status == 0) ? "error" : "success";
        }
        $stmt->close();
    }
    
    // Bulk: Ban All
    if ($action == 'ban_all_duplicates') {
        $sql = "UPDATE shonu_subjects SET $col_status = 0 WHERE $col_ip IN (SELECT $col_ip FROM (SELECT $col_ip FROM shonu_subjects GROUP BY $col_ip HAVING COUNT($col_ip) > 1) as subquery)";
        if(mysqli_query($conn, $sql)) { $msg = "🚫 All Duplicates Banned!"; $msg_type = "error"; }
    }

    // Bulk: Unban All
    if ($action == 'unban_all_duplicates') {
        $sql = "UPDATE shonu_subjects SET $col_status = 1 WHERE $col_ip IN (SELECT $col_ip FROM (SELECT $col_ip FROM shonu_subjects GROUP BY $col_ip HAVING COUNT($col_ip) > 1) as subquery)";
        if(mysqli_query($conn, $sql)) { $msg = "✅ All Unbanned!"; $msg_type = "success"; }
    }
}

// --- 3. FETCH DUPLICATE DATA ---
$sql_fetch = "SELECT * FROM shonu_subjects WHERE $col_ip IN (SELECT $col_ip FROM shonu_subjects GROUP BY $col_ip HAVING COUNT($col_ip) > 1) ORDER BY $col_ip ASC, $col_id ASC";
$result = mysqli_query($conn, $sql_fetch);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=1200, initial-scale=0.5">
    <title>Auto Ban System</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>
        body { background-color: #f4f7f6; min-width: 1200px; }
        .admin-card { background: #fff; border-radius: 15px; padding: 30px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); border-top: 5px solid #6c5ce7; margin-bottom: 30px; }
        .card-header-flex { display: flex; justify-content: space-between; align-items: center; border-bottom: 2px dashed #eee; padding-bottom: 20px; margin-bottom: 20px; }
        .card-title { font-size: 1.5rem; font-weight: 900; color: #6c5ce7; text-transform: uppercase; }
        
        .custom-table { width: 100%; border-collapse: collapse; }
        .custom-table th { background: #2d3436; color: #fff; padding: 15px; text-transform: uppercase; font-size: 0.9rem; }
        .custom-table td { padding: 15px; border-bottom: 1px solid #eee; font-weight: 600; color: #444; }
        
        .badge { padding: 5px 12px; border-radius: 20px; font-size: 0.75rem; font-weight: 800; }
        .badge-active { background: #e0fbe2; color: #00b894; }
        .badge-banned { background: #fee2e2; color: #d63031; }
        .ip-tag { background: #dfe6e9; color: #2d3436; padding: 4px 10px; border-radius: 5px; font-family: monospace; font-weight: bold; }

        .btn-action { padding: 8px 15px; border: none; border-radius: 8px; font-weight: 700; cursor: pointer; color: #fff; font-size: 0.8rem; transition: 0.2s; }
        .btn-ban { background: #ff7675; }
        .btn-unban { background: #00b894; }
        .btn-bulk-ban { background: #d63031; padding: 12px 25px; }
        .btn-bulk-unban { background: #00b894; padding: 12px 25px; }

        .alert-box { padding: 15px; border-radius: 10px; margin-bottom: 20px; text-align: center; font-weight: 700; }
        .alert-success { background: #d1fae5; color: #065f46; border: 1px solid #34d399; }
        .alert-error { background: #fee2e2; color: #991b1b; border: 1px 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" style="background:#fff;">
                <a class="navbar-brand brand-logo" href="dashboard.php"><h4 style="color:#6c5ce7; 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="admin-card">
                        <div class="card-header-flex">
                            <div>
                                <div class="card-title"><i class="fa-solid fa-users-slash"></i> Duplicate IP Monitor</div>
                                <small style="color:#888;">Users with same IP address</small>
                            </div>
                            <div style="display:flex; gap:10px;">
                                <form method="post" onsubmit="return confirm('Unban ALL duplicates?');">
                                    <input type="hidden" name="action_type" value="unban_all_duplicates">
                                    <button class="btn-action btn-bulk-unban"><i class="fa-solid fa-broom"></i> Unban All</button>
                                </form>
                                <form method="post" onsubmit="return confirm('BAN ALL duplicates?');">
                                    <input type="hidden" name="action_type" value="ban_all_duplicates">
                                    <button class="btn-action btn-bulk-ban"><i class="fa-solid fa-ban"></i> Ban All Duplicates</button>
                                </form>
                            </div>
                        </div>

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

                        <?php if (mysqli_num_rows($result) > 0) { ?>
                        <div class="table-responsive">
                            <table class="custom-table">
                                <thead>
                                    <tr>
                                        <th>IP Address</th>
                                        <th>UID</th>
                                        <th>Name</th>
                                        <th>Mobile</th>
                                        <th>Status</th>
                                        <th style="text-align:right;">Action</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php 
                                    $last_ip = "";
                                    while ($row = mysqli_fetch_assoc($result)) { 
                                        $ip_style = ($row[$col_ip] !== $last_ip) ? "border-top: 3px solid #ddd;" : "";
                                        $last_ip = $row[$col_ip];
                                    ?>
                                    <tr style="<?php echo $ip_style; ?>">
                                        <td><span class="ip-tag"><i class="fa-solid fa-network-wired"></i> <?php echo $row[$col_ip]; ?></span></td>
                                        <td><b><?php echo $row[$col_id]; ?></b></td>
                                        <td><?php echo $row[$col_name]; ?></td> <td><?php echo $row[$col_mobile]; ?></td>
                                        <td>
                                            <?php if($row[$col_status] == 1) { ?>
                                                <span class="badge badge-active">Active</span>
                                            <?php } else { ?>
                                                <span class="badge badge-banned">Banned</span>
                                            <?php } ?>
                                        </td>
                                        <td style="text-align:right;">
                                            <form method="post" style="display:inline;">
                                                <input type="hidden" name="action_type" value="single_update">
                                                <input type="hidden" name="user_id" value="<?php echo $row[$col_id]; ?>">
                                                
                                                <?php if($row[$col_status] == 1) { ?>
                                                    <input type="hidden" name="new_status" value="0">
                                                    <button class="btn-action btn-ban"><i class="fa-solid fa-gavel"></i> BAN</button>
                                                <?php } else { ?>
                                                    <input type="hidden" name="new_status" value="1">
                                                    <button class="btn-action btn-unban"><i class="fa-solid fa-check"></i> UNBAN</button>
                                                <?php } ?>
                                            </form>
                                        </td>
                                    </tr>
                                    <?php } ?>
                                </tbody>
                            </table>
                        </div>
                        <?php } else { ?>
                            <div style="text-align:center; padding:50px; color:#aaa;">
                                <i class="fa-solid fa-shield-halved" style="font-size:4rem; color:#00b894; margin-bottom:20px;"></i>
                                <h3>Safe & Clean!</h3>
                                <p>No duplicate IP addresses found.</p>
                            </div>
                        <?php } ?>

                    </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="js/dashboard.js"></script>
    
    <script>
    
    </script>
</body>
</html>