{{--
# ────────────── lee_gwena ──────────────
# Code by: ashley_gwena
# Created: 2026-03-12
# Notes: GL accounts index with hierarchy
# ───────────────────────────────────────
--}}
@extends('layouts.master')

@section('title', 'GL Accounts')

@section('content')
<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=Cormorant+Garamond:wght@400;600;700&family=DM+Sans:wght@300;400;500;600&family=DM+Mono:wght@400&display=swap" rel="stylesheet">

<style>
:root{--sky:#0ea5e9;--sky-deep:#0369a1;--sky-light:#38bdf8;--sky-ghost:rgba(14,165,233,.07);--sky-border:rgba(14,165,233,.2);--ink:#0f172a;--ink-mid:#475569;--ink-soft:#94a3b8;--white:#fff;--off:#f7fbff;--surface:#eef6fd;--border:#daeaf8;--amber:#f59e0b;--amber-bg:rgba(245,158,11,.08);--amber-border:rgba(245,158,11,.22);--danger:#ef4444;--danger-bg:rgba(239,68,68,.08);--danger-border:rgba(239,68,68,.2);--fd:'Cormorant Garamond',Georgia,serif;--fb:'DM Sans',system-ui,sans-serif;--fm:'DM Mono',monospace}
.main-content{background:var(--off);font-family:var(--fb)}
.mt-page{max-width:1200px;margin:0 auto;padding:40px 24px 64px}
.mt-hero{background:linear-gradient(135deg,var(--sky-deep),var(--sky));border-radius:22px;padding:28px 34px;color:#fff;box-shadow:0 10px 28px rgba(14,165,233,.2);display:flex;justify-content:space-between;gap:20px;align-items:center}
.mt-hero h1{margin:0;font-family:var(--fd);font-size:36px}
.mt-hero p{margin:6px 0 0;font-size:13px;color:rgba(255,255,255,.85)}
.mt-badge{background:rgba(255,255,255,.18);border:1px solid rgba(255,255,255,.35);border-radius:16px;padding:12px 18px;text-align:center;min-width:140px}
.mt-badge .val{font-family:var(--fd);font-size:28px;font-weight:700;line-height:1}
.mt-badge .lab{font-size:11px;text-transform:uppercase;letter-spacing:.08em}
.mt-toolbar{display:flex;align-items:center;gap:12px;margin:20px 0;flex-wrap:wrap}
.mt-btn{display:inline-flex;align-items:center;gap:7px;padding:10px 18px;border-radius:10px;border:1px solid var(--sky-border);text-decoration:none;font-size:13px;font-weight:600;background:#fff;color:var(--sky-deep)}
.mt-btn.primary{background:linear-gradient(135deg,var(--sky-deep),var(--sky));color:#fff;border-color:var(--sky-deep)}
.mt-search{margin-left:0;position:relative;flex:1;max-width:320px}
.mt-search input{width:280px;max-width:100%;padding:10px 14px 10px 34px;border:1px solid var(--border);border-radius:10px}
.mt-search .ico{position:absolute;left:12px;top:8px;color:var(--ink-soft)}
.mt-actions-right{margin-left:auto;display:flex;gap:10px;align-items:center}
.mt-filters{display:grid;grid-template-columns:repeat(auto-fit,minmax(180px,1fr));gap:12px;background:#fff;border:1px solid var(--border);border-radius:16px;padding:16px;box-shadow:0 4px 16px rgba(14,165,233,.08)}
.mt-filters label{font-size:12px;color:var(--ink-soft);text-transform:uppercase;letter-spacing:.08em;margin-bottom:6px;display:block}
.mt-filters select,.mt-filters input{width:100%;padding:10px 12px;border:1px solid var(--border);border-radius:10px;font-size:13px}
.mt-table{margin-top:18px;background:#fff;border:1px solid var(--border);border-radius:18px;overflow:hidden;box-shadow:0 6px 20px rgba(14,165,233,.08)}
.mt-head,.mt-row{display:grid;grid-template-columns:1.1fr 1.8fr 1.2fr 1.2fr 0.9fr 0.9fr 1.3fr 1.2fr;gap:12px;align-items:center;padding:14px 18px}
.mt-head{background:var(--surface);font-size:10px;font-weight:700;color:var(--ink-soft);text-transform:uppercase;letter-spacing:.1em}
.mt-row{border-top:1px solid var(--border);font-size:13px;color:var(--ink-mid)}
.mt-row:hover{background:var(--sky-ghost)}
.mt-title{font-weight:700;color:var(--ink)}
.mt-pill{display:inline-flex;align-items:center;gap:6px;padding:4px 10px;border-radius:999px;border:1px solid var(--sky-border);background:var(--sky-ghost);font-size:12px;color:var(--sky-deep)}
.mt-parent{background:#f8fbff}
.mt-subtoggle{padding:6px 10px;border-radius:8px;border:1px solid var(--sky-border);background:var(--sky-ghost);font-size:12px;font-weight:600;color:var(--sky-deep);text-decoration:none;display:inline-flex;align-items:center;gap:6px}
.mt-subtoggle.secondary{background:#fff;border-color:var(--border);color:var(--ink-mid)}
.mt-actions{display:flex;gap:6px;flex-wrap:wrap}
.mt-actions a,.mt-actions button{padding:6px 10px;border-radius:8px;text-decoration:none;font-size:12px;font-weight:600;border:1px solid var(--border);color:var(--ink-mid);background:#fff}
.mt-actions a.view{border-color:var(--sky-border);color:var(--sky-deep);background:var(--sky-ghost)}
.mt-actions a.edit{border-color:var(--amber-border);color:#92400e;background:var(--amber-bg)}
.mt-actions button.delete{border-color:var(--danger-border);color:#991b1b;background:var(--danger-bg)}
.mt-empty{padding:40px;text-align:center;color:var(--ink-soft)}
@media (max-width: 1024px){.mt-head,.mt-row{grid-template-columns:1fr 1fr 1fr;}.mt-head div:nth-child(n+4),.mt-row div:nth-child(n+4){display:none}}
@media (max-width: 768px){.mt-page{padding:24px 14px 40px}.mt-hero{flex-direction:column;align-items:flex-start}.mt-search{margin-left:0;width:100%}.mt-search input{width:100%}}
</style>

<div class="mt-page">
    <nav class="mt-bc" style="font-size:11px;text-transform:uppercase;letter-spacing:.08em;color:var(--ink-soft);margin-bottom:14px;">
        <a href="{{ route('modules.edit') }}" style="color:var(--ink-soft);text-decoration:none;">Edit Menu</a>
        <span>›</span>
        <a href="{{ route('modules.edit.chart-of-accounts') }}" style="color:var(--ink-soft);text-decoration:none;">Chart of Accounts</a>
        <span>›</span>
        <span style="color:var(--sky-deep);">GL Accounts</span>
    </nav>

    <div class="mt-hero">
        <div>
            <h1>GL Accounts</h1>
            <p>Hierarchical GL accounts with business flags and search.</p>
        </div>
        <div class="mt-badge">
            <div class="val">{{ isset($accounts) ? count($accounts) : 0 }}</div>
            <div class="lab">Records</div>
        </div>
    </div>
    @if (session('success'))
        <div style="margin-top:16px;padding:12px 16px;border-radius:12px;background:#dcfce7;color:#166534;border:1px solid rgba(22,163,74,.25);">
            {{ session('success') }}
        </div>
    @endif
    @if ($errors->any())
        <div style="margin-top:16px;padding:12px 16px;border-radius:12px;background:#fee2e2;color:#991b1b;border:1px solid rgba(153,27,27,.2);">
            @foreach ($errors->all() as $error)
                <div>{{ $error }}</div>
            @endforeach
        </div>
    @endif
    <form method="GET" action="{{ route('gl-accounts.index') }}">
    <div class="mt-toolbar">
        <a href="{{ route('modules.edit.chart-of-accounts') }}" class="mt-btn">← COA Home</a>
        <div class="mt-search">
            <span class="ico">🔍</span>
            <input name="search" type="search" value="{{ request('search') }}" placeholder="Search code or name">
        </div>
        <div class="mt-actions-right">
            <a href="{{ route('gl-accounts.create') }}" class="mt-btn primary">+ Add GL Account</a>
        </div>
    </div>

    <div class="mt-filters">
        <div>
            <label>Account Type</label>
            <select name="account_type_id">
                <option value="">All Types</option>
                @foreach($accountTypes as $type)
                    <option value="{{ $type->id }}" {{ request('account_type_id') == $type->id ? 'selected' : '' }}>{{ $type->name }}</option>
                @endforeach
            </select>
        </div>
        <div>
            <label>Active Status</label>
            <select name="active_status">
                <option value="">All</option>
                <option value="1" {{ request('active_status') === '1' ? 'selected' : '' }}>Active</option>
                <option value="0" {{ request('active_status') === '0' ? 'selected' : '' }}>Inactive</option>
            </select>
        </div>
        <div>
            <label>Financial Category</label>
            <select name="financial_category">
                <option value="">All</option>
                <option value="income_statement" {{ request('financial_category') === 'income_statement' ? 'selected' : '' }}>Income Statement</option>
                <option value="balance_sheet" {{ request('financial_category') === 'balance_sheet' ? 'selected' : '' }}>Balance Sheet</option>
            </select>
        </div>
        <div>
            <label>Trust/Business Flag</label>
            <select name="account_flag">
                <option value="">All</option>
                <option value="trust" {{ request('account_flag') === 'trust' ? 'selected' : '' }}>Trust</option>
                <option value="business" {{ request('account_flag') === 'business' ? 'selected' : '' }}>Business</option>
            </select>
        </div>
        <div style="display:flex;gap:8px;align-items:flex-end;">
            <button type="submit" class="mt-btn primary">Apply Filters</button>
            <a href="{{ route('gl-accounts.index') }}" class="mt-btn">Reset</a>
        </div>
    </div>
    </form>

    <div class="mt-table">
        <div class="mt-head">
            <div>Code</div>
            <div>Name</div>
            <div>Type</div>
            <div>Financial Category</div>
            <div>Trust</div>
            <div>Business</div>
            <div>Subaccounts</div>
            <div>Actions</div>
        </div>

        @forelse($accounts as $account)
            @php
                $hasChildren = !empty($childCounts[$account->id] ?? 0);
                $isParent = $hasChildren;
            @endphp
            <div class="mt-row {{ $isParent ? 'mt-parent' : '' }}" data-search="{{ strtolower(($account->account_code ?? '') . ' ' . ($account->account_name ?? '')) }}">
                <div class="mt-title">{{ $account->account_code }}</div>
                <div>{{ $account->account_name }}</div>
                <div><span class="mt-pill">{{ $account->accountType?->name ?? '—' }}</span></div>
                <div>{{ $account->financial_category ? ucwords(str_replace('_', ' ', $account->financial_category)) : '—' }}</div>
                <div>{{ $account->is_trust_account ? 'Yes' : 'No' }}</div>
                <div>{{ $account->is_business ? 'Yes' : 'No' }}</div>
                <div>
                    <a class="mt-subtoggle {{ $hasChildren ? '' : 'secondary' }}" href="{{ route('subaccounts.index', $account) }}">
                        Subaccounts →
                    </a>
                </div>
                <div class="mt-actions">
                    <a class="view" href="{{ route('gl-accounts.show', $account) }}">View</a>
                    <a class="edit" href="{{ route('gl-accounts.edit', $account) }}">Edit</a>
                    <form method="POST" action="{{ route('gl-accounts.destroy', $account) }}" onsubmit="return confirm('Delete this GL account?');">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="delete">Delete</button>
                    </form>
                </div>
            </div>
        @empty
        <div class="mt-empty">No accounts found.</div>
        @endforelse
    </div>
</div>
@endsection
