<?php
include_once("conn.php");
session_start();

function get($param)
{
    global $koneksi;
    $d = isset($_GET[$param]) ? $_GET[$param] : null;
    $d = mysqli_real_escape_string($koneksi, $d);
    $d = filter_var($d, FILTER_SANITIZE_STRING);
    return $d;
}

function post($param)
{
    global $koneksi;
    $d = isset($_POST[$param]) ? $_POST[$param] : null;
    $d = mysqli_real_escape_string($koneksi, $d);
    $d = filter_var($d, FILTER_SANITIZE_STRING);
    return $d;
}
function cekSession()
{
    $login = isset($_SESSION['login']) ? $_SESSION['login'] : null;
    if ($_SESSION['login']) {
        return 1;
    } else {
        return 0;
    }
}
function redirect($target)
{
    echo '
    <script>
    window.location = "' . $target . '";
    </script>
    ';
    exit;
}
function generate_license($length = 16) {
    $characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $randomString = '';

    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
        if ($i % 4 == 3 && $i < $length - 1) {
            $randomString .= '-';
        }
    }

    return $randomString;
}

function check_number($number) {
    global $conn; // Use your database connection variable

    // Sanitize the input (if needed)
    $number = mysqli_real_escape_string($conn, $number);

    // Construct the SQL query
    $query = "SELECT COUNT(*) FROM users WHERE whatsapp_number = '$number'";

    // Execute the query
    $result = mysqli_query($conn, $query);

    if ($result === false) {
        // Handle query execution error
        return false;
    }

    // Fetch the count
    $count = mysqli_fetch_array($result)[0];

    // Check the count and return true if the number exists, otherwise return false
    return ($count > 0);
}



// Fetch all data from settings table
$query = "SELECT brand_name, logo_url, number, amount, api_token, api_url FROM settings WHERE user_id = '1' LIMIT 1"; // Assuming user_id is 1
$result = mysqli_query($conn, $query);

// Initialize variables to store the fetched data
$brandName = '';
$logoUrl = '';
$number = '';
$amount = '';
$apiToken = '';
$apiUrl = '';

if ($result && mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $brandName = $row['brand_name'];
    $logoUrl = $row['logo_url'];
    $number = $row['number'];
    $amount = $row['amount'];
    $apiToken = $row['api_token'];
    $apiUrl = $row['api_url'];
}