<?php
require_once __DIR__ . '/../../core/config.php';
require_once __DIR__ . '/../../core/session.php';
require_once __DIR__ . '/../../core/database.php';
require_once __DIR__ . '/../../core/format.php';
require_once __DIR__ . '/../../core/auth.php';
require_once __DIR__ . '/../../core/permissions.php';
require_once __DIR__ . '/../../services/billing/BillingUnifiedIssuerService.php';

require_login();
require_permission('module.billing');
$page_title = 'Emitir documento';
$service = new BillingUnifiedIssuerService(db());
$defaultIssueEnvironment = defined('FACTO_DEFAULT_ENVIRONMENT') && FACTO_DEFAULT_ENVIRONMENT === 'sandbox' ? 'sandbox' : 'production';
$bootstrap = $service->bootstrap($defaultIssueEnvironment);
$requestedType = trim((string)($_GET['type'] ?? ($_GET['document'] ?? '')));
$allowedRequested = array_column($bootstrap['document_types'], 'document_key');
if (!in_array($requestedType, $allowedRequested, true)) $requestedType = '';
require_once __DIR__ . '/../../layouts/header.php';
require_once __DIR__ . '/../../layouts/sidebar.php';
?>
<main class="app-main billing-integrated-main crm-ui-main" id="manualIssuerMain" data-spa-module="billing:issuer">
    <?php require_once __DIR__ . '/../../layouts/topbar.php'; ?>
    <section class="content manual-page-shell billing-page-shell manual-issuer-content billing-phase-content billing-crm-content crm-ui-page-content crm-ui-page">
            <div class="page-header header-responsive manual-page-header billing-page-header crm-ui-page-header">
                <div class="page-header-content crm-ui-page-header-copy">
                    <span class="label">FINANZAS · DOCUMENTOS</span>
                    <h2>Emitir documento</h2>
                    <p class="text-muted">Selecciona el documento que necesitas generar.</p>
                </div>
                <div class="cluster top-actions-cluster billing-page-header-actions crm-ui-page-actions">
                    <span class="manual-environment-badge <?php echo $defaultIssueEnvironment === 'production' ? 'is-production' : 'is-sandbox'; ?>">
                        <span aria-hidden="true"></span><?php echo $defaultIssueEnvironment === 'production' ? 'Emisión real' : 'Pruebas'; ?>
                    </span>
                    <a class="btn btn-outline btn-icon-mobile" href="/modules/billing/documents.php" title="Consulta tributaria"><i data-lucide="files"></i><span class="mobile-hidden">Consulta tributaria</span></a>
                </div>
            </div>

            <section class="card manual-types-card crm-ui-table-card">
                <div class="card-header crm-ui-table-toolbar"><div><h3>Documentos tributarios</h3><p>Elige una opción para comenzar. Antes de emitir podrás revisar el PDF preliminar.</p></div></div>
                <div class="card-body"><div class="manual-type-grid manual-action-grid">
                    <article class="manual-type-card manual-action-card">
                        <div class="manual-card-icon"><i data-lucide="receipt-text"></i></div>
                        <div class="manual-card-copy"><strong>Factura nacional</strong><span>Factura electrónica afecta o exenta.</span></div>
                        <div class="manual-card-actions">
                            <button type="button" class="btn btn-primary manual-document-action" data-document-key="invoice_electronic">Factura afecta</button>
                            <button type="button" class="btn btn-outline manual-document-action" data-document-key="invoice_exempt_electronic">Factura exenta</button>
                        </div>
                    </article>
                    <article class="manual-type-card manual-action-card">
                        <div class="manual-card-icon"><i data-lucide="globe-2"></i></div>
                        <div class="manual-card-copy"><strong>Factura de exportación</strong><span>Documento en moneda extranjera para clientes internacionales.</span></div>
                        <div class="manual-card-actions"><button type="button" class="btn btn-primary manual-document-action" data-document-key="invoice_export_electronic">Crear factura</button></div>
                    </article>
                    <article class="manual-type-card manual-action-card">
                        <div class="manual-card-icon"><i data-lucide="file-diff"></i></div>
                        <div class="manual-card-copy"><strong>Nota de crédito o débito</strong><span>Parte buscando el documento tributario que deseas modificar.</span></div>
                        <div class="manual-card-actions manual-card-actions-compact">
                            <button type="button" class="btn btn-outline manual-document-action" data-document-key="credit_note_electronic">NC nacional</button>
                            <button type="button" class="btn btn-outline manual-document-action" data-document-key="debit_note_electronic">ND nacional</button>
                            <button type="button" class="btn btn-outline manual-document-action" data-document-key="credit_note_export_electronic">NC export.</button>
                            <button type="button" class="btn btn-outline manual-document-action" data-document-key="debit_note_export_electronic">ND export.</button>
                        </div>
                    </article>
                    <article class="manual-type-card manual-action-card">
                        <div class="manual-card-icon"><i data-lucide="truck"></i></div>
                        <div class="manual-card-copy"><strong>Guía de despacho</strong><span>Para venta, traslado interno, consignación o exportación.</span></div>
                        <div class="manual-card-actions"><button type="button" class="btn btn-primary manual-document-action" data-document-key="dispatch_guide_electronic">Crear guía</button></div>
                    </article>
                </div></div>
            </section>

            <section class="card manual-commercial-card">
                <div class="card-body manual-commercial-row">
                    <div class="manual-commercial-copy"><span class="manual-commercial-icon"><i data-lucide="clipboard-list"></i></span><div><span class="label">DOCUMENTO COMERCIAL · NO TRIBUTARIO</span><h3>Orden de compra ICYLAB</h3><p>Genera una OC numerada, descarga su PDF y envíala al proveedor.</p></div></div>
                    <a class="btn btn-primary" href="/modules/billing/purchase_orders.php?new=1"><i data-lucide="plus"></i><span>Nueva orden de compra</span></a>
                </div>
            </section>
    </section>

