<?php
include "header.php"; // Include your header file

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$messages = [];
$user_id = $userdata['id'];
$role = $userdata['role'];
$mobile = $userdata['mobile'];
$email = $userdata['email'];
// Pagination settings
$limit = 10; // IPs per page
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Website types array
$website_types = [
    'E-commerce' => 'E-commerce Website',
    'MultiRecharge' => 'Multi Recharge Website',
    'Gaming' => 'Gamming/Rummy Casino Website',
    'Business' => 'Business Website',
    'Blog' => 'Blog/News Website',
    'Portfolio' => 'Portfolio Website',
    'Educational' => 'Educational Website',
    'Healthcare' => 'Healthcare Website',
    'Real Estate' => 'Real Estate Website',
    'Restaurant' => 'Restaurant/Food Website',
    'Travel' => 'Travel Website',
    'Technology' => 'Technology IT company Website',
    'Entertainment' => 'Entertainment Website',
    'Social Media' => 'Social Media Platform',
    'Forum' => 'Community Forum',
    'Non-Profit' => 'Non-Profit Website',
    'Government' => 'Government Website',
    'localhiost'=>'Localhost Testing IP',
    'Other' => 'my website Type not in list'
];

// Handle IP Submission (without OTP)
if ($_SERVER["REQUEST_METHOD"] === "POST") {

    // **Common expiry check before any action**
    $expiry = $userdata['expiry']; // Get expiry date from database
    $expiry_timestamp = strtotime($expiry); // Convert to timestamp
    $today = strtotime(date('Y-m-d')); // Today's date

    if ($expiry_timestamp < $today) {
        echo "<script>
            Swal.fire({
                icon: 'error',
                title: 'Plan Expired!',
                text: 'Your plan expired on: " . date('d M, Y', $expiry_timestamp) . ". Please renew your plan to Access.',
                confirmButtonText: 'OK'
            }).then(() => { window.location.href='subscription'; });
        </script>";
        exit;
    }

    // Add IP directly (no OTP)
    if (isset($_POST['ip'])) {
        $ip = trim($_POST['ip']);
        $domain = isset($_POST['domain']) ? trim($_POST['domain']) : '';
        $type = isset($_POST['type']) ? trim($_POST['type']) : '';

        // Validate IP
        if (!filter_var($ip, FILTER_VALIDATE_IP)) {
            $messages[] = ["type" => "danger", "text" => "❌ Invalid IP address."];
        } else {
            // Check duplicate for this user
            $check = $conn->prepare("SELECT id FROM user_ips WHERE ip = ? AND user_id = ?");
            $check->bind_param("si", $ip, $user_id);
            $check->execute();
            $res = $check->get_result();

            if ($res->num_rows > 0) {
                $messages[] = ["type" => "warning", "text" => "⚠️ This IP is already whitelisted."];
            } else {
                // Insert directly with status = 0 (pending)
                $stmt = $conn->prepare("INSERT INTO user_ips (ip, domain, type, user_id, status, created_at) VALUES (?, ?, ?, ?, 0, NOW())");
                $stmt->bind_param("sssi", $ip, $domain, $type, $user_id);

                if ($stmt->execute()) {
                    $messages[] = ["type" => "success", "text" => "✅ IP added successfully. Waiting for admin approval."];

                    // Notify admin (keep admin number or fetch real admin numbers as needed)
                    $admin_msg = "New IP ($ip) of type ($type) added by user ID: $user_id. Please approve.";
                    // sendWA is used in your codebase — keep calling it
                    if (function_exists('sendWA')) {
                        sendWA('9876543210', $admin_msg);
                    }

                    // Optionally notify user that their IP is submitted
                    if (function_exists('sendWA')) {
                        sendWA($mobile, "Your IP $ip has been submitted and is awaiting admin approval.");
                    }
                } else {
                    $messages[] = ["type" => "danger", "text" => "❌ Failed to add IP. Please try again later."];
                }
            }
        }
    }

    // Handle Delete IP (for users and admins)
    if (isset($_POST['delete_ip'])) {
        $ip_id = (int)$_POST['ip_id'];
        $user_stmt = $conn->prepare("SELECT mobile FROM users WHERE id = (SELECT user_id FROM user_ips WHERE id = ?)");
        $user_stmt->bind_param("i", $ip_id);
        $user_stmt->execute();
        $user_mobile = $user_stmt->get_result()->fetch_assoc()['mobile'] ?? null;

        $stmt = $conn->prepare("DELETE FROM user_ips WHERE id = ?");
        $stmt->bind_param("i", $ip_id);
        if ($stmt->execute()) {
            $messages[] = ["type" => "success", "text" => "✅ IP deleted successfully."];
            if ($user_mobile && function_exists('sendWA')) {
                sendWA($user_mobile, "Your IP has been deleted.");
            }
        } else {
            $messages[] = ["type" => "danger", "text" => "❌ Failed to delete IP."];
        }
    }

    // Handle Admin Actions
    if ($role == 'Admin') {
        $user_stmt = $conn->prepare("SELECT mobile FROM users WHERE id = (SELECT user_id FROM user_ips WHERE id = ?)");

        if (isset($_POST['activate_ip'])) {
            $ip_id = (int)$_POST['ip_id'];
            $stmt = $conn->prepare("UPDATE user_ips SET status = 1 WHERE id = ?");
            $stmt->bind_param("i", $ip_id);
            if ($stmt->execute()) {
                $messages[] = ["type" => "success", "text" => "✅ IP activated successfully."];
                $user_stmt->bind_param("i", $ip_id);
                $user_stmt->execute();
                $user_mobile = $user_stmt->get_result()->fetch_assoc()['mobile'] ?? null;
                if ($user_mobile && function_exists('sendWA')) {
                    sendWA($user_mobile, "Your IP has been activated by admin.");
                }
            } else {
                $messages[] = ["type" => "danger", "text" => "❌ Failed to activate IP."];
            }
        }

        if (isset($_POST['deactivate_ip'])) {
            $ip_id = (int)$_POST['ip_id'];
            $stmt = $conn->prepare("UPDATE user_ips SET status = 0 WHERE id = ?");
            $stmt->bind_param("i", $ip_id);
            if ($stmt->execute()) {
                $messages[] = ["type" => "success", "text" => "✅ IP deactivated successfully."];
                $user_stmt->bind_param("i", $ip_id);
                $user_stmt->execute();
                $user_mobile = $user_stmt->get_result()->fetch_assoc()['mobile'] ?? null;
                if ($user_mobile && function_exists('sendWA')) {
                    sendWA($user_mobile, "Your IP has been deactivated by admin.");
                }
            } else {
                $messages[] = ["type" => "danger", "text" => "❌ Failed to deactivate IP."];
            }
        }
    }
}

