<div class="row g-4">
    <div class="col-lg-5">
        <div class="card">
            <div class="card-header"><h6 class="mb-0">Check: <?= e($check['check_number']) ?></h6></div>
            <div class="card-body">
                <table class="table table-sm mb-0">
                    <tr><th>Bank</th><td><?= e($check['bank_name']) ?></td></tr>
                    <tr><th>Branch</th><td><?= e($check['branch'] ?? '—') ?></td></tr>
                    <tr><th>Drawer</th><td><?= e($check['drawer_name']) ?></td></tr>
                    <tr><th>Amount</th><td class="fw-bold"><?= format_money($check['amount']) ?> <?= e($check['currency_code']) ?></td></tr>
                    <tr><th>Check Date</th><td><?= format_date($check['check_date']) ?></td></tr>
                    <tr><th>Received</th><td><?= format_date($check['receive_date']) ?></td></tr>
                    <tr><th>Deposited</th><td><?= format_date($check['deposit_date']) ?></td></tr>
                    <tr><th>Cleared</th><td><?= format_date($check['clearance_date']) ?></td></tr>
                    <tr><th>Member</th><td><?= e($check['member_name'] ?? '—') ?></td></tr>
                    <tr><th>Status</th><td><?= status_badge($check['status']) ?></td></tr>
                    <?php if ($check['bounce_reason']): ?><tr><th>Bounce Reason</th><td class="text-danger"><?= e($check['bounce_reason']) ?></td></tr><?php endif; ?>
                    <?php if ($check['bounce_fee']): ?><tr><th>Bounce Fee</th><td><?= format_money($check['bounce_fee']) ?></td></tr><?php endif; ?>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-7">
        <?php if (in_array($check['status'], ['received','deposited','bounced']) && auth_can('installments.create')): ?>
        <div class="card mb-4">
            <div class="card-header"><h6 class="mb-0"><?= lang('adv_finance.update_status') ?></h6></div>
            <div class="card-body">
                <form method="POST" action="/advanced-finance/checks/<?= $check['id'] ?>/status" data-ajax="true">
                    <?= csrf_field() ?>
                    <div class="row g-3">
                        <div class="col-md-4">
                            <label class="form-label">New Status <span class="text-danger">*</span></label>
                            <select name="new_status" class="form-select" required id="newStatusSelect">
                                <option value="">Select...</option>
                                <?php
                                $allowed = ['received'=>['deposited','cancelled','returned'],'deposited'=>['cleared','bounced'],'bounced'=>['replaced','received']];
                                foreach ($allowed[$check['status']] ?? [] as $s):
                                ?>
                                <option value="<?= $s ?>"><?= ucfirst($s) ?></option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4" id="depositDateWrap" style="display:none">
                            <label class="form-label">Deposit Date</label>
                            <input type="date" name="deposit_date" class="form-control" value="<?= date('Y-m-d') ?>">
                        </div>
                        <div class="col-md-4" id="bankAccountWrap" style="display:none">
                            <label class="form-label">Bank Account</label>
                            <select name="bank_account_id" class="form-select">
                                <option value="">—</option>
                                <?php foreach($bankAccounts as $ba): ?><option value="<?= $ba['id'] ?>"><?= e($ba['bank_name_en']) ?></option><?php endforeach; ?>
                            </select>
                        </div>
                        <div class="col-md-4" id="clearDateWrap" style="display:none">
                            <label class="form-label">Clearance Date</label>
                            <input type="date" name="clearance_date" class="form-control" value="<?= date('Y-m-d') ?>">
                        </div>
                        <div class="col-md-6" id="bounceWrap" style="display:none">
                            <label class="form-label">Bounce Reason</label>
                            <input type="text" name="bounce_reason" class="form-control">
                        </div>
                        <div class="col-md-3" id="bounceFeeWrap" style="display:none">
                            <label class="form-label">Bounce Fee</label>
                            <input type="number" name="bounce_fee" class="form-control" step="0.01" value="0">
                        </div>
                        <div class="col-12"><label class="form-label">Notes</label><textarea name="notes" class="form-control" rows="2"></textarea></div>
                    </div>
                    <div class="mt-3"><button type="submit" class="btn btn-primary btn-sm"><i class="fas fa-save me-1"></i><?= lang('common.save') ?></button></div>
                </form>
            </div>
        </div>
        <?php endif; ?>

        <div class="card">
            <div class="card-header"><h6 class="mb-0">Status History</h6></div>
            <div class="card-body p-0">
                <table class="table table-sm mb-0">
                    <thead><tr><th>From</th><th>To</th><th>Date</th><th>Notes</th></tr></thead>
                    <tbody>
                    <?php foreach($history as $h): ?>
                    <tr><td><?= status_badge($h['from_status'] ?? 'new') ?></td><td><?= status_badge($h['to_status']) ?></td><td><?= format_datetime($h['changed_at']) ?></td><td><?= e($h['notes'] ?? '—') ?></td></tr>
                    <?php endforeach; ?>
                    <?php if (empty($history)): ?><tr><td colspan="4" class="text-center text-muted py-3">No history yet</td></tr><?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?php $__view->push('scripts') ?>
<script>
document.getElementById('newStatusSelect')?.addEventListener('change', function() {
    const v = this.value;
    document.getElementById('depositDateWrap').style.display = v === 'deposited' ? '' : 'none';
    document.getElementById('bankAccountWrap').style.display = v === 'deposited' ? '' : 'none';
    document.getElementById('clearDateWrap').style.display = v === 'cleared' ? '' : 'none';
    document.getElementById('bounceWrap').style.display = v === 'bounced' ? '' : 'none';
    document.getElementById('bounceFeeWrap').style.display = v === 'bounced' ? '' : 'none';
});
</script>
<?php $__view->endPush() ?>