<form action="<?= e(url('configuracion/fiscal/facturacion-electronica')) ?>" method="post" id="electronicBillingForm">
    <?= csrf_field() ?>

    <?php if ($planAllowsElectronicInvoicing): ?>
        <?php $planName = trim((string) ($currentPlan['nombre'] ?? '')); ?>
        <input type="hidden" name="facturacion_electronica" value="1">
        <?php if ($planName !== ''): ?>
            <small class="text-muted d-block mt-1">Plan actual: <?= e($planName) ?></small>
        <?php endif; ?>

        <div id="factusCredentialsBlock" class="border rounded-3 p-3 mt-3">
            <div class="d-flex flex-wrap align-items-center justify-content-between gap-2 mb-2">
                <h3 class="h6 mb-0">Credenciales Factus</h3>
                <small class="text-muted">Se almacenan cifradas y no se muestran en texto plano</small>
            </div>
            <div class="row g-2">
                <div class="col-md-6">
                    <label class="form-label">URL API Factus</label>
                    <input
                        type="url"
                        class="form-control"
                        name="factus_api_url"
                        placeholder="https://api.factus.com.co"
                        value="<?= e($factusApiUrl) ?>"
                        maxlength="255"
                        data-factus-required="1"
                        data-factus-configured="<?= !empty($factusConfigured['api_url']) ? '1' : '0' ?>"
                    >
                </div>
                <div class="col-md-6">
                    <label class="form-label">Usuario / email Factus</label>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            class="form-control"
                            name="factus_username"
                            value="<?= e($factusUsername) ?>"
                            maxlength="120"
                            autocomplete="off"
                            placeholder="<?= !empty($factusConfigured['username']) ? 'Configurado (oculto)' : '' ?>"
                            data-factus-required="1"
                            data-factus-configured="<?= !empty($factusConfigured['username']) ? '1' : '0' ?>"
                        >
                        <?php if (!empty($factusConfigured['username'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Client ID</label>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            class="form-control"
                            name="factus_client_id"
                            value="<?= e($factusClientId) ?>"
                            maxlength="120"
                            autocomplete="off"
                            placeholder="<?= !empty($factusConfigured['client_id']) ? 'Configurado (oculto)' : '' ?>"
                            data-factus-required="1"
                            data-factus-configured="<?= !empty($factusConfigured['client_id']) ? '1' : '0' ?>"
                        >
                        <?php if (!empty($factusConfigured['client_id'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Client Secret</label>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            type="password"
                            class="form-control"
                            name="factus_client_secret"
                            value="<?= e($factusClientSecret) ?>"
                            maxlength="255"
                            autocomplete="new-password"
                            placeholder="<?= !empty($factusConfigured['client_secret']) ? 'Configurado (oculto)' : '' ?>"
                            data-factus-required="1"
                            data-factus-configured="<?= !empty($factusConfigured['client_secret']) ? '1' : '0' ?>"
                        >
                        <?php if (!empty($factusConfigured['client_secret'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Software ID</label>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            class="form-control"
                            name="factus_software_id"
                            value="<?= e($factusSoftwareId) ?>"
                            maxlength="120"
                            autocomplete="off"
                            placeholder="<?= !empty($factusConfigured['software_id']) ? 'Configurado (oculto)' : '' ?>"
                            data-factus-required="1"
                            data-factus-configured="<?= !empty($factusConfigured['software_id']) ? '1' : '0' ?>"
                        >
                        <?php if (!empty($factusConfigured['software_id'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Software PIN</label>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            type="password"
                            class="form-control"
                            name="factus_software_pin"
                            value="<?= e($factusSoftwarePin) ?>"
                            maxlength="120"
                            autocomplete="new-password"
                            placeholder="<?= !empty($factusConfigured['software_pin']) ? 'Configurado (oculto)' : '' ?>"
                            data-factus-required="1"
                            data-factus-configured="<?= !empty($factusConfigured['software_pin']) ? '1' : '0' ?>"
                        >
                        <?php if (!empty($factusConfigured['software_pin'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <label class="form-label">Contraseña Factus</label>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            type="password"
                            class="form-control"
                            name="factus_password"
                            value="<?= e($factusPassword) ?>"
                            maxlength="255"
                            autocomplete="new-password"
                            placeholder="<?= !empty($factusConfigured['password']) ? 'Configurado (oculto)' : '' ?>"
                            data-factus-required="1"
                            data-factus-configured="<?= !empty($factusConfigured['password']) ? '1' : '0' ?>"
                        >
                        <?php if (!empty($factusConfigured['password'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="d-flex align-items-center justify-content-between gap-2">
                        <label class="form-label mb-0">Numbering Range ID</label>
                        <button
                            type="button"
                            class="btn btn-sm btn-outline-secondary"
                            id="factusLoadRangesBtn"
                        >
                            Cargar rangos
                        </button>
                    </div>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            class="form-control"
                            id="factusNumberingRangeInput"
                            name="factus_numbering_range_id"
                            value="<?= e($factusNumberingRangeId) ?>"
                            maxlength="20"
                            inputmode="numeric"
                            pattern="[0-9]+"
                            placeholder="<?= !empty($factusConfigured['numbering_range_id']) ? 'Configurado' : 'Ej: 8' ?>"
                        >
                        <?php if (!empty($factusConfigured['numbering_range_id'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                    <select class="form-select mt-2 d-none" id="factusNumberingRangeSelect">
                        <option value="">Selecciona un rango...</option>
                    </select>
                    <small class="text-muted d-block mt-1" id="factusRangesHint">Puedes cargar y seleccionar un rango activo desde Factus.</small>
                </div>
                <div class="col-md-6">
                    <div class="d-flex align-items-center justify-content-between gap-2">
                        <label class="form-label mb-0">Municipality ID</label>
                        <button
                            type="button"
                            class="btn btn-sm btn-outline-secondary"
                            id="factusLoadMunicipalitiesBtn"
                        >
                            Cargar municipios
                        </button>
                    </div>
                    <div class="d-flex align-items-center gap-2">
                        <input
                            class="form-control"
                            id="factusMunicipalityInput"
                            name="factus_municipality_id"
                            value="<?= e($factusMunicipalityId) ?>"
                            maxlength="20"
                            inputmode="numeric"
                            pattern="[0-9]+"
                            placeholder="<?= !empty($factusConfigured['municipality_id']) ? 'Configurado' : 'Ej: 980' ?>"
                        >
                        <?php if (!empty($factusConfigured['municipality_id'])): ?>
                            <span class="badge text-bg-success">OK</span>
                        <?php endif; ?>
                    </div>
                    <div class="row g-2 mt-1">
                        <div class="col-12 col-md-5">
                            <input
                                type="search"
                                class="form-control"
                                id="factusMunicipalitySearch"
                                placeholder="Buscar municipio..."
                            >
                        </div>
                        <div class="col-12 col-md-7">
                            <select class="form-select d-none" id="factusMunicipalitySelect">
                                <option value="">Selecciona un municipio...</option>
                            </select>
                        </div>
                    </div>
                    <small class="text-muted d-block mt-1" id="factusMunicipalitiesHint">Puedes buscar por nombre y seleccionar el ID correcto desde Factus.</small>
                </div>
            </div>
            <small class="text-muted d-block mt-2">
                Puedes dejar los campos sensibles en blanco para conservar credenciales guardadas. Para probar emisión FE debes tener Numbering Range ID y Municipality ID.
            </small>
        </div>

        <div class="d-flex flex-wrap gap-2 mt-3">
            <button class="btn btn-primary" type="submit">Guardar facturación electrónica</button>
            <div id="factusActionsBlock" class="d-flex flex-wrap gap-2">
                <button
                    class="btn btn-outline-secondary"
                    type="submit"
                    formaction="<?= e(url('configuracion/fiscal/factus/probar')) ?>"
                    formmethod="post"
                >
                    Probar conexión Factus
                </button>
                <button
                    class="btn btn-outline-primary"
                    type="submit"
                    formaction="<?= e(url('configuracion/fiscal/factus/probar-factura')) ?>"
                    formmethod="post"
                >
                    Emitir factura de prueba FE
                </button>
            </div>
        </div>
    <?php else: ?>
        <div class="alert alert-light border mb-0">
            Tu plan actual no incluye facturación electrónica. Activa un plan con FE desde Superadmin para habilitar este módulo.
        </div>
    <?php endif; ?>
</form>
