<?php
$ctrl   = strtolower($this->router->fetch_class());
$method = strtolower($this->router->fetch_method());

function sa($c, $cur) { return $c === $cur ? 'active' : ''; }

$masters_open = in_array($ctrl, ['category','unit','product','customer','supplier']) ? 'open' : '';
$trans_open   = in_array($ctrl, ['purchase','sales','quotation','tailoring_order','report_header'])              ? 'open' : '';
$rep_open     = ($ctrl === 'reports')                                                ? 'open' : '';
?>

<!-- SIDEBAR -->
<div class="sidebar" id="sidebar">

    <!-- Brand -->
    <div class="sidebar-brand">
        <div class="brand-icon"><i class="fa-solid fa-scissors"></i></div>
        <div class="brand-name">
            Tailoring ERP
            <div class="brand-sub">Billing &amp; Inventory</div>
        </div>
    </div>

    <!-- Menu -->
    <ul class="sidebar-menu">

        <li class="menu-label">Main</li>
        <li>
            <a href="<?= site_url('dashboard') ?>" class="<?= sa('dashboard',$ctrl) ?>">
                <i class="fa-solid fa-gauge-high"></i>
                <span class="sb-txt">Dashboard</span>
                <span class="sb-tooltip">Dashboard</span>
            </a>
        </li>

        <li class="menu-label">Masters</li>
        <!-- In mini mode: clicking icon goes to Category directly. In full mode: expands sub-menu -->
        <li class="has-sub <?= $masters_open ?>">
            <a href="<?= site_url('category') ?>"
               class="has-sub-toggle"
               data-mini-href="<?= site_url('category') ?>"
               onclick="return handleSubMenu(this);">
                <i class="fa-solid fa-layer-group"></i>
                <span class="sb-txt">Masters</span>
                <i class="fa-solid fa-chevron-right sb-arrow"></i>
                <span class="sb-tooltip">Masters</span>
            </a>
            <ul class="sub-menu">
                <li><a href="<?= site_url('category') ?>" class="<?= sa('category',$ctrl) ?>"><i class="fa-solid fa-tag"></i><span class="sb-txt"> Category</span></a></li>
                <li><a href="<?= site_url('unit') ?>"     class="<?= sa('unit',$ctrl) ?>"><i class="fa-solid fa-ruler"></i><span class="sb-txt"> Unit</span></a></li>
                <li><a href="<?= site_url('product') ?>"  class="<?= sa('product',$ctrl) ?>"><i class="fa-solid fa-box"></i><span class="sb-txt"> Product</span></a></li>
                <li><a href="<?= site_url('customer') ?>" class="<?= sa('customer',$ctrl) ?>"><i class="fa-solid fa-users"></i><span class="sb-txt"> Customer</span></a></li>
                <li><a href="<?= site_url('supplier') ?>" class="<?= sa('supplier',$ctrl) ?>"><i class="fa-solid fa-truck"></i><span class="sb-txt"> Supplier</span></a></li>
            </ul>
        </li>

        <li class="menu-label">Transactions</li>
        <li class="has-sub <?= $trans_open ?>">
            <a href="<?= site_url('purchase') ?>"
               class="has-sub-toggle"
               data-mini-href="<?= site_url('purchase') ?>"
               onclick="return handleSubMenu(this);">
                <i class="fa-solid fa-receipt"></i>
                <span class="sb-txt">Transactions</span>
                <i class="fa-solid fa-chevron-right sb-arrow"></i>
                <span class="sb-tooltip">Transactions</span>
            </a>
            <ul class="sub-menu">
                <li><a href="<?= site_url('purchase') ?>"        class="<?= sa('purchase',$ctrl) ?>"><i class="fa-solid fa-cart-shopping"></i><span class="sb-txt"> Purchase Entry</span></a></li>
                <li><a href="<?= site_url('sales') ?>"           class="<?= sa('sales',$ctrl) ?>"><i class="fa-solid fa-file-invoice-dollar"></i><span class="sb-txt"> Sales Billing</span></a></li>
                <li>
    <a href="<?= site_url('quotation') ?>"
       class="<?= sa('quotation',$ctrl) ?>">
        <i class="fa-solid fa-file-lines"></i>
        <span class="sb-txt"> Quotation</span>
    </a>
