<?php $__template->layout('Layout.main'); ?>

<?php $__template->section('title'); ?>دليل الحسابات<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<a href="/accounting/chart-of-accounts/create" class="btn btn-primary">+ حساب جديد</a>
<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">دليل الحسابات (شجرة الحسابات)</h3>
    </div>
    <div style="padding:15px 20px;">
        <?php if (empty($tree)): ?>
            <p style="color:#6B7280;text-align:center;padding:40px 0;">لا توجد حسابات — <a href="/accounting/chart-of-accounts/create">أضف حساباً جديداً</a></p>
        <?php else: ?>
            <?php
            function renderAccountTree(array $accounts, int $depth = 0): void {
                foreach ($accounts as $acc) {
                    $indent = $depth * 25;
                    $isHeader = (int)($acc['is_header'] ?? 0);
                    $weight = $isHeader ? '700' : '400';
                    $color = match($acc['account_type']) {
                        'asset' => '#0D7377',
                        'liability' => '#DC2626',
                        'equity' => '#7C3AED',
                        'revenue' => '#059669',
                        'expense' => '#F59E0B',
                        default => '#374151',
                    };
                    $typeLabel = match($acc['account_type']) {
                        'asset' => 'أصول',
                        'liability' => 'خصوم',
                        'equity' => 'حقوق ملكية',
                        'revenue' => 'إيرادات',
                        'expense' => 'مصروفات',
                        default => '',
                    };
                    ?>
                    <div style="display:flex;align-items:center;padding:8px 0;border-bottom:1px solid #F3F4F6;margin-right:<?= $indent ?>px;">
                        <span style="width:80px;direction:ltr;text-align:right;color:<?= $color ?>;font-weight:600;font-size:13px;"><?= e($acc['account_code']) ?></span>
                        <span style="flex:1;margin:0 15px;font-weight:<?= $weight ?>;"><?= e($acc['name_ar']) ?></span>
                        <span style="width:80px;font-size:12px;color:#6B7280;"><?= $typeLabel ?></span>
                        <span style="width:100px;direction:ltr;text-align:left;font-size:13px;"><?= !$isHeader ? money($acc['current_balance'] ?? '0.00') : '' ?></span>
                        <a href="/accounting/chart-of-accounts/<?= (int)$acc['id'] ?>/edit" style="font-size:12px;color:#0D7377;">تعديل</a>
                    </div>
                    <?php
                    if (!empty($acc['children'])) {
                        renderAccountTree($acc['children'], $depth + 1);
                    }
                }
            }
            renderAccountTree($tree);
            ?>
        <?php endif; ?>
    </div>
</div>
<?php $__template->endSection(); ?>
