<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" dir="ltr">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Dashboard') · {{ config('app.name') }}</title>
    <link rel="stylesheet" href="{{ asset('assets/app.css') }}">
</head>
<body>
<div class="app-shell">
    <aside class="sidebar">
        <div class="brand">
            <div class="brand-mark">TA</div>
            <div><strong>{{ auth()->user()->company?->trade_name ?: config('app.name') }}</strong><small>Transport Accounting</small></div>
        </div>
        <a class="nav-link {{ request()->routeIs('dashboard') ? 'active' : '' }}" href="{{ route('dashboard') }}">▦ Dashboard</a>

        <div class="nav-group">Operations</div>
        @if(auth()->user()->hasPermission('transport_jobs.view'))<a class="nav-link {{ request()->routeIs('jobs.*') ? 'active' : '' }}" href="{{ route('jobs.index') }}">▣ Transport Jobs</a>@endif
        @if(auth()->user()->hasPermission('trips.view'))<a class="nav-link {{ request()->routeIs('trips.*') ? 'active' : '' }}" href="{{ route('trips.index') }}">↝ Trips & Dispatch</a>@endif
        @foreach(['service-types'=>'Service Types','routes'=>'Routes','vehicles'=>'Vehicles','drivers'=>'Drivers','maintenance'=>'Maintenance','fuel'=>'Fuel Entries'] as $key=>$label)
            @php($moduleConfig=config("erp.modules.$key"))
            @if($moduleConfig && auth()->user()->hasPermission($moduleConfig['permission'].'.view'))<a class="nav-link {{ request()->is("masters/$key*") ? 'active' : '' }}" href="{{ route('masters.index',$key) }}">• {{ $label }}</a>@endif
        @endforeach

        <div class="nav-group">Sales & Receivables</div>
        @if(auth()->user()->hasPermission('parties.view'))<a class="nav-link {{ request()->routeIs('parties.*') ? 'active' : '' }}" href="{{ route('parties.index') }}">◎ Parties</a>@endif
        @if(auth()->user()->hasPermission('quotations.view'))<a class="nav-link {{ request()->routeIs('quotations.*') ? 'active' : '' }}" href="{{ route('quotations.index') }}">◇ Quotations</a>@endif
        @if(auth()->user()->hasPermission('customer_invoices.view'))<a class="nav-link {{ request()->routeIs('invoices.*') ? 'active' : '' }}" href="{{ route('invoices.index') }}">▤ Customer Invoices</a>@endif
        @if(auth()->user()->hasPermission('receipts.view'))<a class="nav-link {{ request()->routeIs('receipts.*') ? 'active' : '' }}" href="{{ route('receipts.index') }}">↓ Receipts</a>@endif
        @if(auth()->user()->hasPermission('adjustment_notes.view'))<a class="nav-link {{ request()->routeIs('notes.*') ? 'active' : '' }}" href="{{ route('notes.index') }}">± Credit / Debit Notes</a>@endif

        <div class="nav-group">Purchases & Payments</div>
        @if(auth()->user()->hasPermission('vendor_bills.view'))<a class="nav-link {{ request()->routeIs('bills.*') ? 'active' : '' }}" href="{{ route('bills.index') }}">▥ Vendor Bills</a>@endif
        @if(auth()->user()->hasPermission('payments.view'))<a class="nav-link {{ request()->routeIs('payments.*') ? 'active' : '' }}" href="{{ route('payments.index') }}">↑ Payment Vouchers</a>@endif
        @if(auth()->user()->hasPermission('expenses.view'))<a class="nav-link {{ request()->routeIs('expenses.*') ? 'active' : '' }}" href="{{ route('expenses.index') }}">◈ Expenses</a>@endif
        @if(auth()->user()->hasPermission('driver_advances.view'))<a class="nav-link {{ request()->routeIs('driver-advances.*') ? 'active' : '' }}" href="{{ route('driver-advances.index') }}">⇣ Driver Advances</a>@endif
        @if(auth()->user()->hasPermission('contra_settlements.view'))<a class="nav-link {{ request()->routeIs('contra.*') ? 'active' : '' }}" href="{{ route('contra.index') }}">⇄ Contra Settlements</a>@endif

        <div class="nav-group">Contracts & Banking</div>
        @foreach(['contracts'=>'Contracts','rate-cards'=>'Rate Cards','bank-accounts'=>'Bank Accounts'] as $key=>$label)
            @php($moduleConfig=config("erp.modules.$key"))
            @if($moduleConfig && auth()->user()->hasPermission($moduleConfig['permission'].'.view'))<a class="nav-link {{ request()->is("masters/$key*") ? 'active' : '' }}" href="{{ route('masters.index',$key) }}">• {{ $label }}</a>@endif
        @endforeach
        @if(auth()->user()->hasPermission('bank_reconciliation.view'))<a class="nav-link {{ request()->routeIs('bank.*') ? 'active' : '' }}" href="{{ route('bank.index') }}">≋ Bank Reconciliation</a>@endif

        <div class="nav-group">Accounting & Reports</div>
        @if(auth()->user()->hasPermission('journals.view'))<a class="nav-link {{ request()->routeIs('journals.*') ? 'active' : '' }}" href="{{ route('journals.index') }}">▧ Journal Entries</a>@endif
        @if(auth()->user()->hasPermission('reports.view'))<a class="nav-link {{ request()->routeIs('reports.*') ? 'active' : '' }}" href="{{ route('reports.index') }}">▥ Reports</a>@endif
        @foreach(['accounts'=>'Chart of Accounts','tax-codes'=>'VAT Codes','branches'=>'Branches'] as $key=>$label)
            @php($moduleConfig=config("erp.modules.$key"))
            @if($moduleConfig && auth()->user()->hasPermission($moduleConfig['permission'].'.view'))<a class="nav-link {{ request()->is("masters/$key*") ? 'active' : '' }}" href="{{ route('masters.index',$key) }}">• {{ $label }}</a>@endif
        @endforeach

        <div class="nav-group">Administration</div>
        @if(auth()->user()->hasPermission('users.view'))<a class="nav-link {{ request()->routeIs('users.*') ? 'active' : '' }}" href="{{ route('users.index') }}">♙ Users & Roles</a>@endif
        @if(auth()->user()->hasPermission('company_settings.view'))<a class="nav-link {{ request()->routeIs('settings.*') ? 'active' : '' }}" href="{{ route('settings.edit') }}">⚙ Company Settings</a>@endif
        @if(auth()->user()->hasPermission('periods.view'))<a class="nav-link {{ request()->routeIs('periods.*') ? 'active' : '' }}" href="{{ route('periods.index') }}">▦ Fiscal Periods</a>@endif
        @if(auth()->user()->hasPermission('audit_logs.view'))<a class="nav-link {{ request()->routeIs('audit.*') ? 'active' : '' }}" href="{{ route('audit.index') }}">◷ Audit Trail</a>@endif
    </aside>
    <main class="main">
        <header class="topbar">
            <div style="display:flex;align-items:center;gap:12px"><button class="btn btn-secondary mobile-toggle" data-sidebar-toggle>☰</button><div class="topbar-title">@yield('page_title', 'Dashboard')</div></div>
            <div class="user-menu">
                <div class="user-meta text-right"><strong>{{ auth()->user()->name }}</strong><div class="muted" style="font-size:11px">{{ auth()->user()->branch?->name ?? 'All branches' }}</div></div>
                <div class="avatar">{{ strtoupper(substr(auth()->user()->name,0,1)) }}</div>
                <form method="post" action="{{ route('logout') }}">@csrf<button class="btn btn-secondary btn-sm">Logout</button></form>
            </div>
        </header>
        <section class="content">
            @if(session('success'))<div class="alert alert-success" data-auto-hide>{{ session('success') }}</div>@endif
            @if(session('error'))<div class="alert alert-error">{{ session('error') }}</div>@endif
            @if(session('warning'))<div class="alert alert-warning">{{ session('warning') }}</div>@endif
            @if($errors->any())<div class="alert alert-error"><strong>Please correct the following:</strong><ul>@foreach($errors->all() as $error)<li>{{ $error }}</li>@endforeach</ul></div>@endif
            @yield('content')
        </section>
    </main>
</div>
<script src="{{ asset('assets/app.js') }}"></script>
@stack('scripts')
</body>
</html>
