<?php 
    define('LoginSource', 'login.php');
    require_once $_SERVER['DOCUMENT_ROOT'] . "/check_session.php";
    require_once $_SERVER['DOCUMENT_ROOT'] . "/connection.php";

?>


<!DOCTYPE html>
<html lang="en" class="treasury-app">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Treasury System</title>

    <!-- Bootstrap -->
    <link href="assets/vendor/bootstrap/bootstrap.min.css" rel="stylesheet">
    <link href="assets/vendor/bootstrap-icons/bootstrap-icons.min.css" rel="stylesheet">
    <link href="assets/css/global.css?v=20261015-paper-margins3" rel="stylesheet">
    <link href="assets/css/treasury-shell.css?v=20261009-1" rel="stylesheet">
</head>

<body>
<div class="app-shell treasury-shell">
    <aside class="treasury-sidebar offcanvas-lg offcanvas-start" tabindex="-1" id="mobileMenuPanel" aria-labelledby="treasurySidebarTitle">
        <div class="treasury-sidebar-brand">
            <button class="treasury-brand-link" type="button" onclick="load_dashboard(document.getElementById('menu-dashboard'))">
                <img src="assets/logo/bn-logo.png" alt="Barotac Nuevo Logo">
                <span><strong id="treasurySidebarTitle">TREASURY</strong><small>BAROTAC NUEVO</small></span>
            </button>
            <button class="treasury-sidebar-close d-lg-none" type="button" data-bs-dismiss="offcanvas" data-bs-target="#mobileMenuPanel" aria-label="Close navigation"><i class="bi bi-x-lg"></i></button>
        </div>
        <nav class="treasury-sidebar-nav" aria-label="Treasury navigation">
            <div class="treasury-menu-label">Overview</div>
            <button class="top-link active" id="menu-dashboard" onclick="load_dashboard(this)"> <i class="bi bi-grid-1x2-fill"></i> <span>Dashboard</span> </button>
            <div class="treasury-menu-label">Settings</div>
            <button class="dropdown-menu-link" id="menu-business-permit-licensing-fee" onclick="load_business_permit_licensing_fee(this)"> <i class="bi bi-shop-window"></i> <span>Business Permit Licensing Fee</span> </button>
            <button class="dropdown-menu-link" id="menu-or-stub" onclick="load_or_stub_settings(this)"> <i class="bi bi-receipt"></i><span>Accountable Form & OR Stub Registry</span> </button>
            <button class="dropdown-menu-link" id="menu-collection-assignment" onclick="load_collection_assignment(this)"> <i class="bi bi-person-check"></i><span>Collection Assignment</span> </button>           
            <button class="dropdown-menu-link" id="menu-transaction-authorization" onclick="load_transaction_authorization(this)"> <i class="bi bi-person-check"></i><span>Transaction Authorization</span> </button>
            <div class="treasury-menu-label">Transactions</div>
            <button class="top-link" id="menu-collect" onclick="load_collect(this)"> <i class="bi bi-cash-coin"></i> <span>Collect</span> </button>
            <button class="top-link" id="menu-other-receipts" onclick="load_other_receipts(this)"> <i class="bi bi-wallet2"></i> <span>Other Receipts</span> </button>
            <button class="top-link" id="menu-remittance" onclick="load_remittance(this)"> <i class="bi bi-bank"></i> <span>Cashier Collection Reports</span> </button>
            <button class="top-link" id="menu-deposits" onclick="load_deposits(this)"><i class="bi bi-bank"></i><span>Collections &amp; Deposits</span></button>
            <button class="top-link" id="menu-clearing" onclick="load_clearing(this)"><i class="bi bi-check2-square"></i><span>Collection Clearing</span></button>
            <button class="top-link" id="menu-approval" onclick="load_approval(this)"> <i class="bi bi-patch-check"></i> <span>Approval</span> <span class="menu-count-badge">6</span> </button>
            <div class="treasury-menu-label">Monitoring</div>
            <button class="dropdown-menu-link" id="menu-cash-inflow-register" onclick="load_cash_inflow_register(this)"> <i class="bi bi-arrow-down-up"></i><span>Cash Inflow Register</span> </button>
            <button class="dropdown-menu-link" id="menu-national-fund-receipts" onclick="load_national_fund_receipts(this)"> <i class="bi bi-building-down"></i><span>National Fund Receipts</span> </button>
            <button class="dropdown-menu-link" id="menu-cash-donation-receipts" onclick="load_cash_donation_receipts(this)"> <i class="bi bi-gift"></i><span>Cash Donation Receipts</span> </button>
            <button class="dropdown-menu-link" id="menu-outstanding-receivables" onclick="load_outstanding_receivables(this)"> <i class="bi bi-hourglass-split"></i><span>Outstanding Receivables</span> </button>
            <button class="dropdown-menu-link" id="menu-document-register" onclick="load_document_register(this)"> <i class="bi bi-folder2-open"></i><span>Document Register</span> </button>
            <div class="treasury-menu-label">Reports</div>
            <button class="dropdown-menu-link" onclick="load_rpt_daily_collection(this)"><i class="bi bi-calendar-day"></i><span>Daily Collection Report</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_collection_summary(this)"><i class="bi bi-bar-chart"></i><span>Collection Summary</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_cashier_transactions(this)"><i class="bi bi-person-lines-fill"></i><span>Per Cashier Collection Transactions</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_rpt_monthly(this)"><i class="bi bi-houses"></i><span>Real Property Monthly Collections</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_receipt_register(this)"><i class="bi bi-receipt-cutoff"></i><span>Official Receipt Register</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_collection_deposit(this)"><i class="bi bi-bank"></i><span>Collection &amp; Deposit</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_cashier_accountability(this)"><i class="bi bi-person-vcard"></i><span>Cashier Accountability Report</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_deposit_reconciliation(this)"><i class="bi bi-arrow-left-right"></i><span>Deposit Reconciliation</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_revenue_account(this)"><i class="bi bi-pie-chart"></i><span>Revenue by Fund / Account</span></button>
            <button class="dropdown-menu-link" onclick="load_rpt_target_performance(this)"><i class="bi bi-bullseye"></i><span>Collection Performance vs Target</span></button>
        </nav>
        <div class="treasury-sidebar-footer"><img src="assets/logo/municipal-banner.png" alt="Municipality of Barotac Nuevo"></div>
    </aside>
    <main class="main">
        <header class="topnav treasury-topbar">
            <div class="treasury-header-main">
                <button class="treasury-header-toggle d-lg-none" type="button" onclick="toggleMobileMenu()" aria-controls="mobileMenuPanel" aria-label="Open navigation"><i class="bi bi-list"></i></button>
                <div class="treasury-header-title"><span>Municipality of Barotac Nuevo</span><span class="treasury-header-divider" aria-hidden="true">/</span><strong>Municipal Treasury Office</strong></div>
            </div>
            <div class="topnav-right">
                <button class="topnav-icon-btn" type="button" title="User Guide" aria-label="User Guide" onclick="load_user_guide()">
                    <i class="bi bi-journal-text"></i>
                </button>

                <div class="nav-item-wrap topnav-user">
                    <button class="user-trigger" type="button">
                        <div class="user-avatar">RC</div>
                        <div class="user-copy">
                            <div class="user-name">Revenue Officer</div>
                            <div class="user-role">Treasurer's Office</div>
                        </div>
                        <i class="bi bi-chevron-down rc-fs-56rem rc-opacity-7"></i>
                    </button>
                    <div class="nav-dropdown user-menu">
                        <button class="dropdown-menu-link"><i class="bi bi-box-arrow-right"></i><span>Logout</span></button>
                    </div>
                </div>
            </div>
        
        </header>
        <section class="content">
            <div class="content-frame" id="mainContent"></div>
        </section>
        <footer class="treasury-footer"><span>Powered By: E2E Business Solutions, Inc</span><span>Version: 1.0</span></footer>
    </main>