// Handle Search
$search_ip = isset($_POST['search_ip']) ? trim($_POST['search_ip']) : '';
$search_user_id = ($role == 'Admin' && isset($_POST['search_user_id'])) ? trim($_POST['search_user_id']) : '';

if ($role == 'Admin') {
    // Count total IPs for pagination
    if ($search_ip || $search_user_id) {
        $count_stmt = $conn->prepare("SELECT COUNT(*) as total FROM user_ips WHERE ip LIKE ? AND user_id LIKE ?");
        $search_ip_param = "%$search_ip%";
        $search_user_id_param = "%$search_user_id%";
        $count_stmt->bind_param("ss", $search_ip_param, $search_user_id_param);
        $count_stmt->execute();
        $total_ips = $count_stmt->get_result()->fetch_assoc()['total'];
    } else {
        $total_ips = $conn->query("SELECT COUNT(*) as total FROM user_ips")->fetch_assoc()['total'];
    }

    // Fetch IPs
    if ($search_ip || $search_user_id) {
        $stmt = $conn->prepare("SELECT * FROM user_ips WHERE ip LIKE ? AND user_id LIKE ? ORDER BY created_at DESC LIMIT ? OFFSET ?");
        $search_ip_param = "%$search_ip%";
        $search_user_id_param = "%$search_user_id%";
        $stmt->bind_param("ssii", $search_ip_param, $search_user_id_param, $limit, $offset);
        $stmt->execute();
        $ip_list = $stmt->get_result();
    } else {
        $stmt = $conn->prepare("SELECT * FROM user_ips ORDER BY created_at DESC LIMIT ? OFFSET ?");
        $stmt->bind_param("ii", $limit, $offset);
        $stmt->execute();
        $ip_list = $stmt->get_result();
    }
} else {
    // Count total IPs for pagination
    if ($search_ip) {
        $count_stmt = $conn->prepare("SELECT COUNT(*) as total FROM user_ips WHERE user_id = ? AND ip LIKE ?");
        $search_ip_param = "%$search_ip%";
        $count_stmt->bind_param("is", $user_id, $search_ip_param);
        $count_stmt->execute();
        $total_ips = $count_stmt->get_result()->fetch_assoc()['total'];
    } else {
        $total_ips = $conn->query("SELECT COUNT(*) as total FROM user_ips WHERE user_id = $user_id")->fetch_assoc()['total'];
    }

    // Fetch IPs
    if ($search_ip) {
        $stmt = $conn->prepare("SELECT * FROM user_ips WHERE user_id = ? AND ip LIKE ? ORDER BY created_at DESC LIMIT ? OFFSET ?");
        $search_ip_param = "%$search_ip%";
        $stmt->bind_param("isii", $user_id, $search_ip_param, $limit, $offset);
        $stmt->execute();
        $ip_list = $stmt->get_result();
    } else {
        $stmt = $conn->prepare("SELECT * FROM user_ips WHERE user_id = ? ORDER BY created_at DESC LIMIT ? OFFSET ?");
        $stmt->bind_param("iii", $user_id, $limit, $offset);
        $stmt->execute();
        $ip_list = $stmt->get_result();
    }
}

