<?php 
$this->load->view('layouts/header');
$this->load->helper('layout');
?>

    <!-- App Header -->
    <div class="appHeader">
        <div class="left">
            <a href="<?php echo base_url('transactions'); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">Transaction Details</div>
        <div class="right"></div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        <div class="section mt-2 text-center pt-2">
            <div class="rounded-circle bg-<?php echo $transaction->status == 'completed' ? 'success' : ($transaction->status == 'failed' ? 'danger' : 'warning'); ?> d-flex align-items-center justify-content-center mx-auto" style="width: 80px; height: 80px;">
                <ion-icon name="<?php echo $transaction->status == 'completed' ? 'checkmark-outline' : ($transaction->status == 'failed' ? 'close-outline' : 'time-outline'); ?>" class="text-white" style="font-size: 40px;"></ion-icon>
            </div>
            <h1 class="mt-3 mb-0 <?php echo $is_debit ? 'text-danger' : 'text-success'; ?>">
                <?php echo ($is_debit ? '-' : '+') . format_money($transaction->amount, $transaction->currency); ?>
            </h1>
            <p class="text-muted mb-1"><?php echo ucfirst(str_replace('_', ' ', $transaction->type)); ?></p>
            <span class="badge badge-<?php echo $transaction->status == 'completed' ? 'success' : ($transaction->status == 'failed' ? 'danger' : 'warning'); ?>">
                <?php echo ucfirst($transaction->status); ?>
            </span>
        </div>

        <div class="section mt-3 inset">
            <div class="card card-body">
                <ul class="listview flush transparent simple-listview no-space">
                    <li>
                        <strong>Reference</strong>
                        <span class="text-end" style="word-break: break-all;"><?php echo $transaction->reference; ?></span>
                    </li>
                    <li>
                        <strong>Date</strong>
                        <span><?php echo date('M d, Y H:i', strtotime($transaction->created_at)); ?></span>
                    </li>
                    <?php if ($transaction->narration): ?>
                    <li>
                        <strong>Narration</strong>
                        <span><?php echo htmlspecialchars($transaction->narration); ?></span>
                    </li>
                    <?php endif; ?>
                    <?php if ($transaction->fee > 0): ?>
                    <li>
                        <strong>Fee</strong>
                        <span><?php echo format_money($transaction->fee, $transaction->currency); ?></span>
                    </li>
                    <?php endif; ?>
                    <?php if ($source_account): ?>
                    <li>
                        <strong>From</strong>
                        <span><?php echo $source_account->account_number; ?></span>
                    </li>
                    <?php endif; ?>
                    <?php if ($dest_account): ?>
                    <li>
                        <strong>To</strong>
                        <span><?php echo $dest_account->account_number; ?></span>
                    </li>
                    <?php endif; ?>
                    <?php 
                    $meta = $transaction->metadata ? (is_string($transaction->metadata) ? json_decode($transaction->metadata, true) : $transaction->metadata) : null;
                    if (!$dest_account && $meta && in_array($transaction->category, ['external', 'international'])): 
                        $recipient = $meta['recipient_name'] ?? '';
                        $recipient_acc = $meta['recipient_account'] ?? '';
                        $bank = $meta['bank_name'] ?? $meta['recipient_bank_swift'] ?? '';
                    ?>
                    <li>
                        <strong>To</strong>
                        <span><?php echo htmlspecialchars($recipient); ?><?php if ($recipient_acc): ?> (<?php echo htmlspecialchars($recipient_acc); ?>)<?php endif; ?></span>
                    </li>
                    <?php if ($bank): ?>
                    <li>
                        <strong><?php echo $transaction->category === 'international' ? 'SWIFT/BIC' : 'Bank'; ?></strong>
                        <span><?php echo htmlspecialchars($bank); ?></span>
                    </li>
                    <?php endif; ?>
                    <?php endif; ?>
                </ul>
            </div>
        </div>

        <div class="section inset mt-3 mb-3">
            <a href="<?php echo base_url('transactions/receipt/' . $transaction->id); ?>" class="btn btn-primary btn-block btn-lg">
                <ion-icon name="download-outline"></ion-icon> Download Receipt
            </a>
        </div>
    </div>

    <!-- App Bottom Menu -->
    <?php echo get_bottom_menu(''); ?>

<?php $this->load->view('layouts/footer'); ?>