</li>
                <li><a href="<?= site_url('tailoring_order') ?>" class="<?= sa('tailoring_order',$ctrl) ?>"><i class="fa-solid fa-shirt"></i><span class="sb-txt"> Tailoring Orders</span></a></li>
                <li><a href="<?= site_url('report_header') ?>" class="<?= sa('report_header',$ctrl) ?>"><i class="fa-solid fa-file-contract"></i><span class="sb-txt"> Report Header</span></a></li>
            </ul>
        </li>

        <li class="menu-label">Reports</li>
        <li class="has-sub <?= $rep_open ?>">
            <a href="<?= site_url('reports/stock') ?>"
               class="has-sub-toggle"
               data-mini-href="<?= site_url('reports/stock') ?>"
               onclick="return handleSubMenu(this);">
                <i class="fa-solid fa-chart-pie"></i>
                <span class="sb-txt">Reports</span>
                <i class="fa-solid fa-chevron-right sb-arrow"></i>
                <span class="sb-tooltip">Reports</span>
            </a>
            <ul class="sub-menu">
                <li><a href="<?= site_url('reports/stock') ?>"        class="<?= ($ctrl=='reports'&&$method=='stock') ? 'active':'' ?>"><i class="fa-solid fa-chart-bar"></i><span class="sb-txt"> Stock Report</span></a></li>
                <li><a href="<?= site_url('reports/sales_report') ?>" class="<?= ($ctrl=='reports'&&$method=='sales_report') ? 'active':'' ?>"><i class="fa-solid fa-chart-line"></i><span class="sb-txt"> Sales Report</span></a></li>
            </ul>
        </li>

        <li class="menu-label">System</li>
        <li>
            <a href="javascript:void(0)"
               data-backup-url="<?= site_url('backup/download') ?>"
               onclick="confirmBackupDownload(this)"
               class="<?= sa('backup',$ctrl) ?>">
                <i class="fa-solid fa-database"></i>
                <span class="sb-txt">Backup</span>
                <span class="sb-tooltip">Backup</span>
            </a>
        </li>

    </ul>

    <script>
    function confirmBackupDownload(el) {
        var downloadUrl = el.getAttribute('data-backup-url');
        Swal.fire({
            icon: 'warning',
            title: 'Backup Download?',
            text: 'Do you want to download the database backup file?',
            showCancelButton: true,
            confirmButtonText: 'Yes, download it',
            cancelButtonText: 'No',
            confirmButtonColor: '#f59e0b',
            cancelButtonColor: '#6b7280',
            background: '#ffffff',
            color: '#1f2937',
            customClass: { popup: 'swal-compact' },
            showClass: { popup: 'animate__animated animate__zoomIn' },
            hideClass: { popup: 'animate__animated animate__zoomOut' }
        }).then(function (result) {
            if (result.isConfirmed) {
                window.location.href = downloadUrl;
            }
        });
    }
    </script>

    <!-- Footer -->
    <div class="sidebar-footer">
        <div class="sf-avatar"><?= strtoupper(substr($this->session->userdata('admin_name') ?: 'A', 0, 1)) ?></div>
        <div class="sidebar-footer-info">
            <?= htmlspecialchars($this->session->userdata('admin_name') ?: 'Admin') ?>
            <br><small>Administrator</small>
        </div>
        <a href="<?= site_url('Auth/logout') ?>" class="sidebar-logout-btn" title="Logout">
            <i class="fa-solid fa-right-from-bracket"></i>
        </a>
    </div>

</div>
<!-- /SIDEBAR -->

<!-- MAIN WRAPPER -->
<div class="main-wrapper" id="mainWrapper">

    <!-- TOP HEADER -->
    <div class="top-header">
        <div class="hdr-left">
            <button class="sb-toggle" id="sbToggle" onclick="toggleSidebar()" title="Toggle Sidebar">
                <i class="fa-solid fa-bars" id="sbToggleIcon"></i>
            </button>
            <div>
                <div class="page-title-txt"><?= isset($page_title) ? $page_title : 'Dashboard' ?></div>
                <nav class="breadcrumb-nav">
                    <ol class="breadcrumb mb-0">
                        <li class="breadcrumb-item"><a href="<?= site_url('dashboard') ?>">Home</a></li>
                        <?php if(isset($breadcrumb)): foreach($breadcrumb as $bc): ?>
                        <li class="breadcrumb-item <?= isset($bc['active']) ? 'active' : '' ?>">
                            <?= isset($bc['url']) ? '<a href="'.site_url($bc['url']).'">'.$bc['label'].'</a>' : $bc['label'] ?>
                        </li>
                        <?php endforeach; endif; ?>
                    </ol>
                </nav>
            </div>
        </div>
        <div class="hdr-right">
            <div class="hdr-user">
                <div class="hdr-avatar"><?= strtoupper(substr($this->session->userdata('admin_name') ?: 'A', 0, 1)) ?></div>
                <div>
                    <div class="hdr-uname"><?= htmlspecialchars($this->session->userdata('admin_name') ?: 'Admin') ?></div>
                    <div class="hdr-urole">Administrator</div>
                </div>
            </div>
        </div>
    </div>
    <!-- /TOP HEADER -->

    <!-- PAGE BODY -->
    <div class="page-body">