<div class="d-flex justify-content-between align-items-center mb-4">
    <h4 class="mb-0"><i class="fas fa-cash-register me-2"></i><?= lang('finance.cash_register') ?></h4>
    <a href="/finance/cashier/sessions" class="btn btn-outline-secondary"><i class="fas fa-history me-1"></i><?= lang('finance.session_history') ?></a>
</div>

<?php if ($mySession): ?>
<!-- Active Session -->
<div class="card border-success mb-4">
    <div class="card-header bg-success text-white">
        <div class="d-flex justify-content-between align-items-center">
            <h6 class="mb-0"><i class="fas fa-check-circle me-1"></i><?= lang('finance.active_session') ?> — <?= e($mySession['register_name']) ?></h6>
            <span class="badge bg-light text-dark"><?= lang('finance.opened') ?>: <?= format_datetime($mySession['opened_at']) ?></span>
        </div>
    </div>
    <div class="card-body">
        <div class="row g-3 mb-3">
            <div class="col-md-3">
                <div class="card bg-light"><div class="card-body py-2 text-center">
                    <small class="text-muted"><?= lang('finance.opening_balance') ?></small><br><strong><?= money($mySession['opening_balance']) ?></strong>
                </div></div>
            </div>
            <div class="col-md-3">
                <div class="card bg-light"><div class="card-body py-2 text-center">
                    <small class="text-muted"><?= lang('finance.cash_in') ?></small><br><strong class="text-success"><?= money($mySession['total_cash_in']) ?></strong>
                </div></div>
            </div>
            <div class="col-md-3">
                <div class="card bg-light"><div class="card-body py-2 text-center">
                    <small class="text-muted"><?= lang('finance.cash_out') ?></small><br><strong class="text-danger"><?= money($mySession['total_cash_out'] ?? 0) ?></strong>
                </div></div>
            </div>
            <div class="col-md-3">
                <div class="card bg-light"><div class="card-body py-2 text-center">
                    <small class="text-muted"><?= lang('finance.transactions') ?></small><br><strong><?= number_format($mySession['total_transactions']) ?></strong>
                </div></div>
            </div>
        </div>

        <!-- Close Session Form -->
        <form method="POST" action="/finance/cashier/close" data-ajax="true" class="row g-3 align-items-end">
            <?= csrf_field() ?>
            <input type="hidden" name="session_id" value="<?= $mySession['id'] ?>">
            <div class="col-md-4">
                <label class="form-label"><?= lang('finance.closing_balance') ?></label>
                <input type="number" name="closing_balance" class="form-control" step="0.01" min="0" required>
            </div>
            <div class="col-md-4">
                <button type="submit" class="btn btn-danger"><i class="fas fa-lock me-1"></i><?= lang('finance.close_session') ?></button>
            </div>
        </form>

        <!-- Recent Transactions -->
        <?php if (!empty($transactions)): ?>
        <hr>
        <h6><?= lang('finance.recent_transactions') ?></h6>
        <table class="table table-sm">
            <thead><tr><th><?= lang('finance.time') ?></th><th><?= lang('finance.type') ?></th><th><?= lang('finance.method') ?></th><th><?= lang('finance.direction') ?></th><th class="text-end"><?= lang('finance.amount') ?></th><th><?= lang('finance.reference') ?></th></tr></thead>
            <tbody>
            <?php foreach ($transactions as $t): ?>
            <tr>
                <td><small><?= format_datetime($t['created_at']) ?></small></td>
                <td><?= e(ucfirst(str_replace('_',' ',$t['transaction_type']))) ?></td>
                <td><?= e(ucfirst($t['payment_method'])) ?></td>
                <td><?= $t['direction'] === 'in' ? '<span class="text-success">↓ In</span>' : '<span class="text-danger">↑ Out</span>' ?></td>
                <td class="text-end"><?= money($t['amount']) ?></td>
                <td><?= $t['payment_number'] ? e($t['payment_number']) : '' ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
        <?php endif; ?>
    </div>
</div>

<?php else: ?>
<!-- Open Session -->
<div class="row justify-content-center">
    <div class="col-lg-6">
        <div class="card">
            <div class="card-header"><h6 class="mb-0"><?= lang('finance.open_session') ?></h6></div>
            <div class="card-body">
                <form method="POST" action="/finance/cashier/open" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="mb-3">
                        <label class="form-label"><?= lang('finance.select_register') ?> <span class="text-danger">*</span></label>
                        <select name="register_id" class="form-select" required>
                            <?php foreach ($registers as $reg): ?>
                            <option value="<?= $reg['id'] ?>" <?= $reg['session_id'] ? 'disabled' : '' ?>>
                                <?= e($reg['name_en']) ?> <?= $reg['session_id'] ? '(' . lang('finance.in_use') . ')' : '' ?>
                            </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label class="form-label"><?= lang('finance.opening_balance') ?></label>
                        <input type="number" name="opening_balance" class="form-control" step="0.01" value="0" min="0">
                    </div>
                    <button type="submit" class="btn btn-success"><i class="fas fa-unlock me-1"></i><?= lang('finance.open_session') ?></button>
                </form>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>