<?php
if (session_status() === PHP_SESSION_NONE) session_start();
if (!isset($_SESSION['user_id'])) { header('Location: ../login.php'); exit; }
require_once __DIR__ . '/../config.php';

$uid = (int)$_SESSION['user_id'];
$tab = $_GET['tab'] ?? 'all';
$period = $_GET['period'] ?? '7d';

// Period filter
$date_filter = match($period) {
    '1d'  => "AND t.created_at >= NOW() - INTERVAL 1 DAY",
    '30d' => "AND t.created_at >= NOW() - INTERVAL 30 DAY",
    default => "AND t.created_at >= NOW() - INTERVAL 7 DAY"
};

// Fetch deposits
$deposits = [];
if ($tab === 'all' || $tab === 'deposit') {
    $deposits = $pdo->query("SELECT 'deposit' as type, id, amount, method, status, reject_reason, created_at FROM deposits WHERE user_id=$uid $date_filter ORDER BY created_at DESC")->fetchAll();
}

// Fetch withdrawals
$withdrawals = [];
if ($tab === 'all' || $tab === 'withdraw') {
    $withdrawals = $pdo->query("SELECT 'withdrawal' as type, id, amount, method, status, reject_reason, created_at FROM withdrawals WHERE user_id=$uid $date_filter ORDER BY created_at DESC")->fetchAll();
}

// Merge and sort by date
$records = array_merge($deposits, $withdrawals);
usort($records, fn($a, $b) => strtotime($b['created_at']) - strtotime($a['created_at']));

