<?php
require_once '../connection/db.php';
requireLogin();
$user=getCurrentUser();$memberId=(int)$user['member_id'];$db=getDB();
$page=max(1,(int)($_GET['page']??1));$limit=25;$offset=($page-1)*$limit;
$tQ=$db->prepare('SELECT COUNT(*) AS c FROM meddolic_user_wallet_statement WHERE member_id=?');
$tQ->execute([$memberId]);$totalRows=(int)($tQ->fetch()['c']??0);$totalPages=(int)ceil($totalRows/$limit);
$wQ=$db->prepare("SELECT ws.*, wt.statement_type, wt.wallet_remark FROM meddolic_user_wallet_statement ws
  LEFT JOIN meddolic_config_wallet_statement_type wt ON wt.wallet_statement_id=ws.wallet_statement_id
  WHERE ws.member_id=? ORDER BY ws.date_time DESC LIMIT ? OFFSET ?");
$wQ->execute([$memberId,$limit,$offset]);$statements=$wQ->fetchAll();
$balQ=$db->prepare('SELECT wallet FROM meddolic_user_details WHERE member_id=?');
$balQ->execute([$memberId]);$walletBal=(float)($balQ->fetchColumn()??0);
$pageTitle='Wallet Statement';$activePage='wallet';require_once 'Include/header.php';
?>
<div class="layout"><?php require_once 'Include/menu.php';?>
<div class="main-content"><?php require_once 'Include/topbar.php';?>
<div class="page-content">
<div class="page-header">
  <div class="breadcrumb"><i class="fas fa-home"></i> <i class="fas fa-chevron-right"></i> Wallet Statement</div>
  <h3>Wallet Statement</h3><div class="subtitle">All credit and debit transactions</div>
</div>
<div class="stats-grid" style="margin-bottom:20px">
  <div class="stat-card" style="border-color:rgba(241,145,7,.4)">
    <div class="stat-icon" style="background:rgba(241,145,7,.2)"><i class="fas fa-wallet"></i></div>
    <div class="stat-info"><p>Current Balance</p><h5 class="gold" style="font-size:18px"><?=fmtUSD($walletBal)?></h5></div>
  </div>
  <div class="stat-card"><div class="stat-icon"><i class="fas fa-list"></i></div>
    <div class="stat-info"><p>Total Transactions</p><h5><?=number_format($totalRows)?></h5></div></div>
</div>
<div class="card">
  <div class="card-title"><i class="fas fa-wallet"></i> Statement Details</div>
  <div class="table-responsive"><table class="data-table">
    <thead><tr><th>#</th><th>Type</th><th>CR/DR</th><th>Amount</th><th>Remark</th><th>Date</th></tr></thead>
    <tbody>
    <?php if(empty($statements)):?><tr class="empty-row"><td colspan="6"><i class="fas fa-inbox"></i> No transactions found</td></tr>
    <?php else:foreach($statements as $i=>$s):$isCredit=(int)$s['deb_cr']===2;?>
    <tr>
      <td><?=$offset+$i+1?></td>
      <td><span class="badge badge-blue" style="font-size:10px"><?=e($s['statement_type']??'—')?></span></td>
      <td><?=$isCredit?'<span class="badge badge-green">CREDIT</span>':'<span class="badge badge-red">DEBIT</span>'?></td>
      <td class="amount" style="color:<?=$isCredit?'var(--success)':'var(--danger)'?>">
        <?=$isCredit?'+':'-'?>$<?=number_format($s['amount'],5)?>
      </td>
      <td style="font-size:12px;color:var(--text-muted)"><?=e($s['wallet_remark']??'—')?></td>
      <td style="font-size:12px;color:var(--text-muted)"><?=date('d M Y H:i',strtotime($s['date_time']))?></td>
    </tr>
    <?php endforeach;endif;?>
    </tbody>
  </table></div>
  <?php if($totalPages>1):?>
  <div style="display:flex;justify-content:center;gap:8px;margin-top:16px;flex-wrap:wrap">
    <?php for($p=1;$p<=$totalPages;$p++):?>
    <a href="?page=<?=$p?>" style="padding:6px 14px;border-radius:6px;font-size:13px;background:<?=$p==$page?'rgba(241,145,7,.2)':'var(--dark3)'?>;border:1px solid <?=$p==$page?'rgba(241,145,7,.4)':'var(--border)'?>;color:<?=$p==$page?'var(--gold)':'var(--text-muted)'?>;text-decoration:none"><?=$p?></a>
    <?php endfor;?>
  </div>
  <?php endif;?>
</div>
</div><?php require_once 'Include/footer.php';?></div></div>
