<?php
/**
 * Advanced Transactions History (Mobile Responsive)
 */
require_once 'header.php';

// ১. ট্রানজেকশন ডাটা তুলে আনা (আপনার অরিজিনাল টেবিল রিলেশন অনুযায়ী)
try {
    $query = "
        SELECT o.transaction_id, o.invoice_no, o.amount, o.status, o.created_at, 
               u.name as user_name, u.email as user_email, p.title as product_name
        FROM orders o
        LEFT JOIN users u ON o.user_id = u.id
        LEFT JOIN products p ON o.product_id = p.id
        WHERE o.transaction_id IS NOT NULL 
        ORDER BY o.id DESC
    ";
    $stmt = $pdo->query($query);
    $transactions = $stmt->fetchAll();
} catch (PDOException $e) {
    $error = "Transaction fetch error: " . $e->getMessage();
}

$currency = defined('CURRENCY_SYMBOL') ? CURRENCY_SYMBOL : 'BDT ';
?>

<style>
    /* Premium Table Card */
    .table-card { background: #fff; border-radius: 12px; border: 1px solid var(--border-color); padding: 20px; box-shadow: 0 1px 3px rgba(0,0,0,0.05); }
    .table-responsive { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
    .data-table { width: 100%; border-collapse: collapse; text-align: left; min-width: 800px; }
    
    .data-table th { padding: 15px 12px; font-size: 0.8rem; color: var(--text-gray); font-weight: 700; border-bottom: 2px solid var(--border-color); text-transform: uppercase; background: #f9fafb; }
    .data-table td { padding: 15px 12px; border-bottom: 1px solid #f3f4f6; font-size: 0.9rem; color: var(--text-dark); vertical-align: middle; }
    
    /* Transaction Style */
    .trx-id { font-family: monospace; font-weight: 800; color: var(--primary-orange); background: var(--primary-light); padding: 4px 8px; border-radius: 4px; font-size: 0.95rem; }
    .user-info { display: flex; flex-direction: column; }
    .user-name { font-weight: 700; color: #1e293b; }
    .user-email { font-size: 0.8rem; color: #64748b; }
    
    /* Status Badge */
    .status-badge { padding: 5px 12px; border-radius: 20px; font-size: 0.75rem; font-weight: 800; text-transform: uppercase; }
    .status-approved { background: #dcfce7; color: #166534; border: 1px solid #bbf7d0; }
    .status-pending { background: #fef3c7; color: #92400e; border: 1px solid #fde68a; }
    .status-failed { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }

    @media (max-width: 768px) {
        .table-card { padding: 15px 10px; }
        .data-table th, .data-table td { padding: 12px 10px; }
    }
</style>

<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 25px;">
    <h1 class="page-title" style="margin: 0;">Transaction History</h1>
    <div style="font-size: 0.85rem; color: var(--text-gray); font-weight: 600;"> Total Record: <?php echo count($transactions); ?></div>
</div>

<div class="table-card">
    <div class="table-responsive">
        <table class="data-table">
            <thead>
                <tr>
                    <th>Trx ID & Invoice</th>
                    <th>Customer</th>
                    <th>Product</th>
                    <th>Amount</th>
                    <th>Status</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($transactions as $trx): ?>
                <tr>
                    <td>
                        <div class="trx-id"><?php echo htmlspecialchars($trx['transaction_id']); ?></div>
                        <div style="font-size: 0.75rem; margin-top: 5px; color: #64748b; font-weight: 600;">INV: #<?php echo $trx['invoice_no']; ?></div>
                    </td>
                    <td>
                        <div class="user-info">
                            <span class="user-name"><?php echo htmlspecialchars($trx['user_name'] ?? 'Unknown'); ?></span>
                            <span class="user-email"><?php echo htmlspecialchars($trx['user_email'] ?? 'N/A'); ?></span>
                        </div>
                    </td>
                    <td>
                        <div style="max-width: 180px; white-space: normal; line-height: 1.3; font-weight: 600;">
                            <?php echo htmlspecialchars($trx['product_name'] ?? 'N/A'); ?>
                        </div>
                    </td>
                    <td style="font-weight: 800; color: var(--text-dark);">
                        <?php echo $currency . number_format($trx['amount'], 2); ?>
                    </td>
                    <td>
                        <span class="status-badge status-<?php echo $trx['status']; ?>">
                            <?php echo strtoupper($trx['status']); ?>
                        </span>
                    </td>
                    <td style="font-size: 0.8rem; color: #64748b; font-weight: 600;">
                        <?php echo date('d M Y', strtotime($trx['created_at'])); ?><br>
                        <?php echo date('h:i A', strtotime($trx['created_at'])); ?>
                    </td>
                </tr>
                <?php endforeach; ?>
                
                <?php if(empty($transactions)): ?>
                    <tr><td colspan="6" style="text-align: center; padding: 50px; color: #94a3b8; font-weight: 600;">No transactions found.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php echo '</div></div></body></html>'; ?>