<?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="text" name="account_search" class="form-input" value="<?= e($account_search ?? '') ?>" placeholder="مثال: 1101 أو النقدية بالخزينة" list="account-list">
                <input type="hidden" name="account_id" value="<?= (int)$account_id ?>">
                <datalist id="account-list"></datalist>
            </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 elseif (!empty($account_search)): ?>
    <div class="card" style="padding:40px;text-align:center;color:#DC2626;">لم يتم العثور على حساب بهذا الاسم أو الكود: "<?= e($account_search) ?>"</div>
<?php else: ?>
    <div class="card" style="padding:40px;text-align:center;color:#6B7280;">اختر حساباً لعرض دفتر الأستاذ</div>
<?php endif; ?>

<script>
(function(){
    var input = document.querySelector('[name="account_search"]');
    var hidden = document.querySelector('[name="account_id"]');
    var list = document.getElementById('account-list');
    var timer;
    if (!input) return;
    input.addEventListener('input', function(){
        clearTimeout(timer);
        var q = input.value.trim();
        if (q.length < 2) { list.innerHTML = ''; return; }
        hidden.value = '0';
        timer = setTimeout(function(){
            fetch('/accounting/chart-of-accounts/search?q=' + encodeURIComponent(q))
                .then(function(r){ return r.json(); })
                .then(function(data){
                    list.innerHTML = '';
                    (data || []).slice(0, 15).forEach(function(a){
                        var opt = document.createElement('option');
                        opt.value = a.account_code + ' — ' + a.name_ar;
                        opt.setAttribute('data-id', a.id);
                        list.appendChild(opt);
                    });
                });
        }, 300);
    });
    input.addEventListener('change', function(){
        var opts = list.querySelectorAll('option');
        for (var i = 0; i < opts.length; i++) {
            if (opts[i].value === input.value) {
                hidden.value = opts[i].getAttribute('data-id');
                return;
            }
        }
    });
})();
</script>
<?php $__template->endSection(); ?>
