<?php

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

if(!isset($_GET['id'])){

    header("Location: my_schemes.php");
    exit();

}

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

$stmt = $pdo->prepare("
SELECT
subscriptions.*,
plans.plan_name,
plans.description,
plans.frequency,
plans.duration,
plans.amount
FROM subscriptions
INNER JOIN plans
ON subscriptions.plan_id = plans.id
WHERE subscriptions.id=?
AND subscriptions.user_id=?
LIMIT 1
");

$stmt->execute([
    $id,
    $_SESSION['user_id']
]);

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

if(!$scheme){

    header("Location: my_schemes.php");
    exit();

}

/*
|--------------------------------------------------------------------------
| Contribution Progress
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT
COUNT(*) AS total,
SUM(CASE WHEN status='paid' THEN 1 ELSE 0 END) AS paid
FROM contributions
WHERE subscription_id=?
");

$stmt->execute([$scheme['id']]);

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

$totalContributions = (int)$progress['total'];

$paidContributions = (int)$progress['paid'];

$percentage = 0;

if($totalContributions > 0){

    $percentage = ($paidContributions / $totalContributions) * 100;

}

/*
|--------------------------------------------------------------------------
| Next Contribution
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("
SELECT *
FROM contributions
WHERE subscription_id=?
AND status IN ('pending','overdue')
ORDER BY contribution_no ASC
LIMIT 1
");

$stmt->execute([$scheme['id']]);

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

$remainingContributions = $totalContributions - $paidContributions;

?>

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

<head>

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

<title>Scheme Details</title>

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

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

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

<div class="main-content">

<div class="activity-header-bar">

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

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

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

</div>

</div>

<div class="scheme-details-card">

<div class="scheme-summary">

    <div class="scheme-price">

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

        <span>Per Hand</span>

    </div>

    <div class="scheme-status">

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

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

    </div>

</div>

<div class="plan-description">

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

</div>

<div class="scheme-info-grid">

    <div class="scheme-info-box">

        <h5>Contribution</h5>

        <p><?= htmlspecialchars($scheme['frequency']); ?></p>

    </div>

    <div class="scheme-info-box">

        <h5>Duration</h5>

        <p><?= htmlspecialchars($scheme['duration']); ?></p>

    </div>

    <div class="scheme-info-box">

        <h5>Hands Owned</h5>

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

    </div>

    <div class="scheme-info-box">

        <h5>Total Investment</h5>

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

    </div>

    <div class="scheme-info-box">

        <h5>Date Joined</h5>

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

    </div>

    <div class="scheme-info-box">

        <h5>Progress</h5>

       <p><?= number_format($percentage,1); ?>%</p>

    </div>

    <div class="scheme-info-box">

    <h5>Next Contribution</h5>

    <p>

    <?php

    if($nextContribution){

        echo date("d M Y",strtotime($nextContribution['due_date']));

    }else{

        echo "Completed";

    }

    ?>

    </p>

</div>

<div class="scheme-info-box">

    <h5>Remaining</h5>

    <p>

    <?= $remainingContributions; ?>

    of

    <?= $totalContributions; ?>

    </p>

</div>

</div>

<div class="progress-wrapper">

    <div class="progress">

        <div
class="progress-bar"
style="width:<?= $percentage; ?>%;">
</div>

    </div>

    <div class="progress-text">

        <?= $paidContributions; ?> of <?= $totalContributions; ?> Contributions Paid

    </div>

</div>


<?php

$stmt = $pdo->prepare("
SELECT *
FROM contributions
WHERE subscription_id=?
ORDER BY contribution_no ASC
");

$stmt->execute([$scheme['id']]);

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

?>

<div class="contribution-history">

<h3>

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

Contribution History

</h3>

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

<div class="history-row">

<div>

<strong>

Contribution #<?= $item['contribution_no']; ?>

</strong>

<br>

<small>

Due:

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

<?php

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

?>

<br>

Paid:

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

<?php

}

?>

</small>

</div>

<div>

<?php

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

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

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

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

}else{

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

}

?>

</div>

</div>

<?php endforeach; ?>

</div>

</div>

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

</body>

</html>