<?php
require_once '../../core/config.php';
require_once '../../core/session.php';
require_once '../../core/database.php';
require_once '../../core/auth.php';
require_once '../../core/permissions.php';
require_once '../../core/format.php';

require_login();

if (!user_can('billing.payables') && !user_can('billing.payables.aging') && !user_can('module.billing') && current_user_role() !== 'admin') {
    http_response_code(403);
    die('No tienes permisos para acceder a Aging proveedores.');
}

$page_title = 'Aging proveedores';
$billingEnvironment = defined('FACTO_DEFAULT_ENVIRONMENT') && FACTO_DEFAULT_ENVIRONMENT === 'sandbox' ? 'sandbox' : 'production';

require_once '../../layouts/header.php';
require_once '../../layouts/sidebar.php';
?>

<main class="app-main billing-integrated-main crm-ui-main" id="billingSupplierAgingMain" data-spa-module="collections:supplier-aging">
    <?php require_once '../../layouts/topbar.php'; ?>
    <section id="supplierAgingMain" class="content supplier-aging-main billing-page-shell billing-phase-content supplier-aging-content billing-crm-content crm-ui-page-content crm-ui-page">
            <div class="supplier-aging-header page-header header-responsive billing-page-header crm-ui-page-header">
                <div class="page-header-content crm-ui-page-header-copy">
                    <span class="label">FINANZAS</span>
                    <h2>Aging proveedores</h2>
                    <p class="text-muted">Saldos por pagar agrupados por proveedor y antigüedad de vencimiento.</p>
                </div>
                <div class="supplier-aging-header-actions cluster top-actions-cluster billing-page-header-actions crm-ui-page-actions">
                    <input type="hidden" id="agingEnv" value="<?php echo htmlspecialchars($billingEnvironment, ENT_QUOTES, 'UTF-8'); ?>">
                    <details class="billing-actions-menu" data-billing-actions-scope="supplier_aging" data-close-outside><summary class="btn btn-outline"><i data-lucide="ellipsis-vertical"></i><span>Acciones</span></summary><div class="billing-actions-menu__panel"><button type="button" data-billing-bulk-action="xlsx"><i data-lucide="sheet"></i>Descargar XLSX</button><button type="button" data-billing-bulk-action="refresh"><i data-lucide="refresh-cw"></i>Recalcular aging</button></div></details>
                    <button type="button" class="btn btn-primary btn-icon-mobile" id="reloadAgingBtn" title="Actualizar aging de proveedores"><i data-lucide="refresh-cw"></i><span class="mobile-hidden">Actualizar</span></button>
                </div>
            </div>

            <section class="filter-bar-container aging-toolbar billing-toolbar billing-filter-bar billing-filter-bar--three crm-ui-filter">
                <input type="search" id="agingSearch" class="aging-input input" placeholder="Buscar proveedor, RUT o folio" autocomplete="off">
                <select id="agingBucket" class="aging-input input">
                    <option value="all" selected>Todos los saldos abiertos</option>
                    <option value="overdue">Vencidos</option>
                    <option value="current">No vencidos</option>
                    <option value="due_7">Por vencer en 7 días</option>
                    <option value="no_supplier">Sin proveedor vinculado</option>
                    <option value="no_center">Sin centro de costo</option>
                </select>
                <select id="agingCenter" class="aging-input input"><option value="all">Todos los centros</option><option value="none">Sin centro</option></select>
            </section>

            <section class="grid responsive-kpis aging-kpis billing-kpi-grid crm-ui-kpis" style="--crm-kpi-columns: 6;" id="agingKpis">
                <article class="kpi-card aging-kpi"><div class="kpi-label">Proveedores</div><div class="kpi-main"><strong class="kpi-value">-</strong></div></article>
                <article class="kpi-card aging-kpi"><div class="kpi-label">Saldo abierto</div><div class="kpi-main"><strong class="kpi-value">-</strong></div></article>
                <article class="kpi-card aging-kpi"><div class="kpi-label">Vencido</div><div class="kpi-main"><strong class="kpi-value">-</strong></div></article>
                <article class="kpi-card aging-kpi"><div class="kpi-label">Por vencer 7 días</div><div class="kpi-main"><strong class="kpi-value">-</strong></div></article>
                <article class="kpi-card aging-kpi"><div class="kpi-label">Documentos</div><div class="kpi-main"><strong class="kpi-value">-</strong></div></article>
                <article class="kpi-card aging-kpi"><div class="kpi-label">Sin centro</div><div class="kpi-main"><strong class="kpi-value">-</strong></div></article>
            </section>

            <section class="card aging-card billing-data-card billing-table-card crm-ui-table-card">
                <div class="table-wrap aging-table-wrap billing-table-scroll crm-ui-table-scroll">
                    <table class="table aging-table billing-responsive-table billing-crm-table crm-ui-table crm-ui-mobile-table" style="--crm-mobile-columns: minmax(0, 1fr) 104px 64px;">
                        <thead><tr><th class="crm-mobile-primary">Proveedor</th><th class="crm-mobile-hidden">RUT</th><th class="crm-mobile-hidden">Docs</th><th class="crm-mobile-metric">Saldo</th><th class="crm-mobile-hidden">Vencido</th><th class="crm-mobile-hidden">Por vencer 7d</th><th class="crm-mobile-hidden">0-30</th><th class="crm-mobile-hidden">31-60</th><th class="crm-mobile-hidden">61-90</th><th class="crm-mobile-hidden">90+</th><th class="crm-mobile-hidden">Centro</th><th class="crm-ui-action-cell">Acción</th></tr></thead>
                        <tbody id="agingRows"><tr><td colspan="12">Cargando...</td></tr></tbody>
                    </table>
                </div>
            </section>
    </section>

        <div id="agingModal" class="product-modal-backdrop crm-ui-modal-backdrop" data-crm-dismissable="true" style="display:none;" aria-hidden="true" role="dialog" aria-modal="true">
            <div class="product-modal-box aging-modal-box billing-modal-box crm-ui-modal-box">
                <div class="product-modal-header billing-modal-header billing-modal-header--no-back crm-ui-modal-header crm-ui-modal-header--no-back">
                    <div class="billing-modal-heading crm-ui-modal-heading"><h2 id="agingModalTitle">Aging proveedor</h2><p id="agingModalSubtitle" class="aging-muted"></p></div>
                    <button type="button" class="btn btn-outline action-square-btn billing-modal-close crm-ui-modal-icon-btn" id="closeAgingModalBtn" aria-label="Cerrar"><i data-lucide="x"></i></button>
                </div>
                <div class="product-modal-body crm-ui-modal-body" id="agingModalBody"></div>
                <div class="product-modal-footer billing-modal-footer crm-ui-modal-footer" id="agingModalFooter"><div class="modal-footer-actions-right"><button type="button" class="btn btn-outline" id="closeAgingFooterBtn"><i data-lucide="x"></i><span>Cerrar</span></button></div></div>
            </div>
        </div>

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