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

<?php $__template->section('title'); ?>حركة الخزينة اليومية<?php $__template->endSection(); ?>

<?php $__template->section('content'); ?>
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" action="/accounting/statements/daily-cash" style="display:flex;gap:10px;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">التاريخ</label>
                <input type="date" name="date" value="<?= e($date) ?>" class="form-control">
            </div>
            <button type="submit" class="btn btn-primary">عرض</button>
        </form>
    </div>
</div>

<?php
$totalIn = '0.00';
$totalOut = '0.00';
foreach ($movements as $m) {
    if (($m['direction'] ?? '') === 'in') $totalIn = bcadd($totalIn, $m['amount'] ?? '0', 2);
    else $totalOut = bcadd($totalOut, $m['amount'] ?? '0', 2);
}
$net = bcsub($totalIn, $totalOut, 2);
?>
<div style="display:grid;grid-template-columns:1fr 1fr 1fr;gap:15px;margin-bottom:20px;">
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #059669;">
        <div style="font-size:13px;color:#6B7280;">إجمالي الوارد</div>
        <div style="font-size:22px;font-weight:700;color:#059669;"><?= money($totalIn) ?></div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #DC2626;">
        <div style="font-size:13px;color:#6B7280;">إجمالي الصادر</div>
        <div style="font-size:22px;font-weight:700;color:#DC2626;"><?= money($totalOut) ?></div>
    </div>
    <div class="card" style="padding:15px;text-align:center;border-right:4px solid #0D7377;">
        <div style="font-size:13px;color:#6B7280;">صافي الحركة</div>
        <div style="font-size:22px;font-weight:700;color:#0D7377;"><?= money($net) ?></div>
    </div>
</div>

<div class="card">
    <div class="card-header"><h3 style="margin:0;font-size:16px;">حركات <?= e($date) ?> (<?= count($movements) ?> حركة)</h3></div>
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th>البيان</th>
                    <th>النوع</th>
                    <th>وارد</th>
                    <th>صادر</th>
                    <th>المرجع</th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($movements)): ?>
                    <tr><td colspan="5" style="text-align:center;color:#6B7280;">لا توجد حركات في هذا التاريخ</td></tr>
                <?php else: ?>
                    <?php foreach ($movements as $m): ?>
                        <?php
                        $isIn = ($m['direction'] ?? '') === 'in';
                        $typeLabels = ['sale' => 'مبيعات', 'collection' => 'تحصيل', 'disbursement' => 'صرف'];
                        ?>
                        <tr>
                            <td><?= e($m['description'] ?? '—') ?></td>
                            <td><span style="background:<?= $isIn ? '#D1FAE5' : '#FEE2E2' ?>;color:<?= $isIn ? '#065F46' : '#991B1B' ?>;padding:2px 8px;border-radius:10px;font-size:11px;"><?= e($typeLabels[$m['movement_type'] ?? ''] ?? ($m['movement_type'] ?? '—')) ?></span></td>
                            <td style="color:#059669;font-weight:600;"><?= $isIn ? money($m['amount'] ?? '0') : '—' ?></td>
                            <td style="color:#DC2626;font-weight:600;"><?= !$isIn ? money($m['amount'] ?? '0') : '—' ?></td>
                            <td style="font-size:12px;color:#6B7280;"><?= e(($m['reference_type'] ?? '') . ($m['reference_id'] ? ' #' . $m['reference_id'] : '')) ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
