<?php
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_fbr_config'])) {
    $is_enabled = isset($_POST['is_enabled']) ? 1 : 0;
    $api_url = $_POST['api_url'] ?? '';
    $pos_id  = $_POST['pos_id'] ?? null;
    $usin    = $_POST['usin'] ?? '';

    // Check if a row exists
    $check = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id FROM fbr_config LIMIT 1"));

    if ($check) {
        // Update existing row
        mysqli_query($conn, "UPDATE fbr_config SET 
            is_enabled = $is_enabled,
            api_url = '" . mysqli_real_escape_string($conn, $api_url) . "',
            pos_id = " . ($pos_id ? intval($pos_id) : "NULL") . ",
            usin = '" . mysqli_real_escape_string($conn, $usin) . "'
            LIMIT 1
        ");
    } else {
        // Insert new row
        mysqli_query($conn, "INSERT INTO fbr_config (is_enabled, api_url, pos_id, usin)
            VALUES (
                $is_enabled,
                '" . mysqli_real_escape_string($conn, $api_url) . "',
                " . ($pos_id ? intval($pos_id) : "NULL") . ",
                '" . mysqli_real_escape_string($conn, $usin) . "'
            )");
    }

    echo "<div class='alert alert-success'>FBR settings saved.</div>";
}

// Load (or default if none exists)
$fbr = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM fbr_config LIMIT 1")) ?? [
    'is_enabled' => 0,
    'api_url' => 'http://localhost:8524/api/IMSFiscal/GetInvoiceNumberByModel',
    'pos_id' => '',
    'usin' => ''
];

?>

<div class="connected-app-card d-flex w-100">
    <ul class="w-100">
        <li class="flex-column align-items-start w-100">
            <form method="post" class="row g-3 w-100">
                <input type="hidden" name="update_fbr_config" value="1">

                <div class="col-md-12 form-check form-switch mb-2">
                    <input type="checkbox" class="form-check-input" id="is_enabled" name="is_enabled" <?= $fbr['is_enabled'] ? 'checked' : '' ?>>
                    <label class="form-check-label" for="is_enabled">Enable FBR Integration</label>
                </div>

                <div class="col-md-6">
                    <label class="form-label">FBR API URL</label>
                    <input type="text" class="form-control" name="api_url" value="<?= htmlspecialchars($fbr['api_url']) ?>">
                </div>

                <div class="col-md-6">
                    <label class="form-label">FBR POS ID</label>
                    <input type="number" class="form-control" name="pos_id" value="<?= htmlspecialchars($fbr['pos_id']) ?>">
                </div>

                <div class="col-md-6">
                    <label class="form-label">FBR USIN</label>
                    <input type="text" class="form-control" name="usin" value="<?= htmlspecialchars($fbr['usin']) ?>">
                </div>

                <div class="col-12">
                    <button class="btn btn-primary" type="submit">Save FBR Settings</button>
                </div>
            </form>
        </li>
    </ul>
</div>
