<?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.receivables.aging') && !user_can('billing.receivables') && !user_can('module.billing') && current_user_role() !== 'admin') {
    http_response_code(403);
    die('No tienes permisos para acceder a Aging clientes.');
}
$page_title = 'Aging clientes';
$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="billingCustomerAgingMain" data-spa-module="collections:customer-aging">
    <?php require_once '../../layouts/topbar.php'; ?>
    <section id="customerAgingMain" class="content customer-aging-main billing-page-shell billing-phase-content billing-crm-content crm-ui-page-content crm-ui-page">
            <div class="customer-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 clientes</h2><p class="text-muted">Saldos netos por cobrar agrupados por cliente y antigüedad de vencimiento.</p></div>
                <div class="customer-aging-header-actions cluster top-actions-cluster billing-page-header-actions crm-ui-page-actions"><input type="hidden" id="customerAgingEnv" value="<?php echo htmlspecialchars($billingEnvironment, ENT_QUOTES, 'UTF-8'); ?>"><details class="billing-actions-menu" data-billing-actions-scope="customer_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="reloadCustomerAgingBtn" title="Actualizar aging de clientes"><i data-lucide="refresh-cw"></i><span class="mobile-hidden">Actualizar</span></button></div>
            </div>

            <section class="filter-bar-container customer-aging-toolbar billing-toolbar billing-filter-bar billing-filter-bar--three crm-ui-filter"><input type="search" id="customerAgingSearch" class="customer-aging-input input" placeholder="Buscar cliente, RUT o folio" autocomplete="off"><select id="customerAgingBucket" class="customer-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_customer">Sin cliente vinculado</option><option value="no_center">Sin centro de ingreso</option></select><select id="customerAgingCenter" class="customer-aging-input input"><option value="all">Todos los centros</option><option value="none">Sin centro</option></select></section>

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

            <section class="card customer-aging-card billing-data-card billing-table-card crm-ui-table-card"><div class="table-wrap customer-aging-table-wrap billing-table-scroll crm-ui-table-scroll"><table class="table customer-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">Cliente</th><th class="crm-mobile-hidden">RUT</th><th class="crm-mobile-hidden">Docs</th><th class="crm-mobile-metric">Saldo neto</th><th class="crm-mobile-hidden">Vencido neto</th><th class="crm-mobile-hidden">Por vencer 7d neto</th><th class="crm-mobile-hidden">1-30 neto</th><th class="crm-mobile-hidden">31-60 neto</th><th class="crm-mobile-hidden">61-90 neto</th><th class="crm-mobile-hidden">90+ neto</th><th class="crm-mobile-hidden">Centro</th><th class="crm-ui-action-cell">Acción</th></tr></thead><tbody id="customerAgingRows"><tr><td colspan="12">Cargando...</td></tr></tbody></table></div></section>
    </section>

        <div id="customerAgingModal" 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 customer-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="customerAgingModalTitle">Aging cliente</h2><p id="customerAgingModalSubtitle" class="customer-aging-muted"></p></div>
                    <button type="button" class="btn btn-outline action-square-btn billing-modal-close crm-ui-modal-icon-btn" id="closeCustomerAgingModalBtn" aria-label="Cerrar"><i data-lucide="x"></i></button>
                </div>
                <div class="product-modal-body crm-ui-modal-body" id="customerAgingModalBody"></div>
                <div class="product-modal-footer billing-modal-footer crm-ui-modal-footer" id="customerAgingModalFooter"><div class="modal-footer-actions-right"><button type="button" class="btn btn-outline" id="closeCustomerAgingFooterBtn"><i data-lucide="x"></i><span>Cerrar</span></button></div></div>
            </div>
        </div>
<?php $customerAgingJsVersion = @filemtime(__DIR__ . '/customer_aging.js') ?: time(); ?>
<script src="/modules/billing/customer_aging.js?v=<?php echo (int)$customerAgingJsVersion; ?>"></script>
</main>
<?php require_once '../../layouts/footer.php'; ?>
