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

<?php $__template->section('title'); ?>ميزان المراجعة<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<!-- Filters -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/reports/trial-balance" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">من تاريخ</label>
                <input type="date" name="date_from" class="form-input" value="<?= e($date_from) ?>">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
                <input type="date" name="date_to" class="form-input" value="<?= e($date_to) ?>">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">مركز التكلفة</label>
                <select name="cost_center_id" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($cost_centers as $cc): ?>
                        <option value="<?= (int)$cc['id'] ?>" <?= (int)($filters['cost_center_id'] ?? 0) === (int)$cc['id'] ? 'selected' : '' ?>><?= e($cc['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الفرع</label>
                <select name="branch_id" class="form-select">
                    <option value="">الكل</option>
                    <?php foreach ($branches as $b): ?>
                        <option value="<?= (int)$b['id'] ?>" <?= (int)($filters['branch_id'] ?? 0) === (int)$b['id'] ? 'selected' : '' ?>><?= e($b['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">عرض</button>
        </form>
    </div>
</div>

<!-- Report -->
<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;">ميزان المراجعة</h3>
        <span style="font-size:13px;color:#6B7280;">من <?= e($date_from) ?> إلى <?= e($date_to) ?></span>
    </div>
    <div class="table-responsive">
        <table class="data-table" style="width:100%;">
            <thead>
                <tr style="background:#0D7377;color:white;">
                    <th style="padding:12px;">كود الحساب</th>
                    <th style="padding:12px;">اسم الحساب</th>
                    <th style="padding:12px;">النوع</th>
                    <th style="padding:12px;text-align:center;">مدين</th>
                    <th style="padding:12px;text-align:center;">دائن</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($result['accounts'] as $acc): ?>
                    <?php
                    $typeLabel = match($acc['account_type']) {
                        'asset' => 'أصول',
                        'liability' => 'خصوم',
                        'equity' => 'حقوق ملكية',
                        'revenue' => 'إيرادات',
                        'expense' => 'مصروفات',
                        default => '',
                    };
                    ?>
                    <tr>
                        <td style="direction:ltr;text-align:right;font-weight:600;font-size:13px;"><?= e($acc['account_code']) ?></td>
                        <td><?= e($acc['name_ar']) ?></td>
                        <td style="font-size:12px;color:#6B7280;"><?= $typeLabel ?></td>
                        <td style="direction:ltr;text-align:center;<?= bccomp($acc['tb_debit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['tb_debit']) ?></td>
                        <td style="direction:ltr;text-align:center;<?= bccomp($acc['tb_credit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['tb_credit']) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="font-weight:700;font-size:16px;background:#F9FAFB;">
                    <td style="padding:15px;" colspan="3">الإجمالي</td>
                    <td style="direction:ltr;text-align:center;padding:15px;"><?= money($result['total_debit']) ?></td>
                    <td style="direction:ltr;text-align:center;padding:15px;"><?= money($result['total_credit']) ?></td>
                </tr>
                <tr>
                    <td colspan="5" style="text-align:center;padding:10px;">
                        <?php if ($result['is_balanced']): ?>
                            <span style="color:#059669;font-weight:700;">الميزان متوازن</span>
                        <?php else: ?>
                            <span style="color:#DC2626;font-weight:700;">تحذير: الميزان غير متوازن — الفرق: <?= money(bcsub($result['total_debit'], $result['total_credit'], 2)) ?></span>
                        <?php endif; ?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
