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

<?php $__template->section('title'); ?>قيود اليومية<?php $__template->endSection(); ?>

<?php $__template->section('page_actions'); ?>
<?php if (can('accounting.journal.create')): ?>
<a href="/accounting/journal-entries/create" class="btn btn-primary">+ قيد جديد</a>
<?php endif; ?>
<?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/journal-entries" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">بحث</label>
                <input type="text" name="search" class="form-input" value="<?= e($filters['search'] ?? '') ?>" placeholder="رقم القيد، الوصف، كود الحساب، اسم الحساب...">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">الحالة</label>
                <select name="status" class="form-select">
                    <option value="">الكل</option>
                    <option value="draft" <?= ($filters['status'] ?? '') === 'draft' ? 'selected' : '' ?>>مسودة</option>
                    <option value="posted" <?= ($filters['status'] ?? '') === 'posted' ? 'selected' : '' ?>>مرحّل</option>
                    <option value="reversed" <?= ($filters['status'] ?? '') === 'reversed' ? 'selected' : '' ?>>معكوس</option>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">المصدر</label>
                <select name="source_module" class="form-select">
                    <option value="">الكل</option>
                    <option value="payments" <?= ($filters['source_module'] ?? '') === 'payments' ? 'selected' : '' ?>>المدفوعات</option>
                    <option value="sales" <?= ($filters['source_module'] ?? '') === 'sales' ? 'selected' : '' ?>>المبيعات</option>
                    <option value="hr" <?= ($filters['source_module'] ?? '') === 'hr' ? 'selected' : '' ?>>الموارد البشرية</option>
                    <option value="accounting" <?= ($filters['source_module'] ?? '') === 'accounting' ? 'selected' : '' ?>>المحاسبة</option>
                </select>
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">من تاريخ</label>
                <input type="date" name="date_from" class="form-input" value="<?= e($filters['date_from'] ?? '') ?>">
            </div>
            <div>
                <label class="form-label" style="font-size:12px;">إلى تاريخ</label>
                <input type="date" name="date_to" class="form-input" value="<?= e($filters['date_to'] ?? '') ?>">
            </div>
            <button type="submit" class="btn btn-outline">بحث</button>
        </form>
    </div>
</div>

<!-- Results -->
<div class="card">
    <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>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($entries as $entry): ?>
                    <?php
                    $statusColor = match($entry['status']) {
                        'draft' => '#F59E0B',
                        'posted' => '#059669',
                        'reversed' => '#DC2626',
                        default => '#6B7280',
                    };
                    $statusLabel = match($entry['status']) {
                        'draft' => 'مسودة',
                        'posted' => 'مرحّل',
                        'reversed' => 'معكوس',
                        default => $entry['status'],
                    };
                    $sourceLabel = match($entry['source_module'] ?? '') {
                        'payments' => 'مدفوعات',
                        'sales' => 'مبيعات',
                        'hr' => 'رواتب',
                        'fines' => 'غرامات',
                        'installments' => 'أقساط',
                        'accounting' => 'محاسبة',
                        default => $entry['source_module'] ?? 'يدوي',
                    };
                    ?>
                    <tr<?= $entry['status'] === 'reversed' ? ' style="opacity:0.5;text-decoration:line-through;"' : '' ?>>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= e($entry['entry_number']) ?></td>
                        <td><?= e($entry['entry_date']) ?></td>
                        <td><?= e(mb_substr($entry['description_ar'], 0, 60)) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($entry['total_debit']) ?></td>
                        <td style="direction:ltr;text-align:right;"><?= money($entry['total_credit']) ?></td>
                        <td style="font-size:12px;"><?= $sourceLabel ?></td>
                        <td><span style="color:<?= $statusColor ?>;font-weight:600;font-size:13px;"><?= $statusLabel ?></span></td>
                        <td><a href="/accounting/journal-entries/<?= (int)$entry['id'] ?>" class="btn btn-sm btn-outline">عرض</a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($entries)): ?>
                    <tr><td colspan="8" style="text-align:center;color:#6B7280;padding:30px;">لا توجد قيود</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($pages > 1): ?>
        <div style="padding:15px 20px;display:flex;justify-content:center;gap:5px;">
            <?php for ($p = 1; $p <= $pages; $p++): ?>
                <a href="?page=<?= $p ?>&<?= http_build_query(array_filter($filters)) ?>"
                   class="btn btn-sm <?= $p === $page ? 'btn-primary' : 'btn-outline' ?>"><?= $p ?></a>
            <?php endfor; ?>
        </div>
    <?php endif; ?>
</div>
<?php $__template->endSection(); ?>
