<?php
/**
 * @var array $accounts
 * @var array $transactions
 * @var string $accountId
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-university me-2 text-gold"></i>Bank 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">Bank Transactions</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#addBankModal">
                <i class="fas fa-plus me-1"></i>Add Transaction
            </button>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-4">
                <select class="form-select form-select-sm bg-transparent" onchange="window.location.href=this.value">
                    <option value="<?= url('accounting/bank-book') ?>">All Accounts</option>
                    <?php foreach ($accounts as $acc): ?>
                        <option value="<?= url('accounting/bank-book?account_id=' . $acc->id) ?>"
                            <?= $accountId == $acc->id ? 'selected' : '' ?>>
                            <?= e($acc->account_name) ?> (<?= e($acc->account_number) ?>)
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Date</th>
                        <th>Account</th>
                        <th>Type</th>
                        <th>Description</th>
                        <th class="text-end">Amount</th>
                        <th>Reference</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($transactions)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-university fa-2x mb-2"></i>
                                <p class="mb-0">No transactions found. Select an account or add a transaction.</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->account_name) ?></td>
                                <td>
                                    <span class="badge bg-<?= $txn->type === 'deposit' ? 'success' : ($txn->type === 'withdrawal' ? 'danger' : 'info') ?>">
                                        <?= e(ucfirst($txn->type)) ?>
                                    </span>
                                </td>
                                <td><?= e(truncate($txn->description, 50)) ?></td>
                                <td class="text-end fw-bold <?= $txn->type === 'deposit' ? 'text-success' : 'text-danger' ?>">
                                    <?= currency($txn->amount) ?>
                                </td>
                                <td><code><?= e($txn->reference ?? '-') ?></code></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<div class="modal fade" id="addBankModal" 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 Bank Transaction</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('accounting/bank-book/store') ?>">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Bank Account</label>
                        <select name="bank_account_id" class="form-select bg-transparent" required>
                            <option value="">Select Account</option>
                            <?php foreach ($accounts as $acc): ?>
                                <option value="<?= $acc->id ?>"><?= e($acc->account_name) ?> (<?= e($acc->account_number) ?>)</option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Type</label>
                        <select name="type" class="form-select bg-transparent" required>
                            <option value="deposit">Deposit</option>
                            <option value="withdrawal">Withdrawal</option>
                            <option value="transfer">Transfer</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">Transaction Date</label>
                        <input type="date" name="transaction_date" class="form-control bg-transparent" value="<?= date('Y-m-d') ?>" required>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Reference</label>
                        <input type="text" name="reference" class="form-control bg-transparent" maxlength="100">
                    </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>
