<?php

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

if(!isset($_GET['id'])){
    header("Location: members.php");
    exit();
}

$memberId = (int)$_GET['id'];

/*
|--------------------------------------------------------------------------
| Member Details
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT *
FROM users
WHERE id=?
LIMIT 1
");

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

$member = $stmt->fetch(PDO::FETCH_ASSOC);

if(!$member){
    die("Member not found.");
}



/*
|--------------------------------------------------------------------------
| Schemes Joined
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT

p.plan_name,
p.amount,
p.frequency,
s.hands,
s.status

FROM subscriptions s

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

WHERE s.user_id=?

ORDER BY s.id DESC
");

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

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


/*
|--------------------------------------------------------------------------
| Wallet Summary
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT

COUNT(*) total_transactions,

IFNULL(SUM(
CASE
WHEN transaction_type='deposit'
THEN amount
ELSE 0
END
),0) total_deposit,

IFNULL(SUM(
CASE
WHEN transaction_type='deduction'
THEN amount
ELSE 0
END
),0) total_deduction

FROM wallet_transactions

WHERE user_id=?
");

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

$wallet = $stmt->fetch(PDO::FETCH_ASSOC);



/*
|--------------------------------------------------------------------------
| 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
LIMIT 20
");

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

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


/*
|--------------------------------------------------------------------------
| Contribution History
|--------------------------------------------------------------------------
*/

$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([$memberId]);

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

?>

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

<head>

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

<title>Member Details</title>

<link rel="stylesheet" href="../assets/css/admin.css">
<link rel="stylesheet" href="../assets/css/responsive.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/admin_sidebar.php"; ?>

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

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

<div class="main-content">

<div class="member-details-header">

<div class="member-profile">

<div class="member-avatar">

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

</div>

<div>

<h2>
<?= htmlspecialchars($member['fullname']); ?>
</h2>

<p>
<?= htmlspecialchars($member['phone']); ?>
</p>

</div>

</div>

<a href="members.php" class="back-btn">
<i class="fas fa-arrow-left"></i>
Back to Members
</a>

</div>

<div class="stats-grid">

<div class="stat-card">

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

<h3>

₦<?= number_format($member['wallet_balance'],2); ?>

</h3>

<p>Wallet Balance</p>

</div>


<div class="stat-card">

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

<h3>

₦<?= number_format($wallet['total_deposit'],2); ?>

</h3>

<p>Total Deposits</p>

</div>


<div class="stat-card">

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

<h3>

₦<?= number_format($wallet['total_deduction'],2); ?>

</h3>

<p>Total Deductions</p>

</div>


<div class="stat-card">

<i class="fas fa-exchange-alt"></i>

<h3>

<?= number_format($wallet['total_transactions']); ?>

</h3>

<p>Transactions</p>

</div>

</div>



<div class="table-card">

<h3>

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

Member Information

</h3>

<table class="member-table">

<tr>

<th width="220">Full Name</th>

<td><?= htmlspecialchars($member['fullname']); ?></td>

</tr>

<tr>

<th>Email</th>

<td><?= htmlspecialchars($member['email']); ?></td>

</tr>

<tr>

<th>Phone</th>

<td><?= htmlspecialchars($member['phone']); ?></td>

</tr>

<tr>

<th>Status</th>

<td><?= ucfirst($member['status']); ?></td>

</tr>

<tr>

<th>Virtual Bank</th>

<td><?= $member['virtual_bank'] ?: "-"; ?></td>

</tr>

<tr>

<th>Virtual Account</th>

<td><?= $member['virtual_account_number'] ?: "-"; ?></td>

</tr>

<tr>

<th>Account Name</th>

<td><?= $member['virtual_account_name'] ?: "-"; ?></td>

</tr>

<tr>

<th>Date Joined</th>

<td><?= date("d M Y",strtotime($member['created_at'])); ?></td>

</tr>

</table>

</div>



<div class="table-card">

<h3>

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

Joined Schemes

</h3>

<table class="member-table">

<thead>

<tr>

<th>Scheme</th>

<th>Amount</th>

<th>Hands</th>

<th>Frequency</th>

<th>Status</th>

</tr>

</thead>

<tbody>

<?php foreach($schemes as $scheme): ?>

<tr>

<td><?= htmlspecialchars($scheme['plan_name']); ?></td>

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

<td><?= $scheme['hands']; ?></td>

<td><?= $scheme['frequency']; ?></td>

<td><?= ucfirst($scheme['status']); ?></td>

</tr>

<?php endforeach; ?>

</tbody>

</table>

</div>

<div class="table-card">

<h3>
<i class="fas fa-wallet"></i>
Wallet Transactions
</h3>

<table class="member-table">

<thead>

<tr>

<th>Date</th>

<th>Type</th>

<th>Amount</th>

<th>Reference</th>

<th>Status</th>

</tr>

</thead>

<tbody>

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

<?php foreach($transactions as $t): ?>

<tr>

<td><?= date("d M Y",strtotime($t['created_at'])); ?></td>

<td><?= ucfirst($t['transaction_type']); ?></td>

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

<td><?= htmlspecialchars($t['reference']); ?></td>

<td>

<?php
if($t['status']=="successful"){
    echo "<span class='badge success'>Successful</span>";
}else{
    echo "<span class='badge warning'>Pending</span>";
}
?>

</td>

</tr>

<?php endforeach; ?>

<?php else: ?>

<tr>

<td colspan="5" style="text-align:center;">
No wallet transactions found.
</td>

</tr>

<?php endif; ?>

</tbody>

</table>

</div>

<div class="table-card">

<h3>
<i class="fas fa-calendar-check"></i>
Contribution History
</h3>

<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 $c): ?>

<tr>

<td>
<?= htmlspecialchars($c['plan_name']); ?>
</td>

<td>
<?= $c['contribution_no']; ?>
</td>

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

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

<td>

<?= $c['paid_date']
    ? date("d M Y", strtotime($c['paid_date']))
    : "-"; ?>

</td>

<td>

<?php

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

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

}elseif($c['status']=="pending"){

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

}else{

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

}

?>

</td>

</tr>

<?php endforeach; ?>

<?php else: ?>

<tr>

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

No contribution records found.

</td>

</tr>

<?php endif; ?>

</tbody>

</table>

</div>

</div>

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

<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<?php if(isset($_SESSION['success'])): ?>

<script>

Swal.fire({

    icon: 'success',

    title: 'Updated Successfully',

    text: '<?= htmlspecialchars($_SESSION["success"]); ?>',

    confirmButtonColor: '#0D6EFD'

});

</script>

<?php unset($_SESSION['success']); endif; ?>

</body>

</html>