<?php
// Handle form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_fbr_digital_config'])) {
    $is_enabled = isset($_POST['is_enabled']) ? 1 : 0;
    $api_url    = $_POST['api_url'] ?? '';
    $token      = $_POST['token'] ?? '';
    $mode       = $_POST['mode'] ?? 'sandbox';

    $check = mysqli_fetch_assoc(mysqli_query($conn, "SELECT id FROM fbr_digital LIMIT 1"));

    if ($check) {
        mysqli_query($conn, "UPDATE fbr_digital SET 
            is_enabled = $is_enabled,
            api_url = '" . mysqli_real_escape_string($conn, $api_url) . "',
            token = '" . mysqli_real_escape_string($conn, $token) . "',
            mode = '" . mysqli_real_escape_string($conn, $mode) . "',
            updated_at = NOW()
            LIMIT 1
        ");
    } else {
        mysqli_query($conn, "INSERT INTO fbr_digital (is_enabled, api_url, token, mode)
            VALUES (
                $is_enabled,
                '" . mysqli_real_escape_string($conn, $api_url) . "',
                '" . mysqli_real_escape_string($conn, $token) . "',
                '" . mysqli_real_escape_string($conn, $mode) . "'
            )");
    }

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

// Load current settings or use default fallback
$fbr = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM fbr_digital LIMIT 1")) ?? [
    'is_enabled' => 0,
    'api_url'    => 'https://gw.fbr.gov.pk/di_data/v1/di/postinvoicedata_sb',
    'token'      => '',
    'mode'       => 'sandbox'
];
?>

<div class="connected-app-card d-flex w-100 mt-4">
    <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_digital_config" value="1">

                <h5>FBR Digital Invoicing</h5>

                <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 Digital Integration</label>
                </div>

                <div class="col-md-6">
                    <label class="form-label">API Mode</label>
                    <select name="mode" class="form-control">
                        <option value="sandbox" <?= $fbr['mode'] === 'sandbox' ? 'selected' : '' ?>>Sandbox</option>
                        <option value="production" <?= $fbr['mode'] === 'production' ? 'selected' : '' ?>>Production</option>
                    </select>
                </div>

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

               <div class="col-md-12">
    <label class="form-label">Bearer Token</label>
    <textarea
        class="form-control"
        name="token"
        rows="3"
        placeholder="07a40105-b4aa-339f-b7de-8b8ee63e2ea9"
    ><?= htmlspecialchars($fbr['token'] ?: '07a40105-b4aa-339f-b7de-8b8ee63e2ea9') ?></textarea>
</div>



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