<?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/balance-sheet" 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="as_of_date" class="form-input" value="<?= e($as_of_date) ?>">
            </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>
            <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['code'] . ' — ' . $cc['name_ar']) ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">عرض</button>
            <a href="/accounting/reports/balance-sheet/export-csv?as_of_date=<?= e($as_of_date) ?>&branch_id=<?= (int)($filters['branch_id'] ?? 0) ?>&cost_center_id=<?= (int)($filters['cost_center_id'] ?? 0) ?>" class="btn btn-secondary" style="margin-right:5px;">تصدير CSV</a>
        </form>
    </div>
</div>

<div class="card">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;text-align:center;">
        <h2 style="margin:0;">الميزانية العمومية</h2>
        <p style="color:#6B7280;margin:5px 0 0;">حتى تاريخ <?= e($as_of_date) ?></p>
    </div>
    <div style="padding:20px;">
        <div style="display:grid;grid-template-columns:1fr 1fr;gap:30px;">
            <!-- Assets -->
            <div>
                <h3 style="color:#1e3a5f;border-bottom:2px solid #1e3a5f;padding-bottom:8px;">الأصول</h3>
                <table style="width:100%;">
                    <?php foreach ($result['assets'] as $a): ?>
                        <tr>
                            <td style="padding:5px 0;font-size:13px;color:#6B7280;width:15%;direction:ltr;text-align:right;"><?= e($a['account_code']) ?></td>
                            <td style="padding:5px 0;"><?= e($a['name_ar']) ?></td>
                            <td style="padding:5px 0;direction:ltr;text-align:left;width:25%;"><?= money($a['balance']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <tr style="font-weight:700;border-top:3px double #1e3a5f;">
                        <td style="padding:12px 0;" colspan="2">إجمالي الأصول</td>
                        <td style="padding:12px 0;direction:ltr;text-align:left;color:#1e3a5f;font-size:20px;"><?= money($result['total_assets']) ?></td>
                    </tr>
                </table>
            </div>

            <!-- Liabilities + Equity -->
            <div>
                <h3 style="color:#DC2626;border-bottom:2px solid #DC2626;padding-bottom:8px;">الخصوم</h3>
                <table style="width:100%;">
                    <?php foreach ($result['liabilities'] as $l): ?>
                        <tr>
                            <td style="padding:5px 0;font-size:13px;color:#6B7280;width:15%;direction:ltr;text-align:right;"><?= e($l['account_code']) ?></td>
                            <td style="padding:5px 0;"><?= e($l['name_ar']) ?></td>
                            <td style="padding:5px 0;direction:ltr;text-align:left;width:25%;"><?= money($l['balance']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <tr style="font-weight:600;border-top:1px solid #DC2626;">
                        <td style="padding:8px 0;" colspan="2">إجمالي الخصوم</td>
                        <td style="padding:8px 0;direction:ltr;text-align:left;"><?= money($result['total_liabilities']) ?></td>
                    </tr>
                </table>

                <h3 style="color:#7C3AED;border-bottom:2px solid #7C3AED;padding-bottom:8px;margin-top:20px;">حقوق الملكية</h3>
                <table style="width:100%;">
                    <?php foreach ($result['equity'] as $eq): ?>
                        <tr>
                            <td style="padding:5px 0;font-size:13px;color:#6B7280;width:15%;direction:ltr;text-align:right;"><?= e($eq['account_code']) ?></td>
                            <td style="padding:5px 0;"><?= e($eq['name_ar']) ?></td>
                            <td style="padding:5px 0;direction:ltr;text-align:left;width:25%;"><?= money($eq['balance']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <tr style="font-weight:600;border-top:1px solid #7C3AED;">
                        <td style="padding:8px 0;" colspan="2">إجمالي حقوق الملكية</td>
                        <td style="padding:8px 0;direction:ltr;text-align:left;"><?= money($result['total_equity']) ?></td>
                    </tr>
                </table>

                <div style="font-weight:700;border-top:3px double #374151;padding:12px 0;margin-top:10px;display:flex;justify-content:space-between;">
                    <span>إجمالي الخصوم + حقوق الملكية</span>
                    <span style="direction:ltr;font-size:20px;"><?= money($result['total_liabilities_and_equity']) ?></span>
                </div>
            </div>
        </div>

        <!-- Balance Check -->
        <div style="margin-top:20px;text-align:center;padding:15px;border-radius:8px;background:<?= $result['is_balanced'] ? '#F0FDF4' : '#FEF2F2' ?>;">
            <?php if ($result['is_balanced']): ?>
                <span style="color:#059669;font-weight:700;font-size:16px;">الميزانية متوازنة (الأصول = الخصوم + حقوق الملكية)</span>
            <?php else: ?>
                <span style="color:#DC2626;font-weight:700;font-size:16px;">تحذير: الميزانية غير متوازنة — فرق: <?= money(bcsub($result['total_assets'], $result['total_liabilities_and_equity'], 2)) ?></span>
            <?php endif; ?>
        </div>
    </div>
</div>
<?php $__template->endSection(); ?>
