<?php
/**
 * Remittance Reports
 * Generate remittance reports for employer
 * Kenyan Pension & Life Insurance Management System
 */

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

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

$employer_id = $_SESSION['employer_id'] ?? $_SESSION['user_id'];
$date_from = $_GET['date_from'] ?? date('Y-m-01', strtotime('-12 months'));
$date_to = $_GET['date_to'] ?? date('Y-m-t');

$remittances = $conn->query("SELECT * FROM remittances 
                            WHERE employer_id = $employer_id 
                            AND DATE(remittance_date) BETWEEN '$date_from' AND '$date_to'
                            ORDER BY remittance_date DESC")->fetch_all(MYSQLI_ASSOC);

// Calculate statistics
$total_remitted = array_sum(array_column($remittances, 'total_amount'));
$by_status = [];
foreach ($remittances as $remit) {
    $status = $remit['status'];
    if (!isset($by_status[$status])) {
        $by_status[$status] = ['count' => 0, 'amount' => 0];
    }
    $by_status[$status]['count']++;
    $by_status[$status]['amount'] += $remit['total_amount'];
}

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

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Remittance 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, #06b6d4 0%, #0891b2 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Remittance Reports</h2>
                                <p>Generate comprehensive remittance reports</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="date_from" class="form-control" style="width: auto;" value="<?php echo $date_from; ?>">
                                <input type="date" name="date_to" class="form-control" style="width: auto;" value="<?php echo $date_to; ?>">
                                <button type="submit" class="btn btn-primary">Generate Report</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="metrics-row">
                        <div class="metric-card contributions">
                            <div class="metric-header">
                                <h6 class="metric-title">Total Remitted</h6>
                                <span class="metric-icon"><i class="ri-money-dollar-circle-line"></i></span>
                            </div>
                            <div class="metric-value">KES <?php echo number_format($total_remitted, 0); ?></div>
                            <div class="metric-change">Period total</div>
                        </div>

                        <div class="metric-card balances">
                            <div class="metric-header">
                                <h6 class="metric-title">Processed</h6>
                                <span class="metric-icon"><i class="ri-check-line"></i></span>
                            </div>
                            <div class="metric-value"><?php echo $by_status['PROCESSED']['count'] ?? 0; ?></div>
                            <div class="metric-change">KES <?php echo number_format($by_status['PROCESSED']['amount'] ?? 0, 0); ?></div>
                        </div>

                        <div class="metric-card withdrawals">
                            <div class="metric-header">
                                <h6 class="metric-title">Pending</h6>
                                <span class="metric-icon"><i class="ri-time-line"></i></span>
                            </div>
                            <div class="metric-value"><?php echo $by_status['PENDING']['count'] ?? 0; ?></div>
                            <div class="metric-change">KES <?php echo number_format($by_status['PENDING']['amount'] ?? 0, 0); ?></div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Remittance Report</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Period</th>
                                                    <th>Amount (KES)</th>
                                                    <th>Reference</th>
                                                    <th>Status</th>
                                                    <th>Processed Date</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($remittances)): ?>
                                                    <tr><td colspan="6" class="text-center">No remittances found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($remittances as $remit): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y', strtotime($remit['remittance_date'])); ?></td>
                                                            <td><?php echo date('M Y', strtotime($remit['remittance_period'])); ?></td>
                                                            <td><strong>KES <?php echo number_format($remit['total_amount'], 2); ?></strong></td>
                                                            <td><?php echo htmlspecialchars($remit['reference_number'] ?? 'N/A'); ?></td>
                                                            <td><span class="status-badge status-<?php echo strtolower($remit['status']); ?>"><?php echo htmlspecialchars($remit['status']); ?></span></td>
                                                            <td><?php echo $remit['processed_at'] ? date('d M Y', strtotime($remit['processed_at'])) : 'N/A'; ?></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>
