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

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

<?php $__template->section('content'); ?>
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:15px;">
    <h2 style="margin:0;">الحركات اليومية — <?= e($date) ?></h2>
</div>

<!-- Date Selector -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;">
        <form method="GET" style="display:flex;gap:10px;align-items:end;">
            <div>
                <label class="form-label" style="font-size:12px;">التاريخ</label>
                <input type="date" name="date" class="form-input" value="<?= e($date) ?>" dir="ltr" onchange="this.form.submit()">
            </div>
        </form>
    </div>
</div>

<!-- Quick Entry Form -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;">
        <h3 style="margin:0;">إدخال سريع</h3>
    </div>
    <div style="padding:20px;">
        <form method="POST" action="/accounting/daily-transactions">
            <?= csrf_field() ?>
            <input type="hidden" name="transaction_date" value="<?= e($date) ?>">
            <div style="display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:15px;">
                <div>
                    <label class="form-label">الحساب <span style="color:#DC2626;">*</span></label>
                    <select name="account_id" class="form-select" required>
                        <option value="">— اختر —</option>
                        <?php foreach ($accounts as $acc): ?>
                            <option value="<?= (int)$acc['id'] ?>"><?= e($acc['account_code']) ?> — <?= e($acc['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">المبلغ <span style="color:#DC2626;">*</span></label>
                    <input type="number" name="amount" class="form-input" required step="0.01" min="0.01" dir="ltr">
                </div>
                <div>
                    <label class="form-label">الاتجاه <span style="color:#DC2626;">*</span></label>
                    <select name="direction" class="form-select" required>
                        <option value="debit">مدين</option>
                        <option value="credit">دائن</option>
                    </select>
                </div>
                <div>
                    <label class="form-label">نوع اليومية</label>
                    <select name="journal_type_id" class="form-select">
                        <option value="">— بدون —</option>
                        <?php foreach ($journalTypes as $jt): ?>
                            <option value="<?= (int)$jt['id'] ?>"><?= e($jt['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">مركز التكلفة</label>
                    <select name="cost_center_id" class="form-select">
                        <option value="">— بدون —</option>
                        <?php foreach ($costCenters as $cc): ?>
                            <option value="<?= (int)$cc['id'] ?>"><?= e($cc['code']) ?> — <?= e($cc['name_ar']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="form-label">رقم المستند</label>
                    <input type="text" name="document_number" class="form-input" dir="ltr">
                </div>
                <div style="grid-column:span 2;">
                    <label class="form-label">بيان</label>
                    <input type="text" name="description" class="form-input">
                </div>
            </div>
            <div style="margin-top:15px;">
                <button type="submit" class="btn btn-primary">تسجيل</button>
            </div>
        </form>
    </div>
</div>

<!-- Transactions List -->
<div class="card" style="margin-bottom:15px;">
    <div style="padding:15px 20px;border-bottom:1px solid #E5E7EB;display:flex;justify-content:space-between;align-items:center;">
        <h3 style="margin:0;">حركات اليوم</h3>
        <?php
        $unconsolidated = array_filter($transactions, fn($t) => !(int)$t['is_consolidated']);
        ?>
        <?php if (!empty($unconsolidated)): ?>
        <form method="POST" action="/accounting/daily-transactions/consolidate" style="display:inline;">
            <?= csrf_field() ?>
            <input type="hidden" name="date" value="<?= e($date) ?>">
            <button type="submit" class="btn btn-primary" onclick="return confirm('سيتم تجميع كل الحركات غير المجمعة في قيد يومية واحد. متابعة؟');">
                تجميع في قيد (<?= count($unconsolidated) ?> حركة)
            </button>
        </form>
        <?php endif; ?>
    </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>
                    <th>الحالة</th>
                </tr>
            </thead>
            <tbody>
                <?php
                $totalDebit = 0; $totalCredit = 0;
                foreach ($transactions as $i => $tx):
                    $isDebit = $tx['direction'] === 'debit';
                    $amount = (float)$tx['amount'];
                    if ($isDebit) $totalDebit += $amount; else $totalCredit += $amount;
                    $acc = $accountMap[(int)$tx['account_id']] ?? null;
                    $jt = isset($tx['journal_type_id']) ? ($typeMap[(int)$tx['journal_type_id']] ?? null) : null;
                ?>
                    <tr>
                        <td><?= $i + 1 ?></td>
                        <td><?= $acc ? e($acc['account_code'] . ' — ' . $acc['name_ar']) : '#' . $tx['account_id'] ?></td>
                        <td style="text-align:center;<?= $isDebit ? 'font-weight:600;' : 'color:#9CA3AF;' ?>"><?= $isDebit ? number_format($amount, 2) : '—' ?></td>
                        <td style="text-align:center;<?= !$isDebit ? 'font-weight:600;' : 'color:#9CA3AF;' ?>"><?= !$isDebit ? number_format($amount, 2) : '—' ?></td>
                        <td><?= $jt ? e($jt['name_ar']) : '—' ?></td>
                        <td><?= e($tx['description'] ?? '—') ?></td>
                        <td style="direction:ltr;text-align:right;"><?= e($tx['document_number'] ?? '—') ?></td>
                        <td>
                            <?php if ((int)$tx['is_consolidated']): ?>
                                <span style="color:#059669;font-weight:600;">مجمّع</span>
                            <?php else: ?>
                                <span style="color:#D97706;">معلّق</span>
                            <?php endif; ?>
                        </td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($transactions)): ?>
                    <tr><td colspan="8" style="text-align:center;color:#6B7280;padding:30px;">لا توجد حركات لهذا اليوم</td></tr>
                <?php endif; ?>
            </tbody>
            <?php if (!empty($transactions)): ?>
            <tfoot>
                <tr style="font-weight:700;background:#F9FAFB;">
                    <td colspan="2">الإجمالي</td>
                    <td style="text-align:center;"><?= number_format($totalDebit, 2) ?></td>
                    <td style="text-align:center;"><?= number_format($totalCredit, 2) ?></td>
                    <td colspan="4"></td>
                </tr>
            </tfoot>
            <?php endif; ?>
        </table>
    </div>
</div>
<?php $__template->endSection(); ?>
