<?php
$pageTitle = 'My Transactions';
require_once '../config/database.php';
$extraCSS = [BASE_URL . 'assets/css/admin.css'];
require_once '../includes/header.php';

if (!isLoggedIn() || isAdmin()) redirect(BASE_URL . 'login.php');

$user = getUserById($_SESSION['user_id']);
$transactions = getUserTransactions($_SESSION['user_id']);
?>

<div class="user-panel-wrapper">
    <div class="user-panel-container">
        <!-- Mobile Header -->
        <div class="user-panel-header">
            <h2>My Transactions</h2>
            <button class="user-panel-toggle" onclick="toggleUserSidebar()" aria-label="Toggle menu">
                <i class="fas fa-bars"></i>
            </button>
        </div>
        
        <!-- Sidebar Overlay -->
        <div class="user-sidebar-overlay" onclick="toggleUserSidebar()"></div>
        
        <aside class="user-sidebar">
            <button class="sidebar-close" onclick="toggleUserSidebar()" aria-label="Close menu">
                <i class="fas fa-times"></i>
            </button>
            <div class="user-avatar">
                <?php $sidebarAvatarUrl = !empty($user['profile_image']) ? getImageUrl($user['profile_image'], 'profiles') : 'https://ui-avatars.com/api/?name=' . urlencode($user['name']) . '&background=FF6B35&color=fff&size=80'; ?>
                <img src="<?= $sidebarAvatarUrl ?>" alt="<?= sanitize($user['name']) ?>">
                <h4><?= sanitize($user['name']) ?></h4>
                <p><?= sanitize($user['email']) ?></p>
            </div>
            <nav class="user-menu" role="navigation" aria-label="User menu">
                <a href="<?= USER_URL ?>"><i class="fas fa-tachometer-alt" aria-hidden="true"></i> Dashboard</a>
                <a href="<?= USER_URL ?>bookings.php"><i class="fas fa-calendar-check" aria-hidden="true"></i> My Bookings</a>
                <a href="<?= USER_URL ?>transactions.php" class="active" aria-current="page"><i class="fas fa-credit-card" aria-hidden="true"></i> Transactions</a>
                <a href="<?= USER_URL ?>settings.php"><i class="fas fa-cog" aria-hidden="true"></i> Settings</a>
                <a href="<?= BASE_URL ?>logout.php"><i class="fas fa-sign-out-alt" aria-hidden="true"></i> Logout</a>
            </nav>
        </aside>
        
        <div class="user-content">
            <h1 class="page-title">
                <i class="fas fa-credit-card" aria-hidden="true"></i> My Transactions
            </h1>
            
            <div class="admin-card">
                <div class="card-body" style="padding:0;">
                    <div class="table-responsive">
                        <table class="data-table transaction-table" role="table" aria-label="Transaction history">
                            <thead>
                                <tr>
                                    <th scope="col">Transaction ID</th>
                                    <th scope="col">Booking</th>
                                    <th scope="col">Package</th>
                                    <th scope="col">Amount</th>
                                    <th scope="col">Method</th>
                                    <th scope="col">Status</th>
                                    <th scope="col">Date</th>
                                    <th scope="col">Invoice</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($transactions as $t): ?>
                                    <tr>
                                        <td><strong><?= $t['transaction_id'] ?></strong></td>
                                        <td><?= $t['booking_code'] ?></td>
                                        <td><?= sanitize(truncateText($t['package_title'], 25)) ?></td>
                                        <td><strong><?= formatCurrency($t['amount']) ?></strong></td>
                                        <td style="text-transform:capitalize;"><?= $t['payment_method'] ?></td>
                                        <td><span class="status-badge status-<?= $t['status'] ?>"><?= ucfirst($t['status']) ?></span></td>
                                        <td><?= formatDate($t['created_at'], 'd M Y') ?></td>
                                        <td>
                                            <a href="<?= USER_URL ?>invoice.php?id=<?= $t['id'] ?>" class="invoice-btn" title="View Invoice" aria-label="View invoice for transaction <?= $t['transaction_id'] ?>">
                                                <i class="fas fa-download" aria-hidden="true"></i>
                                            </a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                                <?php if (empty($transactions)): ?>
                                    <tr><td colspan="8" style="text-align:center;color:var(--gray);padding:30px;">No transactions found.</td></tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<?php require_once '../includes/footer.php'; ?>
