<?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/statements/supplier" style="display:flex;gap:10px;flex-wrap:wrap;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">المورد</label>
                <select name="supplier_id" class="form-select">
                    <option value="">— اختر مورد —</option>
                    <?php foreach ($suppliers as $s): ?>
                        <option value="<?= (int)$s['id'] ?>" <?= (int)($filters['supplier_id'] ?? 0) === (int)$s['id'] ? 'selected' : '' ?>><?= e($s['name']) ?></option>
                    <?php endforeach; ?>
                </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'] ?? '') ?>" dir="ltr">
            </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'] ?? '') ?>" dir="ltr">
            </div>
            <button type="submit" class="btn btn-primary">عرض</button>
        </form>
    </div>
</div>

<?php if (!empty($supplier) && !empty($statement)): ?>
<!-- Statement -->
<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;">كشف حساب: <?= e($supplier['name']) ?></h3>
        <button type="button" class="btn btn-outline" onclick="window.print()">طباعة</button>
    </div>

    <!-- Opening Balance -->
    <div style="padding:10px 20px;background:#F9FAFB;border-bottom:1px solid #E5E7EB;">
        <strong>الرصيد الافتتاحي:</strong>
        <span style="font-weight:700;direction:ltr;display:inline-block;"><?= money($statement['opening_balance'] ?? 0) ?></span>
    </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>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($statement['entries'] ?? [] as $entry): ?>
                    <tr>
                        <td style="direction:ltr;text-align:right;"><?= e($entry['date']) ?></td>
                        <td><?= e($entry['doc_type'] ?? '—') ?></td>
                        <td><?= e($entry['description'] ?? '') ?></td>
                        <td style="direction:ltr;text-align:right;color:#DC2626;"><?= (float)($entry['debit'] ?? 0) > 0 ? money($entry['debit']) : '' ?></td>
                        <td style="direction:ltr;text-align:right;color:#059669;"><?= (float)($entry['credit'] ?? 0) > 0 ? money($entry['credit']) : '' ?></td>
                        <td style="direction:ltr;text-align:right;font-weight:600;"><?= money($entry['balance'] ?? 0) ?></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($statement['entries'])): ?>
                    <tr><td colspan="6" style="text-align:center;color:#6B7280;padding:30px;">لا توجد حركات</td></tr>
                <?php endif; ?>
            </tbody>
            <tfoot>
                <tr style="font-weight:700;background:#F9FAFB;">
                    <td colspan="3">الإجمالي</td>
                    <td style="direction:ltr;text-align:right;color:#DC2626;"><?= money($statement['total_debit'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:right;color:#059669;"><?= money($statement['total_credit'] ?? 0) ?></td>
                    <td style="direction:ltr;text-align:right;"><?= money($statement['closing_balance'] ?? 0) ?></td>
                </tr>
            </tfoot>
        </table>
    </div>

    <!-- Closing Balance -->
    <div style="padding:10px 20px;background:#F0FDF4;border-top:1px solid #E5E7EB;">
        <strong>الرصيد الختامي:</strong>
        <span style="font-weight:700;direction:ltr;display:inline-block;"><?= money($statement['closing_balance'] ?? 0) ?></span>
    </div>
</div>
<?php elseif (!empty($filters['supplier_id'])): ?>
<div class="card">
    <div style="padding:30px;text-align:center;color:#6B7280;">لا توجد بيانات للعرض</div>
</div>
<?php endif; ?>
<?php $__template->endSection(); ?>
