<?php
/**
 * Balance Reconciliation
 * Reconcile member account balances
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('reconcile_balances');

$scheme_id = $_GET['scheme_id'] ?? '';
$action = $_GET['action'] ?? 'view';

// Get schemes
$schemes = $conn->query("SELECT scheme_id, scheme_name FROM pension_schemes WHERE is_active = 1")->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'Balance Reconciliation';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Balance Reconciliation')); ?>
    <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>Balance Reconciliation</h2>
                                <p>Reconcile and verify member account balances</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">Reconciliation Tools</h4>
                                </div>
                                <div class="page-card-body">
                                    <form method="GET">
                                        <input type="hidden" name="action" value="reconcile">
                                        <div class="mb-3">
                                            <label class="form-label">Scheme</label>
                                            <select name="scheme_id" class="form-select">
                                                <option value="">All Schemes</option>
                                                <?php foreach ($schemes as $scheme): ?>
                                                    <option value="<?php echo $scheme['scheme_id']; ?>" <?php echo $scheme_id == $scheme['scheme_id'] ? 'selected' : ''; ?>>
                                                        <?php echo htmlspecialchars($scheme['scheme_name']); ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        
                                        <div class="alert alert-info">
                                            <i class="ri-information-line me-2"></i>
                                            <strong>Balance Reconciliation:</strong> This process will verify that member account balances match the sum of contributions, returns, and withdrawals. 
                                            Any discrepancies will be flagged for review.
                                        </div>
                                        
                                        <button type="submit" class="btn btn-primary">
                                            <i class="ri-refresh-line me-2"></i>Run Reconciliation
                                        </button>
                                    </form>
                                    
                                    <?php if ($action === 'reconcile'): ?>
                                        <div class="alert alert-success mt-4">
                                            <i class="ri-check-line me-2"></i>
                                            Reconciliation process initiated. This feature will verify balances against transaction history.
                                        </div>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
