<?php

include "../includes/member_auth.php";
include "../config/database.php";

$user_id = $_SESSION['user_id'];

/*
|--------------------------------------------------------------------------
| Wallet Transactions
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT
        transaction_type,
        amount,
        balance_before,
        balance_after,
        reference,
        description,
        status,
        created_at
    FROM wallet_transactions
    WHERE user_id=?
    ORDER BY id DESC
");

$stmt->execute([$user_id]);

$walletTransactions = $stmt->fetchAll(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| Scheme Contributions
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
    SELECT

        c.contribution_no,
        c.amount,
        c.due_date,
        c.paid_date,
        c.status,

        p.plan_name

    FROM contributions c

    INNER JOIN subscriptions s
        ON c.subscription_id=s.id

    INNER JOIN plans p
        ON s.plan_id=p.id

    WHERE s.user_id=?

    ORDER BY c.due_date DESC
");

$stmt->execute([$user_id]);

$contributions = $stmt->fetchAll(PDO::FETCH_ASSOC);


/*
|--------------------------------------------------------------------------
| Totals
|--------------------------------------------------------------------------
*/

$totalDeposits = 0;
$totalDeductions = 0;
$totalPaid = 0;
$totalRemaining = 0;


/*
|--------------------------------------------------------------------------
| Wallet Totals
|--------------------------------------------------------------------------
*/

foreach($walletTransactions as $transaction){

    if($transaction['transaction_type'] == "deposit"){

        $totalDeposits += $transaction['amount'];

    }else{

        $totalDeductions += $transaction['amount'];

    }

}


/*
|--------------------------------------------------------------------------
| Contribution Totals
|--------------------------------------------------------------------------
*/

foreach($contributions as $contribution){

    if($contribution['status'] == "paid"){

        $totalPaid += $contribution['amount'];

    }else{

        $totalRemaining += $contribution['amount'];

    }

}

?>

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Transaction History</title>


<link rel="stylesheet" href="../assets/css/admin.css">

<link rel="stylesheet" href="../assets/css/responsive.css">

<link rel="stylesheet" href="../assets/css/member.css">


<link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">

</head>


<body>


<?php include "../includes/member_sidebar.php"; ?>

<div class="sidebar-overlay"></div>

<?php include "../includes/member_topbar.php"; ?>


<div class="main-content">


<!-- PAGE HEADER -->

<div class="page-header">

<div>

<h1>

<i class="fas fa-clock-rotate-left"></i>

Transaction History

</h1>

<p>

View your wallet transactions and scheme contributions.

</p>

</div>

</div>



<!-- SUMMARY CARDS -->

<div class="dashboard-grid">


<div class="dashboard-card">

<div class="card-icon wallet">

<i class="fas fa-arrow-down"></i>

</div>

<h4>Total Wallet Deposits</h4>

<h2>

₦<?= number_format($totalDeposits,2); ?>

</h2>

</div>


<div class="dashboard-card">

<div class="card-icon scheme">

<i class="fas fa-arrow-up"></i>

</div>

<h4>Wallet Deductions</h4>

<h2>

₦<?= number_format($totalDeductions,2); ?>

</h2>

</div>


<div class="dashboard-card">

<div class="card-icon target">

<i class="fas fa-circle-check"></i>

</div>

<h4>Contributions Paid</h4>

<h2>

₦<?= number_format($totalPaid,2); ?>

</h2>

</div>


<div class="dashboard-card">

<div class="card-icon bank">

<i class="fas fa-clock"></i>

</div>

<h4>Contributions Remaining</h4>

<h2>

₦<?= number_format($totalRemaining,2); ?>

</h2>

</div>


</div>



<!-- WALLET TRANSACTIONS -->

<div class="activity-card">


<div class="activity-title-wrap">

<i class="fas fa-wallet"></i>

<h3>Wallet Transactions</h3>

</div>


<div class="activity-list">


<?php if(count($walletTransactions) > 0): ?>


<?php foreach($walletTransactions as $transaction): ?>


<div class="activity-item">


<?php if($transaction['transaction_type']=="deposit"): ?>

<div class="activity-icon success">

<i class="fas fa-arrow-down"></i>

</div>

<?php else: ?>

<div class="activity-icon pending">

<i class="fas fa-arrow-up"></i>

</div>

<?php endif; ?>


<div class="activity-details">

<h4>

<?= ucfirst(
    htmlspecialchars($transaction['transaction_type'])
); ?>

</h4>

<p>

<?= htmlspecialchars(
    $transaction['description'] ?? 'Wallet transaction'
); ?>

<br>

<small>

<?= date(
    "d M Y, h:i A",
    strtotime($transaction['created_at'])
); ?>

</small>

</p>

</div>


<span class="<?=

$transaction['transaction_type']=="deposit"

? "amount-positive"

: "amount-negative"

?>">

<?= $transaction['transaction_type']=="deposit"
    ? "+"
    : "-"
?>

₦<?= number_format($transaction['amount'],2); ?>

</span>


</div>


<?php endforeach; ?>


<?php else: ?>


<div class="activity-item">

<div class="activity-icon pending">

<i class="fas fa-wallet"></i>

</div>

<div class="activity-details">

<h4>No Wallet Transactions</h4>

<p>

Your wallet transactions will appear here.

</p>

</div>

<span>--</span>

</div>


<?php endif; ?>


</div>

</div>



<!-- CONTRIBUTIONS -->

<div class="activity-card">


<div class="activity-title-wrap">

<i class="fas fa-layer-group"></i>

<h3>Scheme Contributions</h3>

</div>


<div class="history-table-wrap">


<table class="member-table">


<thead>

<tr>

<th>Scheme</th>

<th>No.</th>

<th>Amount</th>

<th>Due Date</th>

<th>Paid Date</th>

<th>Status</th>

</tr>

</thead>


<tbody>


<?php if(count($contributions) > 0): ?>


<?php foreach($contributions as $contribution): ?>


<tr>


<td>

<strong>

<?= htmlspecialchars(
    $contribution['plan_name']
); ?>

</strong>

</td>


<td>

<?= $contribution['contribution_no']; ?>

</td>


<td>

₦<?= number_format(
    $contribution['amount'],
    2
); ?>

</td>


<td>

<?= date(
    "d M Y",
    strtotime($contribution['due_date'])
); ?>

</td>


<td>

<?php

if(!empty($contribution['paid_date'])){

    echo date(
        "d M Y",
        strtotime($contribution['paid_date'])
    );

}else{

    echo "-";

}

?>

</td>


<td>


<?php

if($contribution['status']=="paid"){

    echo '<span class="badge success">
            Paid
          </span>';

}elseif($contribution['status']=="overdue"){

    echo '<span class="badge danger">
            Overdue
          </span>';

}else{

    echo '<span class="badge warning">
            Pending
          </span>';

}

?>


</td>


</tr>


<?php endforeach; ?>


<?php else: ?>


<tr>

<td colspan="6" style="text-align:center;">

No scheme contributions yet.

</td>

</tr>


<?php endif; ?>


</tbody>

</table>


</div>

</div>


</div>



<script src="../assets/js/admin.js"></script>


</body>

</html>