</div>

<script src="assets/vendor/bootstrap/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/jquery/jquery.min.js"></script>
<script src="assets/vendor/xlsx/xlsx.full.min.js"></script>

<?php include 'includes/ui_controller.php'; ?>
<?php include 'includes/js_controller.php'; ?>

<script>
    const mainContent = document.getElementById('mainContent');
    const mobileMenuPanel = document.getElementById('mobileMenuPanel');

    function toggleMobileMenu(){
        bootstrap.Offcanvas.getOrCreateInstance(mobileMenuPanel).toggle();
    }

    function closeMobileMenu(){
        const sidebar = bootstrap.Offcanvas.getInstance(mobileMenuPanel);
        if (sidebar) sidebar.hide();
    }

    function closeDesktopDropdown(element){
        const group = element ? element.closest('.nav-item-wrap') : null;
        if(!group) return;

        group.classList.add('dropdown-closed');
        if(document.activeElement && typeof document.activeElement.blur === 'function'){
            document.activeElement.blur();
        }

        const resetDropdown = function(){
            group.classList.remove('dropdown-closed');
            group.removeEventListener('mouseleave', resetDropdown);
        };
        group.addEventListener('mouseleave', resetDropdown);
    }

    function setActiveMenu(element){
        document.querySelectorAll('.top-link, .dropdown-menu-link').forEach(item => item.classList.remove('active'));
        if(element) element.classList.add('active');

        document.querySelectorAll('.nav-item-wrap').forEach(group => {
            const parent = group.querySelector(':scope > .dropdown-parent');
            if(parent && group.querySelector('.dropdown-menu-link.active')) parent.classList.add('active');
        });
    }

    function executeLoadedScripts(container){
        container.querySelectorAll('script').forEach(function(oldScript){
            const newScript = document.createElement('script');

            Array.from(oldScript.attributes).forEach(function(attribute){
                newScript.setAttribute(attribute.name, attribute.value);
            });

            newScript.textContent = oldScript.textContent;
            oldScript.replaceWith(newScript);
        });
    }

   function loadPage(path, title, element){
       setActiveMenu(element);
       closeDesktopDropdown(element);
       closeMobileMenu();

       mainContent.innerHTML = `
           <div class="loading-state rc-page-scroll" tabindex="0">
               <div class="spinner-border mb-2" role="status"></div>
               <div>Loading ${title}...</div>
           </div>
       `;

       fetch(path, {
           method: 'POST',
           headers: {
               'X-Requested-With': 'XMLHttpRequest'
           },
           cache: 'no-store'
       })
       .then(response => {
           if(!response.ok) throw new Error('Page not found');
           return response.text();
       })
       .then(html => {
           mainContent.innerHTML = html;
           executeLoadedScripts(mainContent);
           const pageScroller = mainContent.querySelector(':scope > .rc-page-scroll');
           if (pageScroller) pageScroller.scrollTo({ top: 0, left: 0, behavior: 'instant' });
       })
       .catch(() => {
           mainContent.innerHTML = `
               <div class="error-state rc-page-scroll" tabindex="0">
                   <i class="bi bi-exclamation-triangle"></i>
                   <div class="fw-bold mb-1">${title}</div>
                   <div>Unablex to load <strong>${path}</strong>.</div>
               </div>
           `;
       });
   }

   function load_user_guide(){
       loadPage('/router/?route=menu_system_ug_user_guide', 'User Guide', null);
   }

   /* Main transactions */
   function load_dashboard(el){
       loadPage('/router/?route=menu_system_dash_revenue_collection', 'Dashboard', el);
   }

   function load_collect(el){
       loadPage('/router/?route=menu_system_trx_collect', 'Collect', el);
   }

   function load_other_receipts(el){
       loadPage('/router/?route=menu_system_trx_other_receipts', 'Other Receipts / Fund Receipts', el);
   }

   function load_remittance(el){
       loadPage('/router/?route=menu_system_trx_remit_deposit', 'Cashier Collection Reports', el);
   }

   function load_deposits(el){ loadPage('/router/?route=menu_system_trx_deposits', 'Collections & Deposits', el); }
   function load_clearing(el){ loadPage('/router/?route=menu_system_trx_collection_clearing', 'Collection Clearing', el); }

   function load_approval(el){
       loadPage('/router/?route=menu_system_trx_approval', 'Approval', el);
   }

   /* Monitoring */
   function load_cash_inflow_register(el){
       loadPage('/router/?route=menu_system_mon_cash_inflow', 'Cash Inflow Register', el);
   }

   function load_national_fund_receipts(el){
       loadPage('/router/?route=menu_system_mon_national_fund', 'National Fund Receipts', el);
   }

   function load_cash_donation_receipts(el){
       loadPage('/router/?route=menu_system_mon_cash_donation', 'Cash Donation Receipts', el);
   }

   function load_outstanding_receivables(el){
       loadPage('/router/?route=menu_system_mon_outstanding_receivables', 'Outstanding Receivables', el);
   }

   function load_document_register(el){
       loadPage('/router/?route=menu_system_mon_document_register', 'Document Register', el);
   }

   /* Treasury-only Settings */
   function load_business_permit_licensing_fee(el){
       loadPage('/router/?route=menu_system_set_business_permit_fee', 'Business Permit Licensing Fee', el);
   }

   function load_or_stub_settings(el){
       loadPage('/router/?route=menu_system_set_or_stub', 'OR Stub Registry', el);
   }

   function load_collection_assignment(el){
       loadPage('/router/?route=menu_system_set_collection_assign', 'Collection Assignment', el);
   }

   /* Reports */
   function load_rpt_daily_collection(el){
       loadPage('/router/?route=menu_system_rpt_daily_collection', 'Daily Collection Report', el);
   }

   function load_rpt_collection_summary(el){
       loadPage('/router/?route=menu_system_rpt_collection_summary', 'Collection Summary', el);
   }

   function load_rpt_cashier_transactions(el){
       loadPage('/router/?route=menu_system_rpt_cashier_transactions', 'Per Cashier Collection Transactions', el);
   }

   function load_rpt_rpt_monthly(el){
       loadPage('/router/?route=menu_system_rpt_rp_monthly_collection', 'Real Property Monthly Collections Report', el);
   }

   function load_rpt_receipt_register(el){
       loadPage('/router/?route=menu_system_rpt_or_register', 'Official Receipt Register', el);
   }

   function load_rpt_collection_deposit(el){
       loadPage('/router/?route=menu_system_rpt_collection_deposit', 'Collection & Deposit', el);
   }

   function load_rpt_cashier_accountability(el){
       loadPage('/router/?route=menu_system_rpt_cashier_accountability', 'Cashier Accountability Report', el);
   }

   function load_rpt_deposit_reconciliation(el){
       loadPage('/router/?route=menu_system_rpt_deposit_recon', 'Deposit Reconciliation', el);
   }

   function load_rpt_revenue_account(el){
       loadPage('/router/?route=menu_system_rpt_revenue_fund_account', 'Revenue by Fund / Account', el);
   }

   function load_rpt_target_performance(el){
       loadPage('/router/?route=menu_system_rpt_collect_perf_target', 'Collection Performance vs Target', el);
   }   
   
   function load_transaction_authorization(el){
       loadPage('/router/?route=menu_system_set_transaction_auth', 'Transaction Authorization', el);
   }

    document.addEventListener('DOMContentLoaded', function(){
        load_dashboard(document.getElementById('menu-dashboard'));
    });
</script>


</body>
</html>
