<?php
/**
 * Automated Compliance Reports
 * Generate and schedule RBA compliance reports
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('generate_compliance_reports');

$complianceReporter = new ComplianceReporter($conn);
$message = '';
$messageType = '';

// Generate report
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'generate') {
    $quarter = $_POST['quarter'] ?? null;
    $year = $_POST['year'] ?? date('Y');
    
    if (!$quarter) {
        $message = 'Quarter is required';
        $messageType = 'danger';
    } else {
        $report = $complianceReporter->generateRBAQuarterlyReport($quarter, $year);
        $reportData = $report;
    }
}

$pageTitle = 'Compliance Reports';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Compliance Reports')); ?>
    <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>Compliance Reports</h2>
                                <p>Generate and schedule RBA compliance reports</p>
                            </div>
                        </div>
                    </div>

                    <?php if ($message): ?>
                    <div class="alert alert-<?php echo $messageType; ?> alert-dismissible fade show">
                        <?php echo htmlspecialchars($message); ?>
                        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                    </div>
                    <?php endif; ?>

                    <div class="row">
                        <div class="col-md-4">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Generate Report</h4>
                                </div>
                                <div class="page-card-body">
                                    <form method="POST">
                                        <input type="hidden" name="action" value="generate">
                                        
                                        <div class="mb-3">
                                            <label class="form-label">Quarter</label>
                                            <select name="quarter" class="form-select" required>
                                                <option value="">Select Quarter</option>
                                                <option value="1">Q1 (Jan - Mar)</option>
                                                <option value="2">Q2 (Apr - Jun)</option>
                                                <option value="3">Q3 (Jul - Sep)</option>
                                                <option value="4">Q4 (Oct - Dec)</option>
                                            </select>
                                        </div>
                                        
                                        <div class="mb-3">
                                            <label class="form-label">Year</label>
                                            <input type="number" name="year" class="form-control" value="<?php echo date('Y'); ?>" min="2020" max="<?php echo date('Y'); ?>" required>
                                        </div>
                                        
                                        <button type="submit" class="btn btn-primary w-100">
                                            <i class="ri-file-chart-line me-2"></i>Generate Report
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                        
                        <?php if (isset($reportData)): ?>
                        <div class="col-md-8">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">RBA Quarterly Report - <?php echo htmlspecialchars($reportData['period']); ?></h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="mb-4">
                                        <h5>Summary</h5>
                                        <div class="row">
                                            <div class="col-md-6">
                                                <p><strong>Total Schemes:</strong> <?php echo $reportData['summary']['total_schemes']; ?></p>
                                                <p><strong>Total Members:</strong> <?php echo number_format($reportData['summary']['total_members']); ?></p>
                                            </div>
                                            <div class="col-md-6">
                                                <p><strong>Total Contributions:</strong> KES <?php echo number_format($reportData['summary']['total_contributions'], 2); ?></p>
                                                <p><strong>Total Fund Value:</strong> KES <?php echo number_format($reportData['summary']['total_fund_value'], 2); ?></p>
                                            </div>
                                        </div>
                                    </div>
                                    
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Scheme</th>
                                                    <th>Members</th>
                                                    <th>Contributions</th>
                                                    <th>Withdrawals</th>
                                                    <th>Fund Value</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php foreach ($reportData['schemes'] as $scheme): ?>
                                                    <tr>
                                                        <td><strong><?php echo htmlspecialchars($scheme['scheme_name']); ?></strong></td>
                                                        <td><?php echo $scheme['member_statistics']['total_members']; ?></td>
                                                        <td>KES <?php echo number_format(($scheme['contribution_statistics']['employee_contributions'] ?? 0) + ($scheme['contribution_statistics']['employer_contributions'] ?? 0), 2); ?></td>
                                                        <td>KES <?php echo number_format($scheme['withdrawal_statistics']['total_withdrawn'] ?? 0, 2); ?></td>
                                                        <td>KES <?php echo number_format($scheme['fund_balance']['total_accrued'] ?? 0, 2); ?></td>
                                                    </tr>
                                                <?php endforeach; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                    
                                    <div class="mt-3">
                                        <button onclick="window.print()" class="btn btn-success">
                                            <i class="ri-printer-line me-2"></i>Print Report
                                        </button>
                                        <button onclick="exportReport()" class="btn btn-primary">
                                            <i class="ri-download-line me-2"></i>Export PDF
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
    <script>
    function exportReport() {
        // Export report as PDF
        window.location.href = 'export_compliance_report.php?quarter=<?php echo $_POST['quarter'] ?? ''; ?>&year=<?php echo $_POST['year'] ?? date('Y'); ?>';
    }
    </script>
</body>
</html>
