<?php
require_once '../includes/session.php';
require_once '../includes/db_connection.php'; // This should set up $conn (mysqli)

$mode = $_GET['mode'] ?? '';
$lines = 20;
$page = $_GET['page'] ?? 1;
if (!is_numeric($page) || $page < 1) $page = 1;

// DELETE mode
if ($mode === 'delete') {
    $from    = $_GET['from'] ?? '';
    $to      = $_GET['to'] ?? '';
    $code2   = $_GET['code2'] ?? '';
    $code3   = $_GET['code3'] ?? '';
    $country = $_GET['country'] ?? '';

    $stmt = $conn->prepare("DELETE FROM partners_countryFlag WHERE ip_from = ? AND ip_to = ? AND country_code2 = ? AND country_code3 = ? AND country_name = ?");
    $stmt->bind_param("sssss", $from, $to, $code2, $code3, $country);
    $stmt->execute();
    $stmt->close();
}

// UPDATE mode
if ($mode === 'update') {
    $old_from    = $_POST['old_from'] ?? '';
    $old_to      = $_POST['old_to'] ?? '';
    $old_code2   = $_POST['old_code2'] ?? '';
    $old_code3   = $_POST['old_code3'] ?? '';
    $old_country = $_POST['old_country'] ?? '';

    $from    = $_POST['from'] ?? '';
    $to      = $_POST['to'] ?? '';
    $code2   = $_POST['code2'] ?? '';
    $code3   = $_POST['code3'] ?? '';
    $country = $_POST['country'] ?? '';

    $stmt = $conn->prepare("UPDATE partners_countryFlag 
        SET ip_from = ?, ip_to = ?, country_code2 = ?, country_code3 = ?, country_name = ? 
        WHERE ip_from = ? AND ip_to = ? AND country_code2 = ? AND country_code3 = ? AND country_name = ?");
    $stmt->bind_param("ssssssssss", $from, $to, $code2, $code3, $country, $old_from, $old_to, $old_code2, $old_code3, $old_country);
    $stmt->execute();
    $stmt->close();
}

// EDIT mode – prepare values
if ($mode === 'edit') {
    $from    = $_GET['from'] ?? '';
    $to      = $_GET['to'] ?? '';
    $code2   = $_GET['code2'] ?? '';
    $code3   = $_GET['code3'] ?? '';
    $country = $_GET['country'] ?? '';
?>

<form name="frm_ip" action="index.php?Act=ip_country&mode=update" method="post">
    <input type="hidden" name="old_from" value="<?= htmlspecialchars($from) ?>" />
    <input type="hidden" name="old_to" value="<?= htmlspecialchars($to) ?>" />
    <input type="hidden" name="old_code2" value="<?= htmlspecialchars($code2) ?>" />
    <input type="hidden" name="old_code3" value="<?= htmlspecialchars($code3) ?>" />
    <input type="hidden" name="old_country" value="<?= htmlspecialchars($country) ?>" />
    <table cellpadding="0" cellspacing="0" align="center" width="50%" class="tablebdr">
        <tr><td colspan="3" class="tdhead" height="25" align="left">Update IP Range</td></tr>
        <tr><td align="right">From</td><td></td><td><input type="text" name="from" value="<?= htmlspecialchars($from) ?>" /></td></tr>
        <tr><td align="right">To</td><td></td><td><input type="text" name="to" value="<?= htmlspecialchars($to) ?>" /></td></tr>
        <tr><td align="right">Country Code(2 digit)</td><td></td><td><input type="text" name="code2" value="<?= htmlspecialchars($code2) ?>" /></td></tr>
        <tr><td align="right">Country Code(3 digit)</td><td></td><td><input type="text" name="code3" value="<?= htmlspecialchars($code3) ?>" /></td></tr>
        <tr><td align="right">Country Name</td><td></td><td><input type="text" name="country" value="<?= htmlspecialchars($country) ?>" /></td></tr>
        <tr><td></td><td></td><td><input type="submit" name="Submit" value="Update" /></td></tr>
    </table>
</form>
<?php } ?>

<div align="center">
    <table border="0" cellpadding="2" cellspacing="1" width="90%" class="tablebdr">
        <tr><td colspan="7" class="tdhead">IP - Country Database</td></tr>
        <tr><td colspan="7" align="right"><a href="index.php?Act=ip_country_update">Import Data From SQL</a></td></tr>

<?php
$sqlCount = "SELECT COUNT(*) AS total FROM partners_countryFlag";
$resultCount = $conn->query($sqlCount);
$totalRows = $resultCount->fetch_assoc()['total'];

$sql = "SELECT * FROM partners_countryFlag LIMIT ?, ?";
$stmt = $conn->prepare($sql);
$offset = ($page - 1) * $lines;
$stmt->bind_param("ii", $offset, $lines);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
?>
        <tr><td colspan="7" class="textred" align="center">Sorry....The IP-Country Database is empty right now.</td></tr>
<?php
} else {
?>
        <tr class="tdhead">
            <td>IP(From)</td><td>IP(To)</td><td>CountryCode(2 digit)</td><td>CountryCode(3 digit)</td>
            <td>Country</td><td></td><td></td>
        </tr>
<?php
    while ($row = $result->fetch_object()) {
?>
        <tr>
            <td><?= htmlspecialchars($row->ip_from) ?></td>
            <td><?= htmlspecialchars($row->ip_to) ?></td>
            <td><?= htmlspecialchars($row->country_code2) ?></td>
            <td><?= htmlspecialchars($row->country_code3) ?></td>
            <td><?= htmlspecialchars($row->country_name) ?></td>
            <td><a href="index.php?Act=ip_country&mode=edit&from=<?= urlencode($row->ip_from) ?>&to=<?= urlencode($row->ip_to) ?>&code2=<?= urlencode($row->country_code2) ?>&code3=<?= urlencode($row->country_code3) ?>&country=<?= urlencode($row->country_name) ?>">Edit</a></td>
            <td><a href="#" onclick="confirm_deletion('<?= addslashes($row->ip_from) ?>','<?= addslashes($row->ip_to) ?>','<?= addslashes($row->country_code2) ?>','<?= addslashes($row->country_code3) ?>','<?= addslashes($row->country_name) ?>')">Delete</a></td>
        </tr>
<?php
    }
}
$stmt->close();
?>
        <tr><td colspan="7">
<?php
// Pagination links
$totalPages = ceil($totalRows / $lines);
$url = "index.php?Act=ip_country";
include '../includes/show_pagenos.php';
?>
        </td></tr>
    </table>
</div>

<br>

<script>
function confirm_deletion(from, to, code2, code3, country) {
    if (confirm("Are you sure you want to delete this IP range?")) {
        const url = `index.php?Act=ip_country&mode=delete&from=${encodeURIComponent(from)}&to=${encodeURIComponent(to)}&code2=${encodeURIComponent(code2)}&code3=${encodeURIComponent(code3)}&country=${encodeURIComponent(country)}`;
        window.location = url;
    }
}
</script>
