<?php
require_once 'db/config.php';
require_once 'includes/header.php';
require_once 'includes/accounting_functions.php';

// Check permission
if (!canView('accounting')) {
    echo "<div class='container mt-4' dir='rtl'>لا تملك صلاحية الوصول لهذه الصفحة.</div>";
    require_once 'includes/footer.php';
    exit;
}

$trial_balance = get_trial_balance();
?>

<div class="container mt-4" dir="rtl">
    <h2 class="text-right">ميزان المراجعة (Trial Balance)</h2>
    
    <div class="card">
        <div class="card-body">
            <table class="table table-bordered text-right">
                <thead><tr><th>الحساب</th><th>مدين</th><th>دائن</th></tr></thead>
                <tbody>
                    <?php 
                    $total_debit = 0;
                    $total_credit = 0;
                    foreach ($trial_balance as $row): 
                        $total_debit += $row['total_debit'];
                        $total_credit += $row['total_credit'];
                    ?>
                    <tr>
                        <td><?= htmlspecialchars($row['account_name']) ?></td>
                        <td><?= number_format($row['total_debit'], 2) ?></td>
                        <td><?= number_format($row['total_credit'], 2) ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr class="fw-bold bg-light">
                        <td>الإجمالي</td>
                        <td><?= number_format($total_debit, 2) ?></td>
                        <td><?= number_format($total_credit, 2) ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
</div>

<?php require_once 'includes/footer.php'; ?>