$total_pages = ($total_ips > 0) ? ceil($total_ips / $limit) : 1;
?>

<div class="container mt-5">
    <h3 class="mb-4">⚙️ IP Settings</h3>

    <!-- Security Explanation -->
    <div class="alert alert-info" role="alert">
        <strong>IP Security:</strong> Add and manage your whitelisted IP addresses here. Only IPs with "Active" status can be used to place new orders or process transactions.
    </div>

    <!-- Flash Messages -->
    <?php foreach ($messages as $msg): ?>
        <div class="alert alert-<?= htmlspecialchars($msg['type']); ?> alert-dismissible fade show" role="alert">
            <?= htmlspecialchars($msg['text']); ?>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endforeach; ?>

    <!-- Add IP Form (no OTP) -->
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <form method="POST">
                <div class="row">
                    <div class="col-md-6 mb-3">
                        <label for="ip" class="form-label">Enter IP Address</label>
                        <input type="text" class="form-control" id="ip" name="ip" placeholder="e.g. 192.168.0.1" required>
                    </div>
                    <div class="col-md-6 mb-3">
                        <label for="domain" class="form-label">Enter Website Domain</label>
                        <input type="text" class="form-control" id="domain" name="domain" placeholder="e.g. https://yourdomain.com" required>
                    </div>
                </div>
                <div class="mb-3">
                    <label for="type" class="form-label">Select Website Type</label>
                    <select class="form-select" id="type" name="type" required>
                        <option value="">Choose website type...</option>
                        <?php foreach ($website_types as $key => $value): ?>
                            <option value="<?= htmlspecialchars($key); ?>"><?= htmlspecialchars($value); ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <button type="submit" class="btn btn-primary">Add IP</button>
            </form>
        </div>
    </div>

    <!-- Whitelisted IPs Table with Search -->
    <div class="card shadow-sm">
        <div class="card-body">
            <h5 class="card-title mb-3">✅ Your Whitelisted IPs</h5>

            <!-- Search Form -->
            <form method="POST" class="mb-3">
                <div class="input-group">
                    <input type="text" class="form-control" name="search_ip" placeholder="Search by IP address" value="<?= htmlspecialchars($search_ip); ?>">
                    <?php if ($role == 'Admin'): ?>
                        <input type="text" class="form-control" name="search_user_id" placeholder="Search by User ID" value="<?= htmlspecialchars($search_user_id); ?>">
                    <?php endif; ?>
                    <button type="submit" class="btn btn-primary">Search</button>
                </div>
            </form>

            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="table-light">
                        <tr>
                            <th>#</th>
                            <th>User ID</th>
                            <th>IP Address</th>
                            <th>Domain</th>
                            <th>Website Type</th>
                            <th>Status</th>
                            <th>Added On</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php $i = $offset + 1; while($row = $ip_list->fetch_assoc()): ?>
                            <tr>
                                <td><?= $i++; ?></td>
                                <td><?= htmlspecialchars($row['user_id']); ?></td>
                                <td><?= htmlspecialchars($row['ip']); ?></td>
                                <td>
                                    <a href="<?= htmlspecialchars($row['domain']); ?>" target="_blank" class="text-decoration-none">
                                        <?= htmlspecialchars($row['domain']); ?>
                                    </a>
                                </td>
                                <td>
                                    <span class="badge bg-secondary">
                                        <?= htmlspecialchars($row['type'] ?? 'Not Set'); ?>
                                    </span>
                                </td>
                                <td>
                                    <span class="badge bg-<?= $row['status'] ? 'success' : 'info'; ?>">
                                        <?= $row['status'] ? 'Active' : 'Pending'; ?>
                                    </span>
                                </td>
                                <td><?= htmlspecialchars($row['created_at']); ?></td>
                                <td>
                                    <!-- User and Admin can delete -->
                                    <form method="POST" style="display:inline;">
                                        <input type="hidden" name="ip_id" value="<?= (int)$row['id']; ?>">
                                        <button type="submit" name="delete_ip" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to delete this IP?');">Delete</button>
                                    </form>
                                    <!-- Admin-specific actions -->
                                    <?php if ($role == 'Admin'): ?>
                                        <?php if ($row['status']): ?>
                                            <form method="POST" style="display:inline;">
                                                <input type="hidden" name="ip_id" value="<?= (int)$row['id']; ?>">
                                                <button type="submit" name="deactivate_ip" class="btn btn-sm btn-warning">Deactivate</button>
                                            </form>
                                        <?php else: ?>
                                            <form method="POST" style="display:inline;">
                                                <input type="hidden" name="ip_id" value="<?= (int)$row['id']; ?>">
                                                <button type="submit" name="activate_ip" class="btn btn-sm btn-success">Activate</button>
                                            </form>
                                        <?php endif; ?>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endwhile; ?>
                        <?php if ($ip_list->num_rows == 0): ?>
                            <tr>
                                <td colspan="8" class="text-center">No IPs found.</td>
                            </tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>

            <!-- Pagination -->
            <?php if ($total_pages > 1): ?>
                <nav aria-label="Page navigation">
                    <ul class="pagination">
                        <li class="page-item <?= $page <= 1 ? 'disabled' : ''; ?>">
                            <a class="page-link" href="?page=<?= $page - 1; ?>" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <?php for ($p = 1; $p <= $total_pages; $p++): ?>
                            <li class="page-item <?= $p == $page ? 'active' : ''; ?>">
                                <a class="page-link" href="?page=<?= $p; ?>"><?= $p; ?></a>
                            </li>
                        <?php endfor; ?>
                        <li class="page-item <?= $page >= $total_pages ? 'disabled' : ''; ?>">
                            <a class="page-link" href="?page=<?= $page + 1; ?>" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            <?php endif; ?>
            <p class="text-muted">Showing <?= $ip_list->num_rows; ?> of <?= $total_ips; ?> IPs.</p>
        </div>
    </div>
</div>
