<?php $this->start('body');
    $srcTypes = ['PAYMENT','RECEIPT','INVOICE','MANUAL','OPENING','REVERSAL'];
    $src      = $this->v('source_type');
    $from     = $this->v('from');
    $to       = $this->v('to');
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-file-alt me-2 text-muted"></i>Journals</h5>
</div>

<form method="get" action="<?= PROOT; ?>Journals" class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3">
                <label class="form-label">Source type</label>
                <select name="source_type" class="form-select form-select-sm">
                    <option value="">All</option>
                    <?php foreach ($srcTypes as $s): ?>
                        <option value="<?= $s; ?>" <?= ($src===$s?'selected':''); ?>><?= ucwords(strtolower($s)); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">From</label>
                <input type="date" name="from" value="<?= htmlspecialchars($from); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3">
                <label class="form-label">To</label>
                <input type="date" name="to" value="<?= htmlspecialchars($to); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-3 text-end">
                <button type="submit" class="btn btn-sm btn-outline-secondary"><i class="fas fa-filter me-1"></i>Filter</button>
                <a href="<?= PROOT; ?>Journals" class="btn btn-sm btn-outline-secondary">Reset</a>
            </div>
        </div>
    </div>
</form>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>JE #</th><th>Date</th><th>Source</th><th>Ref</th><th>Narrative</th>
                    <th class="text-end">Debit</th><th class="text-end">Credit</th><th>Status</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($this->data['rows'] as $r):
                    $sc = ['POSTED'=>'bg-success','REVERSED'=>'bg-secondary'][$r['status']] ?? 'bg-light text-muted';
                ?>
                    <tr>
                        <td><a href="<?= PROOT; ?>Journals/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['je_no']); ?></strong></a></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['entry_date']); ?></td>
                        <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($r['source_type'])); ?></span></td>
                        <td class="text-muted small"><?= htmlspecialchars($r['source_ref']); ?></td>
                        <td class="text-muted" style="max-width:340px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="<?= htmlspecialchars($r['narrative']); ?>"><?= htmlspecialchars($r['narrative']); ?></td>
                        <td class="text-end"><?= number_format((float)$r['total_debit']); ?></td>
                        <td class="text-end"><?= number_format((float)$r['total_credit']); ?></td>
                        <td><span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($r['status'])); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Journals/view/<?= (int)$r['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($this->data['rows'])): ?>
                    <tr><td colspan="9" class="text-center text-muted py-4">No journal entries match this filter.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>