// Totals
$total_dep = $pdo->query("SELECT COALESCE(SUM(amount),0) FROM deposits WHERE user_id=$uid AND status='approved'")->fetchColumn();
$total_wd  = $pdo->query("SELECT COALESCE(SUM(amount),0) FROM withdrawals WHERE user_id=$uid AND status='approved'")->fetchColumn();
?><!DOCTYPE html>
<html lang="en"><head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Transaction History — PakClub</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="../css/global.css">
<style>
body{background:#140904;color:#fff;font-family:'Segoe UI',sans-serif;max-width:414px;margin:0 auto;min-height:100vh;padding-bottom:30px}
.header{background:#1c100a;padding:14px 16px;display:flex;align-items:center;justify-content:space-between;position:sticky;top:0;z-index:10;border-bottom:1px solid #33241d}
.header-title{font-size:17px;font-weight:700;flex:1;text-align:center}
.hbtn{color:#fff;font-size:19px;text-decoration:none;width:28px;display:flex;justify-content:center}

/* Summary Cards */
.summary-row{display:grid;grid-template-columns:1fr 1fr;gap:10px;padding:16px}
.sum-card{background:#1c100a;border:1px solid #33241d;border-radius:12px;padding:14px 16px;text-align:center}
.sum-label{font-size:11px;color:#8b7a70;margin-bottom:6px;text-transform:uppercase;letter-spacing:.5px}
.sum-val{font-size:20px;font-weight:800}
.sum-val.dep{color:#22c55e}
.sum-val.wd{color:#f26d0f}

/* Tab filters */
.type-tabs{display:flex;gap:6px;padding:0 16px;margin-bottom:12px}
.type-tab{flex:1;padding:10px;text-align:center;font-size:12px;font-weight:700;border-radius:8px;background:#1c100a;border:1px solid #33241d;color:#8b7a70;cursor:pointer;text-decoration:none;transition:all .2s}
.type-tab.active{background:rgba(242,109,15,.12);border-color:#f26d0f;color:#f26d0f}

.period-tabs{display:flex;border-bottom:1px solid #33241d;margin-bottom:14px}
.period-tab{flex:1;padding:11px;text-align:center;font-size:13px;font-weight:600;color:#8b7a70;text-decoration:none;border-bottom:2px solid transparent;transition:all .2s}
.period-tab.active{color:#f26d0f;border-color:#f26d0f}

/* Records */
.records{padding:0 16px}
.record{background:#1c100a;border:1px solid #33241d;border-radius:10px;padding:14px;margin-bottom:10px;transition:all .2s}
.record:active{transform:scale(.99)}
.rec-top{display:flex;align-items:center;gap:10px;margin-bottom:6px}
.rec-icon{width:38px;height:38px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-size:16px;flex-shrink:0}
.rec-icon.dep-icon{background:rgba(34,197,94,.1);color:#22c55e}
.rec-icon.wd-icon{background:rgba(242,109,15,.1);color:#f26d0f}
.rec-info{flex:1}
.rec-type{font-size:14px;font-weight:700}
.rec-date{font-size:11px;color:#6b7280;margin-top:2px}
.rec-amount{font-size:16px;font-weight:800;flex-shrink:0}
.rec-amount.dep{color:#22c55e}
.rec-amount.wd{color:#f26d0f}
.rec-details{display:flex;justify-content:space-between;align-items:center;margin-top:6px;padding-top:8px;border-top:1px solid rgba(255,255,255,.04)}
.rec-method{font-size:11px;color:#8b7a70;display:flex;align-items:center;gap:4px}
.pill{padding:3px 10px;border-radius:20px;font-size:10px;font-weight:700}
.pill-pending{background:rgba(245,158,11,.15);color:#f59e0b}
.pill-approved{background:rgba(34,197,94,.15);color:#22c55e}
.pill-rejected{background:rgba(239,68,68,.15);color:#ef4444}
.reject-reason{font-size:11px;color:#f87171;font-style:italic;margin-top:6px;padding:6px 10px;background:rgba(239,68,68,.06);border-radius:6px}
.empty{text-align:center;color:#4b5563;padding:50px 20px;font-size:14px}
.empty i{font-size:40px;color:#33241d;margin-bottom:12px;display:block}
.date-range{font-size:12px;color:#8b7a70;padding:0 16px;margin-bottom:12px}
</style>
</head><body>

<div class="header">
  <a href="../me.php" class="hbtn"><i class="fa-solid fa-angle-left"></i></a>
  <div class="header-title">Transaction History</div>
  <div style="width:28px"></div>
</div>

<!-- Summary -->
<div class="summary-row">
  <div class="sum-card">
    <div class="sum-label"><i class="fa-solid fa-arrow-down"></i> Total Deposits</div>
    <div class="sum-val dep">Rs<?= number_format($total_dep,0) ?></div>
  </div>
  <div class="sum-card">
    <div class="sum-label"><i class="fa-solid fa-arrow-up"></i> Total Withdrawn</div>
    <div class="sum-val wd">Rs<?= number_format($total_wd,0) ?></div>
  </div>
</div>

<!-- Type filter -->
<div class="type-tabs">
  <a href="?tab=all&period=<?= $period ?>" class="type-tab <?= $tab==='all'?'active':'' ?>">All</a>
  <a href="?tab=deposit&period=<?= $period ?>" class="type-tab <?= $tab==='deposit'?'active':'' ?>"><i class="fa-solid fa-arrow-down"></i> Deposits</a>
  <a href="?tab=withdraw&period=<?= $period ?>" class="type-tab <?= $tab==='withdraw'?'active':'' ?>"><i class="fa-solid fa-arrow-up"></i> Withdrawals</a>
</div>

<!-- Period filter -->
<div class="period-tabs">
  <a href="?tab=<?= $tab ?>&period=1d" class="period-tab <?= $period==='1d'?'active':'' ?>">1 Day</a>
  <a href="?tab=<?= $tab ?>&period=7d" class="period-tab <?= $period==='7d'?'active':'' ?>">7 Days</a>
  <a href="?tab=<?= $tab ?>&period=30d" class="period-tab <?= $period==='30d'?'active':'' ?>">30 Days</a>
</div>

<div class="date-range">
  <?= date('Y-m-d', strtotime($period==='30d'?'-30 days':($period==='1d'?'today':'-7 days'))) ?> to <?= date('Y-m-d') ?>
</div>

<div class="records">
<?php if ($records): foreach ($records as $r):
  $is_dep = ($r['type'] === 'deposit');
?>
  <div class="record">
    <div class="rec-top">
      <div class="rec-icon <?= $is_dep?'dep-icon':'wd-icon' ?>">
        <i class="fa-solid fa-<?= $is_dep?'arrow-down':'arrow-up' ?>"></i>
      </div>
      <div class="rec-info">
        <div class="rec-type"><?= $is_dep ? 'Deposit' : 'Withdrawal' ?></div>
        <div class="rec-date"><?= date('Y-m-d H:i:s', strtotime($r['created_at'])) ?></div>
      </div>
      <div class="rec-amount <?= $is_dep?'dep':'wd' ?>"><?= $is_dep?'+':'-' ?>Rs<?= number_format($r['amount'],2) ?></div>
    </div>
    <div class="rec-details">
      <span class="rec-method"><i class="fa-solid fa-credit-card"></i> <?= $r['method'] ?></span>
      <span class="pill pill-<?= $r['status'] ?>"><?= ucfirst($r['status']) ?></span>
    </div>
    <?php if ($r['status']==='rejected' && $r['reject_reason']): ?>
    <div class="reject-reason"><i class="fa-solid fa-circle-info"></i> <?= htmlspecialchars($r['reject_reason']) ?></div>
    <?php endif; ?>
  </div>
<?php endforeach; else: ?>
  <div class="empty">
    <i class="fa-solid fa-receipt"></i>
    No transactions found for this period
  </div>
<?php endif; ?>
</div>

</body></html>
