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

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

<?php $__template->section('page_actions'); ?>
<a href="/accounting/reports/trial-balance/export-csv?date_from=<?= e($date_from) ?>&date_to=<?= e($date_to) ?>&cost_center_id=<?= (int)($filters['cost_center_id'] ?? 0) ?>&branch_id=<?= (int)($filters['branch_id'] ?? 0) ?>" class="btn btn-secondary" style="font-size:13px;">تصدير CSV</a>
<?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;">ميزان المراجعة <span style="font-size:13px;color:#6B7280;font-weight:400;">(<?= count($result['accounts']) ?> حساب)</span></h3>
        <span style="font-size:13px;color:#6B7280;">من <?= e($date_from) ?> إلى <?= e($date_to) ?></span>
    </div>
    <div style="overflow-x:auto;">
        <table class="data-table" style="width:100%;min-width:900px;">
            <thead>
                <tr style="background:#0D7377;color:white;">
                    <th style="padding:10px;" rowspan="2">كود</th>
                    <th style="padding:10px;" rowspan="2">اسم الحساب</th>
                    <th style="padding:10px;text-align:center;border-left:1px solid rgba(255,255,255,0.3);" colspan="2">رصيد أول المدة</th>
                    <th style="padding:10px;text-align:center;border-left:1px solid rgba(255,255,255,0.3);" colspan="2">حركة الفترة</th>
                    <th style="padding:10px;text-align:center;" colspan="2">رصيد آخر المدة</th>
                </tr>
                <tr style="background:#0D7377;color:white;">
                    <th style="padding:8px;text-align:center;font-size:12px;border-left:1px solid rgba(255,255,255,0.3);">مدين</th>
                    <th style="padding:8px;text-align:center;font-size:12px;">دائن</th>
                    <th style="padding:8px;text-align:center;font-size:12px;border-left:1px solid rgba(255,255,255,0.3);">مدين</th>
                    <th style="padding:8px;text-align:center;font-size:12px;">دائن</th>
                    <th style="padding:8px;text-align:center;font-size:12px;">مدين</th>
                    <th style="padding:8px;text-align:center;font-size:12px;">دائن</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($result['accounts'] as $acc): ?>
                    <tr>
                        <td style="direction:ltr;text-align:right;font-weight:600;font-size:12px;padding:8px;white-space:nowrap;"><?= e($acc['account_code']) ?></td>
                        <td style="padding:8px;font-size:13px;"><?= e($acc['name_ar']) ?></td>
                        <td style="direction:ltr;text-align:center;padding:8px;font-size:12px;border-left:1px solid #E5E7EB;<?= bccomp($acc['opening_debit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['opening_debit']) ?></td>
                        <td style="direction:ltr;text-align:center;padding:8px;font-size:12px;<?= bccomp($acc['opening_credit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['opening_credit']) ?></td>
                        <td style="direction:ltr;text-align:center;padding:8px;font-size:12px;border-left:1px solid #E5E7EB;<?= bccomp($acc['period_debit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['period_debit']) ?></td>
                        <td style="direction:ltr;text-align:center;padding:8px;font-size:12px;<?= bccomp($acc['period_credit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['period_credit']) ?></td>
                        <td style="direction:ltr;text-align:center;padding:8px;font-size:12px;border-left:1px solid #E5E7EB;<?= bccomp($acc['closing_debit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['closing_debit']) ?></td>
                        <td style="direction:ltr;text-align:center;padding:8px;font-size:12px;<?= bccomp($acc['closing_credit'], '0.00', 2) > 0 ? 'font-weight:600;' : 'color:#D1D5DB;' ?>"><?= money($acc['closing_credit']) ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr style="font-weight:700;font-size:14px;background:#F9FAFB;border-top:2px solid #0D7377;">
                    <td style="padding:12px;" colspan="2">الإجمالي</td>
                    <td style="direction:ltr;text-align:center;padding:12px;border-left:1px solid #E5E7EB;"><?= money($result['total_opening_debit']) ?></td>
                    <td style="direction:ltr;text-align:center;padding:12px;"><?= money($result['total_opening_credit']) ?></td>
                    <td style="direction:ltr;text-align:center;padding:12px;border-left:1px solid #E5E7EB;"><?= money($result['total_period_debit']) ?></td>
                    <td style="direction:ltr;text-align:center;padding:12px;"><?= money($result['total_period_credit']) ?></td>
                    <td style="direction:ltr;text-align:center;padding:12px;border-left:1px solid #E5E7EB;"><?= money($result['total_closing_debit']) ?></td>
                    <td style="direction:ltr;text-align:center;padding:12px;"><?= money($result['total_closing_credit']) ?></td>
                </tr>
                <tr>
                    <td colspan="8" 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_closing_debit'], $result['total_closing_credit'], 2)) ?></span>
                        <?php endif; ?>
                    </td>
                </tr>
            </tfoot>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