<div class="product-modal-backdrop manual-modal-backdrop crm-ui-modal-backdrop" data-crm-dismissable="true" id="manualIssuerModal" aria-hidden="true" role="dialog" aria-modal="true">
    <div class="product-modal-box manual-modal-box billing-modal-box crm-ui-modal-box" role="dialog" aria-modal="true" aria-labelledby="manualModalTitle">
        <header class="product-modal-header manual-modal-header billing-modal-header crm-ui-modal-header">
            <button class="btn btn-outline action-square-btn manual-icon-btn billing-modal-back crm-ui-modal-icon-btn" type="button" id="manualBackBtn" aria-label="Volver"><i data-lucide="arrow-left"></i></button>
            <div class="manual-modal-heading billing-modal-heading crm-ui-modal-heading"><h2 id="manualModalTitle">Emitir documento</h2><p id="manualModalSubtitle">Flujo manual</p></div>
            <button class="btn btn-outline action-square-btn manual-icon-btn billing-modal-close crm-ui-modal-icon-btn" type="button" id="manualCloseBtn" aria-label="Cerrar"><i data-lucide="x"></i></button>
        </header>
        <div class="product-modal-body manual-modal-body crm-ui-modal-body">
            <nav class="manual-stepbar billing-flow-steps" id="manualStepbar">
                <span data-step="document">1. Documento</span>
                <span data-step="detail">2. Receptor y detalle</span>
                <span data-step="preview">3. Revisar</span>
                <span data-step="confirm">4. Confirmar</span>
            </nav>
            <div id="manualLayerContent"></div>
        </div>
        <footer class="product-modal-footer manual-modal-footer billing-modal-footer crm-ui-modal-footer" id="manualModalFooter"></footer>
    </div>
</div>

<script>
window.ICY_MANUAL_BOOTSTRAP = <?php echo json_encode($bootstrap, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>;
window.ICY_MANUAL_REQUESTED_TYPE = <?php echo json_encode($requestedType); ?>;
</script>

<?php $manualIssuerJsVersion = @filemtime(__DIR__ . '/manual_issuer.js') ?: time(); ?>
<script src="/modules/billing/manual_issuer.js?v=<?php echo (int)$manualIssuerJsVersion; ?>"></script>
</main>
<?php require_once __DIR__ . '/../../layouts/footer.php'; ?>
