<?php
include_once '../config/connect.php';
include_once __DIR__ . '/../../../../vendor/autoload.php';

use HTMLPurifier;
use HTMLPurifier_Config;

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_bank'])) {
    $config = HTMLPurifier_Config::createDefault();
    $purifier = new HTMLPurifier($config);
    
    $bank_namex = $purifier->purify(trim($_POST['bank_name'] ?? ''));
    $account_numberx = $purifier->purify(trim($_POST['account_number'] ?? ''));
    $bank_codex = $purifier->purify(trim($_POST['bank_code'] ?? ''));

    if (empty($bank_namex) || empty($account_numberx) || empty($bank_codex)) {
        header('Location: ../index?page=Settings&msg=36');
        exit();
    }

    if (!ctype_digit($account_numberx)) {
        header('Location: ../index?page=Settings&msg=37');
        exit();
    }

    $bank_name = mysqli_real_escape_string($call, $bank_namex);
    $account_number = mysqli_real_escape_string($call, $account_numberx);
    $bank_code = mysqli_real_escape_string($call, $bank_codex);

    $query = "INSERT INTO tb_billing (bank, nama_rekening, nomor_rekening) VALUES ('$bank_code', '$bank_name', '$account_number')";

    if (mysqli_query($call, $query)) {
        header('Location: ../index?page=Settings&msg=40');
        exit();
    } else {
        header('Location: ../index?page=Settings&msg=13');
        exit();
    }
} else {
        header('Location: ../index?page=Settings&msg=39');
        exit();
}
?>
