<?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/general-ledger" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div style="flex:2;">
                <label class="form-label" style="font-size:12px;">الحساب</label>
                <input type="number" name="account_id" class="form-input" value="<?= $account_id ?>" placeholder="رقم الحساب (ID)">
            </div>
            <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>
            <button type="submit" class="btn btn-primary">عرض</button>
        </form>
    </div>
</div>

<?php if ($ledger && $ledger['account']): ?>
    <div class="card">
        <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
            <h3 style="margin:0;">
                <span style="direction:ltr;color:#0D7377;"><?= e($ledger['account']['account_code']) ?></span>
                — <?= e($ledger['account']['name_ar']) ?>
            </h3>
            <p style="color:#6B7280;margin:5px 0 0;font-size:13px;">من <?= e($date_from) ?> إلى <?= e($date_to) ?></p>
        </div>
        <div class="table-responsive">
            <table class="data-table" style="width:100%;">
                <thead>
                    <tr>
                        <th>التاريخ</th>
                        <th>رقم القيد</th>
                        <th>البيان</th>
                        <th>مرجع</th>
                        <th>مدين</th>
                        <th>دائن</th>
                        <th>الرصيد</th>
                    </tr>
                </thead>
                <tbody>
                    <!-- Opening Balance Row -->
                    <tr style="background:#F9FAFB;font-weight:600;">
                        <td colspan="4">رصيد أول المدة</td>
                        <td></td>
                        <td></td>
                        <td style="direction:ltr;text-align:right;"><?= money($ledger['opening_balance']) ?></td>
                    </tr>
                    <?php foreach ($ledger['entries'] as $e_row): ?>
                        <tr>
                            <td><?= e($e_row['entry_date']) ?></td>
                            <td style="direction:ltr;text-align:right;font-size:12px;"><?= e($e_row['entry_number']) ?></td>
                            <td style="font-size:13px;"><?= e($e_row['description_ar'] ?? $e_row['entry_description'] ?? '') ?></td>
                            <td style="font-size:12px;color:#6B7280;"><?= e($e_row['reference_number'] ?? '') ?></td>
                            <td style="direction:ltr;text-align:right;"><?= bccomp((string)$e_row['debit'], '0.00', 2) > 0 ? money($e_row['debit']) : '' ?></td>
                            <td style="direction:ltr;text-align:right;"><?= bccomp((string)$e_row['credit'], '0.00', 2) > 0 ? money($e_row['credit']) : '' ?></td>
                            <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($e_row['running_balance']) ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
                <tfoot>
                    <tr style="font-weight:700;background:#F0FDF4;">
                        <td style="padding:12px;" colspan="4">رصيد آخر المدة</td>
                        <td></td>
                        <td></td>
                        <td style="direction:ltr;text-align:right;padding:12px;font-size:16px;"><?= money($ledger['closing_balance']) ?></td>
                    </tr>
                </tfoot>
            </table>
        </div>
    </div>
<?php elseif ($account_id > 0): ?>
    <div class="card" style="padding:40px;text-align:center;color:#6B7280;">الحساب غير موجود</div>
<?php else: ?>
    <div class="card" style="padding:40px;text-align:center;color:#6B7280;">اختر حساباً لعرض دفتر الأستاذ</div>
<?php endif; ?>
<?php $__template->endSection(); ?>
