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

    <!-- App Header -->
    <div class="appHeader bg-primary text-light">
        <div class="left">
            <a href="<?php echo base_url('admin/transaction/' . $transaction->id); ?>" class="headerButton">
                <ion-icon name="chevron-back-outline"></ion-icon>
            </a>
        </div>
        <div class="pageTitle">Edit Transaction</div>
        <div class="right">
            <a href="#" class="headerButton" data-bs-toggle="modal" data-bs-target="#adminSidebar">
                <ion-icon name="menu-outline"></ion-icon>
            </a>
        </div>
    </div>

    <!-- App Capsule -->
    <div id="appCapsule">
        
        <?php if ($this->session->flashdata('success')): ?>
        <div class="section mt-2">
            <div class="alert alert-success"><?php echo $this->session->flashdata('success'); ?></div>
        </div>
        <?php endif; ?>

        <!-- Transaction Info -->
        <div class="section mt-2 text-center">
            <div class="card">
                <div class="card-body py-3">
                    <p class="text-muted mb-1">Reference</p>
                    <p class="small mb-0"><?php echo $transaction->reference; ?></p>
                </div>
            </div>
        </div>

        <div class="section mt-2 mb-3">
            <form action="<?php echo base_url('admin/edit_transaction/' . $transaction->id); ?>" method="POST">
                <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">
                
                <div class="card">
                    <div class="card-header">Transaction Details</div>
                    <div class="card-body">
                        <div class="form-group basic">
                            <label class="label">Amount (<?php echo $transaction->currency; ?>)</label>
                            <input type="number" name="amount" class="form-control" step="0.01" min="0.01" value="<?php echo $transaction->amount; ?>">
                        </div>
                        <div class="form-group basic">
                            <label class="label">Narration</label>
                            <input type="text" name="narration" class="form-control" value="<?php echo htmlspecialchars($transaction->narration ?? ''); ?>">
                        </div>
                        <div class="form-group basic">
                            <label class="label">Status</label>
                            <select name="status" class="form-control">
                                <option value="pending" <?php echo $transaction->status == 'pending' ? 'selected' : ''; ?>>Pending</option>
                                <option value="completed" <?php echo $transaction->status == 'completed' ? 'selected' : ''; ?>>Completed</option>
                                <option value="failed" <?php echo $transaction->status == 'failed' ? 'selected' : ''; ?>>Failed</option>
                                <option value="reversed" <?php echo $transaction->status == 'reversed' ? 'selected' : ''; ?>>Reversed</option>
                            </select>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Transaction Date</label>
                            <input type="datetime-local" name="created_at" class="form-control" value="<?php echo date('Y-m-d\TH:i', strtotime($transaction->created_at)); ?>">
                        </div>
                    </div>
                </div>

                <!-- Source & Destination Info (Read-only) -->
                <div class="card mt-2">
                    <div class="card-header">Account Information</div>
                    <div class="card-body">
                        <div class="form-group basic">
                            <label class="label">Type</label>
                            <input type="text" class="form-control" value="<?php echo ucfirst($transaction->type); ?>" readonly disabled>
                        </div>
                        <div class="form-group basic">
                            <label class="label">Category</label>
                            <input type="text" class="form-control" value="<?php echo ucfirst(str_replace('_', ' ', $transaction->category ?? 'N/A')); ?>" readonly disabled>
                        </div>
                        <?php if ($source_account): ?>
                        <div class="form-group basic">
                            <label class="label">Source Account</label>
                            <input type="text" class="form-control" value="<?php echo $source_account->account_number; ?> - <?php echo htmlspecialchars($source_account->first_name . ' ' . $source_account->last_name); ?>" readonly disabled>
                        </div>
                        <?php endif; ?>
                        <?php if ($dest_account): ?>
                        <div class="form-group basic">
                            <label class="label">Destination Account</label>
                            <input type="text" class="form-control" value="<?php echo $dest_account->account_number; ?> - <?php echo htmlspecialchars($dest_account->first_name . ' ' . $dest_account->last_name); ?>" readonly disabled>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="section mt-2">
                    <button type="submit" class="btn btn-primary btn-block btn-lg">
                        <ion-icon name="save-outline"></ion-icon> Save Changes
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- Admin Sidebar -->
    <?php $this->load->view('admin/partials/sidebar'); ?>

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