<?php
/**
 * Member Statement
 * Generate member pension statement (single member view)
 * 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'];
$period_start = $_GET['period_start'] ?? date('Y-m-01', strtotime('-1 year'));
$period_end = $_GET['period_end'] ?? date('Y-m-t');

// Get member and account data
$member = $conn->query("SELECT m.*, ps.scheme_name, ps.scheme_code
                       FROM members m
                       LEFT JOIN pension_schemes ps ON m.scheme_id = ps.scheme_id
                       WHERE m.member_id = $member_id")->fetch_assoc();

$account = $conn->query("SELECT * FROM member_accounts WHERE member_id = $member_id")->fetch_assoc();

// Get contributions for period
$contributions = $conn->query("SELECT * FROM contributions 
                              WHERE member_id = $member_id 
                              AND DATE(contribution_period) BETWEEN '$period_start' AND '$period_end'
                              ORDER BY contribution_period DESC")->fetch_all(MYSQLI_ASSOC);

// Get withdrawals for period
$withdrawals = $conn->query("SELECT * FROM withdrawal_requests 
                            WHERE member_id = $member_id 
                            AND DATE(requested_date) BETWEEN '$period_start' AND '$period_end'
                            ORDER BY requested_date DESC")->fetch_all(MYSQLI_ASSOC);

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'My Statement')); ?>
    <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 Pension Statement</h2>
                                <p>View your pension account statement</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <form method="GET" class="d-inline-flex gap-2">
                                <input type="date" name="period_start" class="form-control" style="width: auto;" value="<?php echo $period_start; ?>">
                                <input type="date" name="period_end" class="form-control" style="width: auto;" value="<?php echo $period_end; ?>">
                                <button type="submit" class="btn btn-primary">Generate Statement</button>
                                <button type="button" onclick="window.print()" class="btn btn-success">
                                    <i class="ri-printer-line me-2"></i>Print
                                </button>
                            </form>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Pension Statement</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="mb-4">
                                        <h5>Member Information</h5>
                                        <table class="table table-borderless">
                                            <tr>
                                                <td><strong>Member Number:</strong></td>
                                                <td><?php echo htmlspecialchars($member['member_number']); ?></td>
                                                <td><strong>Scheme:</strong></td>
                                                <td><?php echo htmlspecialchars($member['scheme_name'] ?? 'N/A'); ?></td>
                                            </tr>
                                            <tr>
                                                <td><strong>Name:</strong></td>
                                                <td><?php echo htmlspecialchars($member['first_name'] . ' ' . $member['last_name']); ?></td>
                                                <td><strong>Statement Period:</strong></td>
                                                <td><?php echo date('d M Y', strtotime($period_start)); ?> to <?php echo date('d M Y', strtotime($period_end)); ?></td>
                                            </tr>
                                        </table>
                                    </div>
                                    
                                    <div class="mb-4">
                                        <h5>Account Summary</h5>
                                        <table class="table table-bordered">
                                            <tr>
                                                <td><strong>Employee Contributions:</strong></td>
                                                <td class="text-end">KES <?php echo number_format($account['total_employee_contributions'] ?? 0, 2); ?></td>
                                            </tr>
                                            <tr>
                                                <td><strong>Employer Contributions:</strong></td>
                                                <td class="text-end">KES <?php echo number_format($account['total_employer_contributions'] ?? 0, 2); ?></td>
                                            </tr>
                                            <tr>
                                                <td><strong>Investment Returns:</strong></td>
                                                <td class="text-end">KES <?php echo number_format($account['allocated_investment_returns'] ?? 0, 2); ?></td>
                                            </tr>
                                            <tr>
                                                <td><strong>Total Withdrawn:</strong></td>
                                                <td class="text-end">KES <?php echo number_format($account['total_withdrawn'] ?? 0, 2); ?></td>
                                            </tr>
                                            <tr class="table-primary">
                                                <td><strong>Available Balance:</strong></td>
                                                <td class="text-end"><strong>KES <?php echo number_format($account['available_balance'] ?? 0, 2); ?></strong></td>
                                            </tr>
                                        </table>
                                    </div>
                                    
                                    <div class="mb-4">
                                        <h5>Contributions (<?php echo date('M Y', strtotime($period_start)); ?> - <?php echo date('M Y', strtotime($period_end)); ?>)</h5>
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Period</th>
                                                        <th>Type</th>
                                                        <th>Amount (KES)</th>
                                                        <th>Status</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php if (empty($contributions)): ?>
                                                        <tr><td colspan="4" class="text-center">No contributions in this period</td></tr>
                                                    <?php else: ?>
                                                        <?php foreach ($contributions as $contrib): ?>
                                                            <tr>
                                                                <td><?php echo date('M Y', strtotime($contrib['contribution_period'])); ?></td>
                                                                <td><?php echo htmlspecialchars($contrib['contribution_type']); ?></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>
                                                            </tr>
                                                        <?php endforeach; ?>
                                                    <?php endif; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    
                                    <?php if (!empty($withdrawals)): ?>
                                    <div class="mb-4">
                                        <h5>Withdrawals (<?php echo date('M Y', strtotime($period_start)); ?> - <?php echo date('M Y', strtotime($period_end)); ?>)</h5>
                                        <div class="table-responsive">
                                            <table class="data-table">
                                                <thead>
                                                    <tr>
                                                        <th>Date</th>
                                                        <th>Type</th>
                                                        <th>Amount (KES)</th>
                                                        <th>Status</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php foreach ($withdrawals as $withdrawal): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($withdrawal['requested_date'])); ?></td>
                                                            <td><?php echo htmlspecialchars($withdrawal['withdrawal_type']); ?></td>
                                                            <td>KES <?php echo number_format($withdrawal['calculated_amount'] ?? $withdrawal['requested_amount'], 2); ?></td>
                                                            <td><span class="status-badge status-<?php echo strtolower($withdrawal['status']); ?>"><?php echo htmlspecialchars($withdrawal['status']); ?></span></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
