<?php
/**
 * Employer Remittances (Admin View)
 * View all employer remittances across the system
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('view_employer_remittances');

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

$where = ["1=1"];
$params = [];
$types = '';

if ($employer_id) {
    $where[] = "r.employer_id = ?";
    $params[] = $employer_id;
    $types .= 'i';
}

if ($status) {
    $where[] = "r.status = ?";
    $params[] = $status;
    $types .= 's';
}

$where[] = "DATE(r.remittance_date) BETWEEN ? AND ?";
$params[] = $date_from;
$params[] = $date_to;
$types .= 'ss';

$whereClause = implode(' AND ', $where);

$sql = "SELECT r.*, e.employer_name, e.employer_code
        FROM remittances r
        INNER JOIN employers e ON r.employer_id = e.employer_id
        WHERE $whereClause
        ORDER BY r.remittance_date DESC";

$stmt = $conn->prepare($sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$remittances = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$employers = $conn->query("SELECT employer_id, employer_name FROM employers WHERE is_active = 1 ORDER BY employer_name")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'Employer Remittances';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Employer Remittances')); ?>
    <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>Employer Remittances</h2>
                                <p>View all employer remittances across the system</p>
                            </div>
                        </div>
                    </div>

                    <div class="row mb-3">
                        <div class="col-12">
                            <form method="GET" class="d-inline-flex gap-2">
                                <select name="employer_id" class="form-select" style="width: auto;">
                                    <option value="">All Employers</option>
                                    <?php foreach ($employers as $emp): ?>
                                        <option value="<?php echo $emp['employer_id']; ?>" <?php echo $employer_id == $emp['employer_id'] ? 'selected' : ''; ?>>
                                            <?php echo htmlspecialchars($emp['employer_name']); ?>
                                        </option>
                                    <?php endforeach; ?>
                                </select>
                                <select name="status" class="form-select" style="width: auto;">
                                    <option value="">All Status</option>
                                    <option value="PENDING" <?php echo $status === 'PENDING' ? 'selected' : ''; ?>>Pending</option>
                                    <option value="PROCESSED" <?php echo $status === 'PROCESSED' ? 'selected' : ''; ?>>Processed</option>
                                </select>
                                <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">Filter</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">Remittances List</h4>
                                </div>
                                <div class="page-card-body">
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Date</th>
                                                    <th>Employer</th>
                                                    <th>Period</th>
                                                    <th>Amount (KES)</th>
                                                    <th>Reference</th>
                                                    <th>Status</th>
                                                    <th>Actions</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($remittances)): ?>
                                                    <tr><td colspan="7" 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 htmlspecialchars($remit['employer_name']); ?></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>
                                                                <a href="remittance_details.php?id=<?php echo $remit['remittance_id']; ?>" class="btn btn-sm btn-info">
                                                                    <i class="ri-eye-line"></i>
                                                                </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>
