<?php
/**
 * Member Contributions History
 * View member's contribution history
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';

requireLogin();
if (!isMember()) {
    header('Location: access_denied.php');
    exit();
}

$member_id = $_SESSION['member_id'] ?? $_SESSION['user_id'];

$contributions = $conn->query("SELECT c.*, ps.scheme_name 
                              FROM contributions c
                              INNER JOIN members m ON c.member_id = m.member_id
                              LEFT JOIN pension_schemes ps ON m.scheme_id = ps.scheme_id
                              WHERE c.member_id = $member_id
                              ORDER BY c.contribution_period DESC, c.created_at DESC")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'My Contributions';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Contributions')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #10b981 0%, #059669 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>My Contributions</h2>
                                <p>View your contribution history</p>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Contribution History</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Period</th>
                                                    <th>Type</th>
                                                    <th>Amount (KES)</th>
                                                    <th>Status</th>
                                                    <th>Date Posted</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($contributions)): ?>
                                                    <tr><td colspan="5" class="text-center">No contributions found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($contributions as $contrib): ?>
                                                        <tr>
                                                            <td><?php echo date('M Y', strtotime($contrib['contribution_period'])); ?></td>
                                                            <td><span class="badge bg-info"><?php echo htmlspecialchars($contrib['contribution_type']); ?></span></td>
                                                            <td>KES <?php echo number_format($contrib['contribution_amount'], 2); ?></td>
                                                            <td><span class="status-badge status-<?php echo strtolower($contrib['status']); ?>"><?php echo htmlspecialchars($contrib['status']); ?></span></td>
                                                            <td><?php echo date('d M Y', strtotime($contrib['created_at'])); ?></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
