<?php
/**
 * Bulk Upload Contributions
 * Upload contributions via Excel/CSV file
 * Kenyan Pension & Life Insurance Management System
 */

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

requireLogin();
requirePermission('bulk_upload_contributions');

$pensionService = new PensionService($conn);
$message = '';
$messageType = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['upload_file'])) {
    if ($_FILES['upload_file']['error'] === UPLOAD_ERR_OK) {
        $filePath = $_FILES['upload_file']['tmp_name'];
        $fileName = $_FILES['upload_file']['name'];
        $fileExtension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION));
        
        if (!in_array($fileExtension, ['csv', 'xlsx', 'xls'])) {
            $message = 'Invalid file format. Please upload CSV or Excel file.';
            $messageType = 'danger';
        } else {
            // Process file (simplified - would need proper Excel/CSV parsing library)
            $message = 'Bulk upload functionality will process the file. File uploaded: ' . htmlspecialchars($fileName);
            $messageType = 'success';
        }
    } else {
        $message = 'File upload failed. Please try again.';
        $messageType = 'danger';
    }
}

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

$pageTitle = 'Bulk Upload Contributions';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Bulk Upload Contributions')); ?>
    <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">
                    <!-- Page Header -->
                    <div class="welcome-header" style="background: linear-gradient(135deg, #f59e0b 0%, #d97706 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Bulk Upload Contributions</h2>
                                <p>Upload contributions via Excel or CSV file</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-8">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Upload Contributions File</h4>
                                </div>
                                <div class="page-card-body">
                                    <form method="POST" enctype="multipart/form-data">
                                        <div class="mb-3">
                                            <label class="form-label">Select File <span class="text-danger">*</span></label>
                                            <input type="file" name="upload_file" class="form-control" accept=".csv,.xlsx,.xls" required>
                                            <small class="text-muted">Accepted formats: CSV, Excel (XLSX, XLS)</small>
                                        </div>
                                        
                                        <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 htmlspecialchars($scheme['scheme_name']); ?>
                                                    </option>
                                                <?php endforeach; ?>
                                            </select>
                                        </div>
                                        
                                        <div class="alert alert-info">
                                            <h6><i class="ri-information-line me-2"></i>File Format Requirements</h6>
                                            <p class="mb-2"><strong>Required columns:</strong></p>
                                            <ul class="mb-0">
                                                <li>Member Number</li>
                                                <li>Contribution Period (YYYY-MM)</li>
                                                <li>Contribution Type (EMPLOYEE/EMPLOYER/VOLUNTARY)</li>
                                                <li>Contribution Amount (KES)</li>
                                                <li>Reference Number (optional)</li>
                                            </ul>
                                        </div>
                                        
                                        <button type="submit" class="btn btn-primary">
                                            <i class="ri-upload-line me-2"></i>Upload & Process
                                        </button>
                                        <a href="contributions.php" class="btn btn-secondary">Cancel</a>
                                    </form>
                                </div>
                            </div>
                        </div>
                        
                        <div class="col-md-4">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Download Template</h4>
                                </div>
                                <div class="page-card-body">
                                    <p class="text-muted">Download a sample template file to ensure correct formatting.</p>
                                    <a href="download_contribution_template.php" class="btn btn-success w-100">
                                        <i class="ri-download-line me-2"></i>Download Template
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
