<?php
ob_start();
/************************************************************************/
/*     PROGRAMMER     :  SMA                                            */
/*     SCRIPT NAME    :  currency_process.php                           */
/*     MODERNIZED     :  GPT-4o | 2025                                  */
/************************************************************************/

require_once '../includes/constants.php';
require_once '../includes/functions.php';
require_once '../includes/session.php';
require_once '../includes/function1.php';

// Database connection
$partners = new partners();
$partners->connection($host, $user, $pass, $db);

$commonobj = new common();
$currencyobj = new currency();

// Get form variables safely
$mode = filter_input(INPUT_POST, 'mode', FILTER_SANITIZE_STRING) ?? filter_input(INPUT_GET, 'mode', FILTER_SANITIZE_STRING);

$txt_caption = trim($_POST['txt_caption'] ?? '');
$txt_code = trim($_POST['txt_code'] ?? '');
$txt_symbol = trim($_POST['txt_symbol'] ?? '');
$txt_relation = trim($_POST['txt_relation'] ?? '');

$validatestring = "$txt_caption~*$txt_code~*$txt_symbol~*$txt_relation";
$returnstring = "txt_caption=$txt_caption&txt_code=$txt_code&txt_symbol=$txt_symbol&txt_relation=$txt_relation";

if ($mode === 'add') {
    if ($commonobj->nullvalidation($validatestring)) {
        $note = "Please enter all the Required Fields";
        header("Location: index.php?Act=currency&mode=add&note=$note&$returnstring");
        exit;
    }

    if (!is_numeric($txt_relation)) {
        $note = "Please enter a numeric value for Currency Relation";
        header("Location: index.php?Act=currency&mode=add&note=$note&$returnstring");
        exit;
    }

    if (!$currencyobj->ifCurrencyDetailsExists('caption', $txt_caption)) {
        $note = "Currency Caption already exists";
        header("Location: index.php?Act=currency&mode=add&note=$note&$returnstring");
        exit;
    }

    if (!$currencyobj->ifCurrencyDetailsExists('code', $txt_code)) {
        $note = "Currency Code already exists";
        header("Location: index.php?Act=currency&mode=add&note=$note&$returnstring");
        exit;
    }

    if (!$currencyobj->ifCurrencyDetailsExists('symbol', $txt_symbol)) {
        $note = "Currency Symbol already exists";
        header("Location: index.php?Act=currency&mode=add&note=$note&$returnstring");
        exit;
    }

    if (!$currencyobj->insertCurrency($txt_caption, $txt_code, $txt_symbol, $txt_relation)) {
        $note = "Unknown Error!. Insertion failed";
        header("Location: index.php?Act=currency&mode=add&note=$note&$returnstring");
        exit;
    } else {
        $note = "Currency details Inserted Successfully";
        header("Location: index.php?Act=currency&mode=add&note=$note");
        exit;
    }
}

elseif ($mode === 'change') {
    $basecurrency = $_POST['basecurrency'] ?? '';

    $stmt = $partners->db->prepare("SELECT currency_symbol, currency_caption FROM partners_currency WHERE currency_code = ?");
    $stmt->bind_param("s", $basecurrency);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $stmt->bind_result($new_symbol, $new_caption);
        $stmt->fetch();

        $currencyobj->ConvertToNewBaseCurrency($basecurrency);

        // Update constants.php
        $filename = "../includes/constants.php";
        $contents = file_get_contents($filename);

        $contents = preg_replace('/\$default_currency_code\s*=.*?;/', "\$default_currency_code = '$basecurrency';", $contents);
        $contents = preg_replace('/\$default_currency_caption\s*=.*?;/', "\$default_currency_caption = '$new_caption';", $contents);

        file_put_contents($filename, $contents);

        include_once '../update_currency_rates.php';

        $msg = "Base Currency Changed successfully";
    } else {
        $msg = "Unknown Error!. Change Base Currency Failed";
    }

    $stmt->close();
    header("Location: index.php?Act=currency&mode=add&note1=" . urlencode($msg));
    exit;
}

elseif ($mode === 'rate') {
    $chk_currency = isset($_POST['chk_currency']) ? 1 : 0;

    $filename = "../includes/constants.php";
    $contents = file_get_contents($filename);
    $contents = preg_replace('/\$const_getCurrencyRatesFromXe\s*=.*?;/', "\$const_getCurrencyRatesFromXe = $chk_currency;", $contents);
    file_put_contents($filename, $contents);

    header("Location: index.php?Act=currency");
    exit;
}
?>
