<?php
/**
 * @var array $transactions
 * @var string $fromDate
 * @var string $toDate
 * @var float $openingBalance
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-money-bill-wave me-2 text-gold"></i>Cash Book</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-book card-icon"></i>
            <h6 class="card-title">Cash Transactions</h6>
        </div>
        <div class="card-header-right">
            <span class="text-muted small me-3">Opening Balance: <strong class="text-gold"><?= currency($openingBalance) ?></strong></span>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('accounting/cash-book') ?>" class="row mb-3 g-2">
            <div class="col-md-3">
                <input type="date" name="from_date" class="form-control form-control-sm bg-transparent"
                       value="<?= e($fromDate) ?>">
            </div>
            <div class="col-md-3">
                <input type="date" name="to_date" class="form-control form-control-sm bg-transparent"
                       value="<?= e($toDate) ?>">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-sm btn-gold w-100">
                    <i class="fas fa-filter me-1"></i>Filter
                </button>
            </div>
            <div class="col-md-4 text-end">
                <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#addCashModal">
                    <i class="fas fa-plus me-1"></i>Add Transaction
                </button>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Date</th>
                        <th>Description</th>
                        <th class="text-end">Inflow</th>
                        <th class="text-end">Outflow</th>
                        <th class="text-end fw-bold text-gold">Balance</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($transactions)): ?>
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">
                                <i class="fas fa-coins fa-2x mb-2"></i>
                                <p class="mb-0">No transactions found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = 1; ?>
                        <?php foreach ($transactions as $txn): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= formatDate($txn->transaction_date) ?></td>
                                <td><?= e($txn->description) ?></td>
                                <td class="text-end text-success">
                                    <?= $txn->type === 'inflow' ? currency($txn->amount) : '-' ?>
                                </td>
                                <td class="text-end text-danger">
                                    <?= $txn->type === 'outflow' ? currency($txn->amount) : '-' ?>
                                </td>
                                <td class="text-end fw-bold"><?= currency($txn->running_balance) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="addCashModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h6 class="modal-title"><i class="fas fa-plus-circle text-gold me-2"></i>Add Cash Transaction</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('accounting/cash-book/store') ?>">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Type</label>
                        <select name="type" class="form-select bg-transparent" required>
                            <option value="inflow">Inflow</option>
                            <option value="outflow">Outflow</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Amount</label>
                        <input type="number" step="0.01" name="amount" class="form-control bg-transparent" required min="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control bg-transparent" rows="2" required maxlength="500"></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Date</label>
                        <input type="date" name="date" class="form-control bg-transparent" value="<?= date('Y-m-d') ?>" required>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Save Transaction</button>
                </div>
            </form>
        </div>
    </div>
</div>
