<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta name="axis-user-id" content="{{ auth()->id() }}">
    <meta name="axis-company-id" content="{{ auth()->user()->company_id }}">
    <meta name="axis-window-title" content="@yield('title', 'Dashboard')">
    <title>@yield('title', 'Dashboard') — {{ auth()->user()->company->name ?? config('app.name') }}</title>

    <link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css" rel="stylesheet">
    <link href="{{ asset('css/axis-enterprise.css') }}?v={{ filemtime(public_path('css/axis-enterprise.css')) }}" rel="stylesheet">
    @stack('styles')
</head>
<body class="@yield('body-class')">

<div id="sidebar-backdrop" aria-hidden="true"></div>

<!-- ═══════ SIDEBAR ══════════════════════════════════════════════════════ -->
<nav id="sidebar">
    @php $sidebarCompany = auth()->user()->company()->first(); @endphp
    <a class="sidebar-brand" href="{{ route('dashboard') }}">
        <x-company-logo context="sidebar" :company="$sidebarCompany" />
        <span>{{ $sidebarCompany->name }}</span>
    </a>

    <div class="pt-2 pb-3">

        {{-- DASHBOARD --}}
        <a href="{{ route('dashboard') }}" class="nav-item-link {{ request()->routeIs('dashboard') ? 'active':'' }}">
            <i class="bi bi-speedometer2"></i><span>Dashboard</span>
        </a>

        {{-- VOUCHER --}}
        @php $vActive = request()->routeIs('vouchers.*'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupVoucher" aria-expanded="{{ $vActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-receipt"></i><span>Voucher</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $vActive ? 'show':'' }}" id="groupVoucher">
            @foreach([
                ['cash_payment','Cash Payment','bi-cash-stack'],
                ['bank_payment','Bank Payment','bi-bank'],
                ['cash_receipt','Cash Receipt','bi-cash-coin'],
                ['bank_receipt','Bank Receipt','bi-bank2'],
                ['contra','Contra','bi-arrow-left-right'],
                ['journal','Journal','bi-journal-text'],
            ] as [$type,$typeName,$icon])
            <a href="{{ route('vouchers.index', $type) }}"
               class="nav-item-link {{ request()->route('voucherType')===$type ? 'active':'' }}">
                <i class="bi {{ $icon }}"></i><span>{{ $typeName }}</span>
            </a>
            @endforeach
        </div>

        {{-- LIST --}}
        @php
            $usePayrollModule = \App\Models\Setting::get(auth()->user()->company_id, 'use_payroll_module', '0') === '1';
            $useFixedAssetsModule = \App\Models\Setting::get(auth()->user()->company_id, 'use_fixed_assets_module', '0') === '1';
            $useManufacturingModule = \App\Models\Setting::get(auth()->user()->company_id, 'use_manufacturing', '0') === '1';
            $listRoutes = ['coa.*','dimensions.*','masters.other-names.*','masters.currencies.*','masters.tax-codes.*','masters.price-levels.*','masters.opening-balance.*'];
            if (!$usePayrollModule) {
                $listRoutes[] = 'masters.employees.*';
            }
            $listActive = request()->routeIs(...$listRoutes);
            $payrollActive = request()->routeIs('payroll.*') || ($usePayrollModule && request()->routeIs('masters.employees.*'));
            $fixedAssetsActive = request()->routeIs('fixed-assets.*');
        @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupList" aria-expanded="{{ $listActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-card-list"></i><span>List</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $listActive ? 'show':'' }}" id="groupList">
            <a href="{{ route('coa.index') }}"
               class="nav-item-link {{ request()->routeIs('coa.*') ? 'active':'' }}">
                <i class="bi bi-diagram-3"></i><span>Chart of Accounts</span>
            </a>
            <a href="{{ route('dimensions.index') }}"
               class="nav-item-link {{ request()->routeIs('dimensions.*') ? 'active':'' }}">
                <i class="bi bi-layers"></i><span>Dimensions</span>
            </a>
            @if(!$usePayrollModule)
            <a href="{{ route('masters.employees.index') }}" class="nav-item-link {{ request()->routeIs('masters.employees.*') ? 'active':'' }}"><i class="bi bi-people"></i><span>Employees</span></a>
            @endif
            <a href="{{ route('masters.other-names.index') }}" class="nav-item-link {{ request()->routeIs('masters.other-names.*') ? 'active':'' }}"><i class="bi bi-person-lines-fill"></i><span>Other Names</span></a>
            <a href="{{ route('masters.currencies.index') }}"
               class="nav-item-link {{ request()->routeIs('masters.currencies.*') ? 'active':'' }}">
                <i class="bi bi-currency-exchange"></i><span>Currencies</span>
            </a>
            <a href="{{ route('masters.tax-codes.index') }}"
               class="nav-item-link {{ request()->routeIs('masters.tax-codes.*') ? 'active':'' }}">
                <i class="bi bi-percent"></i><span>Tax Codes</span>
            </a>
            <a href="{{ route('masters.price-levels.index') }}"
               class="nav-item-link {{ request()->routeIs('masters.price-levels.*') ? 'active':'' }}">
                <i class="bi bi-tags"></i><span>Price Levels</span>
            </a>
            <a href="{{ route('masters.opening-balance.index', ['tab' => 'accounts']) }}"
               class="nav-item-link {{ request()->routeIs('masters.opening-balance.*') ? 'active':'' }}">
                <i class="bi bi-wallet2"></i><span>Opening Balance</span>
            </a>
        </div>

        {{-- AR --}}
        @php $arActive = request()->routeIs('masters.customers.*'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupAR" aria-expanded="{{ $arActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-arrow-down-circle"></i><span>Accounts Receivable</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $arActive ? 'show':'' }}" id="groupAR">
            <a href="{{ route('masters.customers.index') }}" class="nav-item-link {{ request()->routeIs('masters.customers.*') ? 'active':'' }}"><i class="bi bi-person-badge"></i><span>Customers</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-file-earmark-text"></i><span>Quotations</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-cart-check"></i><span>Sales Orders</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-truck"></i><span>Delivery Notes</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-receipt-cutoff"></i><span>Sales Invoices</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-arrow-return-left"></i><span>Sales Returns</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-cash-stack"></i><span>Cash Sales</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-credit-card"></i><span>Receive Payment</span></a>
        </div>

        {{-- AP --}}
        @php $apActive = request()->routeIs('masters.vendors.*'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupAP" aria-expanded="{{ $apActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-arrow-up-circle"></i><span>Accounts Payable</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $apActive ? 'show':'' }}" id="groupAP">
            <a href="{{ route('masters.vendors.index') }}" class="nav-item-link {{ request()->routeIs('masters.vendors.*') ? 'active':'' }}"><i class="bi bi-shop"></i><span>Vendors</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-clipboard-check"></i><span>Purchase Requisitions</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-bag-check"></i><span>Purchase Orders</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-box-arrow-in-down"></i><span>GRN</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-file-earmark-ruled"></i><span>Bills</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-wallet"></i><span>Bill Payments</span></a>
        </div>

        {{-- INVENTORY --}}
        @php $invActive = request()->routeIs('inventory.*'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupInventory" aria-expanded="{{ $invActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-box-seam"></i><span>Inventory</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $invActive ? 'show':'' }}" id="groupInventory">
            <a href="{{ route('inventory.items.index') }}"
               class="nav-item-link {{ request()->routeIs('inventory.items.*') ? 'active':'' }}">
                <i class="bi bi-box"></i><span>Items</span>
            </a>
            <a href="{{ route('inventory.categories.index') }}"
               class="nav-item-link {{ request()->routeIs('inventory.categories.*') ? 'active':'' }}">
                <i class="bi bi-folder2"></i><span>Item Categories</span>
            </a>
            <a href="{{ route('inventory.warehouses.index') }}"
               class="nav-item-link {{ request()->routeIs('inventory.warehouses.*') ? 'active':'' }}">
                <i class="bi bi-house-door"></i><span>Warehouses</span>
            </a>
            <a href="{{ route('inventory.uoms.index') }}"
               class="nav-item-link {{ request()->routeIs('inventory.uoms.*') ? 'active':'' }}">
                <i class="bi bi-rulers"></i><span>Units of Measure</span>
            </a>
            <a href="#" class="nav-item-link"><i class="bi bi-sliders"></i><span>Stock Adjustment</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-arrow-repeat"></i><span>Warehouse Transfer</span></a>
        </div>

        @if($useManufacturingModule)
        {{-- MANUFACTURING --}}
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupManufacturing" aria-expanded="false" type="button">
            <span class="nav-group-leading"><i class="bi bi-gear-wide-connected"></i><span>Manufacturing</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu" id="groupManufacturing">
            <a href="#" class="nav-item-link"><i class="bi bi-diagram-2"></i><span>Bill of Materials</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-hammer"></i><span>Production Orders</span></a>
        </div>
        @endif

        @if($useFixedAssetsModule && auth()->user()->hasPermission('fixed_assets', 'view'))
        {{-- FIXED ASSETS --}}
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupFixedAssets" aria-expanded="{{ $fixedAssetsActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-buildings"></i><span>Fixed Assets</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $fixedAssetsActive ? 'show' : '' }}" id="groupFixedAssets">
            @include('partials.fixed-assets-menu-items', ['variant' => 'sidebar'])
        </div>
        @endif

        @if($usePayrollModule && (auth()->user()->hasPermission('payroll', 'view') || auth()->user()->hasPermission('masters', 'view')))
        {{-- PAYROLL --}}
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupPayroll" aria-expanded="{{ $payrollActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-wallet2"></i><span>Payroll</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $payrollActive ? 'show' : '' }}" id="groupPayroll">
            @include('partials.payroll-menu-items', ['variant' => 'sidebar'])
        </div>
        @endif

        @if(auth()->user()->hasPermission('banking', 'view'))
        {{-- BANKING --}}
        @php $bankActive = request()->routeIs('banking.*'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupBanking" aria-expanded="{{ $bankActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-bank"></i><span>Banking</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $bankActive ? 'show' : '' }}" id="groupBanking">
            @include('partials.banking-menu-items', ['variant' => 'sidebar'])
        </div>
        @endif

        {{-- COMPANY --}}
        @php
            $companyActive = request()->routeIs('admin.users.*', 'admin.roles.*', 'admin.quickbooks.*', 'admin.company-reset.*', 'admin.budgets.*');
            $qbImportActive = request()->routeIs('admin.quickbooks.*');
            $companyResetActive = request()->routeIs('admin.company-reset.*');
        @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupCompany" aria-expanded="{{ $companyActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-building-gear"></i><span>Company</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $companyActive ? 'show':'' }}" id="groupCompany">
            <a href="{{ route('admin.users.index') }}"
               class="nav-item-link {{ request()->routeIs('admin.users.*') ? 'active':'' }}">
                <i class="bi bi-person-gear"></i><span>Users</span>
            </a>
            <a href="{{ route('admin.roles.index') }}"
               class="nav-item-link {{ request()->routeIs('admin.roles.*') ? 'active':'' }}">
                <i class="bi bi-shield-lock"></i><span>Roles &amp; Permissions</span>
            </a>
            @if(auth()->user()->hasPermission('budget', 'view'))
            <a href="{{ route('admin.budgets.index') }}"
               class="nav-item-link {{ request()->routeIs('admin.budgets.*') ? 'active':'' }}">
                <i class="bi bi-calculator"></i><span>Budgets</span>
            </a>
            @endif
            <button class="nav-item-link nav-submenu-toggle {{ $qbImportActive ? 'active':'' }}"
                    data-bs-toggle="collapse"
                    data-bs-target="#groupQBImport"
                    aria-expanded="{{ $qbImportActive ? 'true' : 'false' }}"
                    type="button">
                <i class="bi bi-cloud-download"></i>
                <span>Import from QuickBooks</span>
                <i class="bi bi-chevron-right arrow"></i>
            </button>
            <div class="collapse nav-sub-submenu {{ $qbImportActive ? 'show':'' }}" id="groupQBImport">
                <a href="{{ route('admin.quickbooks.list-import') }}"
                   class="nav-item-link {{ request()->routeIs('admin.quickbooks.list-import') ? 'active':'' }}">
                    <i class="bi bi-list-check"></i><span>QuickBooks List Import</span>
                </a>
                <a href="{{ route('admin.quickbooks.data-import') }}"
                   class="nav-item-link {{ request()->routeIs('admin.quickbooks.data-import') ? 'active':'' }}">
                    <i class="bi bi-database-down"></i><span>QuickBooks Data Import</span>
                </a>
            </div>
            <hr class="nav-submenu-divider my-1 mx-3 opacity-25">
            <a href="{{ route('admin.company-reset.transactions') }}"
               class="nav-item-link text-danger {{ request()->routeIs('admin.company-reset.transactions*') ? 'active':'' }}">
                <i class="bi bi-trash"></i><span>Delete All Transactions</span>
            </a>
            <a href="{{ route('admin.company-reset.listings') }}"
               class="nav-item-link text-danger {{ request()->routeIs('admin.company-reset.listings*') ? 'active':'' }}">
                <i class="bi bi-trash3"></i><span>Delete all Listings</span>
            </a>
        </div>

        {{-- REPORTS --}}
        @php $reportsActive = request()->routeIs('reports.*'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupReports" aria-expanded="{{ $reportsActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-bar-chart-line"></i><span>Reports</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $reportsActive ? 'show':'' }}" id="groupReports">
            <a href="{{ route('reports.profit-loss') }}" class="nav-item-link {{ request()->routeIs('reports.profit-loss') ? 'active':'' }}"><i class="bi bi-graph-up-arrow"></i><span>Profit &amp; Loss</span></a>
            <a href="{{ route('reports.balance-sheet') }}" class="nav-item-link {{ request()->routeIs('reports.balance-sheet') ? 'active':'' }}"><i class="bi bi-clipboard-data"></i><span>Balance Sheet</span></a>
            <a href="{{ route('reports.journal') }}" class="nav-item-link {{ request()->routeIs('reports.journal') ? 'active':'' }}"><i class="bi bi-journal-bookmark"></i><span>Journal Report</span></a>
            <a href="{{ route('reports.trial-balance') }}" class="nav-item-link {{ request()->routeIs('reports.trial-balance') ? 'active':'' }}"><i class="bi bi-layout-three-columns"></i><span>Trial Balance</span></a>
            <a href="{{ route('reports.general-ledger') }}" class="nav-item-link {{ request()->routeIs('reports.general-ledger') ? 'active':'' }}"><i class="bi bi-book"></i><span>General Ledger</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-hourglass-split"></i><span>Customer Aging</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-hourglass"></i><span>Vendor Aging</span></a>
            <a href="#" class="nav-item-link"><i class="bi bi-boxes"></i><span>Stock Summary</span></a>
        </div>

        {{-- SETTINGS — Phase 2 routes wired --}}
        @php $adminActive = request()->routeIs('settings.*','subscription','admin.fiscal-years.*','admin.voucher-numbering.*','admin.audit-trail','settings.inventory','settings.arap'); @endphp
        <button class="nav-group-label nav-group-toggle" data-bs-toggle="collapse" data-bs-target="#groupAdmin" aria-expanded="{{ $adminActive ? 'true' : 'false' }}" type="button">
            <span class="nav-group-leading"><i class="bi bi-gear"></i><span>Settings</span></span>
            <i class="bi bi-chevron-right group-arrow"></i>
        </button>
        <div class="collapse nav-submenu {{ $adminActive ? 'show':'' }}" id="groupAdmin">
            <a href="{{ route('settings.general') }}"
               class="nav-item-link {{ request()->routeIs('settings.*') ? 'active':'' }}">
                <i class="bi bi-sliders"></i><span>Company Settings</span>
            </a>
            @if(auth()->user()->is_admin)
            <a href="{{ route('subscription') }}"
               class="nav-item-link {{ request()->routeIs('subscription') ? 'active':'' }}">
                <i class="bi bi-credit-card"></i><span>Subscription</span>
            </a>
            @endif
            <a href="{{ route('admin.fiscal-years.index') }}"
               class="nav-item-link {{ request()->routeIs('admin.fiscal-years.*') ? 'active':'' }}">
                <i class="bi bi-calendar-check"></i><span>Period Closing</span>
            </a>
            <a href="{{ route('admin.voucher-numbering.index') }}"
               class="nav-item-link {{ request()->routeIs('admin.voucher-numbering.*') ? 'active':'' }}">
                <i class="bi bi-hash"></i><span>Voucher Numbering</span>
            </a>
            <a href="{{ route('admin.audit-trail') }}"
               class="nav-item-link {{ request()->routeIs('admin.audit-trail') ? 'active':'' }}">
                <i class="bi bi-clock-history"></i><span>Audit Trail</span>
            </a>
        </div>

    </div>
</nav>

<!-- ═══════ TOPBAR ════════════════════════════════════════════════════════ -->
<header id="topbar">
    <button id="sidebarToggle" title="Toggle Sidebar"><i class="bi bi-list"></i></button>

    <a href="{{ route('dashboard') }}" class="topbar-brand-logo-link topbar-brand-logo-link--mobile d-inline-flex d-md-none" title="{{ config('app.name') }}">
        <x-app-logo context="topbar" />
    </a>

    <div class="topbar-center d-none d-md-flex">
        @include('partials.open-windows')
    </div>

    <div class="topbar-right">
        <a href="{{ route('dashboard') }}" class="topbar-brand-logo-link topbar-brand-logo-link--desktop d-none d-md-inline-flex" title="{{ config('app.name') }}">
            <x-app-logo context="topbar" />
        </a>
        @include('partials.topbar-user-menu')
    </div>
</header>

@include('partials.top-nav-menu')
@include('partials.user-profile-modal')

<!-- ═══════ MAIN CONTENT ═════════════════════════════════════════════════ -->
<main id="main">
    <div class="page-content">

        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="bi bi-check-circle me-2"></i>{{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif
        @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-triangle me-2"></i>{{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif
        @if($errors->any() && !$errors->has('company_code') && !$errors->has('username'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="bi bi-exclamation-triangle me-2"></i>
                <strong>Please fix the errors below:</strong>
                <ul class="mb-0 mt-1 ps-3">
                    @foreach($errors->all() as $e)
                        <li style="font-size:13px">{{ $e }}</li>
                    @endforeach
                </ul>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        @endif

        @yield('content')
    </div>
</main>

@stack('modals')

<!-- ═══════ SCRIPTS — jQuery for form pages; other libs lazy-loaded ════════ -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" defer></script>
<script src="{{ asset('js/axis-core.js') }}?v={{ filemtime(public_path('js/axis-core.js')) }}" defer></script>
@stack('scripts')
</body>
</html>
